kpsul/kfet/tests/testcases.py
Aurélien Delobelle 05eeb6a25c core -- Install django-allauth-ens
Refer to allauth doc for an accurate features list:
  http://django-allauth.readthedocs.io/en/latest/

Users can now change their password, ask for a password reset, or set
one if they don't have one.

In particular, it allows users whose account has been created via a
clipper authentication to configure a password before losing their
clipper. Even if they have already lost it, they are able to get one
using the "Reset password" functionality.

Allauth multiple emails management is deactivated. Requests to the
related url redirect to the home page.

All the login and logout views are replaced by the allauth' ones. It
also concerns the Django and Wagtail admin sites.

Note that users are no longer logged out of the clipper CAS server when
they authenticated via this server. Instead a message suggests the user
to disconnect.

Clipper connections and `login_clipper`
---------------------------------------

- Non-empty `login_clipper` are now unique among `CofProfile` instances.
- They are created once for users with a non-empty 'login_clipper' (with
the data migration 0014_create_clipper_connections).
- The `login_clipper` of CofProfile instances are sync with their
clipper connections:
    * `CofProfile.sync_clipper_connections` method updates the
connections based on `login_clipper`.
    * Signals receivers `sync_clipper…` update `login_clipper` based on
connections creations/updates/deletions.

Misc
----

- Add NullCharField (model field) which allows to use `unique=True` on
CharField (even with empty strings).
- Parts of kfet mixins for TestCase are now in shared.tests.testcase,
  as they are used elsewhere than in the kfet app.
2018-10-21 17:09:12 +02:00

120 lines
4.3 KiB
Python

from shared.tests.testcases import (
TestCaseMixin as BaseTestCaseMixin,
ViewTestCaseMixin as BaseViewTestCaseMixin,
)
from .utils import create_root, create_team, create_user
class TestCaseMixin(BaseTestCaseMixin):
"""Extends TestCase for kfet application tests."""
def assertForbiddenKfet(self, response, form_ctx="form"):
"""
Test that a response (retrieved with a Client) contains error due to
lack of kfet permissions.
It checks that 'Permission refusée' is present in the non-field errors
of the form of response context at key 'form_ctx', or present in
messages.
This should be used for pages which can be accessed by the kfet team
members, but require additionnal permission(s) to make an operation.
"""
try:
self.assertEqual(response.status_code, 200)
try:
form = response.context[form_ctx]
self.assertIn("Permission refusée", form.non_field_errors())
except (AssertionError, AttributeError, KeyError):
messages = [str(msg) for msg in response.context["messages"]]
self.assertIn("Permission refusée", messages)
except AssertionError:
request = response.wsgi_request
raise AssertionError(
"%(http_method)s request at %(path)s should raise an error "
"for %(username)s user.\n"
"Cannot find any errors in non-field errors of form "
"'%(form_ctx)s', nor in messages."
% {
"http_method": request.method,
"path": request.get_full_path(),
"username": (
"'%s'" % request.user
if request.user.is_authenticated
else "anonymous"
),
"form_ctx": form_ctx,
}
)
def assertInstanceExpected(self, instance, expected):
"""
Test that the values of the attributes and without-argument methods of
'instance' are equal to 'expected' pairs.
"""
for attr, expected_value in expected.items():
value = getattr(instance, attr)
if callable(value):
value = value()
self.assertEqual(value, expected_value)
class ViewTestCaseMixin(TestCaseMixin, BaseViewTestCaseMixin):
"""
TestCase extension to ease tests of kfet views.
Most information can be found in the base parent class doc.
This class performs some changes to users management, detailed below.
During setup, three users are created with their kfet account:
- 'user': a basic user without any permission, account trigramme: 000,
- 'team': a user with kfet.is_team permission, account trigramme: 100,
- 'root': a superuser, account trigramme: 200,
- 'liq': if class attribute 'with_liq' is 'True', account
trigramme: LIQ.
Their password is their username.
By using 'register_user' method, these users can then be accessed at
'users' attribute by their label. Similarly, their kfet account, if any, is
registered on 'accounts' attribute.
"""
with_liq = False
def setUp(self):
"""
Warning: Do not forget to call super().setUp() in subclasses.
"""
self.accounts = {}
super().setUp()
def get_users_base(self):
"""
Dict of <label: user instance>.
Note: Don't access yourself this property. Use 'users_base' attribute
which cache the returned value from here.
It allows to give functions calls, which creates users instances, as
values here.
"""
# Format desc: username, password, trigramme
users_base = {
# user, user, 000
"user": create_user(),
# team, team, 100
"team": create_team(),
# root, root, 200
"root": create_root(),
}
if self.with_liq:
users_base["liq"] = create_user("liq", "LIQ")
return users_base
def register_user(self, label, user):
super().register_user(label, user)
if hasattr(user.profile, "account_kfet"):
self.accounts[label] = user.profile.account_kfet