-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added endpoint for getting an auth token
- Loading branch information
Showing
5 changed files
with
96 additions
and
3 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
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
from rest_framework import status | ||
|
||
CREATE_USER_URL = reverse('user:create') | ||
TOKEN_URL = reverse('user:token') | ||
|
||
|
||
def create_user(**params): | ||
|
@@ -17,6 +18,10 @@ class PublicUserAPI(TestCase): | |
def setUp(self): | ||
self.client = APIClient() | ||
|
||
""" | ||
Create User Tests | ||
""" | ||
|
||
def test_create_user_success(self): | ||
payload = { | ||
'email': '[email protected]', | ||
|
@@ -59,3 +64,56 @@ def test_password_too_short_error(self): | |
email=payload['email'] | ||
).exists() | ||
self.assertFalse(user_exists) | ||
|
||
""" | ||
Create Token Tests | ||
""" | ||
|
||
def test_create_token_for_user(self): | ||
user_details = { | ||
'email': '[email protected]', | ||
'password': 'testpass123', | ||
'name': 'Test Name' | ||
} | ||
create_user(**user_details) | ||
|
||
payload = { | ||
'email': user_details['email'], | ||
'password': user_details['password'] | ||
} | ||
res = self.client.post(TOKEN_URL, payload) | ||
|
||
self.assertIn('token', res.data) | ||
self.assertEqual(res.status_code, status.HTTP_200_OK) | ||
|
||
def test_create_token_bad_credentials(self): | ||
user_details = { | ||
'email': '[email protected]', | ||
'password': 'goodpass123' | ||
} | ||
create_user(**user_details) | ||
|
||
payload = { | ||
'email': user_details['email'], | ||
'password': 'badpass123' | ||
} | ||
res = self.client.post(TOKEN_URL, payload) | ||
|
||
self.assertNotIn('token', res.data) | ||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
def test_create_token_blank_password(self): | ||
user_details = { | ||
'email': '[email protected]', | ||
'password': 'testpass123' | ||
} | ||
create_user(**user_details) | ||
|
||
payload = { | ||
'email': user_details['email'], | ||
'password': '' | ||
} | ||
res = self.client.post(TOKEN_URL, payload) | ||
|
||
self.assertNotIn('token', res.data) | ||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) |
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,6 +1,14 @@ | ||
from rest_framework import generics | ||
from user.serializers import UserSerializer | ||
from rest_framework.authtoken.views import ObtainAuthToken | ||
from rest_framework.settings import api_settings | ||
|
||
from user.serializers import UserSerializer, AuthTokenSerializer | ||
|
||
|
||
class CreateUserView(generics.CreateAPIView): | ||
serializer_class = UserSerializer | ||
|
||
|
||
class CreateTokenView(ObtainAuthToken): | ||
serializer_class = AuthTokenSerializer | ||
renderer_class = api_settings.DEFAULT_RENDERER_CLASSES |