From 6eea76d984ae920c9c33185d3a945f5e5ac9333e Mon Sep 17 00:00:00 2001 From: Valentin Samir Date: Fri, 29 Jul 2016 13:56:23 +0200 Subject: [PATCH] Add pytest-warning to tests and correct some warnings, complete coverage (essentially branch) --- Makefile | 2 +- cas_server/forms.py | 2 +- .../cas_server/{login.css => styles.css} | 0 cas_server/templates/cas_server/base.html | 13 +++--- cas_server/tests/settings.py | 16 ++++++++ cas_server/tests/test_models.py | 40 +++++++++++++++++++ cas_server/tests/utils.py | 11 +++-- cas_server/urls.py | 4 +- requirements-dev.txt | 1 + setup.py | 2 +- tox.ini | 2 +- 11 files changed, 78 insertions(+), 15 deletions(-) rename cas_server/static/cas_server/{login.css => styles.css} (100%) diff --git a/Makefile b/Makefile index c719834..d802dc0 100644 --- a/Makefile +++ b/Makefile @@ -62,7 +62,7 @@ run_server: test_project run_tests: test_venv python setup.py check --restructuredtext --stric - test_venv/bin/py.test --cov=cas_server --cov-report html + test_venv/bin/py.test -rw -x --cov=cas_server --cov-report html rm htmlcov/coverage_html.js # I am really pissed off by those keybord shortcuts test_venv/bin/sphinx-build: test_venv diff --git a/cas_server/forms.py b/cas_server/forms.py index 03c7515..2f18ed9 100644 --- a/cas_server/forms.py +++ b/cas_server/forms.py @@ -32,7 +32,7 @@ class BootsrapForm(forms.Form): self[name].checkbox = True else: attrs['class'] = "form-control" - if field.label: + if field.label: # pragma: no branch (currently all field are hidden or labeled) attrs["placeholder"] = field.label if field.required: attrs["required"] = "required" diff --git a/cas_server/static/cas_server/login.css b/cas_server/static/cas_server/styles.css similarity index 100% rename from cas_server/static/cas_server/login.css rename to cas_server/static/cas_server/styles.css diff --git a/cas_server/templates/cas_server/base.html b/cas_server/templates/cas_server/base.html index db61e1b..bd8663a 100644 --- a/cas_server/templates/cas_server/base.html +++ b/cas_server/templates/cas_server/base.html @@ -15,7 +15,7 @@ - +
@@ -36,18 +36,17 @@ {% for message in messages %}
- {{ message }}
{% endfor %} diff --git a/cas_server/tests/settings.py b/cas_server/tests/settings.py index 4e17ceb..c873ea2 100644 --- a/cas_server/tests/settings.py +++ b/cas_server/tests/settings.py @@ -51,6 +51,22 @@ MIDDLEWARE_CLASSES = [ 'django.middleware.locale.LocaleMiddleware', ] +TEMPLATES = [ + { + 'APP_DIRS': True, + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages' + ] + } + } +] + ROOT_URLCONF = 'cas_server.tests.urls' # Database diff --git a/cas_server/tests/test_models.py b/cas_server/tests/test_models.py index 7a4403c..49f9c75 100644 --- a/cas_server/tests/test_models.py +++ b/cas_server/tests/test_models.py @@ -60,6 +60,24 @@ class FederatedUserTestCase(TestCase, UserModels, FederatedIendityProviderModel) with self.assertRaises(models.FederatedUser.DoesNotExist): models.FederatedUser.objects.get(username="test2") + def test_json_attributes(self): + """test the json storage of ``atrributs`` in ``_attributs``""" + provider = models.FederatedIendityProvider.objects.get(suffix="example.com") + user = models.FederatedUser.objects.create( + username=settings.CAS_TEST_USER, + provider=provider, + attributs=settings.CAS_TEST_ATTRIBUTES, + ticket="" + ) + self.assertEqual(utils.json_encode(settings.CAS_TEST_ATTRIBUTES), user._attributs) + user.delete() + user = models.FederatedUser.objects.create( + username=settings.CAS_TEST_USER, + provider=provider, + ticket="" + ) + self.assertIsNone(user._attributs) + self.assertIsNone(user.attributs) class FederateSLOTestCase(TestCase, UserModels): """test for the federated SLO model""" @@ -231,3 +249,25 @@ class TicketTestCase(TestCase, UserModels, BaseServicePattern): self.assertTrue(b'logoutRequest' in params and params[b'logoutRequest']) # only 1 ticket remain in the db self.assertEqual(len(models.ServiceTicket.objects.all()), 1) + + def test_json_attributes(self): + """test the json storage of ``atrributs`` in ``_attributs``""" + # ge an authenticated client + client = get_auth_client() + # get the user associated to the client + user = self.get_user(client) + ticket = models.ServiceTicket.objects.create( + user=user, + service=self.service, + attributs=settings.CAS_TEST_ATTRIBUTES, + service_pattern=self.service_pattern + ) + self.assertEqual(utils.json_encode(settings.CAS_TEST_ATTRIBUTES), ticket._attributs) + ticket.delete() + ticket = models.ServiceTicket.objects.create( + user=user, + service=self.service, + service_pattern=self.service_pattern + ) + self.assertIsNone(ticket._attributs) + self.assertIsNone(ticket.attributs) diff --git a/cas_server/tests/utils.py b/cas_server/tests/utils.py index 515b653..d020724 100644 --- a/cas_server/tests/utils.py +++ b/cas_server/tests/utils.py @@ -12,16 +12,21 @@ """Some utils functions for tests""" from cas_server.default_settings import settings +import django from django.test import Client -from django.template import loader, Context +from django.template import loader from django.utils import timezone +if django.VERSION < (1, 8): + from django.template import Context +else: + Context = lambda x:x import cgi import six from threading import Thread from lxml import etree from six.moves import BaseHTTPServer -from six.moves.urllib.parse import urlparse, parse_qsl +from six.moves.urllib.parse import urlparse, parse_qsl, parse_qs from datetime import timedelta from cas_server import models @@ -166,7 +171,7 @@ class HttpParamsHandler(BaseHTTPServer.BaseHTTPRequestHandler): postvars = cgi.parse_multipart(self.rfile, pdict) elif ctype == 'application/x-www-form-urlencoded': length = int(self.headers.get('content-length')) - postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1) + postvars = parse_qs(self.rfile.read(length), keep_blank_values=1) else: postvars = {} self.server.PARAMS = postvars diff --git a/cas_server/urls.py b/cas_server/urls.py index aa014f2..d4e691c 100644 --- a/cas_server/urls.py +++ b/cas_server/urls.py @@ -16,8 +16,10 @@ from django.views.decorators.debug import sensitive_post_parameters, sensitive_v from cas_server import views +app_name = "cas_server" + urlpatterns = [ - url(r'^$', RedirectView.as_view(pattern_name="cas_server:login")), + url(r'^$', RedirectView.as_view(pattern_name="cas_server:login", permanent=False)), url( '^login$', sensitive_post_parameters('password')( diff --git a/requirements-dev.txt b/requirements-dev.txt index c394fb1..d2d3902 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -3,6 +3,7 @@ tox>=1.8.1 pytest>=2.6.4 pytest-django>=2.8.0 pytest-pythonpath>=0.3 +pytest-warnings pytest-cov>=2.2.1 requests>=2.4 requests_futures>=0.9.5 diff --git a/setup.py b/setup.py index eb0af62..a8a5a8d 100644 --- a/setup.py +++ b/setup.py @@ -66,5 +66,5 @@ if __name__ == '__main__': download_url="https://github.com/nitmir/django-cas-server/releases", zip_safe=False, setup_requires=['pytest-runner'], - tests_require=['pytest', 'pytest-django', 'pytest-pythonpath'], + tests_require=['pytest', 'pytest-django', 'pytest-pythonpath', 'pytest-warnings', 'mock>=1'], ) diff --git a/tox.ini b/tox.ini index bdf50f0..401c249 100644 --- a/tox.ini +++ b/tox.ini @@ -31,7 +31,7 @@ whitelist_externals= [testenv] commands= - py.test {posargs:cas_server/tests/} + py.test -rw {posargs:cas_server/tests/} {[post_cmd]commands} whitelist_externals={[post_cmd]whitelist_externals}