test(access): History Entry checks for Organization model
ref: #605 closes #607
This commit is contained in:
@ -0,0 +1,32 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from access.models.organization_history import Organization, OrganizationHistory
|
||||
|
||||
from core.tests.abstract.test_functional_history import HistoryEntriesCommon
|
||||
|
||||
|
||||
|
||||
class History(
|
||||
HistoryEntriesCommon,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
model = Organization
|
||||
|
||||
history_model = OrganizationHistory
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
|
||||
super().setUpTestData()
|
||||
|
||||
self.obj = self.model.objects.create(
|
||||
name = self.field_value_original,
|
||||
)
|
||||
|
||||
self.obj_delete = self.model.objects.create(
|
||||
name = self.field_value_delete,
|
||||
)
|
||||
|
||||
self.call_the_banners()
|
@ -1,187 +0,0 @@
|
||||
|
||||
# import pytest
|
||||
# import unittest
|
||||
# import requests
|
||||
|
||||
# from django.test import TestCase, Client
|
||||
|
||||
# from access.models import Organization
|
||||
|
||||
# from core.models.history import History
|
||||
|
||||
# from access.models import Organization
|
||||
|
||||
|
||||
|
||||
# class OrganizationHistory(TestCase):
|
||||
|
||||
# model = Organization
|
||||
|
||||
# model_name = 'organization'
|
||||
|
||||
|
||||
# @classmethod
|
||||
# def setUpTestData(self):
|
||||
# """ Setup Test """
|
||||
|
||||
# organization = Organization.objects.create(name='test_org')
|
||||
|
||||
# self.organization = organization
|
||||
|
||||
# self.item_create = self.model.objects.create(
|
||||
# name = 'test_item_' + self.model_name,
|
||||
# )
|
||||
|
||||
|
||||
# self.history_create = History.objects.get(
|
||||
# action = int(History.Actions.ADD),
|
||||
# item_pk = self.item_create.pk,
|
||||
# item_class = self.model._meta.model_name,
|
||||
# )
|
||||
|
||||
# self.item_change = self.item_create
|
||||
# self.item_change.name = 'test_item_' + self.model_name + '_changed'
|
||||
# self.item_change.save()
|
||||
|
||||
# self.history_change = History.objects.get(
|
||||
# action = int(History.Actions.UPDATE),
|
||||
# item_pk = self.item_change.pk,
|
||||
# item_class = self.model._meta.model_name,
|
||||
# )
|
||||
|
||||
# self.item_delete = self.model.objects.create(
|
||||
# name = 'test_item_delete_' + self.model_name,
|
||||
# )
|
||||
|
||||
# self.item_delete.delete()
|
||||
|
||||
# self.history_delete = History.objects.filter(
|
||||
# item_pk = self.item_delete.pk,
|
||||
# item_class = self.model._meta.model_name,
|
||||
# )
|
||||
|
||||
# self.history_delete_children = History.objects.filter(
|
||||
# item_parent_pk = self.item_delete.pk,
|
||||
# item_parent_class = self.model._meta.model_name,
|
||||
# )
|
||||
|
||||
|
||||
|
||||
# def test_history_entry_item_add_field_action(self):
|
||||
# """ Ensure action is "add" for item creation """
|
||||
|
||||
# history = self.history_create.__dict__
|
||||
|
||||
# assert history['action'] == int(History.Actions.ADD)
|
||||
# # assert type(history['action']) is int
|
||||
|
||||
|
||||
# @pytest.mark.skip(reason="to be written")
|
||||
# def test_history_entry_item_add_field_after(self):
|
||||
# """ Ensure after field contains correct value """
|
||||
|
||||
# history = self.history_create.__dict__
|
||||
|
||||
# assert history['after'] == str('{}')
|
||||
# # assert type(history['after']) is str
|
||||
|
||||
|
||||
# def test_history_entry_item_add_field_before(self):
|
||||
# """ Ensure before field is an empty JSON string for create """
|
||||
|
||||
# history = self.history_create.__dict__
|
||||
|
||||
# assert history['before'] == str('{}')
|
||||
# # assert type(history['before']) is str
|
||||
|
||||
|
||||
# def test_history_entry_item_add_field_item_pk(self):
|
||||
# """ Ensure history entry field item_pk is the created items pk """
|
||||
|
||||
# history = self.history_create.__dict__
|
||||
|
||||
# assert history['item_pk'] == self.item_create.pk
|
||||
# # assert type(history['item_pk']) is int
|
||||
|
||||
|
||||
# def test_history_entry_item_add_field_item_class(self):
|
||||
# """ Ensure history entry field item_class is the model name """
|
||||
|
||||
# history = self.history_create.__dict__
|
||||
|
||||
# assert history['item_class'] == self.model._meta.model_name
|
||||
# # assert type(history['item_class']) is str
|
||||
|
||||
|
||||
|
||||
|
||||
# ################################## Change ##################################
|
||||
|
||||
|
||||
|
||||
|
||||
# def test_history_entry_item_change_field_action(self):
|
||||
# """ Ensure action is "add" for item creation """
|
||||
|
||||
# history = self.history_change.__dict__
|
||||
|
||||
# assert history['action'] == int(History.Actions.UPDATE)
|
||||
# # assert type(history['action']) is int
|
||||
|
||||
|
||||
# def test_history_entry_item_change_field_after(self):
|
||||
# """ Ensure after field contains correct value """
|
||||
|
||||
# history = self.history_change.__dict__
|
||||
|
||||
# assert history['after'] == str('{"name": "test_item_' + self.model_name + '_changed"}')
|
||||
# # assert type(history['after']) is str
|
||||
|
||||
|
||||
# @pytest.mark.skip(reason="to be written")
|
||||
# def test_history_entry_item_change_field_before(self):
|
||||
# """ Ensure before field is an empty JSON string for create """
|
||||
|
||||
# history = self.history_change.__dict__
|
||||
|
||||
# assert history['before'] == str('{}')
|
||||
# # assert type(history['before']) is str
|
||||
|
||||
|
||||
# def test_history_entry_item_change_field_item_pk(self):
|
||||
# """ Ensure history entry field item_pk is the created items pk """
|
||||
|
||||
# history = self.history_change.__dict__
|
||||
|
||||
# assert history['item_pk'] == self.item_create.pk
|
||||
# # assert type(history['item_pk']) is int
|
||||
|
||||
|
||||
# def test_history_entry_item_change_field_item_class(self):
|
||||
# """ Ensure history entry field item_class is the model name """
|
||||
|
||||
# history = self.history_change.__dict__
|
||||
|
||||
# assert history['item_class'] == self.model._meta.model_name
|
||||
# # assert type(history['item_class']) is str
|
||||
|
||||
|
||||
|
||||
|
||||
# ################################## Delete ##################################
|
||||
|
||||
|
||||
|
||||
|
||||
# def test_device_history_entry_delete(self):
|
||||
# """ When an item is deleted, it's history entries must be removed """
|
||||
|
||||
# assert self.history_delete.exists() is False
|
||||
|
||||
|
||||
# def test_device_history_entry_children_delete(self):
|
||||
# """ When an item is deleted, it's history entries must be removed """
|
||||
|
||||
# assert self.history_delete_children.exists() is False
|
||||
|
||||
|
@ -1,165 +0,0 @@
|
||||
# # from django.conf import settings
|
||||
# from django.contrib.auth import get_user_model
|
||||
# from django.contrib.auth.models import AnonymousUser, User
|
||||
# from django.contrib.contenttypes.models import ContentType
|
||||
# from django.shortcuts import reverse
|
||||
# from django.test import TestCase, Client
|
||||
|
||||
# import pytest
|
||||
# import unittest
|
||||
# import requests
|
||||
|
||||
# from access.models import Organization, Team, TeamUsers, Permission
|
||||
|
||||
# from core.models.history import History
|
||||
|
||||
|
||||
# class OrganizationHistoryPermissions(TestCase):
|
||||
|
||||
|
||||
# item_model = Organization
|
||||
|
||||
|
||||
# model = History
|
||||
|
||||
# model_name = 'history'
|
||||
|
||||
# app_label = 'core'
|
||||
|
||||
# namespace = ''
|
||||
|
||||
# name_view = '_history'
|
||||
|
||||
|
||||
# @classmethod
|
||||
# def setUpTestData(self):
|
||||
# """Setup Test
|
||||
|
||||
# 1. Create an organization for user and item
|
||||
# 2. create an organization that is different to item
|
||||
# 3. Create a device
|
||||
# 4. Add history device history entry as item
|
||||
# 5. create a user
|
||||
# 6. create user in different organization (with the required permission)
|
||||
# """
|
||||
|
||||
# organization = Organization.objects.create(name='test_org')
|
||||
|
||||
# self.organization = organization
|
||||
|
||||
# different_organization = Organization.objects.create(name='test_different_organization')
|
||||
|
||||
# self.item = self.organization
|
||||
|
||||
# self.history_model_name = self.item._meta.model_name
|
||||
|
||||
# self.history = self.model.objects.get(
|
||||
# item_pk = self.item.id,
|
||||
# item_class = self.item._meta.model_name,
|
||||
# action = self.model.Actions.ADD,
|
||||
# )
|
||||
|
||||
# view_permissions = Permission.objects.get(
|
||||
# codename = 'view_' + self.model_name,
|
||||
# content_type = ContentType.objects.get(
|
||||
# app_label = self.app_label,
|
||||
# model = self.model_name,
|
||||
# )
|
||||
# )
|
||||
|
||||
# view_team = Team.objects.create(
|
||||
# team_name = 'view_team',
|
||||
# organization = organization,
|
||||
# )
|
||||
|
||||
# view_team.permissions.set([view_permissions])
|
||||
|
||||
|
||||
# self.no_permissions_user = User.objects.create_user(username="test_no_permissions", password="password")
|
||||
|
||||
|
||||
# self.view_user = User.objects.create_user(username="test_user_view", password="password")
|
||||
# teamuser = TeamUsers.objects.create(
|
||||
# team = view_team,
|
||||
# user = self.view_user
|
||||
# )
|
||||
|
||||
# self.different_organization_user = User.objects.create_user(username="test_different_organization_user", password="password")
|
||||
|
||||
|
||||
# different_organization_team = Team.objects.create(
|
||||
# team_name = 'different_organization_team',
|
||||
# organization = different_organization,
|
||||
# )
|
||||
|
||||
# different_organization_team.permissions.set([
|
||||
# view_permissions,
|
||||
# ])
|
||||
|
||||
# TeamUsers.objects.create(
|
||||
# team = different_organization_team,
|
||||
# user = self.different_organization_user
|
||||
# )
|
||||
|
||||
|
||||
|
||||
# def test_auth_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.history_model_name, 'model_pk': self.item.id})
|
||||
|
||||
# response = client.get(url)
|
||||
|
||||
# assert response.status_code == 302 and response.url.startswith('/account/login')
|
||||
|
||||
|
||||
# def test_auth_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.history_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_auth_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.history_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_auth_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.history_model_name, 'model_pk': self.item.id})
|
||||
|
||||
|
||||
# client.force_login(self.view_user)
|
||||
# response = client.get(url)
|
||||
|
||||
# assert response.status_code == 200
|
Reference in New Issue
Block a user