Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
SamWinterhalder committed Sep 18, 2023
1 parent f4dec68 commit 3610a15
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
22 changes: 22 additions & 0 deletions memberships/tests/api_utils.py
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
26 changes: 26 additions & 0 deletions memberships/tests/test_signon_with_password.py
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))
8 changes: 5 additions & 3 deletions memberships/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from jwt import encode, decode
from rest_framework.parsers import JSONParser
from rest_framework.decorators import api_view

from .payments import handle_stripe_payment
from .forms import *
Expand Down Expand Up @@ -282,13 +283,14 @@ def verify(request, uidb64, token):


@csrf_exempt
@api_view(["POST"])
def signon_with_password(request):
"""
Create JWT for user
"""
try:
verifyPostMethod(request.method)
requestJson = jsonFromRequest(request)
requestJson = request.data
verifyPasswordSignonRequestBody(requestJson)
userEmail = requestJson["email"]
if checkPasswordForUserWithEmail(userEmail, requestJson["password"]):
Expand All @@ -303,6 +305,7 @@ def signon_with_password(request):
else:
raise ERROR_CODE_ENUM.FORBIDDEN.throw()
except Exception as err:
print(err)
if not isinstance(err, APIError):
err = ERROR_CODE_ENUM.INTERNAL_SERVER_ERROR.value
err.log()
Expand All @@ -316,8 +319,7 @@ def token_refresh(request):
"""

try:
if request.method != "POST":
raise ERROR_CODE_ENUM.METHOD_NOT_ALLOWED.throw()
verifyPostMethod(request.method)

token = request.headers["Authorization"][7:]
refresh = JSONParser().parse(request)["refreshToken"]
Expand Down
1 change: 1 addition & 0 deletions test.http
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Signon with Username and Password
POST http://127.0.0.1:8000/memberships/signonWithPassword/
Content-Type: application/json

{
"email": "example@example.com",
Expand Down

0 comments on commit 3610a15

Please sign in to comment.