General extension test utils

Django tests extension module.

flash_message_test(response: TestHttpResponse, expected_message: str) None

Test displaying Django message.

Parameters

responseTestHttpResponse

Test http response.

expected_messagestr

Expected message in response.

class UserAuthTestCase(methodName='runTest')

Bases: TestCase

Authorized page test class.

Inherit your test class from this.

Extends the django.test.TestCase class by adding a user, a method to get an authorized response, a method to add data to a session, and a Django message test method.

Examples

class TestUpdateUserView(UserAuthTestMixin):

    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()
        cls.path_schema = reverse(
            'users:update',
            kwargs={'pk': cls.user.id},
        )
        cls.update_user_data = {
            'username': 'update_user',
            'password1': '1q2s3d4r',
            'password2': '1q2s3d4r',
        }

    def test_is_auth_response(self):
        response = self.get_auth_response()
        self.assertEqual(response.wsgi_request.user, self.user)

    def test_update_user(self):
        response = self.get_auth_response(
            path_schema=self.path_schema,
            method='post',
            **self.update_user_data,
        )
        self.assertRedirects(
            response, SUCCESS_REDIRECT_PATH, 302,
        )
        assert UserApp.objects.filter(
            username='update_user'
        ).exists()
url = '/'

Page path schema (str).

user = None

User Django model instance (Model).

get_auth_response(path_schema: str | None = None, user: Model | None = None, method: str = 'get', **kwargs: object) TestHttpResponse

Get response with logged user.

Parameters

path_schemastr | None

Page path schema (default is class attribute value).

userModel | None

User for login (default is class attribute value).

methodstr

A HTML request method (‘get’, by default).

kwargs: object

Arbitrary keyword arguments.

Returns

TestHttpResponse

Test http response.

set_session(**data: dict[str, object]) None

Save data to session.

Parameters

datadict[str, object]

Arbitrary keyword arguments.

Examples

def test(self):
    task_conditions = {'timeout': 1, 'language_order': 'EN'}
    self.set_session(**{'task_conditions': task_conditions})
    ...
static assertMessage(response: TestHttpResponse, expected_message: str) None

Test displaying Django message.

Parameters

responseTestHttpResponse

Test http response.

expected_messagestr

Expected message in response.

Examples

def test(self):
    response = self.client.get(self.login_url, self.user_data)
    user = auth.get_user(self.client)
    ...
    self.assertMessage(response, 'Message text')