Skip to main content

Testing Suite for DjangoRestFramework developers with deadlines.

Project description

DRF Tester

This module aims to help developers with testing DjangoRestFramework API endpoints:

  • Minimize the time (and lines of code) required
  • Mantain consistent testing
  • Increase productivity!

The philosophy behind the design of this module, is that a developer should only need to:

  1. Prepare the setUp method of a Test class
  2. Choose the correct classes to inheritb for each type of access
  3. Smile!!

This saves us developers lots of time writing repetitive, boiler-plate code, while reassuring that our tests are, at the very least, consistent.

For more details, read the docs

Table of Contents

Requirements

  • Python 3.7
  • Django 2.2
  • DjagnoRESTFramework 3.12.2
  • factory-boy 3.1.0

Installation

To install drf-tester in your systems, use pip:

pip install drf-tester

BaseDrfTest

Located in drf_tester.utils this class is the glue that makes it all work.

It contains the setUp method to be run before every test, as well as some helper functions and Object variables.

Check out the code for more details.

Built-in Functions

Some generic methods are created for DRYer single-action test classes.

  • get_admin_user(self, data: dict) -> User
  • get_active_user(self, data: dict) -> User
  • get_active_staff(self, data: dict) -> User
  • get_model_instances(self) -> list

Object Variables

Some Object-level variables are declared outside of setUp for convenience.

  • EXACT_AMOUNT: if is set to an integer value, get_model_instances will return a list of instances of exactly that size.
  • MIN and MAX: used as limits when using random.randint to create a list of instances of random size. Default: 5 and 10.

setUp()

The variables required for correct operation:

self.endpoint = None    # string with the url of the endpoint
self.factory = None     # factory-boy class to create model instances
self.model = None       # the model accessed through the endpoint
self.instance_data = {}     # dict of valid SERIALIZED data for instance creation
self.view = viewsets.YourViewSet.as_view({"get": "list", "post": "create", "put": "update", "delete": "destroy"})
self.user_data = {}     # Required for authenticated user testing
self.admin_data = {}    # Required for super user testing
self.staff_data = {}    # Required for staff user testing
self.USER_FIELD_NAME = 'creator'    # Required for testing user object access

Access Level

Once you know what level of access each kind of user should have, just add those classes to your tests, after APITestCase.

Example:

from drf_tester.viewsets import anon, admin, auth

class YourViewSetTest(APITestCase, anon.AnonNoAccess, auth.AuthFullAccess, admin.AdminFullAccess):
    """To test a ModelViewSet with IsAuthenticated as the only Permission
    """

    def setUp(self):
        ...

Viewset Tests

All the classes to assist with the development of tests for ViewSets are located under drf_tester.viewsets, and separated by user type:

  • anon.py: Anonymous users
  • auth.py: Authenticated users
  • admin.py: Admin user (superusers)
  • staff.py: Staff users

Within each file, there are classes that test the effects of different actions on the endpoint. For example:

# drf_tester/viewsets/anon.py

class NoCreate(BaseDrfTest):
    def test_anon_user_cannot_create_instance(self):
        """Anonymous user cannot create new instance"""
        ...

These single-action classes are grouped in bigger classes meant to be inherited by the final test cases. For example:

class AnonReadOnly(CanList, CanRetrieve, NoCreate, NoUpdate, NoDestroy):
    """
    Anonymous user has only read access to endopint
    """

    pass

A few different combinations are provided for your convenience:

  • For anonymous users:
    • AnonNoAccess
    • AnonReadOnly
    • AnonFullAccess
  • For authenticated users:
    • AuthFullAccess
    • AuthNoAccess
    • AuthReadOnly
    • AuthOwner: only controls instances linked to user
  • For admin users:
    • AdminNoAccess
    • AdminReadOnly
    • AdminFullAccess
  • For staff users:
    • StaffNoAccess
    • StaffReadOnly
    • StaffFullAccess

Custom groups can be made mixing and matching classes according with the level of access expected by each user-type from each endpoint.

Example

Included in the repository, there's an example illustrating how to implement in your project.

From example_one:

class ThingViewSetTest(APITestCase, AnonNoAccess, AuthFullAccess, AdminFullAccess):
    """
    Thing viewset tests
    Permission level: IsAuthenticated
    """

    def setUp(self):
        """Tests setup"""
        self.endpoint = "/api/v1/things/"
        self.factory = factories.ThingFactory
        self.model = models.Thing
        self.view = views.ThingViewSet.as_view({"get": "list", "post": "create", "put": "update", "delete": "destroy"})
        self.instance_data = {...}
        self.user_data = {...}
        self.admin_data = {...}

Contributions

I would love for the project to cover as many use-cases as possible, and for that I need your help.

There will always be some customized endpoint functionality that will never be covered by this module, but I'd like to make it as extensive as reasonably possible.

If you know of any situations for which the current solution won't work, please let me know and I'll try to make allowances for it.

Check out CONTRIBUTING.md for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf-tester-0.1.2.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_tester-0.1.2-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file drf-tester-0.1.2.tar.gz.

File metadata

  • Download URL: drf-tester-0.1.2.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.3

File hashes

Hashes for drf-tester-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d8afab7800becb568021a7a6b0eb77180e5ef6fbdc539b2e434a0a8d3b5aac4d
MD5 d90e7504d02be54fb4611637aaccfbbe
BLAKE2b-256 3242611926a49f5e4c1a33a3b510f3abda2fac8b4d3266139c4e10166726e0ce

See more details on using hashes here.

File details

Details for the file drf_tester-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: drf_tester-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.3

File hashes

Hashes for drf_tester-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3b1248350711ff37bcf039e169c16f098392a86ee5f37f5abd483e108ae5db93
MD5 65f509d629d9422fe0362413542d7135
BLAKE2b-256 67f7b0dc8aae59d796ffe9c4f065838d827d013d3f0dd43ee684323139cc76b6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page