Skip to content

Commit

Permalink
[Chore #159296016] Write unittests for UserRegistrationView
Browse files Browse the repository at this point in the history
  • Loading branch information
Allan Guwatudde authored and Allan Guwatudde committed Jul 26, 2018
1 parent 39d0c7d commit 2eed248
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ run:


script:
- coverage run --source='.' manage.py test
- coverage run --source='.' manage.py test --settings=authors.settings.testing

after_success:
- coveralls
- coveralls

4 changes: 3 additions & 1 deletion authors/apps/authentication/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
"""Configure JWT Here"""

class JWTAuthentication:
pass

def authenticate(self, x):
pass

Empty file.
80 changes: 80 additions & 0 deletions authors/apps/authentication/tests/test_registration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from django.test import TestCase, RequestFactory
from authors.apps.authentication.views import RegistrationAPIView
import json

class AuthenticationTestCase(TestCase):

def setUp(self):
self.factory = RequestFactory()

self.user = {
"user" : {
"email":"test@gmail.com",
"username":"test",
"password":"testpassword"
}
}

self.request = self.factory.post('/api/users/', data = json.dumps(self.user), content_type='application/json')
self.response = RegistrationAPIView.as_view()(self.request)

def test_user_registration_success(self):

self.assertEqual(self.response.status_code, 201)


def test_user_registration_with_empty_credentials(self):

user = {}
request = self.factory.post('/api/users/', data = json.dumps(user), content_type='application/json')

response = RegistrationAPIView.as_view()(request)

self.assertIn('This field is required.',response.data["errors"]["email"][0])
self.assertIn('This field is required.',response.data["errors"]["username"][0])
self.assertIn('This field is required.',response.data["errors"]["password"][0])
self.assertEqual(response.status_code, 400)


def test_user_registration_with_invalid_email(self):

""" Wrong email address without @ """

user = {
"user" : {
"email":"testgmail.com",
"username":"test",
"password":"testpassword"
}
}

request = self.factory.post('/api/users/', data = json.dumps(user), content_type='application/json')

response = RegistrationAPIView.as_view()(request)

self.assertIn('Enter a valid email address.',response.data["errors"]["email"][0])
self.assertEqual(response.status_code, 400)


def test_user_registration_for_email_that_already_exists(self):

""" Use the same email that was used when setting up """

request = self.factory.post('/api/users/', data = json.dumps(self.user), content_type='application/json')

response = RegistrationAPIView.as_view()(request)

self.assertIn('user with this email already exists.',response.data["errors"]["email"][0])
self.assertEqual(response.status_code, 400)


def test_user_registration_for_username_that_already_exists(self):

""" Use the same username that was used when setting up """

request = self.factory.post('/api/users/', data = json.dumps(self.user), content_type='application/json')

response = RegistrationAPIView.as_view()(request)

self.assertIn('user with this username already exists.',response.data["errors"]["username"][0])
self.assertEqual(response.status_code, 400)

0 comments on commit 2eed248

Please sign in to comment.