Skip to content

Commit

Permalink
feat(JWT): User should receive JWT token upon registration
Browse files Browse the repository at this point in the history
-user gets JWT token after registration
-registered user can login
-authenticated user can access protected route
[Finishes #164798199]
  • Loading branch information
Daniel Lwetabe authored and Daniel Lwetabe committed Apr 1, 2019
1 parent d256178 commit dd9cd86
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
8 changes: 8 additions & 0 deletions authors/apps/authentication/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
"password":"user@12345"
}
}

auth_change_password = {
"user": {
"email":"testuser@gmail.com",
"password":"abcdefgh"
}
}

invalid_login_data = {
"user": {
"email":"testuser@gmail.comhhfhf",
Expand Down
31 changes: 30 additions & 1 deletion authors/apps/authentication/tests/test_user_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
invalid_login_data,
login_data_miss_email,
login_data_miss_password,
empty_login_data_object
empty_login_data_object,
auth_change_password
)
from .test_base import BaseTestCase

Expand Down Expand Up @@ -129,8 +130,36 @@ def test_user_log_in_credentials_miss_password_and_email(self):
self.assertIn(response.data["errors"]["email"][0], 'This field is required.')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_get_current_user_jwt(self):
"""
Get current user from JWT
"""
self.client.post('/api/users/register/', login_credentials_data, format='json')
response1 = self.client.post('/api/users/login/', login_data, format='json')
resp_token = response1.data["token"]
self.client.credentials(HTTP_AUTHORIZATION = 'Bearer '+ resp_token )
response2 = self.client.get('/api/user/', format='json')
self.assertEqual(response2.data["email"], 'testuser@gmail.com')
self.assertEqual(response2.data["username"], 'user')
self.assertEqual(response2.status_code, status.HTTP_200_OK)

def test_change_password_user_jwt_authenticated(self):

"""
change password of authenticated user
"""
self.client.post('/api/users/register/', login_credentials_data, format='json')
response1 = self.client.post('/api/users/login/', login_data, format='json')

resp_token = response1.data["token"]

self.client.credentials(HTTP_AUTHORIZATION = 'Bearer '+ resp_token )

response2 = self.client.put('/api/user/', auth_change_password,format='json')

self.assertEqual(response2.data["email"], 'testuser@gmail.com')
self.assertEqual(response2.data["username"], 'user')
self.assertEqual(response2.status_code, status.HTTP_200_OK)



Expand Down

0 comments on commit dd9cd86

Please sign in to comment.