Skip to content

History Entry Permission Test Cases


Test cases for accessing History

For this test to function properly you must add the history items model to app.core.views.history.View.get_object(). specifically an entry to the switch in the middle of the function.

Source code in app/core/tests/abstract/history_permissions.py
class HistoryPermissions:
    """Test cases for accessing History

    For this test to function properly you must add the history items model to
    `app.core.views.history.View.get_object()`. specifically an entry to the switch in the middle
    of the function.
    """


    item: object
    """Created Model

    Create a new item. 
    """

    model = History
    """ The history Model """

    namespace: str = ''
    """ URL namespace for the history view"""

    name_view: str = '_history'
    """ URL view name for history """

    no_permissions_user: User
    """A User with no permissions to access the item

    Create in `setUpTestData`
    """

    different_organization_user: User
    """A User with the correct permissions to access the item

    This user must be in a different organization than the item

    Create in `setUpTestData`
    """

    view_user: User
    """A User with the correct permissions to access the item

    This user must be in the same organization as the item

    Create in `setUpTestData`
    """


    def test_view_history_user_anon_denied(self):
        """ Check correct permission for view

        Attempt to view as anon user
        """

        client = Client()
        url = reverse(self.namespace + self.name_view, kwargs={'model_name': self.item._meta.model_name, 'model_pk': self.item.id})

        response = client.get(url)

        assert response.status_code == 302 and response.url.startswith('/account/login')


    def test_view_history_no_permission_denied(self):
        """ Check correct permission for view

        Attempt to view with user missing permission
        """

        client = Client()
        url = reverse(self.namespace +  self.name_view, kwargs={'model_name': self.item._meta.model_name, 'model_pk': self.item.id})


        client.force_login(self.no_permissions_user)
        response = client.get(url)

        assert response.status_code == 403


    def test_view_history_different_organizaiton_denied(self):
        """ Check correct permission for view

        Attempt to view with user from different organization
        """

        client = Client()
        url = reverse(self.namespace +  self.name_view, kwargs={'model_name': self.item._meta.model_name, 'model_pk': self.item.id})


        client.force_login(self.different_organization_user)
        response = client.get(url)

        assert response.status_code == 403


    def test_view_history_has_permission(self):
        """ Check correct permission for view

        Attempt to view as user with view permission
        """

        client = Client()
        url = reverse(self.namespace +  self.name_view, kwargs={'model_name': self.item._meta.model_name, 'model_pk': self.item.id})


        client.force_login(self.view_user)
        response = client.get(url)

        assert response.status_code == 200

Attributes

item: object

Created Model

Create a new item.

model = History

The history Model

namespace: str = ''

URL namespace for the history view

name_view: str = '_history'

URL view name for history

no_permissions_user: User

A User with no permissions to access the item

Create in setUpTestData

different_organization_user: User

A User with the correct permissions to access the item

This user must be in a different organization than the item

Create in setUpTestData

view_user: User

A User with the correct permissions to access the item

This user must be in the same organization as the item

Create in setUpTestData

Functions

test_view_history_user_anon_denied()

Check correct permission for view

Attempt to view as anon user

Source code in app/core/tests/abstract/history_permissions.py
def test_view_history_user_anon_denied(self):
    """ Check correct permission for view

    Attempt to view as anon user
    """

    client = Client()
    url = reverse(self.namespace + self.name_view, kwargs={'model_name': self.item._meta.model_name, 'model_pk': self.item.id})

    response = client.get(url)

    assert response.status_code == 302 and response.url.startswith('/account/login')

test_view_history_no_permission_denied()

Check correct permission for view

Attempt to view with user missing permission

Source code in app/core/tests/abstract/history_permissions.py
def test_view_history_no_permission_denied(self):
    """ Check correct permission for view

    Attempt to view with user missing permission
    """

    client = Client()
    url = reverse(self.namespace +  self.name_view, kwargs={'model_name': self.item._meta.model_name, 'model_pk': self.item.id})


    client.force_login(self.no_permissions_user)
    response = client.get(url)

    assert response.status_code == 403

test_view_history_different_organizaiton_denied()

Check correct permission for view

Attempt to view with user from different organization

Source code in app/core/tests/abstract/history_permissions.py
def test_view_history_different_organizaiton_denied(self):
    """ Check correct permission for view

    Attempt to view with user from different organization
    """

    client = Client()
    url = reverse(self.namespace +  self.name_view, kwargs={'model_name': self.item._meta.model_name, 'model_pk': self.item.id})


    client.force_login(self.different_organization_user)
    response = client.get(url)

    assert response.status_code == 403

test_view_history_has_permission()

Check correct permission for view

Attempt to view as user with view permission

Source code in app/core/tests/abstract/history_permissions.py
def test_view_history_has_permission(self):
    """ Check correct permission for view

    Attempt to view as user with view permission
    """

    client = Client()
    url = reverse(self.namespace +  self.name_view, kwargs={'model_name': self.item._meta.model_name, 'model_pk': self.item.id})


    client.force_login(self.view_user)
    response = client.get(url)

    assert response.status_code == 200

About:

This page forms part of our Project Centurion ERP.

Page Metadata
Version: ToDo: place files short git commit here
Date Created: 2024-06-16
Date Edited: 2024-07-09

Contribution:

Would You like to contribute to our Centurion ERP project? You can assist in the following ways:

 

ToDo: Add the page list of contributors