Skip to content

Commit

Permalink
[RED] Add base code and test for Login feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesSetiawan committed Feb 22, 2024
1 parent 5c49d94 commit 58e1696
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
33 changes: 22 additions & 11 deletions authentication/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -13,7 +17,7 @@ def testRegisterValid(self):
"password":"pass1",
"email":"[email protected]"
}
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())

Expand All @@ -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):
Expand All @@ -35,20 +38,28 @@ 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 = {
"username":"user1",
"password":"pass1",
"email":"[email protected]"
}
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 protected]',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)
5 changes: 5 additions & 0 deletions authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == '':
Expand Down

0 comments on commit 58e1696

Please sign in to comment.