-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4dec68
commit 3610a15
Showing
4 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import re | ||
from django.test import TestCase | ||
from django.contrib.auth.models import User | ||
from memberships.models import Member, Membership | ||
|
||
class APITestCase(TestCase): | ||
|
||
JWT_REGEX = r"/^([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_\-\+\/=]*)/gm" | ||
|
||
def create_user(self, name, password, email) -> None: | ||
self.member = Member.create( | ||
full_name="test person", | ||
preferred_name=name, | ||
email=email, | ||
password=password, | ||
birth_date="1991-01-01", | ||
) | ||
|
||
def is_token(string: str) -> bool: | ||
if re.match(APITestCase.JWT_REGEX, string): | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from json import loads | ||
from django.test import TestCase | ||
from rest_framework.test import APIRequestFactory | ||
from memberships.views import signon_with_password | ||
from django.contrib.auth.models import User | ||
from .api_utils import APITestCase | ||
|
||
class SignonWithPasswordTest(APITestCase): | ||
|
||
def setUp(self): | ||
self.factory = APIRequestFactory() | ||
self.create_user('example', 'Secure123!', 'example@example.com') | ||
|
||
|
||
def test_valid_username_and_password_return_token(self): | ||
request = self.factory.post( | ||
'/memberships/signonWithPassword/', | ||
{ 'email': 'example@example.com', 'password': 'Secure123!' }, | ||
format='json' | ||
) | ||
resp = signon_with_password(request) | ||
data: dict = loads(resp.content.decode('utf-8')) | ||
|
||
token = data["token"] | ||
self.assertEqual(resp.status_code, 200) | ||
self.assertTrue(self.is_token(token)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters