Skip to content

Commit

Permalink
test(itim): Cluster Types unit tests
Browse files Browse the repository at this point in the history
ref: #244 #71
  • Loading branch information
jon-nfc committed Aug 19, 2024
1 parent b3883ee commit 12a5dbc
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 0 deletions.
42 changes: 42 additions & 0 deletions app/itim/tests/unit/cluster_types/test_cluster_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest
import unittest

from django.test import TestCase

from access.models import Organization

from app.tests.abstract.models import TenancyModel

from itim.models.clusters import ClusterType



@pytest.mark.django_db
class ClusterTypeModel(
TestCase,
TenancyModel
):

model = ClusterType

@classmethod
def setUpTestData(self):
"""Setup Test
1. Create an organization for user and item
2. Create an item
"""

self.organization = Organization.objects.create(name='test_org')


self.item = self.model.objects.create(
organization = self.organization,
name = 'one',
)

self.second_item = self.model.objects.create(
organization = self.organization,
name = 'one_two',
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

import pytest
import unittest
import requests

from django.test import TestCase, Client

from access.models import Organization

from core.models.history import History
from core.tests.abstract.history_entry import HistoryEntry
from core.tests.abstract.history_entry_parent_model import HistoryEntryParentItem

from itim.models.clusters import ClusterType



class ClusterTypeHistory(TestCase, HistoryEntry, HistoryEntryParentItem):


model = ClusterType


@classmethod
def setUpTestData(self):
""" Setup Test """

organization = Organization.objects.create(name='test_org')

self.organization = organization

self.item_parent = self.model.objects.create(
name = 'test_item_parent_' + self.model._meta.model_name,
organization = self.organization
)

self.item_create = self.model.objects.create(
name = 'test_item_' + self.model._meta.model_name,
organization = self.organization,
)


self.history_create = History.objects.get(
action = History.Actions.ADD[0],
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._meta.model_name + '_changed'
self.item_change.save()

self.field_after_expected_value = '{"name": "' + self.item_change.name + '"}'

self.history_change = History.objects.get(
action = History.Actions.UPDATE[0],
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._meta.model_name,
organization = self.organization,
)

self.deleted_pk = self.item_delete.pk

self.item_delete.delete()

self.history_delete = History.objects.filter(
item_pk = self.deleted_pk,
item_class = self.model._meta.model_name,
)

self.history_delete_children = History.objects.filter(
item_parent_pk = self.deleted_pk,
item_parent_class = self.item_parent._meta.model_name,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# 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 itim.models.clusters import ClusterType

from core.tests.abstract.history_permissions import HistoryPermissions



class ClusterTypeHistoryPermissions(TestCase, HistoryPermissions):


item_model = ClusterType


@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.item_model.objects.create(
organization=organization,
name = 'deviceone'
)

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._meta.model_name,
content_type = ContentType.objects.get(
app_label = self.model._meta.app_label,
model = self.model._meta.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
)
Loading

0 comments on commit 12a5dbc

Please sign in to comment.