chore(core): Remove old History Tests

ref: #783 #735 #759
This commit is contained in:
2025-06-04 11:29:43 +09:30
parent 731df89b40
commit b21b0f0b5b
11 changed files with 0 additions and 1002 deletions

View File

@ -1,122 +0,0 @@
import django
import pytest
import unittest
from django.shortcuts import reverse
from django.test import TestCase, Client
from core.models.history import History
from itam.models.device import Device
User = django.contrib.auth.get_user_model()
class HistoryPermissions:
"""Test cases for accessing History
For this test to function properly you must add the history items model to
`centurion.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

View File

@ -1,34 +0,0 @@
from django.test import TestCase
from core.models.manufacturer_history import Manufacturer, ManufacturerHistory
from core.tests.abstract.test_functional_history import HistoryEntriesCommon
class History(
HistoryEntriesCommon,
TestCase,
):
model = Manufacturer
history_model = ManufacturerHistory
@classmethod
def setUpTestData(self):
super().setUpTestData()
self.obj = self.model.objects.create(
organization = self.organization,
name = self.field_value_original
)
self.obj_delete = self.model.objects.create(
organization = self.organization,
name = self.field_value_delete
)
self.call_the_banners()

View File

@ -1,428 +0,0 @@
import django
from django.contrib.auth.models import ContentType, Permission
from django.contrib.contenttypes.models import ContentType
from django.shortcuts import reverse
from django.test import Client, TestCase
from access.models.tenant import Tenant as Organization
from access.models.team import Team
from access.models.team_user import TeamUsers
from api.tests.abstract.api_permissions_viewset import APIPermissionView
from api.tests.abstract.test_metadata_functional import (
MetadataAttributesFunctionalBase,
MetadataAttributesFunctionalEndpoint,
)
from core.models.model_history import ModelHistory
from itam.models.device import Device
from itam.models.device_history import DeviceHistory
from settings.models.app_settings import AppSettings
User = django.contrib.auth.get_user_model()
class ViewSetBase:
model = ModelHistory
app_namespace = 'v2'
url_name = '_api_v2_model_history'
change_data = {'name': 'device'}
delete_data = {}
@classmethod
def setUpTestData(self):
"""Setup Test
note: permissions set to use device history
1. Create an organization for user and item
. create an organization that is different to item
2. Create a team
3. create teams with each permission: view, add, change, delete
4. create a user per team
"""
organization = Organization.objects.create(name='test_org')
self.organization = organization
self.different_organization = Organization.objects.create(name='test_different_organization')
self.global_organization = Organization.objects.create(
name = 'test_global_organization'
)
self.other_org_item = Device.objects.create(
organization = self.different_organization,
name = 'two'
)
self.global_org_item = Device.objects.create(
organization = self.global_organization,
name = 'global_item'
)
app_settings = AppSettings.objects.get(
owner_organization = None
)
app_settings.global_organization = self.global_organization
app_settings.save()
self.device = Device.objects.create(
organization = self.organization,
name = 'history-device'
)
view_permissions = Permission.objects.get(
codename = 'view_' + DeviceHistory._meta.model_name,
content_type = ContentType.objects.get(
app_label = DeviceHistory._meta.app_label,
model = DeviceHistory._meta.model_name,
)
)
view_team = Team.objects.create(
team_name = 'view_team',
organization = organization,
)
view_team.permissions.set([view_permissions])
add_permissions = Permission.objects.get(
codename = 'add_' + DeviceHistory._meta.model_name,
content_type = ContentType.objects.get(
app_label = DeviceHistory._meta.app_label,
model = DeviceHistory._meta.model_name,
)
)
add_team = Team.objects.create(
team_name = 'add_team',
organization = organization,
)
add_team.permissions.set([add_permissions])
change_permissions = Permission.objects.get(
codename = 'change_' + DeviceHistory._meta.model_name,
content_type = ContentType.objects.get(
app_label = DeviceHistory._meta.app_label,
model = DeviceHistory._meta.model_name,
)
)
change_team = Team.objects.create(
team_name = 'change_team',
organization = organization,
)
change_team.permissions.set([change_permissions])
delete_permissions = Permission.objects.get(
codename = 'delete_' + DeviceHistory._meta.model_name,
content_type = ContentType.objects.get(
app_label = DeviceHistory._meta.app_label,
model = DeviceHistory._meta.model_name,
)
)
delete_team = Team.objects.create(
team_name = 'delete_team',
organization = organization,
)
delete_team.permissions.set([delete_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")
TeamUsers.objects.create(
team = view_team,
user = self.view_user
)
self.item = self.model.objects.filter(
content_type = ContentType.objects.get(
app_label = self.device._meta.app_label,
model = self.device._meta.model_name
)
)[0]
self.url_kwargs = {
'app_label': self.device._meta.app_label,
'model_name': self.device._meta.model_name,
'model_id': self.device.id,
}
self.url_view_kwargs = {
'app_label': self.device._meta.app_label,
'model_name': self.device._meta.model_name,
'model_id': self.device.id,
'pk': self.item.pk
}
self.add_data = {}
self.add_user = User.objects.create_user(username="test_user_add", password="password")
TeamUsers.objects.create(
team = add_team,
user = self.add_user
)
self.change_user = User.objects.create_user(username="test_user_change", password="password")
TeamUsers.objects.create(
team = change_team,
user = self.change_user
)
self.delete_user = User.objects.create_user(username="test_user_delete", password="password")
TeamUsers.objects.create(
team = delete_team,
user = self.delete_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 = self.different_organization,
)
different_organization_team.permissions.set([
view_permissions,
add_permissions,
change_permissions,
delete_permissions,
])
TeamUsers.objects.create(
team = different_organization_team,
user = self.different_organization_user
)
class HistoryPermissionsAPI(
ViewSetBase,
APIPermissionView,
TestCase
):
def test_returned_data_from_user_and_global_organizations_only(self):
"""Check items returned
This test case is a over-ride of a test case with the same name.
Although this model is a tenancy model, the viewset filters by object
ID, so will only return the one item.
Items returned from the query Must be from the users organization and
global ONLY!
"""
client = Client()
url = reverse(self.app_namespace + ':' + self.url_name + '-list', kwargs=self.url_kwargs)
only_from_user_org: bool = True
viewable_organizations = [
self.organization.id,
self.global_organization.id
]
assert getattr(self.global_organization, 'id', False) # fail if no global org set
assert getattr(self.global_org_item, 'id', False) # fail if no global item set
client.force_login(self.view_user)
response = client.get(url)
assert len(response.data['results']) == 1 # fail if only one item extist.
def test_view_list_has_permission(self):
""" Check correct permission for view
Attempt to view as user with view permission
"""
client = Client()
url = reverse(self.app_namespace + ':' + self.url_name + '-list', kwargs=self.url_kwargs)
client.force_login(self.view_user)
response = client.get(url)
assert response.status_code == 200
def test_view_has_permission(self):
""" Check correct permission for view
Custom permission of test case with same name.
This test ensures that the user is denied
Attempt to view as user with view permission
"""
client = Client()
url = reverse(self.app_namespace + ':' + self.url_name + '-detail', kwargs=self.url_view_kwargs)
client.force_login(self.view_user)
response = client.get(url)
assert response.status_code == 200
def test_add_has_permission_method_not_allowed(self):
""" Check correct permission for add
Custom permission of test case with same name.
This test ensures that the user is denied
Attempt to add as user with permission
"""
client = Client()
if self.url_kwargs:
url = reverse(self.app_namespace + ':' + self.url_name + '-list', kwargs = self.url_kwargs)
else:
url = reverse(self.app_namespace + ':' + self.url_name + '-list')
client.force_login(self.add_user)
response = client.post(url, data=self.add_data)
assert response.status_code == 405
def test_change_has_permission_method_not_allowed(self):
""" Check correct permission for change
Custom permission of test case with same name.
This test ensures that the user is denied
Make change with user who has change permission
"""
client = Client()
url = reverse(self.app_namespace + ':' + self.url_name + '-detail', kwargs=self.url_view_kwargs)
client.force_login(self.change_user)
response = client.patch(url, data=self.change_data, content_type='application/json')
assert response.status_code == 405
def test_delete_has_permission_method_not_allowed(self):
""" Check correct permission for delete
Custom permission of test case with same name.
This test ensures that the user is denied
Delete item as user with delete permission
"""
client = Client()
url = reverse(self.app_namespace + ':' + self.url_name + '-detail', kwargs=self.url_view_kwargs)
client.force_login(self.delete_user)
response = client.delete(url, data=self.delete_data)
assert response.status_code == 405
class HistoryMetadata(
ViewSetBase,
MetadataAttributesFunctionalEndpoint,
MetadataAttributesFunctionalBase,
TestCase
):
def test_method_options_request_detail_data_has_key_urls_self(self):
"""Test HTTP/Options Method
This is a custom test of a test with the same name.
history view has no detail view, due to using a custom
view "history",
Ensure the request data returned has key `urls.self`
"""
client = Client()
client.force_login(self.view_user)
response = client.options(
reverse(
self.app_namespace + ':' + self.url_name + '-list',
kwargs=self.url_kwargs
),
content_type='application/json'
)
assert 'urls' in response.data
def test_method_options_request_detail_data_key_urls_self_is_str(self):
"""Test HTTP/Options Method
This is a custom test of a test with the same name.
history view has no detail view, due to using a custom
view "history",
Ensure the request data key `urls.self` is a string
"""
client = Client()
client.force_login(self.view_user)
response = client.options(
reverse(
self.app_namespace + ':' + self.url_name + '-list',
kwargs=self.url_kwargs
),
content_type='application/json'
)
assert type(response.data['urls']['self']) is str

View File

@ -1,34 +0,0 @@
from django.test import TestCase
from core.models.ticket.ticket_category_history import TicketCategory, TicketCategoryHistory
from core.tests.abstract.test_functional_history import HistoryEntriesCommon
class History(
HistoryEntriesCommon,
TestCase,
):
model = TicketCategory
history_model = TicketCategoryHistory
@classmethod
def setUpTestData(self):
super().setUpTestData()
self.obj = self.model.objects.create(
organization = self.organization,
name = self.field_value_original
)
self.obj_delete = self.model.objects.create(
organization = self.organization,
name = self.field_value_delete
)
self.call_the_banners()

View File

@ -1,34 +0,0 @@
from django.test import TestCase
from core.models.ticket.ticket_comment_category_history import TicketCommentCategory, TicketCommentCategoryHistory
from core.tests.abstract.test_functional_history import HistoryEntriesCommon
class History(
HistoryEntriesCommon,
TestCase,
):
model = TicketCommentCategory
history_model = TicketCommentCategoryHistory
@classmethod
def setUpTestData(self):
super().setUpTestData()
self.obj = self.model.objects.create(
organization = self.organization,
name = self.field_value_original
)
self.obj_delete = self.model.objects.create(
organization = self.organization,
name = self.field_value_delete
)
self.call_the_banners()

View File

@ -1,43 +0,0 @@
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from core.models.manufacturer_history import Manufacturer, ManufacturerHistory
from core.tests.abstract.test_unit_model_history_api_v2 import PrimaryModelHistoryAPI
class ModelHistoryAPI(
PrimaryModelHistoryAPI,
TestCase,
):
audit_model = Manufacturer
model = ManufacturerHistory
@classmethod
def setUpTestData(self):
super().setUpTestData()
self.audit_object = self.audit_model.objects.create(
organization = self.organization,
name = 'one',
)
self.history_entry = self.model.objects.create(
organization = self.audit_object.organization,
action = self.model.Actions.ADD,
user = self.view_user,
before = {},
after = {},
content_type = ContentType.objects.get(
app_label = self.audit_object._meta.app_label,
model = self.audit_object._meta.model_name,
),
model = self.audit_object,
)
self.make_request()

View File

@ -1,68 +0,0 @@
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from core.models.manufacturer_history import Manufacturer, ManufacturerHistory
from core.tests.abstract.test_unit_model_history_api_v2 import BaseModelHistoryAPI
class ModelHistoryAPI(
BaseModelHistoryAPI,
TestCase,
):
audit_model = Manufacturer
model = ManufacturerHistory
@classmethod
def setUpTestData(self):
super().setUpTestData()
self.audit_object = self.audit_model.objects.create(
organization = self.organization,
name = 'one',
)
self.history_entry = self.model.objects.create(
organization = self.audit_object.organization,
action = self.model.Actions.ADD,
user = self.view_user,
before = {},
after = {},
content_type = ContentType.objects.get(
app_label = self.audit_object._meta.app_label,
model = self.audit_object._meta.model_name,
),
model = self.audit_object,
)
self.make_request()
# def test_api_field_exists_model_notes(self):
# """ Test for existance of API Field
# Test case is a duplicate of a test with the same name.
# This model does not have a `model_notes` field.
# model_notes field must exist
# """
# assert 'model_notes' not in self.api_data
# def test_api_field_type_model_notes(self):
# """ Test for type for API Field
# Test case is a duplicate of a test with the same name.
# This model does not have a `model_notes` field.
# model_notes field must be str
# """
# pass

