Browse Source

we set up all tests required to reach almost 100% of code coverage for the authentification part

pull/250/head
Guillim 5 years ago
parent
commit
8e6d16e6cf
5 changed files with 124 additions and 1 deletions
  1. 2
      .coveragerc
  2. 36
      app/authentification/tests/test_activate.py
  3. 14
      app/authentification/tests/test_signup.py
  4. 71
      app/authentification/tests/test_template.py
  5. 2
      tools/ci.sh

2
.coveragerc

@ -5,6 +5,8 @@ omit =
app/server/migrations/*
app/server/templatetags/*
app/server/tests/*
app/authentification/tests/*
app/authentification/templatetags/*
exclude_lines =
pragma: no cover

36
app/authentification/tests/test_activate.py

@ -0,0 +1,36 @@
from ..utils import activate
from django.test import RequestFactory, TestCase
from django.utils.http import urlsafe_base64_encode
from django.utils.encoding import force_bytes
from django.urls import reverse
from ..forms import SignupForm
from ..tokens import account_activation_token
import re
class TestActivate(TestCase):
def setUp(self):
# Every test needs access to the request factory.
self.factory = RequestFactory()
request_POST = {'username': 'username5648',
'email': 'email@example.com',
'password1': 'pwd0000Y00$$',
'password2': 'pwd0000Y00$$'}
user = SignupForm(request_POST).save(commit=False)
user.save()
self.token = account_activation_token.make_token(user)
self.uid = urlsafe_base64_encode(force_bytes(user.pk)).decode()
def test_activate_invalid(self):
response = self.client.get(reverse('activate', args=['wrong_uid', 'wrong_token']))
self.assertEqual(response.status_code, 200)
needle = '<p>Activation link is invalid!</p>'
m = re.search(needle, str(response.content))
self.assertTrue(m is None)
def test_activate_valid(self):
"""we make sure code is for the /projects redirection"""
response = self.client.get(reverse('activate', args=[self.uid, self.token]))
self.assertRedirects(response, '/projects/')

14
app/authentification/tests/test_signup.py

@ -1,5 +1,6 @@
from django.test import TestCase
from ..forms import SignupForm
from ..tokens import account_activation_token
class TestSignUp(TestCase):
@ -33,3 +34,16 @@ class TestSignUp(TestCase):
'password1': ['This field is required.'],
'password2': ['This field is required.']
})
class TestToken(TestCase):
"""test for token"""
def test_valid_token(self):
request_POST = {'username': 'username5645',
'email': 'email@example.com',
'password1': 'pwd000000',
'password2': 'pwd000000'}
user = SignupForm(request_POST).save(commit=False)
token = account_activation_token.make_token(user)
self.assertTrue(isinstance(token, str))

71
app/authentification/tests/test_template.py

@ -0,0 +1,71 @@
from django.test import SimpleTestCase, TestCase, RequestFactory
from django.http import HttpRequest
from ..views import SignupView
from app import settings
class AddCSSTemplateTagTest(SimpleTestCase):
def test_rendered(self):
request = HttpRequest()
request.method = 'GET'
needle = '<input type="password" name="password1" class=" input" required id="id_password1">'
self.assertInHTML(needle, str(SignupView.as_view()(request, as_string=True).content))
class ViewsTest(SimpleTestCase):
"""Class for testing views"""
def test_mail_not_set_up(self):
if hasattr(settings, 'EMAIL_HOST'):
has_EMAIL_HOST = True
EMAIL_HOST = settings.EMAIL_HOST
delattr(settings, 'EMAIL_HOST')
else:
has_EMAIL_HOST = False
if hasattr(settings, 'EMAIL_BACKEND'):
has_EMAIL_BACKEND = True
EMAIL_BACKEND = settings.EMAIL_BACKEND
delattr(settings, 'EMAIL_BACKEND')
else:
has_EMAIL_BACKEND = False
request = HttpRequest()
request.method = 'POST'
response = SignupView.as_view()(request, as_string=True)
if has_EMAIL_HOST:
settings.EMAIL_HOST = EMAIL_HOST
if has_EMAIL_BACKEND:
settings.EMAIL_BACKEND = EMAIL_BACKEND
needle = "<span>has not set up any emails</span>"
self.assertInHTML(needle, str(response.content))
def test_signup_not_allowed(self):
ALLOW_SIGNUP = settings.ALLOW_SIGNUP
settings.ALLOW_SIGNUP = False
request = HttpRequest()
request.method = 'POST'
response = SignupView.as_view()(request, as_string=True)
settings.ALLOW_SIGNUP = ALLOW_SIGNUP
self.assertEqual(response.status_code, 302)
class ViewsDBTest(TestCase):
"""Class for testing views with DB queries"""
def test_form_submission(self):
self.factory = RequestFactory()
EMAIL_BACKEND = settings.EMAIL_BACKEND
settings.EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
request = self.factory.post('/signup')
request.POST = {'username': 'username5648',
'email': 'email@example.com',
'password1': 'pwd0000Y00$$',
'password2': 'pwd0000Y00$$'
}
response = SignupView.as_view()(request)
needle = '<span>emailed you instructions to activate your account</span>'
settings.EMAIL_BACKEND = EMAIL_BACKEND
self.assertInHTML(needle, str(response.content))

2
tools/ci.sh

@ -4,7 +4,7 @@ set -o errexit
flake8
python app/manage.py migrate
coverage run --source=app app/manage.py test server.tests
coverage run --source=app app/manage.py test server.tests authentification.tests
coverage report
(cd app/server/static && npm run lint)
Loading…
Cancel
Save