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 5b31def commit cfde344
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ script:
- coverage run --source='.' manage.py test --settings=authors.settings.testing

after_success:
- coveralls
- coveralls

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 RegisterUserTestCase(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 cfde344

Please sign in to comment.