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 . 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