From 58e16965619d4553d489e2a65709504190951985 Mon Sep 17 00:00:00 2001 From: JohannesSetiawan Date: Thu, 22 Feb 2024 19:51:40 +0700 Subject: [PATCH] [RED] Add base code and test for Login feature. --- authentication/tests.py | 33 ++++++++++++++++++++++----------- authentication/views.py | 5 +++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/authentication/tests.py b/authentication/tests.py index d77da07..596df76 100644 --- a/authentication/tests.py +++ b/authentication/tests.py @@ -3,7 +3,11 @@ from django.urls import reverse import json +REGISTER_LINK = reverse('authentication:register') +LOGIN_LINK = reverse('authentication:login') + class RegisterTest(TestCase): + def setUp(self): self.client = Client() @@ -13,7 +17,7 @@ def testRegisterValid(self): "password":"pass1", "email":"email1@email.com" } - response = self.client.post(reverse('authentication:register'), json.dumps(data), content_type='application/json') + response = self.client.post(REGISTER_LINK, json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 200) self.assertTrue(AppUser.objects.filter(username='user1').exists()) @@ -23,10 +27,9 @@ def testEmailFormatNotValid(self): "password":"pass1", "email":"emailnotvalid" } - response = self.client.post(reverse('authentication:register'), json.dumps(data), content_type='application/json') + response = self.client.post(REGISTER_LINK, json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 400) - resp_msg = json.loads(response.content.decode('utf-8'))['msg'] - self.assertEqual(resp_msg,"Email format is wrong.") + self.assertEqual(response.json()['msg'],"Email format is wrong.") self.assertFalse(AppUser.objects.filter(username='user1').exists()) def testEmptyInput(self): @@ -35,10 +38,9 @@ def testEmptyInput(self): "password":"", "email":"" } - response = self.client.post(reverse('authentication:register'), json.dumps(data), content_type='application/json') + response = self.client.post(REGISTER_LINK, json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 400) - resp_msg = json.loads(response.content.decode('utf-8'))['msg'] - self.assertEqual(resp_msg,"Empty input! Make sure all the fields are filled.") + self.assertEqual(response.json()['msg'],"Empty input! Make sure all the fields are filled.") def testUsernameIsAlreadyExist(self): data = { @@ -46,9 +48,18 @@ def testUsernameIsAlreadyExist(self): "password":"pass1", "email":"email1@email.com" } - response = self.client.post(reverse('authentication:register'), json.dumps(data), content_type='application/json') + response = self.client.post(REGISTER_LINK, json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 200) - response = self.client.post(reverse('authentication:register'), json.dumps(data), content_type='application/json') + response = self.client.post(REGISTER_LINK, json.dumps(data), content_type='application/json') self.assertEqual(response.status_code, 400) - resp_msg = json.loads(response.content.decode('utf-8'))['msg'] - self.assertEqual(resp_msg,"Username and/or email already taken!") + self.assertEqual(response.json()['msg'],"Username and/or email already taken!") + +class LoginTest(TestCase): + def setUp(self): + self.client = Client() + self.user = AppUser.objects.create_user(email='email@email.com',username='testuser',password='test') + + def testLoginSuccessful(self): + data = {'username': 'testuser', 'password': 'testpassword'} + response = self.client.post(LOGIN_LINK, data=data) + self.assertEqual(response.status_code, 200) \ No newline at end of file diff --git a/authentication/views.py b/authentication/views.py index a2fb788..d3b2781 100644 --- a/authentication/views.py +++ b/authentication/views.py @@ -22,6 +22,11 @@ def post(self, request): new_user.save() return JsonResponse({'msg': 'Success! User registered'}, status=200) +@method_decorator(csrf_exempt, name='dispatch') +class LoginView(View): + def post(self, request): + return 'None' + class Validators(): def validateInput(username, email, password): if username == '' or password == '' or email == '':