Commit f0b594f6 authored by David Haynes's avatar David Haynes 🙆
Browse files

Merge branch '43-unittests' into '2.2-dev'

Establish framework for testing and complete test_urls.py and test_pf

See merge request !73
parents 98bed2aa fe4a55ef
Pipeline #861 passed with stage
in 1 minute and 11 seconds
...@@ -14,3 +14,4 @@ venv ...@@ -14,3 +14,4 @@ venv
.coverage .coverage
htmlcov/ htmlcov/
.idea .idea
__pycache__/
...@@ -33,9 +33,21 @@ Go-py2.7: ...@@ -33,9 +33,21 @@ Go-py2.7:
script: script:
- python manage.py test - python manage.py test
Go-py3.4:
image: library/python:3.4
type: test
script:
- python manage.py test
Go-py3.5:
image: library/python:3.5
type: test
script:
- python manage.py test
Go-py3.6: Go-py3.6:
image: library/python:3.6 image: library/python:3.6
type: test type: test
script: script:
- coverage run --source=go --omit=*migrations/* manage.py test - coverage run --source=go --omit=*migrations/*,*admin.py,*manage.py,*wsgi.py,*settings.py,*secret.py,*__init__.py,*.pyc,*templates/*,*static/* manage.py test
- coverage html -i && grep pc_cov htmlcov/index.html | egrep -o "[0-9]+\%" | awk '{ print "covered " $1;}' - coverage html -i && grep pc_cov htmlcov/index.html | egrep -o "[0-9]+\%" | awk '{ print "covered " $1;}'
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division
# Django Imports
from django.test import TestCase
# App Imports
from .expirelinks import *
"""
Test cases for the functions in expirelinks
"""
class ExpireLinksTest(TestCase):
"""
Default test case, does not actually test anything
"""
def test_Django_Test(self):
self.assertEqual("Hello World!", "Hello World!")
...@@ -13,29 +13,39 @@ from go.models import RegisteredUser ...@@ -13,29 +13,39 @@ from go.models import RegisteredUser
register = template.Library() 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 @register.filter
def is_registered(user): def is_registered(givenUser):
# try getting the RegisteredUser of the current user # try getting the RegisteredUser of the current user
try: try:
registered = RegisteredUser.objects.get(username=user.username) getRegisteredUser = RegisteredUser.objects.get(user=givenUser)
# if it works then the user is registered # if it works then the user is registered
return True return getRegisteredUser.registered
# This should never happen
except RegisteredUser.DoesNotExist as ex: except RegisteredUser.DoesNotExist as ex:
print(ex)
# if they don't exist then they are not registered # if they don't exist then they are not registered
return False 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 @register.filter
def is_approved(user): def is_approved(givenUser):
# try getting the RegisteredUser of the current user # try getting the RegisteredUser of the current user
try: 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) # 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: except RegisteredUser.DoesNotExist as ex:
print(ex)
# if they don't exist then they are not approved # if they don't exist then they are not approved
return False return False
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division
# Django Imports
from django.test import TestCase
from django.contrib.auth.models import User
# App Imports
from .go_extras import is_registered, is_approved
from go.models import RegisteredUser
"""
Test cases for the template helper functions in go_extras.py
"""
class GoExtrasTest(TestCase):
"""
Create a dummy user to be tested against.
"""
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))
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division
# Django Imports
from django.test import TestCase
# App Imports
from go.cas_callbacks import pfparse, pfinfo, create_user
"""
Test cases for the functions in call_callbacks.
"""
class CasCallbacksTest(TestCase):
"""
Presently enrolled student who has been added to peoplefinder
"""
def test_pf_peoplefinder_method(self):
actual = pfinfo('dhaynes3')
expected = ['David', 'Haynes']
self.assertEqual(expected, actual)
"""
Test the parsing method to ensure that first and last names are seperated
accordingly and correctly.
"""
def test_pfparse_peoplefinder_method(self):
actual = pfparse("Haynes, David M")
expected = ['David', 'Haynes']
self.assertEqual(expected, actual)
"""
student no longer in peoplefinder, or who hasn't yet been added
"""
def test_pfinfo_ldap_method(self):
actual = pfinfo('lfaraone')
expected = ['Luke W', 'Faraone']
self.assertEqual(expected, actual)
"""
student employees will have their staff info return before their student info
"""
def test_pfinfo_employee_method(self):
actual = pfinfo('nander13')
expected = ['Nicholas', 'Anderson']
self.assertEqual(expected, actual)
"""
a name not found for either (should never happen, but gracefully handle anyway)
"""
def test_pfinfo_dne(self):
actual = pfinfo('bobama')
expected = ['', '']
self.assertEqual(expected, actual)
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division
# Django Imports
from django.test import TestCase
# App Imports
from go.forms import *
"""
Test cases for the URL form
"""
class URLFormTest(TestCase):
"""
Default test case, does not actually test anything
"""
def test_Django_Test(self):
self.assertEqual("Hello World!", "Hello World!")
"""
Test cases for the Signup form
"""
class SignupForm(TestCase):
"""
Default test case, does not actually test anything
"""
def test_Django_Test(self):
self.assertEqual("Hello World!", "Hello World!")
...@@ -8,19 +8,17 @@ from django.contrib.auth.models import User ...@@ -8,19 +8,17 @@ from django.contrib.auth.models import User
# App Imports # App Imports
from go.models import URL, RegisteredUser from go.models import URL, RegisteredUser
"""
Test cases for the URL Model
"""
class URLTest(TestCase):
"""
Default test case, does not actually test anything
"""
def test_Django_Test(self):
self.assertEqual("Hello World!", "Hello World!")
""" """
Test cases for the RegisteredUser Model Test cases for the RegisteredUser Model
- check if RegisteredUsers are actually made
- check approval and registration status flipping
- check blocking
- check printing
- add in description
- check organization field
- check full name field
- check print(RegisteredUser)
""" """
class RegisteredUserTest(TestCase): class RegisteredUserTest(TestCase):
...@@ -31,3 +29,23 @@ class RegisteredUserTest(TestCase): ...@@ -31,3 +29,23 @@ class RegisteredUserTest(TestCase):
getUser = User.objects.get(username='dhaynes') getUser = User.objects.get(username='dhaynes')
getRegisteredUser = RegisteredUser.objects.get(user=getUser) getRegisteredUser = RegisteredUser.objects.get(user=getUser)
self.assertTrue(getRegisteredUser) self.assertTrue(getRegisteredUser)
def test_checkPrint(self):
# expected = '<Registered User: %s - Approval Status: %s>' % (self.user, self.approved)
self.assertTrue(True)
"""
Test cases for the URL Model
- check if URL's are actually created
- modify clicks (social, qr, normal)
- check expiration date creation
- check print function
"""
class URLTest(TestCase):
"""
Default test case, does not actually test anything
"""
def test_Django_Test(self):
self.assertEqual("Hello World!", "Hello World!")
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division
# Django Imports
from django.test import TestCase
# App Imports
from go.views import *
"""
Test cases for the index view
"""
class IndexTest(TestCase):
"""
Default test case, does not actually test anything
"""
def test_Django_Test(self):
self.assertEqual("Hello World!", "Hello World!")
"""
Test cases for the "view" view
"""
class ViewTest(TestCase):
"""
Default test case, does not actually test anything
"""
def test_Django_Test(self):
self.assertEqual("Hello World!", "Hello World!")
"""
Test cases for the my_links view
"""
class MyLinksTest(TestCase):
"""
Default test case, does not actually test anything
"""
def test_Django_Test(self):
self.assertEqual("Hello World!", "Hello World!")
"""
Test cases for the delete view
"""
class DeleteTest(TestCase):
"""
Default test case, does not actually test anything
"""
def test_Django_Test(self):
self.assertEqual("Hello World!", "Hello World!")
"""
Test cases for the signup view
"""
class SignupTest(TestCase):
"""
Default test case, does not actually test anything
"""
def test_Django_Test(self):
self.assertEqual("Hello World!", "Hello World!")
"""
Test cases for the redirection view
"""
class RedirectionTest(TestCase):
"""
Default test case, does not actually test anything
"""
def test_Django_Test(self):
self.assertEqual("Hello World!", "Hello World!")
"""
Test cases for the useradmin view
"""
class UserAdminTest(TestCase):
"""
Default test case, does not actually test anything
"""
def test_Django_Test(self):
self.assertEqual("Hello World!", "Hello World!")
...@@ -59,7 +59,6 @@ def index(request): ...@@ -59,7 +59,6 @@ def index(request):
@ratelimit(key='user', rate='3/m', method='POST', block=True) @ratelimit(key='user', rate='3/m', method='POST', block=True)
@ratelimit(key='user', rate='25/d', method='POST', block=True) @ratelimit(key='user', rate='25/d', method='POST', block=True)
def post(request): def post(request):
# Now we initialize the form again but this time we have the POST # Now we initialize the form again but this time we have the POST
# request # request
url_form = URLForm(request.POST, host = request.META.get('HTTP_HOST')) url_form = URLForm(request.POST, host = request.META.get('HTTP_HOST'))
...@@ -158,7 +157,6 @@ def my_links(request): ...@@ -158,7 +157,6 @@ def my_links(request):
}, },
) )
""" """
This view deletes a URL if you have the permission to. User must be This view deletes a URL if you have the permission to. User must be
logged in and registered, and must also be the owner of the URL. logged in and registered, and must also be the owner of the URL.
......
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division
# Django Imports
from django.test import TestCase
from django.urls import reverse, resolve
# App Imports
from .urls import *
# https://stackoverflow.com/questions/18987051/how-do-i-unit-test-django-urls
"""
Test cases for the urls
"""
class UrlsTest(TestCase):
"""
/ - Homepage url.
"""
def test_index_reverse(self):
url = reverse('index')
self.assertEqual(url, '/')
"""
/view/<short> - View URL data.
"""
def test_view_reverse_chars(self):
url = reverse('view', args=['dhaynes'])
self.assertEqual(url, '/view/dhaynes')
"""
/view/<short> - View URL data.
"""
def test_view_reverse_ints(self):
url = reverse('view', args=['123456789'])
self.assertEqual(url, '/view/123456789')
"""
/view/<short> - View URL data.
"""
def test_view_reverse_chars_ints(self):
url = reverse('view', args=['dhaynes123'])
self.assertEqual(url, '/view/dhaynes123')
"""
/view/<short> - View URL data.
"""
def test_view_reverse_full_slug(self):
url = reverse('view', args=['dhaynes123_-'])
self.assertEqual(url, '/view/dhaynes123_-')
"""
/about - About page.
"""
def test_about_reverse(self):
url = reverse('about')
self.assertEqual(url, '/about')
"""
/signup - Signup page for access.
"""
def test_signup_reverse(self):
url = reverse('signup')
self.assertEqual(url, '/signup')
"""
/myLinks - My-Links page, view and review links.
"""
def test_my_links_reverse(self):
url = reverse('my_links')
self.assertEqual(url, '/myLinks')
"""
/delete/<short> - Delete a link, no content display.
"""
def test_delete_reverse_chars(self):
url = reverse('delete', args=['dhaynes'])
self.assertEqual(url, '/delete/dhaynes')
"""
/delete/<short> - Delete a link, no content display.
"""
def test_delete_reverse_ints(self):
url = reverse('delete', args=['123456789'])
self.assertEqual(url, '/delete/123456789')
"""
/delete/<short> - Delete a link, no content display.
"""
def test_delete_reverse_chars_ints(self):
url = reverse('delete', args=['dhaynes123'])
self.assertEqual(url, '/delete/dhaynes123')
"""
/delete/<short> - Delete a link, no content display.
"""
def test_delete_reverse_full_slug(self):
url = reverse('delete', args=['dhaynes123_-'])
self.assertEqual(url, '/delete/dhaynes123_-')
"""
/registered - registration complete page
"""
def test_registered_reverse(self):
url = reverse('registered')
self.assertEqual(url, '/registered')
# The /admin URL is not tested as it is never resolves in source and generally
# Django yells at you if the admin page breaks
"""
/useradmin - user approval interface
"""
def test_useradmin(self):
url = reverse('useradmin')
self.assertEqual(url, '/useradmin')
"""
/login - login portal
"""
def test_useradmin(self):
url = reverse('go_login')
self.assertEqual(url, '/login')
"""
/logout - logout portal
"""
def test_useradmin(self):
url = reverse('go_logout')
self.assertEqual(url, '/logout')
"""
/<short> - Redirect to a go link.
"""
def test_delete_chars(self):
url = reverse('redirection', args=['dhaynes'])
self.assertEqual(url, '/dhaynes')
"""
/<short> - Redirect to a go link.
"""
def test_delete_ints(self):
url = reverse('redirection', args=['123456789'])
self.assertEqual(url, '/123456789')
"""
/<short> - Redirect to a go link.
"""
def test_delete_chars_ints(self):
url = reverse('redirection', args=['dhaynes123'])
self.assertEqual(url, '/dhaynes123')
"""
/<short> - Redirect to a go link.
"""
def test_delete_full_slug(self):
url = reverse('redirection', args=['dhaynes123_-'])
self.assertEqual(url, '/dhaynes123_-')
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division
# Django Imports # Django Imports
from django.conf.urls import include, url from django.conf.urls import include, url
import django.contrib.auth.views import django.contrib.auth.views
...@@ -21,22 +24,24 @@ urlpatterns = [ ...@@ -21,22 +24,24 @@ urlpatterns = [
url(r'^view/(?P<short>[-\w]+)$', go.views.view, name='view'), url(r'^view/(?P<short>[-\w]+)$', go.views.view, name='view'),