-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(jwt):configure jwt on login/registration
-return a token on login/registration -write tests to check for the token [starts #162949210]
- Loading branch information
Showing
9 changed files
with
139 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,24 @@ | ||
# import jwt | ||
# | ||
# from django.conf import settings | ||
# | ||
# from rest_framework import authentication, exceptions | ||
# | ||
# from .models import User | ||
""" | ||
JWT configuration | ||
""" | ||
from rest_framework_jwt.settings import api_settings | ||
|
||
"""Configure JWT Here""" | ||
|
||
""" | ||
By default, django-restframework--jwt generates a token | ||
here we create a function to manually create a token | ||
with user data of our choosing | ||
Here the token is passed the user email | ||
All the the jwt configurations are defined globally in the project settings.py | ||
""" | ||
|
||
|
||
def get_jwt_token(user): | ||
""" | ||
Creates a token manually | ||
more info: https://getblimp.github.io/django-rest-framework-jwt/ | ||
""" | ||
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER | ||
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER | ||
payload = jwt_payload_handler(user) | ||
return jwt_encode_handler(payload) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,5 +116,3 @@ def get_short_name(self): | |
the user's real name, we return their username instead. | ||
""" | ||
return self.username | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
Module to test if token is returned during login_url/registration | ||
""" | ||
from rest_framework import status | ||
from django.test import TestCase, Client | ||
from django.urls import reverse | ||
import json | ||
|
||
|
||
# initialize the APIClient app | ||
client = Client() | ||
|
||
|
||
class TestTokenGeneration(TestCase): | ||
""" Test user registration/login_url returns token """ | ||
|
||
def setUp(self): | ||
self.user_data = { | ||
'user': { | ||
'username': 'fooo', | ||
'email': 'cake@foo.com', | ||
'password': 'yertg234D' | ||
} | ||
} | ||
|
||
def test_token_gen_on_signup(self): | ||
""" | ||
Test if a token is returned after registration | ||
""" | ||
response = client.post( | ||
reverse('authentication:signup_url'), | ||
data=json.dumps(self.user_data), | ||
content_type='application/json' | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
# assert token is in the response | ||
self.assertIn('token', response.data) | ||
|
||
def test_login_url_returns_token(self): | ||
""" | ||
Test if token is returned after login_url | ||
""" | ||
client.post( | ||
reverse('authentication:signup_url'), | ||
data=json.dumps(self.user_data), | ||
content_type='application/json' | ||
) | ||
response = client.post( | ||
reverse('authentication:login_url'), | ||
data=json.dumps(self.user_data), | ||
content_type='application/json' | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
# assert token is in the response | ||
self.assertIn('token', response.data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
|
||
from django.urls import path | ||
from .views import ( | ||
LoginAPIView, RegistrationAPIView, UserRetrieveUpdateAPIView | ||
) | ||
|
||
""" Django 2.0 requires the app_name variable set when using include namespace""" | ||
""" | ||
Django 2.0 requires the app_name variable set when using include namespace | ||
""" | ||
app_name = 'authentication' | ||
|
||
urlpatterns = [ | ||
path('user/', UserRetrieveUpdateAPIView.as_view()), | ||
path('users/', RegistrationAPIView.as_view(), name = "signup_url"), | ||
path('users/login/', LoginAPIView.as_view(), name = "login_url"), | ||
path('user/', UserRetrieveUpdateAPIView.as_view(), name="deatails"), | ||
path('users/', RegistrationAPIView.as_view(), name="signup_url"), | ||
path('users/login/', LoginAPIView.as_view(), name="login_url"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters