From 1ef47d49ad146fcb9b7d622d533b704fc1e94049 Mon Sep 17 00:00:00 2001 From: Chacoon3 Date: Tue, 7 Nov 2023 23:21:20 -0500 Subject: [PATCH] upd testing --- bmgt435_elp/middlewares.py | 1 + bmgt435_elp/test.py | 39 ++++++++++++-------------------------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/bmgt435_elp/middlewares.py b/bmgt435_elp/middlewares.py index 818d5f5..c1642fb 100644 --- a/bmgt435_elp/middlewares.py +++ b/bmgt435_elp/middlewares.py @@ -46,6 +46,7 @@ def middleware(request: HttpRequest): 2. user utility api's are allowed if there is a user id cookie 3. manage api's are allowed if there is a user id cookie, and if the user is an admin (validated by a database query) """ + # no authentication required if request.path.startswith("/bmgt435-service/api/auth/") or request.path.startswith("/bmgt435-service/admin") or request.path.startswith("/bmgt435-service/static"): return get_response(request) diff --git a/bmgt435_elp/test.py b/bmgt435_elp/test.py index 800ec75..ad421ae 100644 --- a/bmgt435_elp/test.py +++ b/bmgt435_elp/test.py @@ -4,11 +4,6 @@ import json -def _clientSetCookies(client:Client, cookies:dict): - for k, v in cookies.items(): - client.cookies[k] = v - - def _clientSignUp(client:Client, did:str, password:str): return client.post( 'bmgt435-service/api/auth/sign-up', @@ -86,14 +81,9 @@ def testRepeatedSignUp(self): def testSignInPositive(self): c = Client() - _clientSignUp(c, 'did', 'pa3232.ssword') - - resp = c.post( - 'bmgt435-service/api/auth/sign-in', - json.dumps({'did': 'did', 'password': 'pa3232.ssword'}), - 'application/json' - ) - + pwd = 'pa3232.ssword' + _clientSignUp(c, 'did', pwd) + resp = _clientSignIn(c, 'did', pwd) self.assertEqual(resp.status_code, 200) @@ -120,13 +110,6 @@ def setUp(self): BMGTUser(first_name='f', last_name='l', did='did323', role='admin', activated=0, password='Grave11.').save() BMGTUser(first_name='first321', last_name='last232', did='did232', role='user', activated=1, password='Grave11.').save() - - def testUserMePositive(self): - resp = Client().get( - 'bmgt435-service/api/user/me', - ) - self.assertNotEqual(resp.status_code, 200) - def testUserMeAfterSignIn(self): c = Client() @@ -147,7 +130,9 @@ def testUserMeNegative(self): def testUserMeNotActivated(self): - resp = Client().get( + c = Client() + c.cookies['id'] = 2 + resp = c.get( 'bmgt435-service/api/users/me', ) self.assertNotEqual(resp.status_code, 200) @@ -155,19 +140,19 @@ def testUserMeNotActivated(self): class GroupApiTest(TestCase): + def setUp(self) -> None: - BMGTUser(first_name='f', last_name='l', did='did', role='admin', activated=1, password='Grave11.').save() - BMGTUser(first_name='f', last_name='l', did='did323', role='admin', activated=0, password='Grave11.').save() - BMGTUser(first_name='first321', last_name='last232', did='did232', role='user', activated=1, password='Grave11.').save() - BMGTSemester(year=2022, season='fall').save() - BMGTGroup(number = 1, semester = BMGTSemester.objects.get(year=2022, season='fall')).save() + BMGTUser.objects.create(first_name='f', last_name='l', did='did', role='admin', activated=1, password='Grave11.') + BMGTUser.objects.create(first_name='f', last_name='l', did='did323', role='admin', activated=0, password='Grave11.') + BMGTUser.objects.create(first_name='first321', last_name='last232', did='did232', role='user', activated=1, password='Grave11.') + BMGTSemester.objects.create(year=2022, season='fall') + BMGTGroup.objects.create(number = 1, semester = BMGTSemester.objects.get(year=2022, season='fall')) def testGetGroupPositive(self): c = Client() _clientSignUp(c, 'did', 'Grave11.') _clientSignIn(c, 'did', 'Grave11.') - _clientSetCookies(c, {'id': 1}) resp = c.get( 'bmgt435-service/api/groups?id=1', )