mirror of https://github.com/doccano/doccano.git
Browse Source
we set up all tests required to reach almost 100% of code coverage for the authentification part
pull/250/head
we set up all tests required to reach almost 100% of code coverage for the authentification part
pull/250/head
Guillim
5 years ago
5 changed files with 124 additions and 1 deletions
Split View
Diff Options
-
2.coveragerc
-
36app/authentification/tests/test_activate.py
-
14app/authentification/tests/test_signup.py
-
71app/authentification/tests/test_template.py
-
2tools/ci.sh
@ -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/') |
@ -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)) |
Write
Preview
Loading…
Cancel
Save