Skip to content

Commit

Permalink
Merge c3c1a22 into 39d0c7d
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipseryazi committed Jul 26, 2018
2 parents 39d0c7d + c3c1a22 commit 9c8a812
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 25 deletions.
4 changes: 4 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[report]
show_missing = True
omit =
*/python?.?/*
*serializers*
*renderers*
*tests*
*__init__*

2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ run:


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

after_success:
- coveralls
6 changes: 5 additions & 1 deletion authors/apps/authentication/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

"""Configure JWT Here"""


class JWTAuthentication:
pass
def authenticate(self, val):
pass

def authenticate_header(self,val):
pass
40 changes: 20 additions & 20 deletions authors/apps/authentication/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
)
from django.db import models


class UserManager(BaseUserManager):
"""
Django requires that custom users define their own Manager class. By
Expand All @@ -33,21 +34,21 @@ def create_user(self, username, email, password=None):
return user

def create_superuser(self, username, email, password):
"""
Create and return a `User` with superuser powers.
"""
Create and return a `User` with superuser powers.
Superuser powers means that this use is an admin that can do anything
they want.
"""
if password is None:
raise TypeError('Superusers must have a password.')
Superuser powers means that this use is an admin that can do anything
they want.
"""
if password is None:
raise TypeError('Superusers must have a password.')

user = self.create_user(username, email, password)
user.is_superuser = True
user.is_staff = True
user.save()
user = self.create_user(username, email, password)
user.is_superuser = True
user.is_staff = True
user.save()

return user
return user


class User(AbstractBaseUser, PermissionsMixin):
Expand Down Expand Up @@ -102,19 +103,18 @@ def __str__(self):

@property
def get_full_name(self):
"""
This method is required by Django for things like handling emails.
Typically, this would be the user's first and last name. Since we do
not store the user's real name, we return their username instead.
"""
return self.username
"""
This method is required by Django for things like handling emails.
Typically, this would be the user's first and last name. Since we do
not store the user's real name, we return their username instead.
"""
return self.username

@property
def get_short_name(self):
"""
This method is required by Django for things like handling emails.
Typically, this would be the user's first name. Since we do not store
the user's real name, we return their username instead.
"""
return self.username


Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from django.test import TestCase, RequestFactory
from authors.apps.authentication.views import UserRetrieveUpdateAPIView
from authors.apps.authentication.models import User


class AuthenticationTestCase(TestCase):
def setUp(self):
self.factory = RequestFactory()
self.manager = User.objects
self.user = self.manager.create_user(
'username', 'email@gmail.com', 'password')

def test_no_auth_headers(self):
"""tests whether authentiction has headers """
request = self.factory.get('api/user/')
response = UserRetrieveUpdateAPIView.as_view()(request)
self.assertEqual(response.status_code, 403)

def test_create_user_success(self):
"""tests whether user is created successfully"""
self.assertEqual(self.user.email, 'email@gmail.com')
self.assertEqual(self.user.get_full_name, 'username')
self.assertEqual(self.user.get_short_name, 'username')

def test_user_create_without_password_success(self):
"""tests whether user is created successfully without password"""
user = self.manager.create_user(
username='username2', email='email2@gmail.com', password=None)
self.assertEqual(user.email, 'email2@gmail.com')

def test_user_created_instance(self):
self.assertTrue(isinstance(self.user, object))

def test_create_user_fail_no_email(self):
"""raise the appropriate exception when an email is missing"""
with self.assertRaises(Exception) as context:
self.manager.create_user(
username='useranme', email=None, password='password')
self.assertTrue(
'Users must have an email address.' in str(context.exception))

def test_create_user_fail_no_username(self):
"""raise the appropriate exception when a username is missing"""
with self.assertRaises(Exception) as context:
self.manager.create_user(
username=None, email='user@gmail.com', password='password')
self.assertTrue(
'Users must have a username.' in str(context.exception))

def test_create_super_user_success(self):
"""tests whether super user is created successfully"""
super_user = self.manager.create_superuser(
'superuser', 'superuser@gmail.com', 'password')
self.assertEqual(super_user.email, 'superuser@gmail.com')
self.assertTrue(super_user.is_superuser)
self.assertTrue(super_user.is_staff)
self.assertEqual(super_user.get_full_name, 'superuser')
self.assertEqual(super_user.get_short_name, 'superuser')

def test_create_super_user_fail_no_password(self):
"""raise the appropriate exception when super user password is missing"""
with self.assertRaises(Exception) as context:
self.manager.create_superuser(
username='superuser', email='superuser@gmail.com', password=None)
self.assertTrue(
'Superusers must have a password.' in str(context.exception))
3 changes: 1 addition & 2 deletions authors/apps/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,4 @@ def update(self, request, *args, **kwargs):
serializer.is_valid(raise_exception=True)
serializer.save()

return Response(serializer.data, status=status.HTTP_200_OK)

return Response(serializer.data, status=status.HTTP_200_OK)
1 change: 0 additions & 1 deletion authors/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@

path('api/', include('authors.apps.authentication.urls', namespace='authentication')),
]

0 comments on commit 9c8a812

Please sign in to comment.