Created
January 23, 2019 07:55
-
-
Save fahdjamy/0cdf51292fde4ce8629790ca67661774 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import json | |
from django.test import TestCase | |
from django.urls import reverse | |
from rest_framework.test import APIClient | |
from rest_framework import status | |
from author_heaven.apps.users.models import User | |
""" | |
The test case below tests for user registration | |
1. The user model | |
2. And the Api endpoint that registers a new user | |
""" | |
class TestUserModelAndApiEndpoint(TestCase): | |
def setUp(self): | |
self.client = APIClient() | |
def test_create_user_with_model(self): | |
user = User.objects.create( | |
username='henney', email='[email protected]', password='krs1krs1') | |
self.assertEqual(user.username, 'henney') | |
self.assertEqual(user.email, '[email protected]') | |
def test_raise_type_error_with_missing_values(self): | |
with self.assertRaises(TypeError): | |
User.objects.create(username='henney', | |
email='[email protected]', password=None) | |
with self.assertRaises(TypeError): | |
User.objects.create(username='henney', | |
email="", password='krs1krs1') | |
def test_user_creation_with_api_endpoint(self): | |
with self.client as c: | |
response = c.post(reverse("user-registration"), data=json.dumps({ | |
'name': 'maggie', | |
'email': '[email protected]', | |
'password'='krs1krs1' | |
}), headers={'content_type': "application/json"}) | |
expected_message = { | |
'message': 'user has been registered successfully'} | |
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | |
self.assertEqual(json.loads(response.data), expected_message) | |
def test_user_creation_api_endpoint_with_an_already_registered_email(self): | |
User.objects.create(username='henney', | |
email='[email protected]', password='krs1krs1') | |
with self.client as c: | |
response = c.post(reverse("user-registration"), data=json.dumps({ | |
'name': 'maggie', | |
'email': '[email protected]', | |
'password'='krs1krs1' | |
}), headers={'content_type': "application/json"}) | |
expected_message = {'message': 'sorry that email is already taken'} | |
# 400 for a bad request | |
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | |
self.assertEqual(json.loads(response.data), expected_message) | |
def test_user_creation_api_endpoint_missing_a_param(self): | |
with self.client as c: | |
response = c.post(reverse("user-registration"), data=json.dumps({ | |
'name': 'maggie',, | |
'password'='krs1krs1' | |
}), headers={'content_type': "application/json"}) | |
expected_message = {'message': {'email':'this field is required'}} | |
# 400 for a bad request | |
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | |
self.assertEqual(json.loads(response.data), expected_message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment