Base test mixins

The following mixins are available to provide helpers and methods that implements helpers and functions commonly used in tests. BaseTestCase, BaseTransactionTestCase are concrete classes implementing all the mixins and extending respectively django.tests.TestCase and django.tests.TransactionTestCase

class app_helper.base_test.RequestTestCaseMixin[source]

Provide methods to get complex request objects.

Resulting request has more realistic attributes (i.e.: all the attributes found in a non-test request) than the plain django.test.RequestFactory.

login_user_context(user, password=None)[source]

Context manager to make logged in requests.

Usage:

with self.login_user_context("<username>", password="<password>"):
    request = self.request("/", lang="en")
    ... # this request has <username> as user
Parameters:
  • user – user username

  • password – user password (if omitted, username is used)

request(path, method='get', data=None, page=None, lang='', user=None, use_middlewares=False, secure=False, use_toolbar=False)[source]

Create a request for the given parameters.

Request will be enriched with:

  • session

  • cookies

  • user (Anonymous if :param:user is None)

  • django CMS toolbar (is set)

  • current_page (if provided)

Parameters:
  • path (str) – request path

  • method (str) – HTTP verb to use

  • data (dict) – payload to pass to the underlying django.test.RequestFactory method

  • page (cms.models.Page) – current page object

  • lang (str) – request language

  • user (django.contrib.auth.models.AbstractUser) – current user

  • use_middlewares (bool) – pass the request through configured middlewares

  • secure (bool) – create HTTPS request

  • use_toolbar – add django CMS toolbar

Returns:

request

class app_helper.base_test.CreateTestDataMixin[source]

Provide methods to automatically create users on test setup and shortcut to generate test data.

classmethod _setup_users()[source]

Create standard users.

  • user: superuser

  • user_staff: staff user

  • user_normal: plain django user

classmethod _teardown_users()[source]

Delete existing users.

_admin_user_username = 'admin'

Username for auto-generated superuser

_admin_user_password = 'admin'

Password for auto-generated superuser

_admin_user_email = 'admin@admin.com'

Email for auto-generated superuser

_staff_user_username = 'staff'

Username for auto-generated staff user

_staff_user_password = 'staff'

Password for auto-generated staff user

_staff_user_email = 'staff@admin.com'

Email for auto-generated staff user

_user_user_username = 'normal'

Username for auto-generated non-staff user

_user_user_password = 'normal'

Password for auto-generated non-staff user

_user_user_email = 'user@admin.com'

Email for auto-generated non-staff user

classmethod create_django_image()[source]

Create a django image file object suitable for FileField It also sets the following attributes:

  • self.image_name: the image base name

  • self.filename: the complete image path

Returns:

(django file object, path to file image)

It requires Pillow installed in the environment to work

create_django_image_object()[source]

Create a django image file object suitable for FileField It also sets the following attributes:

  • self.image_name: the image base name

  • self.filename: the complete image path

Returns:

django file object

It requires Pillow installed in the environment to work

classmethod create_filer_image(user, image_name)[source]

Create a filer image object suitable for FilerImageField It also sets the following attributes:

  • self.image_name: the image base name

  • self.filename: the complete image path

  • self.filer_image: the filer image object

Parameters:
  • user – image owner

  • image_name – image name

Returns:

filer image object

It requires Pillow and django-filer installed in the environment to work

create_filer_image_object()[source]

Create a filer image object suitable for FilerImageField It also sets the following attributes:

  • self.image_name: the image base name

  • self.filename: the complete image path

  • self.filer_image: the filer image object

Returns:

filer image object

It requires Pillow and django-filer installed in the environment to work

static create_image(mode='RGB', size=(800, 600))[source]

Create a random image suitable for saving as DjangoFile :param mode: color mode :param size: tuple of width, height :return: image object

It requires Pillow installed in the environment to work

create_user(username, email, password, is_staff=False, is_superuser=False, base_cms_permissions=False, permissions=None)[source]

Creates a user with the given properties

Parameters:
  • username – Username

  • email – Email

  • password – password

  • is_staff – Staff status

  • is_superuser – Superuser status

  • base_cms_permissions – Base django CMS permissions

  • permissions – Other permissions

Returns:

User instance

class app_helper.base_test.CMSPageRenderingMixin[source]

Provide hooks to create sample pages in tests and helper methods to render pages and plugins.

classmethod _setup_cms()[source]

Setup data required by django CMS as class attributes.

  • site_1: instance of the first Site

  • languages: list of configured languages

_pages_data = ()

List of pages data for the different languages.

Each item of the list is a dictionary containing the attributes (as accepted by cms.api.create_page()) of the page to be created.

The first language will be created with cms.api.create_page() the following languages using cms.api.create_title().

Example:

Single page created in en, fr, it languages:

_pages_data = (
    {
        'en': {'title': 'Page title', 'template': 'page.html', 'publish': True},
        'fr': {'title': 'Titre', 'publish': True},
        'it': {'title': 'Titolo pagina', 'publish': False}
    },
)
static create_pages(source, languages)[source]

Build pages according to the pages data provided by get_pages_data() and returns the list of the draft version of each

get_content_renderer(request)[source]

Returns a the plugin renderer. Only for django CMS 3.4+

Parameters:

request – request instance

Returns:

ContentRenderer instance

get_page_request(page, user, path=None, edit=False, lang='en', use_middlewares=False, secure=False)[source]

Deprecated, use get_toolbar_request().

get_pages()[source]

Create pages using self._pages_data and self.languages

Returns:

list of created pages

get_pages_data()[source]

Construct a list of pages in the different languages available for the project. Default implementation is to return the _pages_data attribute

Returns:

list of pages data

get_plugin_context(page, lang, plugin, edit=False)[source]

Returns a context suitable for CMSPlugin.render_plugin / render_placeholder

Parameters:
  • page – Page object

  • lang – Current language

  • plugin – Plugin instance

  • edit – Enable edit mode for rendering

Returns:

PluginContext instance

get_request(page, lang, user=None, path=None, use_middlewares=False, secure=False, use_toolbar=False)[source]

Create a GET request for the given page and language.

Parameters:
  • page – current page object

  • lang – request language

  • user – current user

  • path – path (if different from the current page path)

  • use_middlewares – pass the request through configured middlewares.

  • secure – create HTTPS request

  • use_toolbar – add django CMS toolbar

Returns:

request

get_toolbar_request(page, user, path=None, edit=False, lang='en', use_middlewares=False, secure=False)[source]

Create a GET request for the given page suitable for use the django CMS toolbar.

This method requires django CMS installed to work. It will raise an ImportError otherwise; not a big deal as this method makes sense only in a django CMS environment.

Parameters:
  • page – current page object

  • user – current user

  • path – path (if different from the current page path)

  • edit – whether enabling editing mode

  • lang – request language

  • use_middlewares – pass the request through configured middlewares.

  • secure – create HTTPS request

Returns:

request

post_request(page, lang, data, user=None, path=None, use_middlewares=False, secure=False, use_toolbar=False)[source]

Create a POST request for the given page and language with CSRF disabled.

Parameters:
  • page – current page object

  • lang – request language

  • data – POST payload

  • user – current user

  • path – path (if different from the current page path)

  • use_middlewares – pass the request through configured middlewares.

  • secure – create HTTPS request

  • use_toolbar – add django CMS toolbar

Returns:

request

render_plugin(page, lang, plugin, edit=False)[source]

Renders a single plugin using CMSPlugin.render_plugin

Parameters:
  • page – Page object

  • lang – Current language

  • plugin – Plugin instance

  • edit – Enable edit mode for rendering

Returns:

Rendered plugin

class app_helper.base_test.GenericHelpersMixin[source]
captured_output()[source]

Context manager that patches stdout / stderr with StringIO and return the instances.

Useful to test output.

Returns:

stdout, stderr wrappers

reload_model(obj)[source]

Reload models instance from database.

Contrary to refresh_from_db returns a completely new instance, instead of updating the current one.

Parameters:

obj – model instance to reload

Returns:

the reloaded model instance

static reload_urlconf(urlconf=None)[source]

Reload django urlconf and any attached apphook.

temp_dir()[source]

Return the context manager of a temporary directory that is removed upon exit.

Usage:

with self.temp_dir() as temp_path:
    test_file = os.path.join(temp_path, "afile")
    ... # do something with test_file
... # test_file and containing directory is removed
class app_helper.base_test.BaseNoDataTestCaseMixin[source]

Provide helper methods to setup and interact with Django testing framework.

Does not create in setUpClass(), but provides all the methods to tap into automatic data generation.

Implements:

class app_helper.base_test.BaseTestCaseMixin[source]

Provide helper methods to setup and interact with Django testing framework.

Like BaseNoDataTestCaseMixin but create sample data in setUpClass() according to CreateTestDataMixin and CMSPageRenderingMixin configuration

Implements:

class app_helper.base_test.BaseTestCase(methodName='runTest')[source]

Base class that implements BaseTestCaseMixin and django.tests.TestCase

class app_helper.base_test.BaseTransactionTestCase(methodName='runTest')[source]

Base class that implements BaseTestCaseMixin and django.tests.TransactionTestCase