Skip to content

Commit 8e6d16e

Browse files
author
Guillim
committed
we set up all tests required to reach almost 100% of code coverage for the authentification part
1 parent 0df433f commit 8e6d16e

File tree

5 files changed

+124
-1
lines changed

5 files changed

+124
-1
lines changed

.coveragerc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ omit =
55
app/server/migrations/*
66
app/server/templatetags/*
77
app/server/tests/*
8+
app/authentification/tests/*
9+
app/authentification/templatetags/*
810

911
exclude_lines =
1012
pragma: no cover
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from ..utils import activate
2+
from django.test import RequestFactory, TestCase
3+
from django.utils.http import urlsafe_base64_encode
4+
from django.utils.encoding import force_bytes
5+
from django.urls import reverse
6+
7+
from ..forms import SignupForm
8+
from ..tokens import account_activation_token
9+
import re
10+
11+
12+
class TestActivate(TestCase):
13+
def setUp(self):
14+
# Every test needs access to the request factory.
15+
self.factory = RequestFactory()
16+
17+
request_POST = {'username': 'username5648',
18+
'email': '[email protected]',
19+
'password1': 'pwd0000Y00$$',
20+
'password2': 'pwd0000Y00$$'}
21+
user = SignupForm(request_POST).save(commit=False)
22+
user.save()
23+
self.token = account_activation_token.make_token(user)
24+
self.uid = urlsafe_base64_encode(force_bytes(user.pk)).decode()
25+
26+
def test_activate_invalid(self):
27+
response = self.client.get(reverse('activate', args=['wrong_uid', 'wrong_token']))
28+
self.assertEqual(response.status_code, 200)
29+
needle = '<p>Activation link is invalid!</p>'
30+
m = re.search(needle, str(response.content))
31+
self.assertTrue(m is None)
32+
33+
def test_activate_valid(self):
34+
"""we make sure code is for the /projects redirection"""
35+
response = self.client.get(reverse('activate', args=[self.uid, self.token]))
36+
self.assertRedirects(response, '/projects/')

app/authentification/tests/test_signup.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.test import TestCase
22
from ..forms import SignupForm
3+
from ..tokens import account_activation_token
34

45

56
class TestSignUp(TestCase):
@@ -33,3 +34,16 @@ def test_blank_signup(self):
3334
'password1': ['This field is required.'],
3435
'password2': ['This field is required.']
3536
})
37+
38+
39+
class TestToken(TestCase):
40+
"""test for token"""
41+
42+
def test_valid_token(self):
43+
request_POST = {'username': 'username5645',
44+
'email': '[email protected]',
45+
'password1': 'pwd000000',
46+
'password2': 'pwd000000'}
47+
user = SignupForm(request_POST).save(commit=False)
48+
token = account_activation_token.make_token(user)
49+
self.assertTrue(isinstance(token, str))
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from django.test import SimpleTestCase, TestCase, RequestFactory
2+
from django.http import HttpRequest
3+
from ..views import SignupView
4+
from app import settings
5+
6+
7+
class AddCSSTemplateTagTest(SimpleTestCase):
8+
9+
def test_rendered(self):
10+
request = HttpRequest()
11+
request.method = 'GET'
12+
needle = '<input type="password" name="password1" class=" input" required id="id_password1">'
13+
self.assertInHTML(needle, str(SignupView.as_view()(request, as_string=True).content))
14+
15+
16+
class ViewsTest(SimpleTestCase):
17+
"""Class for testing views"""
18+
19+
def test_mail_not_set_up(self):
20+
if hasattr(settings, 'EMAIL_HOST'):
21+
has_EMAIL_HOST = True
22+
EMAIL_HOST = settings.EMAIL_HOST
23+
delattr(settings, 'EMAIL_HOST')
24+
else:
25+
has_EMAIL_HOST = False
26+
27+
if hasattr(settings, 'EMAIL_BACKEND'):
28+
has_EMAIL_BACKEND = True
29+
EMAIL_BACKEND = settings.EMAIL_BACKEND
30+
delattr(settings, 'EMAIL_BACKEND')
31+
else:
32+
has_EMAIL_BACKEND = False
33+
34+
request = HttpRequest()
35+
request.method = 'POST'
36+
response = SignupView.as_view()(request, as_string=True)
37+
38+
if has_EMAIL_HOST:
39+
settings.EMAIL_HOST = EMAIL_HOST
40+
if has_EMAIL_BACKEND:
41+
settings.EMAIL_BACKEND = EMAIL_BACKEND
42+
needle = "<span>has not set up any emails</span>"
43+
self.assertInHTML(needle, str(response.content))
44+
45+
def test_signup_not_allowed(self):
46+
ALLOW_SIGNUP = settings.ALLOW_SIGNUP
47+
settings.ALLOW_SIGNUP = False
48+
request = HttpRequest()
49+
request.method = 'POST'
50+
response = SignupView.as_view()(request, as_string=True)
51+
settings.ALLOW_SIGNUP = ALLOW_SIGNUP
52+
self.assertEqual(response.status_code, 302)
53+
54+
55+
class ViewsDBTest(TestCase):
56+
"""Class for testing views with DB queries"""
57+
58+
def test_form_submission(self):
59+
self.factory = RequestFactory()
60+
EMAIL_BACKEND = settings.EMAIL_BACKEND
61+
settings.EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
62+
request = self.factory.post('/signup')
63+
request.POST = {'username': 'username5648',
64+
'email': '[email protected]',
65+
'password1': 'pwd0000Y00$$',
66+
'password2': 'pwd0000Y00$$'
67+
}
68+
response = SignupView.as_view()(request)
69+
needle = '<span>emailed you instructions to activate your account</span>'
70+
settings.EMAIL_BACKEND = EMAIL_BACKEND
71+
self.assertInHTML(needle, str(response.content))

tools/ci.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -o errexit
44

55
flake8
66
python app/manage.py migrate
7-
coverage run --source=app app/manage.py test server.tests
7+
coverage run --source=app app/manage.py test server.tests authentification.tests
88
coverage report
99

1010
(cd app/server/static && npm run lint)

0 commit comments

Comments
 (0)