View File

@ -1,99 +0,0 @@
import django
from django.contrib.auth.models import ContentType
from django.test import TestCase
from access.models.tenant import Tenant as Organization
from centurion.tests.unit.test_unit_models import TenancyObjectInheritedCases
from core.models.manufacturer_history import Manufacturer
from core.models.model_history import ModelHistory
User = django.contrib.auth.get_user_model()
class ModelHistoryTestCases(
TenancyObjectInheritedCases
):
model = ModelHistory
should_model_history_be_saved = True
class ModelHistoryInheritedCases(
ModelHistoryTestCases
):
model = None
class ModelHistoryTest(
ModelHistoryTestCases,
TestCase,
):
should_model_history_be_saved = False
@classmethod
def setUpTestData(self):
"""Setup Test
1. Create an organization for user and item
. create an organization that is different to item
2. Create a device
3. create teams with each permission: view, add, change, delete
4. create a user per team
"""
self.organization = Organization.objects.create(name='test_org')
model = Manufacturer.objects.create(
organization=self.organization,
name = 'man',
)
self.kwargs_item_create = {
'action': self.model.Actions.ADD,
'user': User.objects.create_user(
username="test_user_view", password="password", is_superuser=True
),
'before': {},
'after': {},
'content_type': ContentType.objects.get(
app_label = model._meta.app_label,
model = model._meta.model_name,
),
}
super().setUpTestData()
def test_attribute_type_get_url(self):
"""Attribute Type
This test case is a duplicate test of a case with the same name.
This model does not require this attribute.
get_url is of type str
"""
assert not hasattr(self, 'item.get_url')
def test_attribute_type_get_url_kwargs(self):
"""Attribute Type
This test case is a duplicate test of a case with the same name.
This model does not require this attribute.
get_url_kwargs is of type dict
"""
assert not hasattr(self, 'item.get_url_kwargs')

