2017-10-19 01:12:52 +02:00
|
|
|
from shared.tests.testcases import (
|
|
|
|
TestCaseMixin as BaseTestCaseMixin,
|
|
|
|
ViewTestCaseMixin as BaseViewTestCaseMixin,
|
|
|
|
)
|
2017-08-10 15:02:08 +02:00
|
|
|
|
|
|
|
from .utils import create_root, create_team, create_user
|
|
|
|
|
|
|
|
|
2017-10-19 01:12:52 +02:00
|
|
|
class TestCaseMixin(BaseTestCaseMixin):
|
2017-09-01 12:39:17 +02:00
|
|
|
"""Extends TestCase for kfet application tests."""
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
def assertForbiddenKfet(self, response, form_ctx="form"):
|
2017-09-01 12:39:17 +02:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
|
|
|
|
"""
|
2017-08-16 17:45:59 +02:00
|
|
|
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):
|
2018-10-06 12:35:49 +02:00
|
|
|
messages = [str(msg) for msg in response.context["messages"]]
|
2017-08-16 17:45:59 +02:00
|
|
|
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 "
|
2018-10-06 12:35:49 +02:00
|
|
|
"'%(form_ctx)s', nor in messages."
|
|
|
|
% {
|
|
|
|
"http_method": request.method,
|
|
|
|
"path": request.get_full_path(),
|
|
|
|
"username": (
|
2017-08-16 17:45:59 +02:00
|
|
|
"'%s'" % request.user
|
2017-11-19 18:41:39 +01:00
|
|
|
if request.user.is_authenticated
|
2018-10-06 12:35:49 +02:00
|
|
|
else "anonymous"
|
2017-08-16 17:45:59 +02:00
|
|
|
),
|
2018-10-06 12:35:49 +02:00
|
|
|
"form_ctx": form_ctx,
|
2017-08-16 17:45:59 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
def assertInstanceExpected(self, instance, expected):
|
2017-09-01 12:39:17 +02:00
|
|
|
"""
|
|
|
|
Test that the values of the attributes and without-argument methods of
|
|
|
|
'instance' are equal to 'expected' pairs.
|
|
|
|
"""
|
2017-08-16 17:45:59 +02:00
|
|
|
for attr, expected_value in expected.items():
|
|
|
|
value = getattr(instance, attr)
|
|
|
|
if callable(value):
|
|
|
|
value = value()
|
|
|
|
self.assertEqual(value, expected_value)
|
|
|
|
|
|
|
|
|
2017-10-19 01:12:52 +02:00
|
|
|
class ViewTestCaseMixin(TestCaseMixin, BaseViewTestCaseMixin):
|
2017-09-01 12:39:17 +02:00
|
|
|
"""
|
|
|
|
TestCase extension to ease tests of kfet views.
|
|
|
|
|
2017-10-19 01:12:52 +02:00
|
|
|
Most information can be found in the base parent class doc.
|
|
|
|
This class performs some changes to users management, detailed below.
|
2017-09-01 12:39:17 +02:00
|
|
|
|
|
|
|
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,
|
2018-10-03 18:01:19 +02:00
|
|
|
- 'root': a superuser, account trigramme: 200,
|
|
|
|
- 'liq': if class attribute 'with_liq' is 'True', account
|
|
|
|
trigramme: LIQ.
|
2017-09-01 12:39:17 +02:00
|
|
|
Their password is their username.
|
|
|
|
|
|
|
|
By using 'register_user' method, these users can then be accessed at
|
2017-10-19 01:12:52 +02:00
|
|
|
'users' attribute by their label. Similarly, their kfet account, if any, is
|
2017-09-01 12:39:17 +02:00
|
|
|
registered on 'accounts' attribute.
|
|
|
|
"""
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2018-10-03 18:01:19 +02:00
|
|
|
with_liq = False
|
|
|
|
|
2017-08-10 15:02:08 +02:00
|
|
|
def setUp(self):
|
2017-09-01 12:39:17 +02:00
|
|
|
"""
|
|
|
|
Warning: Do not forget to call super().setUp() in subclasses.
|
|
|
|
"""
|
2017-08-10 15:02:08 +02:00
|
|
|
self.accounts = {}
|
2017-10-19 01:12:52 +02:00
|
|
|
super().setUp()
|
2017-09-01 16:37:14 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
"""
|
2017-08-10 15:02:08 +02:00
|
|
|
# Format desc: username, password, trigramme
|
2018-10-03 18:01:19 +02:00
|
|
|
users_base = {
|
2017-08-10 15:02:08 +02:00
|
|
|
# user, user, 000
|
2018-10-06 12:35:49 +02:00
|
|
|
"user": create_user(),
|
2017-08-10 15:02:08 +02:00
|
|
|
# team, team, 100
|
2018-10-06 12:35:49 +02:00
|
|
|
"team": create_team(),
|
2017-08-10 15:02:08 +02:00
|
|
|
# root, root, 200
|
2018-10-06 12:35:49 +02:00
|
|
|
"root": create_root(),
|
2017-08-10 15:02:08 +02:00
|
|
|
}
|
2018-10-03 18:01:19 +02:00
|
|
|
if self.with_liq:
|
2018-10-06 12:35:49 +02:00
|
|
|
users_base["liq"] = create_user("liq", "LIQ")
|
2018-10-03 18:01:19 +02:00
|
|
|
return users_base
|
2017-08-10 15:02:08 +02:00
|
|
|
|
|
|
|
def register_user(self, label, user):
|
2017-10-19 01:12:52 +02:00
|
|
|
super().register_user(label, user)
|
2018-10-06 12:35:49 +02:00
|
|
|
if hasattr(user.profile, "account_kfet"):
|
2017-08-10 15:02:08 +02:00
|
|
|
self.accounts[label] = user.profile.account_kfet
|