-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test : Create unit tests for User Info , Login & Register
- Loading branch information
1 parent
3bdd883
commit 4381cda
Showing
5 changed files
with
179 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from django.urls import reverse | ||
from django.contrib.auth import get_user_model | ||
from rest_framework import status | ||
from rest_framework.test import APIClient | ||
from rest_framework.test import APITestCase | ||
import json | ||
|
||
User = get_user_model() | ||
|
||
class UserInfoTests(APITestCase): | ||
def setUp(self): | ||
|
||
# Needed for token auths | ||
self.client = APIClient() | ||
|
||
# Register | ||
self.register_data = { | ||
"username":"testuser1", | ||
"email":"[email protected]", | ||
"password":"hello", | ||
"confirm_password":"hello" | ||
} | ||
test_user = User(username=self.register_data['username'],email=self.register_data['email'],is_active=True) | ||
test_user.set_password(self.register_data['password']) | ||
test_user.save() | ||
|
||
# Login and get real token | ||
login_data = { | ||
"username":self.register_data['username'], | ||
"password":self.register_data['password'] | ||
} | ||
response = self.client.post('http://localhost:8000/api/token_auth/token/',login_data,format='json', headers={'Content-Type':'application/json'}) | ||
self.access_token = response.json()['access'] | ||
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.access_token) | ||
|
||
def test_get_user_info_successfully_empty(self): | ||
|
||
response = self.client.get('http://localhost:8000/api/info/', format = 'json') | ||
self.assertEqual(response.data,[]) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_post_user_info_successfully(self): | ||
|
||
body = { | ||
"name":"Test User 1 Full Name", | ||
"user_type":"admin", | ||
"zulip_id": 334084 | ||
} | ||
response = self.client.post('http://localhost:8000/api/info/', body ,format = 'json') | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
|
||
def test_get_user_info_successfully(self): | ||
|
||
response = self.client.get('http://localhost:8000/api/info/', format = 'json') | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_api_wrong_token(self): | ||
|
||
self.client.credentials() | ||
response = self.client.get('http://localhost:8000/api/info/', format = 'json') | ||
self.assertEqual(response.data['detail'],"Authentication credentials were not provided.") | ||
self.assertEqual(response.status_code, 403) | ||
|
||
def test_multiple_post_user_info(self): | ||
|
||
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.access_token) | ||
body = { | ||
"name":"Test User 1 Full Name", | ||
"user_type":"admin", | ||
"zulip_id": 334084 | ||
} | ||
for _ in range(2): | ||
response = self.client.post('http://localhost:8000/api/info/', body ,format = 'json') | ||
self.assertEqual(response.status_code, status.HTTP_409_CONFLICT) | ||
|
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,78 @@ | ||
from django.urls import reverse | ||
from django.contrib.auth import get_user_model | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
|
||
User = get_user_model() | ||
|
||
class LoginTests(APITestCase): | ||
def setUp(self): | ||
|
||
# Register | ||
self.register_data = { | ||
"username":"testuser1", | ||
"email":"[email protected]", | ||
"password":"hello", | ||
"confirm_password":"hello" | ||
} | ||
test_user = User(username=self.register_data['username'],email=self.register_data['email'],is_active=True) | ||
# hashing password | ||
test_user.set_password(self.register_data['password']) | ||
test_user.save() | ||
|
||
def test_login_successfully(self): | ||
""" | ||
Ensure we can login . | ||
""" | ||
|
||
# Login | ||
login_data = { | ||
"username":self.register_data['username'], | ||
"password":self.register_data['password'] | ||
} | ||
response = self.client.post('http://localhost:8000/api/token_auth/token/',login_data,format='json', headers={'Content-Type':'application/json'}) | ||
token = response.data['access'] | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_login_invalid_username(self): | ||
""" | ||
Ensure we can login . | ||
""" | ||
|
||
# Login | ||
login_data = { | ||
"username":"nottestuser1", | ||
"password":self.register_data['password'] | ||
} | ||
response = self.client.post('http://localhost:8000/api/token_auth/token/',login_data,format='json', headers={'Content-Type':'application/json'}) | ||
|
||
self.assertEqual(response.data['detail'],'No active account found with the given credentials') | ||
self.assertEqual(response.status_code, 401) | ||
|
||
def test_login_invalid_password(self): | ||
""" | ||
Ensure we can login . | ||
""" | ||
|
||
# Login | ||
login_data = { | ||
"username":self.register_data['username'], | ||
"password":"hell" | ||
} | ||
response = self.client.post('http://localhost:8000/api/token_auth/token/',login_data,format='json', headers={'Content-Type':'application/json'}) | ||
|
||
self.assertEqual(response.data['detail'],'No active account found with the given credentials') | ||
self.assertEqual(response.status_code, 401) | ||
|
||
def test_login_bad_data(self): | ||
""" | ||
Ensure we can login . | ||
""" | ||
|
||
# Login | ||
login_data = { | ||
"username":self.register_data['username'], | ||
} | ||
response = self.client.post('http://localhost:8000/api/token_auth/token/',login_data,format='json', headers={'Content-Type':'application/json'}) | ||
token = response.data | ||
self.assertEqual(response.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from django.urls import reverse | ||
from django.contrib.auth import get_user_model | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
|
||
User = get_user_model() | ||
|
||
class RegisterTests(APITestCase): | ||
def test_register_successfully(self): | ||
|
||
# Register | ||
register_data = { | ||
"username":"testuser1", | ||
"email":"[email protected]", | ||
"password":"hello", | ||
"confirm_password":"hello" | ||
} | ||
response = self.client.post('http://localhost:8000/api/token_auth/register/', register_data, format='json', headers={'Content-Type':'application/json'}) | ||
self.assertEqual(response.status_code,status.HTTP_201_CREATED) |