View File

@ -1,54 +0,0 @@
from django.test import Client, TestCase
from rest_framework.reverse import reverse
from api.tests.unit.test_unit_common_viewset import ReadOnlyModelViewSetInheritedCases
from core.viewsets.history import ViewSet
from itam.models.device import Device
class HistoryViewsetList(
ReadOnlyModelViewSetInheritedCases,
TestCase,
):
viewset = ViewSet
route_name = 'v2:_api_v2_model_history'
@classmethod
def setUpTestData(self):
"""Setup Test
1. make list request
"""
super().setUpTestData()
device = Device.objects.create(
organization = self.organization,
name = 'dev'
)
self.kwargs = {
'app_label': device._meta.app_label,
'model_name': device._meta.model_name,
'model_id': device.id
}
client = Client()
url = reverse(
self.route_name + '-list',
kwargs = self.kwargs
)
client.force_login(self.view_user)
self.http_options_response_list = client.options(url)

View File

@ -1,43 +0,0 @@
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from core.models.ticket.ticket_category_history import TicketCategory, TicketCategoryHistory
from core.tests.abstract.test_unit_model_history_api_v2 import PrimaryModelHistoryAPI
class ModelHistoryAPI(
PrimaryModelHistoryAPI,
TestCase,
):
audit_model = TicketCategory
model = TicketCategoryHistory
@classmethod
def setUpTestData(self):
super().setUpTestData()
self.audit_object = self.audit_model.objects.create(
organization = self.organization,
name = 'one',
)
self.history_entry = self.model.objects.create(
organization = self.audit_object.organization,
action = self.model.Actions.ADD,
user = self.view_user,
before = {},
after = {},
content_type = ContentType.objects.get(
app_label = self.audit_object._meta.app_label,
model = self.audit_object._meta.model_name,
),
model = self.audit_object,
)
self.make_request()

View File

@ -1,43 +0,0 @@
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from core.models.ticket.ticket_comment_category_history import TicketCommentCategory, TicketCommentCategoryHistory
from core.tests.abstract.test_unit_model_history_api_v2 import PrimaryModelHistoryAPI
class ModelHistoryAPI(
PrimaryModelHistoryAPI,
TestCase,
):
audit_model = TicketCommentCategory
model = TicketCommentCategoryHistory
@classmethod
def setUpTestData(self):
super().setUpTestData()
self.audit_object = self.audit_model.objects.create(
organization = self.organization,
name = 'one',
)
self.history_entry = self.model.objects.create(
organization = self.audit_object.organization,
action = self.model.Actions.ADD,
user = self.view_user,
before = {},
after = {},
content_type = ContentType.objects.get(
app_label = self.audit_object._meta.app_label,
model = self.audit_object._meta.model_name,
),
model = self.audit_object,
)
self.make_request()