Unverified Commit 22372e41 authored by David Haynes's avatar David Haynes
Browse files

Fully test go_extras.py

- unit test each condition
- modify code based on test errors (thanks testing!)
parent 44643277
Pipeline #818 passed with stage
in 1 minute and 7 seconds
......@@ -13,29 +13,39 @@ from go.models import RegisteredUser
register = template.Library()
"""
check if a user is registered
Helper template function to check if a user is registered.
givenUser: The User object that we are checking to see if they are registered
or not.
"""
@register.filter
def is_registered(user):
def is_registered(givenUser):
# try getting the RegisteredUser of the current user
try:
registered = RegisteredUser.objects.get(username=user.username)
getRegisteredUser = RegisteredUser.objects.get(user=givenUser)
# if it works then the user is registered
return True
return getRegisteredUser.registered
# This should never happen
except RegisteredUser.DoesNotExist as ex:
print(ex)
# if they don't exist then they are not registered
return False
"""
check if a user is approved
Helper template function to check if a user is approved.
givenUser: The User object that we are checking to see if they are approved
or not.
"""
@register.filter
def is_approved(user):
def is_approved(givenUser):
# try getting the RegisteredUser of the current user
try:
registered = RegisteredUser.objects.get(username=user.username)
getRegisteredUser = RegisteredUser.objects.get(user=givenUser)
# if they exist, return whether or not they are approved (boolean)
return registered.approved
return getRegisteredUser.approved
# This should never happen
except RegisteredUser.DoesNotExist as ex:
print(ex)
# if they don't exist then they are not approved
return False
......@@ -3,18 +3,69 @@ from __future__ import unicode_literals, absolute_import, print_function, divisi
# Django Imports
from django.test import TestCase
from django.contrib.auth.models import User
# App Imports
from .go_extras import *
from .go_extras import is_registered, is_approved
from go.models import RegisteredUser
"""
Test cases for the functions in expirelinks
Test cases for the template helper functions in go_extras.py
"""
class GoExtrasTest(TestCase):
"""
Default test case, does not actually test anything
Create a dummy user to be tested against.
"""
def test_Django_Test(self):
self.assertEqual("Hello World!", "Hello World!")
def setUp(self):
User.objects.create(username='dhaynes', password='password')
"""
Test the is_registered function to see if it gives correct false answers
"""
def test_is_registeredFalse(self):
getUser = User.objects.get(username='dhaynes')
getRegisteredUser = RegisteredUser.objects.get(user=getUser)
getRegisteredUser.registered = False
getRegisteredUser.save()
self.assertFalse(is_registered(getUser))
"""
Test the is_registered function to see if it gives correct true answers
"""
def test_is_registeredTrue(self):
getUser = User.objects.get(username='dhaynes')
getRegisteredUser = RegisteredUser.objects.get(user=getUser)
getRegisteredUser.registered = True
getRegisteredUser.save()
self.assertTrue(is_registered(getUser))
"""
Test the is_registered function to see if it gives correct false answers
"""
def test_is_approvedFalse(self):
getUser = User.objects.get(username='dhaynes')
getRegisteredUser = RegisteredUser.objects.get(user=getUser)
getRegisteredUser.approved = False
getRegisteredUser.save()
self.assertFalse(is_approved(getUser))
"""
Test the is_registered function to see if it gives correct true answers
"""
def test_is_approvedTrue(self):
getUser = User.objects.get(username='dhaynes')
getRegisteredUser = RegisteredUser.objects.get(user=getUser)
getRegisteredUser.approved = False
getRegisteredUser.save()
self.assertFalse(is_approved(getUser))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment