Compare commits
191 Commits
access-tes
...
861bef0ce1
Author | SHA1 | Date | |
---|---|---|---|
861bef0ce1 | |||
3b86ab0e88 | |||
9f62e2a458 | |||
bd393e3dd4 | |||
9c73885f1b | |||
dbac9326b3 | |||
dd0a6a01b9 | |||
c294f9c8f5 | |||
71a510170d | |||
d577b12a33 | |||
fb090b6f63 | |||
75cda05579 | |||
97e63f1daa | |||
dce169109c | |||
4c6473a7b0 | |||
204a20b793 | |||
bf1a60439f | |||
006d7ab0c2 | |||
cca693d02d | |||
bb88a7025d | |||
8da10a147b | |||
653e29ffe5 | |||
b13610ef0e | |||
8739eba655 | |||
1b338e4c19 | |||
f525411ace | |||
f5f4eb3ff2 | |||
b7c68694a6 | |||
363d044851 | |||
b66adb2ebe | |||
4037127c39 | |||
1640910305 | |||
d0effd582a | |||
ad175f4f7d | |||
6dc42d4cdb | |||
3f08bf532f | |||
9cde656450 | |||
62da0f8c7a | |||
4045f0cffb | |||
6eb4cb12ba | |||
2d720032f6 | |||
bb7cf9d462 | |||
d53eb7101d | |||
0edfaef79a | |||
0c508a7e0b | |||
2bdb12ed7a | |||
187f82feaf | |||
91082b205d | |||
b7d190218c | |||
2daf7dbb27 | |||
f98d2f2884 | |||
fa0b703877 | |||
1b7d92e300 | |||
697d375d06 | |||
3726f3aee9 | |||
28cd56d9a0 | |||
017f5de78a | |||
d8fa00b530 | |||
3765113445 | |||
8f1a9966e9 | |||
e3f6b3f9ad | |||
422acd64fa | |||
dc01c6b0f9 | |||
e24a135616 | |||
6f82a054cd | |||
ecafbcf830 | |||
1c8f641dd4 | |||
a3f4299299 | |||
173a47d942 | |||
24c4607c2d | |||
d68afd8992 | |||
38c9cccf30 | |||
c2344e95c5 | |||
6c1542f572 | |||
10550495d8 | |||
d241a9a683 | |||
1b8be14f12 | |||
4c49bb0bd2 | |||
6578d67250 | |||
0f9cbfdc13 | |||
fc984c5034 | |||
7d47a9ca49 | |||
2890750d50 | |||
c1b44c03fe | |||
207ba2285a | |||
dd337b68bc | |||
42e191ba76 | |||
dfb1af1ff7 | |||
df8265dd55 | |||
e14780eb22 | |||
9104cd021f | |||
5db10a4b6d | |||
6faa185406 | |||
7fb56d87d6 | |||
cf09a15690 | |||
76dfcb8bce | |||
430cbe6385 | |||
bcce98e884 | |||
66706df727 | |||
838bc871c1 | |||
69f1c1c3b6 | |||
8d0d9240a8 | |||
d92f967bb9 | |||
7413a5686d | |||
3d4018e306 | |||
93dc76fc1a | |||
f461b7500f | |||
4b1548afa8 | |||
3fa8bdf468 | |||
8363c50b91 | |||
3bd673b2ae | |||
e9bc7e543a | |||
20694b950a | |||
095deacf65 | |||
5e1ebc949e | |||
9b3340f44c | |||
c93dbf48c9 | |||
6414f70468 | |||
9e24bf4eed | |||
565996bc2a | |||
3bc35777ca | |||
0bc313c198 | |||
ff1085df24 | |||
b6743ebe4c | |||
701193ea82 | |||
55408f1280 | |||
b84c3d1b14 | |||
b0597d354a | |||
c32cc6c954 | |||
b84571bd44 | |||
3a5630995b | |||
ee5055950d | |||
9e6322b5f1 | |||
a805660f0f | |||
7bce005a05 | |||
811c92ada6 | |||
e3b9cba23c | |||
6f58f2985c | |||
98891be181 | |||
bb93b4ebed | |||
a82ff5a41f | |||
224a69319e | |||
854b295807 | |||
fc0b645d2d | |||
4b5caad7d5 | |||
5f5d7923c6 | |||
099c70d466 | |||
2ff5c345e8 | |||
9f20fe9614 | |||
cf5b78b6b2 | |||
cc481f94e9 | |||
3248d5201d | |||
283051a4ad | |||
03d320f6a0 | |||
83ebf12978 | |||
09c1acbb1f | |||
4fc58c5ea1 | |||
c476548aa3 | |||
1812746a02 | |||
78cf8522e3 | |||
9e7c4a8191 | |||
fa1cee32cf | |||
ca3af15847 | |||
c0c65903f4 | |||
c19fe7f874 | |||
05d87c93e6 | |||
ca661c9ebb | |||
2a7bf9c57b | |||
5794d88ce4 | |||
73e3781fe6 | |||
f95639681d | |||
c99166fffa | |||
850e183f45 | |||
10569b6a46 | |||
464bf0da1d | |||
123828d96d | |||
a72e153aa3 | |||
3ed7471c15 | |||
c85b666a84 | |||
6baeb12be6 | |||
2769859db0 | |||
209c01f5a3 | |||
fc5490afe0 | |||
83b1ca577b | |||
04409ed2c8 | |||
7018c2d963 | |||
6c83d0f0d7 | |||
b0cc4b8691 | |||
d7f231c831 | |||
96ec149017 | |||
a623056325 |
9
.vscode/settings.json
vendored
9
.vscode/settings.json
vendored
@ -26,4 +26,13 @@
|
||||
},
|
||||
"telemetry.feedback.enabled": false,
|
||||
"python.languageServer": "None",
|
||||
"debug.javascript.enableNetworkView": false,
|
||||
"typescript.experimental.expandableHover": false,
|
||||
"ipynb.experimental.serialization": false,
|
||||
"notebook.experimental.generate": false,
|
||||
"extensions.experimental.issueQuickAccess": false,
|
||||
"workbench.commandPalette.experimental.enableNaturalLanguageSearch": false,
|
||||
"multiDiffEditor.experimental.enabled": false,
|
||||
"diffEditor.experimental.showEmptyDecorations": false,
|
||||
"editor.experimental.asyncTokenization": false,
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from access.tests.functional.entity.test_functional_entity_permission import (
|
||||
EntityPermissionsAPIInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
class CompanyPermissionsAPITestCases(
|
||||
EntityPermissionsAPIInheritedCases,
|
||||
):
|
||||
|
||||
add_data: dict = {
|
||||
'name': 'Ian1',
|
||||
}
|
||||
|
||||
kwargs_create_item: dict = {
|
||||
'name': 'Ian2',
|
||||
}
|
||||
|
||||
kwargs_create_item_diff_org: dict = {
|
||||
'name': 'Ian3',
|
||||
}
|
||||
|
||||
|
||||
|
||||
class CompanyPermissionsAPIInheritedCases(
|
||||
CompanyPermissionsAPITestCases,
|
||||
):
|
||||
|
||||
add_data: dict = None
|
||||
|
||||
kwargs_create_item: dict = None
|
||||
|
||||
kwargs_create_item_diff_org: dict = None
|
||||
|
||||
|
||||
|
||||
class CompanyPermissionsAPIPyTest(
|
||||
CompanyPermissionsAPITestCases,
|
||||
):
|
||||
|
||||
pass
|
@ -1,46 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from access.tests.functional.entity.test_functional_entity_serializer import (
|
||||
MockView,
|
||||
EntitySerializerInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
class CompanySerializerTestCases(
|
||||
EntitySerializerInheritedCases
|
||||
):
|
||||
|
||||
|
||||
parameterized_test_data: dict = {
|
||||
"name": {
|
||||
'will_create': False,
|
||||
'exception_key': 'required'
|
||||
},
|
||||
}
|
||||
|
||||
valid_data: dict = {
|
||||
'name': 'Ian',
|
||||
}
|
||||
"""Valid data used by serializer to create object"""
|
||||
|
||||
|
||||
|
||||
class CompanySerializerInheritedCases(
|
||||
CompanySerializerTestCases,
|
||||
):
|
||||
|
||||
parameterized_test_data: dict = None
|
||||
|
||||
valid_data: dict = None
|
||||
"""Valid data used by serializer to create object"""
|
||||
|
||||
|
||||
|
||||
class CompanySerializerPyTest(
|
||||
CompanySerializerTestCases,
|
||||
):
|
||||
|
||||
parameterized_test_data: dict = None
|
@ -1,58 +0,0 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from access.models.company_base import Company
|
||||
from access.tests.functional.entity.test_functional_entity_viewset import (
|
||||
EntityViewSetInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
class ViewSetTestCases(
|
||||
EntityViewSetInheritedCases,
|
||||
):
|
||||
|
||||
add_data: dict = {
|
||||
'name': 'Ian',
|
||||
}
|
||||
|
||||
kwargs_create_item: dict = {
|
||||
'name': 'Ian2',
|
||||
}
|
||||
|
||||
kwargs_create_item_diff_org: dict = {
|
||||
'name': 'Ian3',
|
||||
}
|
||||
|
||||
model = Company
|
||||
|
||||
|
||||
|
||||
class CompanyViewSetInheritedCases(
|
||||
ViewSetTestCases,
|
||||
):
|
||||
|
||||
model = None
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
|
||||
self.kwargs_create_item = {
|
||||
**super().kwargs_create_item,
|
||||
**self.kwargs_create_item
|
||||
}
|
||||
|
||||
self.kwargs_create_item_diff_org = {
|
||||
**super().kwargs_create_item_diff_org,
|
||||
**self.kwargs_create_item_diff_org
|
||||
}
|
||||
|
||||
super().setUpTestData()
|
||||
|
||||
|
||||
|
||||
class CompanyViewSetTest(
|
||||
ViewSetTestCases,
|
||||
TestCase,
|
||||
):
|
||||
pass
|
@ -1,70 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from access.tests.functional.person.test_functional_person_permission import (
|
||||
PersonPermissionsAPIInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
class ContactPermissionsAPITestCases(
|
||||
PersonPermissionsAPIInheritedCases,
|
||||
):
|
||||
|
||||
add_data: dict = {
|
||||
'email': 'ipfunny@unit.test',
|
||||
}
|
||||
|
||||
kwargs_create_item: dict = {
|
||||
'email': 'ipweird@unit.test',
|
||||
}
|
||||
|
||||
kwargs_create_item_diff_org: dict = {
|
||||
'email': 'ipstrange@unit.test',
|
||||
}
|
||||
|
||||
|
||||
|
||||
class ContactPermissionsAPIInheritedCases(
|
||||
ContactPermissionsAPITestCases,
|
||||
):
|
||||
|
||||
add_data: dict = None
|
||||
|
||||
kwargs_create_item: dict = None
|
||||
|
||||
kwargs_create_item_diff_org: dict = None
|
||||
|
||||
# url_name = '_api_entity_sub'
|
||||
|
||||
|
||||
# @pytest.fixture(scope='class')
|
||||
# def inherited_var_setup(self, request):
|
||||
|
||||
# request.cls.url_kwargs.update({
|
||||
# 'model_name': self.model._meta.sub_model_type
|
||||
# })
|
||||
|
||||
# request.cls.url_view_kwargs.update({
|
||||
# 'model_name': self.model._meta.sub_model_type
|
||||
# })
|
||||
|
||||
|
||||
|
||||
# @pytest.fixture(scope='class', autouse = True)
|
||||
# def class_setup(self, request, django_db_blocker,
|
||||
# model,
|
||||
# var_setup,
|
||||
# prepare,
|
||||
# inherited_var_setup,
|
||||
# diff_org_model,
|
||||
# create_model,
|
||||
# ):
|
||||
|
||||
# pass
|
||||
|
||||
|
||||
class ContactPermissionsAPIPyTest(
|
||||
ContactPermissionsAPITestCases,
|
||||
):
|
||||
|
||||
pass
|
@ -1,46 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from access.tests.functional.person.test_functional_person_serializer import (
|
||||
MockView,
|
||||
PersonSerializerInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
class ContactSerializerTestCases(
|
||||
PersonSerializerInheritedCases
|
||||
):
|
||||
|
||||
|
||||
parameterized_test_data: dict = {
|
||||
"email": {
|
||||
'will_create': False,
|
||||
'exception_key': 'required'
|
||||
}
|
||||
}
|
||||
|
||||
valid_data: dict = {
|
||||
'email': 'contactentityduplicatetwo@unit.test',
|
||||
}
|
||||
"""Valid data used by serializer to create object"""
|
||||
|
||||
|
||||
|
||||
class ContactSerializerInheritedCases(
|
||||
ContactSerializerTestCases,
|
||||
):
|
||||
|
||||
parameterized_test_data: dict = None
|
||||
|
||||
valid_data: dict = None
|
||||
"""Valid data used by serializer to create object"""
|
||||
|
||||
|
||||
|
||||
class ContactSerializerPyTest(
|
||||
ContactSerializerTestCases,
|
||||
):
|
||||
|
||||
parameterized_test_data: dict = None
|
@ -1,58 +0,0 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from access.models.contact import Contact
|
||||
from access.tests.functional.person.test_functional_person_viewset import (
|
||||
PersonViewSetInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
class ViewSetTestCases(
|
||||
PersonViewSetInheritedCases,
|
||||
):
|
||||
|
||||
add_data: dict = {
|
||||
'email': 'ipfunny@unit.test',
|
||||
}
|
||||
|
||||
kwargs_create_item: dict = {
|
||||
'email': 'ipweird@unit.test',
|
||||
}
|
||||
|
||||
kwargs_create_item_diff_org: dict = {
|
||||
'email': 'ipstrange@unit.test',
|
||||
}
|
||||
|
||||
model = Contact
|
||||
|
||||
|
||||
|
||||
class ContactViewSetInheritedCases(
|
||||
ViewSetTestCases,
|
||||
):
|
||||
|
||||
model = None
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
|
||||
self.kwargs_create_item = {
|
||||
**super().kwargs_create_item,
|
||||
**self.kwargs_create_item
|
||||
}
|
||||
|
||||
self.kwargs_create_item_diff_org = {
|
||||
**super().kwargs_create_item_diff_org,
|
||||
**self.kwargs_create_item_diff_org
|
||||
}
|
||||
|
||||
super().setUpTestData()
|
||||
|
||||
|
||||
|
||||
class ContactViewSetTest(
|
||||
ViewSetTestCases,
|
||||
TestCase,
|
||||
):
|
||||
pass
|
@ -1,89 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from api.tests.functional.test_functional_api_permissions import (
|
||||
APIPermissionsInheritedCases,
|
||||
)
|
||||
|
||||
|
||||
|
||||
class EntityPermissionsAPITestCases(
|
||||
APIPermissionsInheritedCases,
|
||||
):
|
||||
|
||||
add_data: dict = {}
|
||||
|
||||
app_namespace = 'v2'
|
||||
|
||||
change_data = {}
|
||||
|
||||
delete_data = {}
|
||||
|
||||
kwargs_create_item: dict = {}
|
||||
|
||||
kwargs_create_item_diff_org: dict = {}
|
||||
|
||||
url_kwargs: dict = {}
|
||||
|
||||
url_name = '_api_entity'
|
||||
|
||||
url_view_kwargs: dict = {}
|
||||
|
||||
|
||||
|
||||
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.
|
||||
This model is not a tenancy model making this test not-applicable.
|
||||
|
||||
Items returned from the query Must be from the users organization and
|
||||
global ONLY!
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class EntityPermissionsAPIInheritedCases(
|
||||
EntityPermissionsAPITestCases,
|
||||
):
|
||||
|
||||
add_data: dict = None
|
||||
|
||||
kwargs_create_item: dict = None
|
||||
|
||||
kwargs_create_item_diff_org: dict = None
|
||||
|
||||
url_name = '_api_entity_sub'
|
||||
|
||||
|
||||
@pytest.fixture(scope='class')
|
||||
def inherited_var_setup(self, request):
|
||||
|
||||
request.cls.url_kwargs.update({
|
||||
'model_name': self.model._meta.sub_model_type
|
||||
})
|
||||
|
||||
request.cls.url_view_kwargs.update({
|
||||
'model_name': self.model._meta.sub_model_type
|
||||
})
|
||||
|
||||
|
||||
|
||||
@pytest.fixture(scope='class', autouse = True)
|
||||
def class_setup(self, request, django_db_blocker,
|
||||
model,
|
||||
var_setup,
|
||||
prepare,
|
||||
inherited_var_setup,
|
||||
diff_org_model,
|
||||
create_model,
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class EntityPermissionsAPIPyTest(
|
||||
EntityPermissionsAPITestCases,
|
||||
):
|
||||
|
||||
pass
|
@ -28,7 +28,7 @@ class MockView:
|
||||
"""
|
||||
|
||||
|
||||
|
||||
@pytest.mark.skip(reason = 'see #874, tests being refactored')
|
||||
class EntitySerializerTestCases:
|
||||
|
||||
|
||||
|
@ -1,266 +0,0 @@
|
||||
import django
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.test import TestCase
|
||||
|
||||
from access.models.entity import Entity
|
||||
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_serializer_viewset import SerializersTestCases
|
||||
|
||||
User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
|
||||
class ViewSetBase:
|
||||
|
||||
add_data: dict = {
|
||||
'model_notes': 'added model note'
|
||||
}
|
||||
|
||||
app_namespace = 'v2'
|
||||
|
||||
base_model = Entity
|
||||
"""Base model for this sub model
|
||||
don't change or override this value
|
||||
"""
|
||||
|
||||
change_data = None
|
||||
|
||||
delete_data = {}
|
||||
|
||||
kwargs_create_item: dict = {
|
||||
'model_notes': 'added model note'
|
||||
}
|
||||
|
||||
kwargs_create_item_diff_org: dict = {
|
||||
'model_notes': 'added model note'
|
||||
}
|
||||
|
||||
model = None
|
||||
|
||||
url_kwargs: dict = {}
|
||||
|
||||
url_view_kwargs: dict = {}
|
||||
|
||||
url_name = None
|
||||
|
||||
|
||||
@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 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.view_user = User.objects.create_user(username="test_user_view", password="password")
|
||||
|
||||
self.item = self.model.objects.create(
|
||||
organization = organization,
|
||||
**self.kwargs_create_item
|
||||
)
|
||||
|
||||
self.other_org_item = self.model.objects.create(
|
||||
organization = self.different_organization,
|
||||
**self.kwargs_create_item_diff_org
|
||||
)
|
||||
|
||||
|
||||
self.url_view_kwargs.update({ 'pk': self.item.id })
|
||||
|
||||
if self.add_data is not None:
|
||||
|
||||
self.add_data.update({
|
||||
'organization': self.organization.id,
|
||||
})
|
||||
|
||||
|
||||
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])
|
||||
|
||||
|
||||
|
||||
add_permissions = Permission.objects.get(
|
||||
codename = 'add_' + self.model._meta.model_name,
|
||||
content_type = ContentType.objects.get(
|
||||
app_label = self.model._meta.app_label,
|
||||
model = self.model._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_' + self.model._meta.model_name,
|
||||
content_type = ContentType.objects.get(
|
||||
app_label = self.model._meta.app_label,
|
||||
model = self.model._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_' + self.model._meta.model_name,
|
||||
content_type = ContentType.objects.get(
|
||||
app_label = self.model._meta.app_label,
|
||||
model = self.model._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")
|
||||
|
||||
|
||||
TeamUsers.objects.create(
|
||||
team = view_team,
|
||||
user = self.view_user
|
||||
)
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
|
||||
def test_sanity_is_asset_sub_model(self):
|
||||
"""Sanity Test
|
||||
|
||||
This test ensures that the model being tested `self.model` is a
|
||||
sub-model of `self.base_model`.
|
||||
This test is required as the same viewset is used for all sub-models
|
||||
of `Entity`
|
||||
"""
|
||||
|
||||
assert issubclass(self.model, self.base_model)
|
||||
|
||||
|
||||
|
||||
class ViewSetTestCases(
|
||||
ViewSetBase,
|
||||
SerializersTestCases,
|
||||
):
|
||||
|
||||
model = Entity
|
||||
|
||||
|
||||
|
||||
class EntityViewSetInheritedCases(
|
||||
ViewSetTestCases,
|
||||
):
|
||||
|
||||
model = None
|
||||
|
||||
url_name = '_api_entity_sub'
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
|
||||
self.kwargs_create_item = {
|
||||
**super().kwargs_create_item,
|
||||
**self.kwargs_create_item
|
||||
}
|
||||
|
||||
self.kwargs_create_item_diff_org = {
|
||||
**super().kwargs_create_item_diff_org,
|
||||
**self.kwargs_create_item_diff_org
|
||||
}
|
||||
|
||||
self.url_kwargs = {
|
||||
'model_name': self.model._meta.sub_model_type
|
||||
}
|
||||
|
||||
self.url_view_kwargs = {
|
||||
'model_name': self.model._meta.sub_model_type
|
||||
}
|
||||
|
||||
super().setUpTestData()
|
||||
|
||||
|
||||
|
||||
class EntityViewSetTest(
|
||||
ViewSetTestCases,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
url_name = '_api_entity'
|
@ -1,94 +0,0 @@
|
||||
import django
|
||||
import pytest
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from access.serializers.organization import (
|
||||
Tenant,
|
||||
TenantModelSerializer
|
||||
)
|
||||
|
||||
User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
|
||||
class OrganizationValidationAPI(
|
||||
TestCase,
|
||||
):
|
||||
|
||||
model = Tenant
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. Create an org
|
||||
2. Create an item
|
||||
"""
|
||||
|
||||
self.user = User.objects.create(username = 'org_user', password='random password')
|
||||
|
||||
self.valid_data = {
|
||||
'name': 'valid_org_data',
|
||||
'manager': self.user.id
|
||||
}
|
||||
|
||||
self.item = self.model.objects.create(
|
||||
name = 'random title',
|
||||
)
|
||||
|
||||
|
||||
|
||||
def test_serializer_valid_data(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no name is provided a validation error occurs
|
||||
"""
|
||||
|
||||
serializer = TenantModelSerializer(
|
||||
data = self.valid_data
|
||||
)
|
||||
|
||||
assert serializer.is_valid(raise_exception = True)
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_name(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no name is provided a validation error occurs
|
||||
"""
|
||||
|
||||
data = self.valid_data.copy()
|
||||
|
||||
del data['name']
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = TenantModelSerializer(
|
||||
data = data
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['name'][0] == 'required'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_manager_optional(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no name is provided a validation error occurs
|
||||
"""
|
||||
|
||||
data = self.valid_data.copy()
|
||||
|
||||
del data['manager']
|
||||
|
||||
serializer = TenantModelSerializer(
|
||||
data = data
|
||||
)
|
||||
|
||||
assert serializer.is_valid(raise_exception = True)
|
@ -1,91 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from access.tests.functional.entity.test_functional_entity_permission import (
|
||||
EntityPermissionsAPIInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
class PersonPermissionsAPITestCases(
|
||||
EntityPermissionsAPIInheritedCases,
|
||||
):
|
||||
|
||||
add_data: dict = {
|
||||
'f_name': 'Ian',
|
||||
'm_name': 'Peter',
|
||||
'l_name': 'Strange',
|
||||
'dob': '2025-04-08',
|
||||
}
|
||||
|
||||
# app_namespace = 'v2'
|
||||
|
||||
# change_data = {}
|
||||
|
||||
# delete_data = {}
|
||||
|
||||
kwargs_create_item: dict = {
|
||||
'f_name': 'Ian',
|
||||
'm_name': 'Peter',
|
||||
'l_name': 'Weird',
|
||||
'dob': '2025-04-08',
|
||||
}
|
||||
|
||||
kwargs_create_item_diff_org: dict = {
|
||||
'f_name': 'Ian',
|
||||
'm_name': 'Peter',
|
||||
'l_name': 'Funny',
|
||||
'dob': '2025-04-08',
|
||||
}
|
||||
|
||||
# url_kwargs: dict = {}
|
||||
|
||||
# url_name = '_api_entity'
|
||||
|
||||
# url_view_kwargs: dict = {}
|
||||
|
||||
|
||||
|
||||
class PersonPermissionsAPIInheritedCases(
|
||||
PersonPermissionsAPITestCases,
|
||||
):
|
||||
|
||||
add_data: dict = None
|
||||
|
||||
kwargs_create_item: dict = None
|
||||
|
||||
kwargs_create_item_diff_org: dict = None
|
||||
|
||||
# url_name = '_api_entity_sub'
|
||||
|
||||
|
||||
# @pytest.fixture(scope='class')
|
||||
# def inherited_var_setup(self, request):
|
||||
|
||||
# request.cls.url_kwargs.update({
|
||||
# 'model_name': self.model._meta.sub_model_type
|
||||
# })
|
||||
|
||||
# request.cls.url_view_kwargs.update({
|
||||
# 'model_name': self.model._meta.sub_model_type
|
||||
# })
|
||||
|
||||
|
||||
|
||||
# @pytest.fixture(scope='class', autouse = True)
|
||||
# def class_setup(self, request, django_db_blocker,
|
||||
# model,
|
||||
# var_setup,
|
||||
# prepare,
|
||||
# inherited_var_setup,
|
||||
# diff_org_model,
|
||||
# create_model,
|
||||
# ):
|
||||
|
||||
# pass
|
||||
|
||||
|
||||
class PersonPermissionsAPIPyTest(
|
||||
PersonPermissionsAPITestCases,
|
||||
):
|
||||
|
||||
pass
|
@ -1,67 +0,0 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from access.models.person import Person
|
||||
from access.tests.functional.entity.test_functional_entity_viewset import (
|
||||
EntityViewSetInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
class ViewSetTestCases(
|
||||
EntityViewSetInheritedCases,
|
||||
):
|
||||
|
||||
add_data: dict = {
|
||||
'f_name': 'Ian',
|
||||
'm_name': 'Peter',
|
||||
'l_name': 'Strange',
|
||||
'dob': '2025-04-08',
|
||||
}
|
||||
|
||||
kwargs_create_item: dict = {
|
||||
'f_name': 'Ian',
|
||||
'm_name': 'Peter',
|
||||
'l_name': 'Weird',
|
||||
'dob': '2025-04-08',
|
||||
}
|
||||
|
||||
kwargs_create_item_diff_org: dict = {
|
||||
'f_name': 'Ian',
|
||||
'm_name': 'Peter',
|
||||
'l_name': 'Funny',
|
||||
'dob': '2025-04-08',
|
||||
}
|
||||
|
||||
model = Person
|
||||
|
||||
|
||||
|
||||
class PersonViewSetInheritedCases(
|
||||
ViewSetTestCases,
|
||||
):
|
||||
|
||||
model = None
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
|
||||
self.kwargs_create_item = {
|
||||
**super().kwargs_create_item,
|
||||
**self.kwargs_create_item
|
||||
}
|
||||
|
||||
self.kwargs_create_item_diff_org = {
|
||||
**super().kwargs_create_item_diff_org,
|
||||
**self.kwargs_create_item_diff_org
|
||||
}
|
||||
|
||||
super().setUpTestData()
|
||||
|
||||
|
||||
|
||||
class PersonViewSetTest(
|
||||
ViewSetTestCases,
|
||||
TestCase,
|
||||
):
|
||||
pass
|
25
app/access/tests/functional/tenant/conftest.py
Normal file
25
app/access/tests/functional/tenant/conftest.py
Normal file
@ -0,0 +1,25 @@
|
||||
import pytest
|
||||
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model(model_tenant):
|
||||
|
||||
yield model_tenant
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class', autouse = True)
|
||||
def model_kwargs(request, kwargs_tenant):
|
||||
|
||||
request.cls.kwargs_create_item = kwargs_tenant.copy()
|
||||
|
||||
yield kwargs_tenant.copy()
|
||||
|
||||
if hasattr(request.cls, 'kwargs_create_item'):
|
||||
del request.cls.kwargs_create_item
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model_serializer(serializer_tenant):
|
||||
|
||||
yield serializer_tenant
|
@ -0,0 +1,117 @@
|
||||
import pytest
|
||||
|
||||
from rest_framework.relations import Hyperlink
|
||||
|
||||
from django.db import models
|
||||
from django.test import Client
|
||||
|
||||
from api.tests.functional.test_functional_api_fields import (
|
||||
APIFieldsInheritedCases,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_tenant
|
||||
class TenantAPITestCases(
|
||||
APIFieldsInheritedCases,
|
||||
):
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def make_request(self, django_db_blocker,
|
||||
request, organization_one,
|
||||
api_request_permissions,
|
||||
):
|
||||
|
||||
client = Client()
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
organization_one.manager = api_request_permissions['user']['view']
|
||||
organization_one.model_notes = 'sad'
|
||||
organization_one.save()
|
||||
|
||||
client.force_login( api_request_permissions['user']['view'] )
|
||||
response = client.get(
|
||||
organization_one.get_url()
|
||||
)
|
||||
|
||||
request.cls.api_data = response.data
|
||||
|
||||
|
||||
|
||||
item_two = getattr(request.cls, 'item_two', None)
|
||||
|
||||
if item_two:
|
||||
|
||||
response_two = client.get( self.item_two.get_url() )
|
||||
|
||||
request.cls.api_data_two = response_two.data
|
||||
|
||||
else:
|
||||
|
||||
request.cls.api_data_two = {}
|
||||
|
||||
|
||||
yield
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
organization_one.manager = None
|
||||
organization_one.model_notes = None
|
||||
organization_one.save()
|
||||
|
||||
|
||||
@property
|
||||
def parameterized_api_fields(self):
|
||||
|
||||
return {
|
||||
'_urls.notes': {
|
||||
'expected': models.NOT_PROVIDED
|
||||
},
|
||||
'organization': {
|
||||
'expected': models.NOT_PROVIDED
|
||||
},
|
||||
'organization.id': {
|
||||
'expected': models.NOT_PROVIDED
|
||||
},
|
||||
'organization.display_name': {
|
||||
'expected': models.NOT_PROVIDED
|
||||
},
|
||||
'organization.url': {
|
||||
'expected': models.NOT_PROVIDED
|
||||
},
|
||||
'name': {
|
||||
'expected': str
|
||||
},
|
||||
'manager': {
|
||||
'expected': dict
|
||||
},
|
||||
'manager.id': {
|
||||
'expected': int
|
||||
},
|
||||
'manager.display_name': {
|
||||
'expected': str
|
||||
},
|
||||
'manager.url': {
|
||||
'expected': Hyperlink
|
||||
},
|
||||
'modified': {
|
||||
'expected': str
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class TenantAPIInheritedCases(
|
||||
TenantAPITestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_access
|
||||
class TenantAPIPyTest(
|
||||
TenantAPITestCases,
|
||||
):
|
||||
|
||||
pass
|
@ -0,0 +1,28 @@
|
||||
import pytest
|
||||
|
||||
from core.tests.functional.centurion_abstract.test_functional_centurion_abstract_model import (
|
||||
CenturionAbstractModelInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_tenant
|
||||
class TenantModelTestCases(
|
||||
CenturionAbstractModelInheritedCases
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class TenantModelInheritedCases(
|
||||
TenantModelTestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_access
|
||||
class TenantModelPyTest(
|
||||
TenantModelTestCases,
|
||||
):
|
||||
pass
|
@ -16,6 +16,7 @@ User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_tenant
|
||||
class ViewSetBase:
|
||||
|
||||
model = Organization
|
||||
@ -191,16 +192,7 @@ class ViewSetBase:
|
||||
|
||||
|
||||
|
||||
class OrganizationViewSet(
|
||||
ViewSetBase,
|
||||
SerializersTestCases,
|
||||
TestCase
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_access
|
||||
class OrganizationMetadata(
|
||||
ViewSetBase,
|
||||
MetadataAttributesFunctional,
|
@ -1,7 +1,5 @@
|
||||
import pytest
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from access.models.company_base import Company
|
||||
from access.tests.unit.entity.test_unit_entity_viewset import (
|
||||
EntityViewsetInheritedCases
|
||||
@ -14,7 +12,14 @@ class ViewsetTestCases(
|
||||
EntityViewsetInheritedCases,
|
||||
):
|
||||
|
||||
model: str = Company
|
||||
|
||||
@property
|
||||
def parameterized_class_attributes(self):
|
||||
return {
|
||||
'model': {
|
||||
'value': Company
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -26,15 +31,13 @@ class CompanyViewsetInheritedCases(
|
||||
Test Cases for Entity models that inherit from model Company
|
||||
"""
|
||||
|
||||
model: str = None
|
||||
"""name of the model to test"""
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_access
|
||||
class CompanyViewsetTest(
|
||||
class CompanyViewsetPyTest(
|
||||
ViewsetTestCases,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
pass
|
||||
|
@ -1,7 +1,5 @@
|
||||
import pytest
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from access.models.contact import Contact
|
||||
from access.tests.unit.person.test_unit_person_viewset import (
|
||||
PersonViewsetInheritedCases
|
||||
@ -14,7 +12,14 @@ class ViewsetTestCases(
|
||||
PersonViewsetInheritedCases,
|
||||
):
|
||||
|
||||
model: str = Contact
|
||||
|
||||
@property
|
||||
def parameterized_class_attributes(self):
|
||||
return {
|
||||
'model': {
|
||||
'value': Contact
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -26,15 +31,13 @@ class ContactViewsetInheritedCases(
|
||||
Test Cases for Entity models that inherit from model Contact
|
||||
"""
|
||||
|
||||
model: str = None
|
||||
"""name of the model to test"""
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_access
|
||||
class ContactViewsetTest(
|
||||
class ContactViewsetPyTest(
|
||||
ViewsetTestCases,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
pass
|
||||
|
@ -1,89 +1,107 @@
|
||||
import pytest
|
||||
import logging
|
||||
|
||||
from django.test import Client, TestCase
|
||||
|
||||
from rest_framework.reverse import reverse
|
||||
|
||||
from access.models.entity import Entity
|
||||
from access.viewsets.entity import (
|
||||
Entity,
|
||||
NoDocsViewSet,
|
||||
ViewSet,
|
||||
)
|
||||
|
||||
from api.tests.unit.test_unit_common_viewset import ModelViewSetInheritedCases
|
||||
from api.tests.unit.test_unit_common_viewset import SubModelViewSetInheritedCases
|
||||
|
||||
|
||||
|
||||
@pytest.mark.skip(reason = 'see #895, tests being refactored')
|
||||
@pytest.mark.model_entity
|
||||
class ViewsetTestCases(
|
||||
ModelViewSetInheritedCases,
|
||||
SubModelViewSetInheritedCases,
|
||||
):
|
||||
|
||||
kwargs = None
|
||||
|
||||
model = Entity
|
||||
|
||||
viewset = None
|
||||
|
||||
route_name = None
|
||||
@pytest.fixture( scope = 'function' )
|
||||
def viewset(self):
|
||||
return ViewSet
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. make list request
|
||||
"""
|
||||
|
||||
|
||||
super().setUpTestData()
|
||||
|
||||
|
||||
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)
|
||||
@property
|
||||
def parameterized_class_attributes(self):
|
||||
return {
|
||||
# '_log': {
|
||||
# 'type': logging.Logger,
|
||||
# 'value': None
|
||||
# },
|
||||
'_model_documentation': {
|
||||
'type': type(None),
|
||||
'value': None
|
||||
},
|
||||
'back_url': {
|
||||
'type': type(None),
|
||||
'value': None
|
||||
},
|
||||
'base_model': {
|
||||
'value': Entity
|
||||
},
|
||||
'documentation': {
|
||||
'type': type(None),
|
||||
'value': None
|
||||
},
|
||||
'filterset_fields': {
|
||||
'value': [
|
||||
'organization'
|
||||
]
|
||||
},
|
||||
'model': {
|
||||
'value': Entity
|
||||
},
|
||||
'model_documentation': {
|
||||
'type': type(None),
|
||||
'value': None
|
||||
},
|
||||
'model_kwarg': {
|
||||
'value': 'model_name'
|
||||
},
|
||||
'model_suffix': {
|
||||
'type': type(None)
|
||||
},
|
||||
'queryset': {
|
||||
'type': type(None),
|
||||
'value': None
|
||||
},
|
||||
'serializer_class': {
|
||||
'type': type(None),
|
||||
'value': None
|
||||
},
|
||||
'search_fields': {
|
||||
'value': [
|
||||
'model_notes'
|
||||
]
|
||||
},
|
||||
'view_description': {
|
||||
'value': 'All entities'
|
||||
},
|
||||
'view_name': {
|
||||
'type': type(None),
|
||||
'value': None
|
||||
},
|
||||
'view_serializer_name': {
|
||||
'type': type(None),
|
||||
'value': None
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class EntityViewsetInheritedCases(
|
||||
ViewsetTestCases,
|
||||
):
|
||||
|
||||
model: str = None
|
||||
"""name of the model to test"""
|
||||
|
||||
route_name = 'API:_api_entity_sub'
|
||||
|
||||
viewset = ViewSet
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
|
||||
self.kwargs = {
|
||||
'model_name': self.model._meta.model_name
|
||||
}
|
||||
|
||||
super().setUpTestData()
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_access
|
||||
class EntityViewsetTest(
|
||||
class EntityViewsetPyTest(
|
||||
ViewsetTestCases,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
kwargs = {}
|
||||
|
||||
route_name = 'API:_api_entity'
|
||||
|
||||
viewset = NoDocsViewSet
|
||||
@pytest.fixture( scope = 'function' )
|
||||
def viewset(self):
|
||||
return NoDocsViewSet
|
||||
|
@ -1,7 +1,5 @@
|
||||
import pytest
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from access.models.person import Person
|
||||
from access.tests.unit.entity.test_unit_entity_viewset import (
|
||||
EntityViewsetInheritedCases
|
||||
@ -14,7 +12,15 @@ class ViewsetTestCases(
|
||||
EntityViewsetInheritedCases,
|
||||
):
|
||||
|
||||
model: str = Person
|
||||
|
||||
@property
|
||||
def parameterized_class_attributes(self):
|
||||
return {
|
||||
'model': {
|
||||
'value': Person
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -26,15 +32,13 @@ class PersonViewsetInheritedCases(
|
||||
Test Cases for Entity models that inherit from model Person
|
||||
"""
|
||||
|
||||
model: str = None
|
||||
"""name of the model to test"""
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_access
|
||||
class PersonViewsetTest(
|
||||
class PersonViewsetPyTest(
|
||||
ViewsetTestCases,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
pass
|
||||
|
@ -1,199 +0,0 @@
|
||||
import django
|
||||
import pytest
|
||||
import unittest
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import AnonymousUser, Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.shortcuts import reverse
|
||||
from django.test import Client, TestCase
|
||||
|
||||
from rest_framework.relations import Hyperlink
|
||||
|
||||
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_fields import APICommonFields
|
||||
|
||||
User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
|
||||
class OrganizationAPI(
|
||||
TestCase,
|
||||
APICommonFields
|
||||
):
|
||||
|
||||
model = Organization
|
||||
|
||||
app_namespace = 'v2'
|
||||
|
||||
url_name = '_api_tenant'
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. Create the object
|
||||
2. create view user
|
||||
3. add user as org manager
|
||||
4. make api request
|
||||
"""
|
||||
|
||||
organization = Organization.objects.create(name='test_org', model_notes='random text')
|
||||
|
||||
self.organization = organization
|
||||
|
||||
|
||||
self.item = organization
|
||||
|
||||
self.url_view_kwargs = {'pk': self.item.id}
|
||||
|
||||
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.view_user = User.objects.create_user(username="test_user_view", password="password")
|
||||
teamuser = TeamUsers.objects.create(
|
||||
team = view_team,
|
||||
user = self.view_user
|
||||
)
|
||||
|
||||
organization.manager = self.view_user
|
||||
|
||||
organization.save()
|
||||
|
||||
|
||||
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)
|
||||
|
||||
self.api_data = response.data
|
||||
|
||||
|
||||
|
||||
def test_api_field_exists_name(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
name field must exist
|
||||
"""
|
||||
|
||||
assert 'name' in self.api_data
|
||||
|
||||
|
||||
def test_api_field_type_name(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
name field must be str
|
||||
"""
|
||||
|
||||
assert type(self.api_data['name']) is str
|
||||
|
||||
|
||||
|
||||
def test_api_field_exists_manager(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
manager field must exist
|
||||
"""
|
||||
|
||||
assert 'manager' in self.api_data
|
||||
|
||||
|
||||
def test_api_field_type_manager(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
manager field must be dict
|
||||
"""
|
||||
|
||||
assert type(self.api_data['manager']) is dict
|
||||
|
||||
|
||||
def test_api_field_exists_manager_id(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
manager.id field must exist
|
||||
"""
|
||||
|
||||
assert 'id' in self.api_data['manager']
|
||||
|
||||
|
||||
def test_api_field_type_manager_id(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
manager.id field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['manager']['id']) is int
|
||||
|
||||
|
||||
def test_api_field_exists_manager_display_name(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
manager.display_name field must exist
|
||||
"""
|
||||
|
||||
assert 'display_name' in self.api_data['manager']
|
||||
|
||||
|
||||
def test_api_field_type_manager_display_name(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
manager.display_name field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['manager']['display_name']) is str
|
||||
|
||||
|
||||
def test_api_field_exists_manager_url(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
manager.display_name field must exist
|
||||
"""
|
||||
|
||||
assert 'url' in self.api_data['manager']
|
||||
|
||||
|
||||
def test_api_field_type_manager_url(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
manager.url field must be Hyperlink
|
||||
"""
|
||||
|
||||
assert type(self.api_data['manager']['url']) is Hyperlink
|
||||
|
||||
|
||||
|
||||
def test_api_field_exists_url_teams(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
_urls.teams field must exist
|
||||
"""
|
||||
|
||||
assert 'teams' in self.api_data['_urls']
|
||||
|
||||
|
||||
def test_api_field_type_url_teams(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
_urls.teams field must be Hyperlink
|
||||
"""
|
||||
|
||||
assert type(self.api_data['_urls']['teams']) is str
|
@ -1,43 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from django.test import Client, TestCase
|
||||
|
||||
from rest_framework.reverse import reverse
|
||||
|
||||
from access.viewsets.organization import ViewSet
|
||||
|
||||
from api.tests.unit.test_unit_common_viewset import ModelViewSetInheritedCases
|
||||
|
||||
|
||||
|
||||
@pytest.mark.skip(reason = 'see #895, tests being refactored')
|
||||
class OrganizationViewsetList(
|
||||
ModelViewSetInheritedCases,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
viewset = ViewSet
|
||||
|
||||
route_name = 'API:_api_tenant'
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. make list request
|
||||
"""
|
||||
|
||||
|
||||
super().setUpTestData()
|
||||
|
||||
|
||||
client = Client()
|
||||
|
||||
url = reverse(
|
||||
self.route_name + '-list'
|
||||
)
|
||||
|
||||
client.force_login(self.view_user)
|
||||
|
||||
self.http_options_response_list = client.options(url)
|
@ -1,18 +1,94 @@
|
||||
import pytest
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from api.tests.unit.test_unit_serializer import (
|
||||
SerializerTestCases
|
||||
)
|
||||
|
||||
|
||||
class TenantSerializerTestCases(
|
||||
SerializerTestCases
|
||||
):
|
||||
pass
|
||||
from centurion.tests.abstract.mock_view import MockView
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_tenant
|
||||
class TenantSerializerTestCases(
|
||||
SerializerTestCases
|
||||
):
|
||||
|
||||
@pytest.fixture( scope = 'function' )
|
||||
def created_model(self, django_db_blocker, model, model_kwargs):
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
item = model.objects.create( **model_kwargs )
|
||||
|
||||
yield item
|
||||
|
||||
item.delete()
|
||||
|
||||
|
||||
def test_serializer_validation_no_name(self,
|
||||
kwargs_api_create, model, model_serializer, request_user
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no name is provided a validation error occurs
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
kwargs = kwargs_api_create.copy()
|
||||
del kwargs['name']
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data = kwargs
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['name'][0] == 'required'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_manager_optional(self,
|
||||
kwargs_api_create, model, model_serializer, request_user
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no name is provided a validation error occurs
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
kwargs = kwargs_api_create.copy()
|
||||
del kwargs['manager']
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data = kwargs
|
||||
)
|
||||
|
||||
assert serializer.is_valid(raise_exception = True)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_access
|
||||
class TenantSerializerPyTest(
|
||||
TenantSerializerTestCases
|
||||
|
85
app/access/tests/unit/tenant/test_unit_tenant_viewset.py
Normal file
85
app/access/tests/unit/tenant/test_unit_tenant_viewset.py
Normal file
@ -0,0 +1,85 @@
|
||||
import pytest
|
||||
|
||||
from access.viewsets.organization import (
|
||||
Tenant,
|
||||
ViewSet,
|
||||
)
|
||||
|
||||
from api.tests.unit.test_unit_common_viewset import (
|
||||
ModelViewSetInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_tenant
|
||||
class ViewsetTestCases(
|
||||
ModelViewSetInheritedCases,
|
||||
):
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'function' )
|
||||
def viewset(self):
|
||||
return ViewSet
|
||||
|
||||
|
||||
@property
|
||||
def parameterized_class_attributes(self):
|
||||
return {
|
||||
'_model_documentation': {
|
||||
'type': type(None),
|
||||
'value': None
|
||||
},
|
||||
'back_url': {
|
||||
'type': type(None),
|
||||
},
|
||||
'documentation': {
|
||||
'type': type(None),
|
||||
},
|
||||
'filterset_fields': {
|
||||
'value': [
|
||||
'name',
|
||||
'manager',
|
||||
]
|
||||
},
|
||||
'model': {
|
||||
'value': Tenant
|
||||
},
|
||||
'model_documentation': {
|
||||
'type': type(None),
|
||||
},
|
||||
'queryset': {
|
||||
'type': type(None),
|
||||
},
|
||||
'serializer_class': {
|
||||
'type': type(None),
|
||||
},
|
||||
'search_fields': {
|
||||
'value': [
|
||||
'name'
|
||||
]
|
||||
},
|
||||
'view_description': {
|
||||
'value': 'Centurion Tenants'
|
||||
},
|
||||
'view_name': {
|
||||
'type': type(None),
|
||||
},
|
||||
'view_serializer_name': {
|
||||
'type': type(None),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class TenantViewsetInheritedCases(
|
||||
ViewsetTestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_access
|
||||
class TenantViewsetPyTest(
|
||||
ViewsetTestCases,
|
||||
):
|
||||
pass
|
@ -92,10 +92,39 @@ class APIFieldsTestCases:
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
kwargs_many_to_many = {}
|
||||
|
||||
kwargs = {}
|
||||
|
||||
for key, value in model_kwargs.items():
|
||||
|
||||
field = model._meta.get_field(key)
|
||||
|
||||
if isinstance(field, models.ManyToManyField):
|
||||
|
||||
kwargs_many_to_many.update({
|
||||
key: value
|
||||
})
|
||||
|
||||
else:
|
||||
|
||||
kwargs.update({
|
||||
key: value
|
||||
})
|
||||
|
||||
|
||||
item = model.objects.create(
|
||||
**model_kwargs
|
||||
**kwargs
|
||||
)
|
||||
|
||||
for key, value in kwargs_many_to_many.items():
|
||||
|
||||
field = getattr(item, key)
|
||||
|
||||
for entry in value:
|
||||
|
||||
field.add(entry)
|
||||
|
||||
request.cls.item = item
|
||||
|
||||
yield item
|
||||
@ -198,7 +227,7 @@ class APIFieldsTestCases:
|
||||
else:
|
||||
|
||||
assert(
|
||||
type( api_data['value'] ) is param_expected
|
||||
type( api_data.get('value', 'is empty') ) is param_expected
|
||||
or type( api_data_two.get('value', 'is empty') ) is param_expected
|
||||
)
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,29 @@
|
||||
# Generated by Django 5.1.10 on 2025-07-29 04:13
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("assistance", "0013_alter_modelknowledgebasearticle_model"),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name="knowledgebase",
|
||||
name="responsible_user",
|
||||
field=models.ForeignKey(
|
||||
blank=True,
|
||||
help_text="User(s) whom is considered the articles owner.",
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.PROTECT,
|
||||
related_name="responsible_user",
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
verbose_name="Responsible User",
|
||||
),
|
||||
),
|
||||
]
|
@ -103,7 +103,7 @@ class KnowledgeBase(
|
||||
|
||||
responsible_user = models.ForeignKey(
|
||||
User,
|
||||
blank = False,
|
||||
blank = True,
|
||||
help_text = 'User(s) whom is considered the articles owner.',
|
||||
null = True,
|
||||
on_delete = models.PROTECT,
|
||||
|
25
app/assistance/tests/functional/knowledge_base/conftest.py
Normal file
25
app/assistance/tests/functional/knowledge_base/conftest.py
Normal file
@ -0,0 +1,25 @@
|
||||
import pytest
|
||||
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model(model_knowledgebase):
|
||||
|
||||
yield model_knowledgebase
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class', autouse = True)
|
||||
def model_kwargs(request, kwargs_knowledgebase):
|
||||
|
||||
request.cls.kwargs_create_item = kwargs_knowledgebase.copy()
|
||||
|
||||
yield kwargs_knowledgebase.copy()
|
||||
|
||||
if hasattr(request.cls, 'kwargs_create_item'):
|
||||
del request.cls.kwargs_create_item
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model_serializer(serializer_knowledgebase):
|
||||
|
||||
yield serializer_knowledgebase
|
@ -1,216 +0,0 @@
|
||||
import django
|
||||
import json
|
||||
import pytest
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from access.models.tenant import Tenant as Organization
|
||||
from access.models.team import Team
|
||||
|
||||
from centurion.tests.abstract.mock_view import MockView
|
||||
|
||||
from assistance.models.knowledge_base import KnowledgeBase
|
||||
from assistance.serializers.knowledge_base import KnowledgeBaseModelSerializer
|
||||
|
||||
User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
|
||||
class KnowledgeBaseValidationAPI(
|
||||
TestCase,
|
||||
):
|
||||
|
||||
model = KnowledgeBase
|
||||
|
||||
app_namespace = 'API'
|
||||
|
||||
url_name = '_api_knowledgebase'
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. Create an org
|
||||
2. Create a team
|
||||
4. Add user to add team
|
||||
"""
|
||||
|
||||
organization = Organization.objects.create(name='test_org')
|
||||
|
||||
self.user = User.objects.create_user(username="test_user_view", password="password")
|
||||
|
||||
self.mock_view = MockView( user = self.user )
|
||||
|
||||
self.organization = organization
|
||||
|
||||
self.add_team = Team.objects.create(
|
||||
organization=organization,
|
||||
team_name = 'teamone',
|
||||
model_notes = 'random note'
|
||||
)
|
||||
|
||||
self.add_user = User.objects.create_user(username="test_user_add", password="password")
|
||||
|
||||
self.item_has_target_user = self.model.objects.create(
|
||||
organization=organization,
|
||||
title = 'random title',
|
||||
content = 'random note',
|
||||
summary = 'a summary',
|
||||
target_user = self.add_user,
|
||||
release_date = '2024-01-01 12:00:00',
|
||||
expiry_date = '2024-01-01 12:00:01',
|
||||
responsible_user = self.add_user,
|
||||
)
|
||||
|
||||
self.item_has_target_team = self.model.objects.create(
|
||||
organization=organization,
|
||||
title = 'random title',
|
||||
content = 'random note',
|
||||
summary = 'a summary',
|
||||
release_date = '2024-01-01 12:00:00',
|
||||
expiry_date = '2024-01-01 12:00:01',
|
||||
responsible_user = self.add_user,
|
||||
)
|
||||
|
||||
self.item_has_target_team.target_team.set([ self.add_team ])
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_title(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no title is provided a validation error occurs
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = KnowledgeBaseModelSerializer(
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"organization": self.organization.id,
|
||||
"content": "random note",
|
||||
"target_user": self.add_user.id,
|
||||
"target_team": [
|
||||
self.add_team.id
|
||||
],
|
||||
"responsible_user": self.add_user.id,
|
||||
})
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['title'][0] == 'required'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_both_target_team_target_user(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that both target user and target team raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = KnowledgeBaseModelSerializer(
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"organization": self.organization.id,
|
||||
"title": "teamone",
|
||||
"content": "random note",
|
||||
"target_user": self.add_user.id,
|
||||
"target_team": [
|
||||
self.add_team.id
|
||||
],
|
||||
"responsible_user": self.add_user.id,
|
||||
})
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['non_field_errors'][0] == 'invalid_not_both_target_team_user'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_target_team_target_user(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if either target user and target team is missing it raises validation error
|
||||
"""
|
||||
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = KnowledgeBaseModelSerializer(
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"organization": self.organization.id,
|
||||
"title": 'teamone',
|
||||
"content": 'random note',
|
||||
"responsible_user": self.add_user.id,
|
||||
})
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['non_field_errors'][0] == 'invalid_need_target_team_or_user'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_target_user(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item with target user is updated to include a target_team
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = KnowledgeBaseModelSerializer(
|
||||
self.item_has_target_user,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"target_team": [ self.add_team.id ]
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['non_field_errors'][0] == 'invalid_not_both_target_team_user'
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_target_team(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item with target team is updated to include a target_user
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = KnowledgeBaseModelSerializer(
|
||||
self.item_has_target_team,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"target_user": self.add_user.id
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['non_field_errors'][0] == 'invalid_not_both_target_team_user'
|
@ -0,0 +1,191 @@
|
||||
import pytest
|
||||
|
||||
from django.db import models
|
||||
|
||||
from rest_framework.relations import Hyperlink
|
||||
|
||||
from api.tests.functional.test_functional_api_fields import (
|
||||
APIFieldsInheritedCases,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_knowledgebase
|
||||
class knowledgeBaseAPITestCases(
|
||||
APIFieldsInheritedCases,
|
||||
):
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def second_model(self, request, django_db_blocker,
|
||||
model, model_kwargs
|
||||
):
|
||||
|
||||
item = None
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
kwargs_many_to_many = {}
|
||||
|
||||
kwargs = {}
|
||||
|
||||
for key, value in model_kwargs.items():
|
||||
|
||||
field = model._meta.get_field(key)
|
||||
|
||||
if isinstance(field, models.ManyToManyField):
|
||||
|
||||
kwargs_many_to_many.update({
|
||||
key: value
|
||||
})
|
||||
|
||||
else:
|
||||
|
||||
kwargs.update({
|
||||
key: value
|
||||
})
|
||||
|
||||
|
||||
# Switch model fields so all fields can be checked
|
||||
kwargs_many_to_many.update({ 'responsible_teams': kwargs_many_to_many['target_team']})
|
||||
del kwargs_many_to_many['target_team']
|
||||
|
||||
kwargs.update({ 'target_user': kwargs['responsible_user']})
|
||||
del kwargs['responsible_user']
|
||||
|
||||
|
||||
item_two = model.objects.create(
|
||||
**kwargs
|
||||
)
|
||||
|
||||
|
||||
for key, value in kwargs_many_to_many.items():
|
||||
|
||||
field = getattr(item_two, key)
|
||||
|
||||
for entry in value:
|
||||
|
||||
field.add(entry)
|
||||
|
||||
|
||||
request.cls.item_two = item_two
|
||||
|
||||
yield item_two
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
item_two.delete()
|
||||
|
||||
del request.cls.item_two
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class', autouse = True)
|
||||
def class_setup(self,
|
||||
create_model,
|
||||
second_model,
|
||||
make_request,
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
@property
|
||||
def parameterized_api_fields(self):
|
||||
|
||||
return {
|
||||
'title': {
|
||||
'expected': str
|
||||
},
|
||||
'summary': {
|
||||
'expected': str
|
||||
},
|
||||
'content': {
|
||||
'expected': str
|
||||
},
|
||||
'category': {
|
||||
'expected': dict
|
||||
},
|
||||
'category.id': {
|
||||
'expected': int
|
||||
},
|
||||
'category.display_name': {
|
||||
'expected': str
|
||||
},
|
||||
'category.url': {
|
||||
'expected': str
|
||||
},
|
||||
'release_date': {
|
||||
'expected': str
|
||||
},
|
||||
'expiry_date': {
|
||||
'expected': str
|
||||
},
|
||||
'target_team': {
|
||||
'expected': list
|
||||
},
|
||||
'target_team.0.id': {
|
||||
'expected': int
|
||||
},
|
||||
'target_team.0.display_name': {
|
||||
'expected': str
|
||||
},
|
||||
'target_team.0.url': {
|
||||
'expected': str
|
||||
},
|
||||
'target_user': {
|
||||
'expected': dict
|
||||
},
|
||||
'target_user.id': {
|
||||
'expected': int
|
||||
},
|
||||
'target_user.display_name': {
|
||||
'expected': str
|
||||
},
|
||||
'target_user.url': {
|
||||
'expected': Hyperlink
|
||||
},
|
||||
'responsible_user': {
|
||||
'expected': dict
|
||||
},
|
||||
'responsible_user.id': {
|
||||
'expected': int
|
||||
},
|
||||
'responsible_user.display_name': {
|
||||
'expected': str
|
||||
},
|
||||
'responsible_user.url': {
|
||||
'expected': Hyperlink
|
||||
},
|
||||
'responsible_teams': {
|
||||
'expected': list
|
||||
},
|
||||
'responsible_teams.0.id': {
|
||||
'expected': int
|
||||
},
|
||||
'responsible_teams.0.display_name': {
|
||||
'expected': str
|
||||
},
|
||||
'responsible_teams.0.url': {
|
||||
'expected': str
|
||||
},
|
||||
'public': {
|
||||
'expected': bool
|
||||
},
|
||||
'modified': {
|
||||
'expected': str
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class knowledgeBaseAPIInheritedCases(
|
||||
knowledgeBaseAPITestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_assistance
|
||||
class knowledgeBaseAPIPyTest(
|
||||
knowledgeBaseAPITestCases,
|
||||
):
|
||||
|
||||
pass
|
@ -0,0 +1,28 @@
|
||||
import pytest
|
||||
|
||||
from core.tests.functional.centurion_abstract.test_functional_centurion_abstract_model import (
|
||||
CenturionAbstractModelInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_knowledgebase
|
||||
class knowledgeBaseModelTestCases(
|
||||
CenturionAbstractModelInheritedCases
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class knowledgeBaseModelInheritedCases(
|
||||
knowledgeBaseModelTestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_assistance
|
||||
class knowledgeBaseModelPyTest(
|
||||
knowledgeBaseModelTestCases,
|
||||
):
|
||||
pass
|
@ -1,11 +1,7 @@
|
||||
import django
|
||||
import pytest
|
||||
import unittest
|
||||
import requests
|
||||
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import AnonymousUser, Permission
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.test import TestCase
|
||||
|
||||
@ -13,11 +9,9 @@ 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 APIPermissions
|
||||
from api.tests.abstract.api_serializer_viewset import SerializersTestCases
|
||||
from api.tests.abstract.test_metadata_functional import MetadataAttributesFunctional, MetaDataNavigationEntriesFunctional
|
||||
|
||||
from assistance.models.knowledge_base import KnowledgeBase
|
||||
from assistance.models.knowledge_base import KnowledgeBase, KnowledgeBaseCategory
|
||||
|
||||
from settings.models.app_settings import AppSettings
|
||||
|
||||
@ -25,7 +19,6 @@ User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
|
||||
|
||||
class ViewSetBase:
|
||||
|
||||
model = KnowledgeBase
|
||||
@ -65,11 +58,17 @@ class ViewSetBase:
|
||||
name = 'test_global_organization'
|
||||
)
|
||||
|
||||
category = KnowledgeBaseCategory.objects.create(
|
||||
organization = self.global_organization,
|
||||
name = 'cat2'
|
||||
)
|
||||
|
||||
self.global_org_item = self.model.objects.create(
|
||||
organization = self.global_organization,
|
||||
title = 'one',
|
||||
content = 'some text for bodygfdgdf',
|
||||
target_user = self.view_user
|
||||
target_user = self.view_user,
|
||||
category = category,
|
||||
)
|
||||
|
||||
app_settings = AppSettings.objects.get(
|
||||
@ -163,19 +162,29 @@ class ViewSetBase:
|
||||
user = self.view_user
|
||||
)
|
||||
|
||||
category_item = KnowledgeBaseCategory.objects.create(
|
||||
organization = self.organization,
|
||||
name = 'cat'
|
||||
)
|
||||
|
||||
self.item = self.model.objects.create(
|
||||
organization = self.organization,
|
||||
title = 'one',
|
||||
content = 'some text for body',
|
||||
target_user = self.view_user
|
||||
target_user = self.view_user,
|
||||
category = category_item,
|
||||
)
|
||||
|
||||
category = KnowledgeBaseCategory.objects.create(
|
||||
organization = self.different_organization,
|
||||
name = 'cat1'
|
||||
)
|
||||
self.other_org_item = self.model.objects.create(
|
||||
organization = self.different_organization,
|
||||
title = 'two',
|
||||
content = 'some text for body',
|
||||
target_user = self.view_user_b
|
||||
target_user = self.view_user_b,
|
||||
category = category,
|
||||
)
|
||||
|
||||
|
||||
@ -185,7 +194,8 @@ class ViewSetBase:
|
||||
'title': 'team_post',
|
||||
'organization': self.organization.id,
|
||||
'content': 'article text',
|
||||
'target_user': self.view_user.id
|
||||
'target_user': self.view_user.id,
|
||||
'category': category_item.id,
|
||||
}
|
||||
|
||||
|
||||
@ -229,24 +239,6 @@ class ViewSetBase:
|
||||
)
|
||||
|
||||
|
||||
class KnowledgeBasePermissionsAPI(
|
||||
ViewSetBase,
|
||||
APIPermissions,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class KnowledgeBaseViewSet(
|
||||
ViewSetBase,
|
||||
SerializersTestCases,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class KnowledgeBaseMetadata(
|
||||
ViewSetBase,
|
@ -0,0 +1,25 @@
|
||||
import pytest
|
||||
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model(model_knowledgebasecategory):
|
||||
|
||||
yield model_knowledgebasecategory
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class', autouse = True)
|
||||
def model_kwargs(request, kwargs_knowledgebasecategory):
|
||||
|
||||
request.cls.kwargs_create_item = kwargs_knowledgebasecategory.copy()
|
||||
|
||||
yield kwargs_knowledgebasecategory.copy()
|
||||
|
||||
if hasattr(request.cls, 'kwargs_create_item'):
|
||||
del request.cls.kwargs_create_item
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model_serializer(serializer_knowledgebasecategory):
|
||||
|
||||
yield serializer_knowledgebasecategory
|
@ -1,222 +0,0 @@
|
||||
import django
|
||||
import json
|
||||
import pytest
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from access.models.tenant import Tenant as Organization
|
||||
from access.models.team import Team
|
||||
|
||||
from centurion.tests.abstract.mock_view import MockView
|
||||
|
||||
from assistance.serializers.knowledge_base_category import KnowledgeBaseCategory, KnowledgeBaseCategoryModelSerializer
|
||||
|
||||
User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
|
||||
class KnowledgeBaseCategoryValidationAPI(
|
||||
TestCase,
|
||||
):
|
||||
|
||||
model = KnowledgeBaseCategory
|
||||
|
||||
app_namespace = 'API'
|
||||
|
||||
url_name = '_api_v2_knowledge_base'
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. Create an org
|
||||
2. Create a team
|
||||
4. Add user to add team
|
||||
"""
|
||||
|
||||
organization = Organization.objects.create(name='test_org')
|
||||
|
||||
self.organization = organization
|
||||
|
||||
self.add_team = Team.objects.create(
|
||||
organization=organization,
|
||||
team_name = 'teamone',
|
||||
model_notes = 'random note'
|
||||
)
|
||||
|
||||
self.add_user = User.objects.create_user(username="test_user_add", password="password")
|
||||
|
||||
self.mock_view = MockView( user = self.add_user )
|
||||
|
||||
self.item_has_target_user = self.model.objects.create(
|
||||
organization=organization,
|
||||
name = 'random title',
|
||||
target_user = self.add_user,
|
||||
)
|
||||
|
||||
self.item_has_target_team = self.model.objects.create(
|
||||
organization=organization,
|
||||
name = 'random title0',
|
||||
)
|
||||
|
||||
self.item_has_target_team.target_team.set([ self.add_team ])
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_name(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no name is provided a validation error occurs
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = KnowledgeBaseCategoryModelSerializer(
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"organization": self.organization.id,
|
||||
"target_user": self.add_user.id,
|
||||
"target_team": [
|
||||
self.add_team.id
|
||||
]
|
||||
})
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['name'][0] == 'required'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_parent_category_not_self(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that you cant assisgn self as parent category
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = KnowledgeBaseCategoryModelSerializer(
|
||||
self.item_has_target_user,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"parent_category": self.item_has_target_user.id
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['parent_category'][0] == 'parent_category_not_self'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_both_target_team_target_user(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that both target user and target team raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = KnowledgeBaseCategoryModelSerializer(
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"organization": self.organization.id,
|
||||
"name": "teamone",
|
||||
"target_user": self.add_user.id,
|
||||
"target_team": [
|
||||
self.add_team.id
|
||||
]
|
||||
})
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['non_field_errors'][0] == 'invalid_not_both_target_team_user'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_target_team_target_user(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if either target user and target team is missing it raises validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = KnowledgeBaseCategoryModelSerializer(
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"organization": self.organization.id,
|
||||
"name": 'teamone'
|
||||
})
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['non_field_errors'][0] == 'invalid_need_target_team_or_user'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_target_user(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item with target user is updated to include a target_team
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = KnowledgeBaseCategoryModelSerializer(
|
||||
self.item_has_target_user,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"target_team": [ self.add_team.id ]
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['non_field_errors'][0] == 'invalid_not_both_target_team_user'
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_target_team(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item with target team is updated to include a target_user
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = KnowledgeBaseCategoryModelSerializer(
|
||||
self.item_has_target_team,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"target_user": self.add_user.id
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['non_field_errors'][0] == 'invalid_not_both_target_team_user'
|
@ -0,0 +1,154 @@
|
||||
import pytest
|
||||
|
||||
from django.db import models
|
||||
|
||||
from rest_framework.relations import Hyperlink
|
||||
|
||||
from api.tests.functional.test_functional_api_fields import (
|
||||
APIFieldsInheritedCases,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_knowledgebasecategory
|
||||
class knowledgeBaseCategoryAPITestCases(
|
||||
APIFieldsInheritedCases,
|
||||
):
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def second_model(self, request, django_db_blocker,
|
||||
model, model_kwargs, model_team, kwargs_team
|
||||
):
|
||||
|
||||
item = None
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
kwargs_many_to_many = {}
|
||||
|
||||
kwargs = {}
|
||||
|
||||
for key, value in model_kwargs.items():
|
||||
|
||||
field = model._meta.get_field(key)
|
||||
|
||||
if isinstance(field, models.ManyToManyField):
|
||||
|
||||
kwargs_many_to_many.update({
|
||||
key: value
|
||||
})
|
||||
|
||||
else:
|
||||
|
||||
kwargs.update({
|
||||
key: value
|
||||
})
|
||||
|
||||
|
||||
# Switch model fields so all fields can be checked
|
||||
team = model_team.objects.create( **kwargs_team )
|
||||
kwargs_many_to_many.update({ 'target_team': [ team ]})
|
||||
del kwargs['target_user']
|
||||
|
||||
kwargs['parent_category'] = request.cls.item
|
||||
|
||||
|
||||
item_two = model.objects.create(
|
||||
**kwargs
|
||||
)
|
||||
|
||||
|
||||
for key, value in kwargs_many_to_many.items():
|
||||
|
||||
field = getattr(item_two, key)
|
||||
|
||||
for entry in value:
|
||||
|
||||
field.add(entry)
|
||||
|
||||
|
||||
request.cls.item_two = item_two
|
||||
|
||||
yield item_two
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
item_two.delete()
|
||||
|
||||
team.delete()
|
||||
|
||||
del request.cls.item_two
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class', autouse = True)
|
||||
def class_setup(self,
|
||||
create_model,
|
||||
second_model,
|
||||
make_request,
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
@property
|
||||
def parameterized_api_fields(self):
|
||||
|
||||
return {
|
||||
'parent_category': {
|
||||
'expected': dict
|
||||
},
|
||||
'parent_category.id': {
|
||||
'expected': int
|
||||
},
|
||||
'parent_category.display_name': {
|
||||
'expected': str
|
||||
},
|
||||
'parent_category.url': {
|
||||
'expected': str
|
||||
},
|
||||
'name': {
|
||||
'expected': str
|
||||
},
|
||||
'target_team': {
|
||||
'expected': list
|
||||
},
|
||||
'target_team.0.id': {
|
||||
'expected': int
|
||||
},
|
||||
'target_team.0.display_name': {
|
||||
'expected': str
|
||||
},
|
||||
'target_team.0.url': {
|
||||
'expected': str
|
||||
},
|
||||
'target_user': {
|
||||
'expected': dict
|
||||
},
|
||||
'target_user.id': {
|
||||
'expected': int
|
||||
},
|
||||
'target_user.display_name': {
|
||||
'expected': str
|
||||
},
|
||||
'target_user.url': {
|
||||
'expected': Hyperlink
|
||||
},
|
||||
'modified': {
|
||||
'expected': str
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class knowledgeBaseCategoryAPIInheritedCases(
|
||||
knowledgeBaseCategoryAPITestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_assistance
|
||||
class knowledgeBaseCategoryAPIPyTest(
|
||||
knowledgeBaseCategoryAPITestCases,
|
||||
):
|
||||
|
||||
pass
|
@ -0,0 +1,28 @@
|
||||
import pytest
|
||||
|
||||
from core.tests.functional.centurion_abstract.test_functional_centurion_abstract_model import (
|
||||
CenturionAbstractModelInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_knowledgebasecategory
|
||||
class knowledgeBaseCategoryModelTestCases(
|
||||
CenturionAbstractModelInheritedCases
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class knowledgeBaseCategoryModelInheritedCases(
|
||||
knowledgeBaseCategoryModelTestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_assistance
|
||||
class knowledgeBaseCategoryModelPyTest(
|
||||
knowledgeBaseCategoryModelTestCases,
|
||||
):
|
||||
pass
|
@ -9,8 +9,6 @@ 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 APIPermissions
|
||||
from api.tests.abstract.api_serializer_viewset import SerializersTestCases
|
||||
from api.tests.abstract.test_metadata_functional import MetadataAttributesFunctional
|
||||
|
||||
from assistance.models.knowledge_base_category import KnowledgeBaseCategory
|
||||
@ -27,7 +25,7 @@ class ViewSetBase:
|
||||
|
||||
app_namespace = 'v2'
|
||||
|
||||
url_name = '_api_v2_knowledge_base_category'
|
||||
url_name = '_api_knowledgebasecategory'
|
||||
|
||||
change_data = {'name': 'device'}
|
||||
|
||||
@ -218,25 +216,6 @@ class ViewSetBase:
|
||||
|
||||
|
||||
|
||||
class KnowledgeBaseCategoryPermissionsAPI(
|
||||
ViewSetBase,
|
||||
APIPermissions,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class KnowledgeBaseCategoryViewSet(
|
||||
ViewSetBase,
|
||||
SerializersTestCases,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class KnowledgeBaseCategoryMetadata(
|
||||
ViewSetBase,
|
||||
MetadataAttributesFunctional,
|
@ -17,3 +17,9 @@ def model_kwargs(request, kwargs_knowledgebase):
|
||||
|
||||
if hasattr(request.cls, 'kwargs_create_item'):
|
||||
del request.cls.kwargs_create_item
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model_serializer(serializer_knowledgebase):
|
||||
|
||||
yield serializer_knowledgebase
|
||||
|
@ -1,485 +0,0 @@
|
||||
import django
|
||||
import pytest
|
||||
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.shortcuts import reverse
|
||||
from django.test import Client, TestCase
|
||||
|
||||
from rest_framework.relations import Hyperlink
|
||||
|
||||
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_fields import APITenancyObject
|
||||
|
||||
from assistance.models.knowledge_base import KnowledgeBase
|
||||
from assistance.models.knowledge_base_category import KnowledgeBaseCategory
|
||||
|
||||
User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
class KnowledgeBaseAPI(
|
||||
TestCase,
|
||||
APITenancyObject
|
||||
):
|
||||
|
||||
model = KnowledgeBase
|
||||
|
||||
app_namespace = 'v2'
|
||||
|
||||
url_name = '_api_knowledgebase'
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. Create the object
|
||||
2. create view user
|
||||
4. make api request
|
||||
"""
|
||||
|
||||
organization = Organization.objects.create(name='test_org')
|
||||
|
||||
self.organization = organization
|
||||
|
||||
different_organization = Organization.objects.create(name='test_different_organization')
|
||||
|
||||
|
||||
self.view_team = Team.objects.create(
|
||||
organization=organization,
|
||||
team_name = 'teamone',
|
||||
model_notes = 'random note'
|
||||
)
|
||||
|
||||
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,
|
||||
)
|
||||
)
|
||||
|
||||
self.view_team.permissions.set([view_permissions])
|
||||
|
||||
self.view_user = User.objects.create_user(username="test_user_view", password="password")
|
||||
|
||||
|
||||
|
||||
self.item = self.model.objects.create(
|
||||
organization=organization,
|
||||
title = 'teamone',
|
||||
content = 'random note',
|
||||
summary = 'a summary',
|
||||
target_user = self.view_user,
|
||||
release_date = '2024-01-01 12:00:00',
|
||||
expiry_date = '2024-01-01 12:00:01',
|
||||
responsible_user = self.view_user,
|
||||
category = KnowledgeBaseCategory.objects.create(
|
||||
name='cat',
|
||||
target_user = self.view_user,
|
||||
organization=organization,
|
||||
)
|
||||
)
|
||||
|
||||
self.item.responsible_teams.set([self.view_team])
|
||||
|
||||
self.url_view_kwargs = {'pk': self.item.id}
|
||||
|
||||
teamuser = TeamUsers.objects.create(
|
||||
team = self.view_team,
|
||||
user = self.view_user
|
||||
)
|
||||
|
||||
organization.manager = self.view_user
|
||||
|
||||
organization.save()
|
||||
|
||||
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)
|
||||
|
||||
self.api_data = response.data
|
||||
|
||||
|
||||
|
||||
def test_api_field_exists_model_notes(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
model_notes field does not exist for KB articles
|
||||
"""
|
||||
|
||||
assert 'model_notes' not in self.api_data
|
||||
|
||||
|
||||
def test_api_field_type_model_notes(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
model_notes does not exist for KB articles
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
def test_api_field_exists_category(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
category field must exist
|
||||
"""
|
||||
|
||||
assert 'category' in self.api_data
|
||||
|
||||
|
||||
def test_api_field_type_category(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
category field must be dict
|
||||
"""
|
||||
|
||||
assert type(self.api_data['category']) is dict
|
||||
|
||||
|
||||
def test_api_field_exists_category_id(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
category.id field must exist
|
||||
"""
|
||||
|
||||
assert 'id' in self.api_data['category']
|
||||
|
||||
|
||||
def test_api_field_type_category_id(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
category.id field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['category']['id']) is int
|
||||
|
||||
|
||||
def test_api_field_exists_category_display_name(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
category.display_name field must exist
|
||||
"""
|
||||
|
||||
assert 'display_name' in self.api_data['category']
|
||||
|
||||
|
||||
def test_api_field_type_category_display_name(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
category.display_name field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['category']['display_name']) is str
|
||||
|
||||
|
||||
def test_api_field_exists_category_url(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
category.url field must exist
|
||||
"""
|
||||
|
||||
assert 'url' in self.api_data['category']
|
||||
|
||||
|
||||
def test_api_field_type_category_url(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
category.url field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['category']['url']) is str
|
||||
|
||||
|
||||
|
||||
def test_api_field_exists_summary(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
summary field must exist
|
||||
"""
|
||||
|
||||
assert 'summary' in self.api_data
|
||||
|
||||
|
||||
def test_api_field_type_summary(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
summary field must be str
|
||||
"""
|
||||
|
||||
assert type(self.api_data['summary']) is str
|
||||
|
||||
|
||||
|
||||
def test_api_field_exists_content(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
content field must exist
|
||||
"""
|
||||
|
||||
assert 'content' in self.api_data
|
||||
|
||||
|
||||
def test_api_field_type_summary(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
content field must be str
|
||||
"""
|
||||
|
||||
assert type(self.api_data['content']) is str
|
||||
|
||||
|
||||
|
||||
def test_api_field_exists_release_date(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
release_date field must exist
|
||||
"""
|
||||
|
||||
assert 'release_date' in self.api_data
|
||||
|
||||
|
||||
def test_api_field_type_release_date(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
release_date field must be str
|
||||
"""
|
||||
|
||||
assert type(self.api_data['release_date']) is str
|
||||
|
||||
|
||||
|
||||
def test_api_field_exists_expiry_date(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
expiry_date field must exist
|
||||
"""
|
||||
|
||||
assert 'expiry_date' in self.api_data
|
||||
|
||||
|
||||
def test_api_field_type_expiry_date(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
expiry_date field must be str
|
||||
"""
|
||||
|
||||
assert type(self.api_data['expiry_date']) is str
|
||||
|
||||
|
||||
|
||||
def test_api_field_exists_public(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
public field must exist
|
||||
"""
|
||||
|
||||
assert 'public' in self.api_data
|
||||
|
||||
|
||||
def test_api_field_type_public(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
public field must be bool
|
||||
"""
|
||||
|
||||
assert type(self.api_data['public']) is bool
|
||||
|
||||
|
||||
|
||||
def test_api_field_type_target_user(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
target_user field must be dict
|
||||
"""
|
||||
|
||||
assert type(self.api_data['target_user']) is dict
|
||||
|
||||
|
||||
def test_api_field_exists_target_user_id(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
target_user.id field must exist
|
||||
"""
|
||||
|
||||
assert 'id' in self.api_data['target_user']
|
||||
|
||||
|
||||
def test_api_field_type_target_user_id(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
target_user.id field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['target_user']['id']) is int
|
||||
|
||||
|
||||
def test_api_field_exists_target_user_display_name(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
target_user.display_name field must exist
|
||||
"""
|
||||
|
||||
assert 'display_name' in self.api_data['target_user']
|
||||
|
||||
|
||||
def test_api_field_type_target_user_display_name(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
target_user.display_name field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['target_user']['display_name']) is str
|
||||
|
||||
|
||||
def test_api_field_exists_target_user_url(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
target_user.url field must exist
|
||||
"""
|
||||
|
||||
assert 'url' in self.api_data['target_user']
|
||||
|
||||
|
||||
def test_api_field_type_target_user_url(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
target_user.url field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['target_user']['url']) is Hyperlink
|
||||
|
||||
|
||||
|
||||
def test_api_field_type_responsible_user(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
responsible_user field must be dict
|
||||
"""
|
||||
|
||||
assert type(self.api_data['responsible_user']) is dict
|
||||
|
||||
|
||||
def test_api_field_exists_responsible_user_id(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
responsible_user.id field must exist
|
||||
"""
|
||||
|
||||
assert 'id' in self.api_data['responsible_user']
|
||||
|
||||
|
||||
def test_api_field_type_responsible_user_id(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
responsible_user.id field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['responsible_user']['id']) is int
|
||||
|
||||
|
||||
def test_api_field_exists_responsible_user_display_name(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
responsible_user.display_name field must exist
|
||||
"""
|
||||
|
||||
assert 'display_name' in self.api_data['responsible_user']
|
||||
|
||||
|
||||
def test_api_field_type_responsible_user_display_name(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
responsible_user.display_name field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['responsible_user']['display_name']) is str
|
||||
|
||||
|
||||
def test_api_field_exists_responsible_user_url(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
responsible_user.url field must exist
|
||||
"""
|
||||
|
||||
assert 'url' in self.api_data['responsible_user']
|
||||
|
||||
|
||||
def test_api_field_type_responsible_user_url(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
responsible_user.url field must be Hyperlink
|
||||
"""
|
||||
|
||||
assert type(self.api_data['responsible_user']['url']) is Hyperlink
|
||||
|
||||
|
||||
|
||||
def test_api_field_type_responsible_teams(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
responsible_teams field must be list
|
||||
"""
|
||||
|
||||
assert type(self.api_data['responsible_teams']) is list
|
||||
|
||||
|
||||
def test_api_field_exists_responsible_teams_id(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
responsible_teams.id field must exist
|
||||
"""
|
||||
|
||||
assert 'id' in self.api_data['responsible_teams'][0]
|
||||
|
||||
|
||||
def test_api_field_type_responsible_teams_id(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
responsible_teams.id field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['responsible_teams'][0]['id']) is int
|
||||
|
||||
|
||||
def test_api_field_exists_responsible_teams_display_name(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
responsible_teams.display_name field must exist
|
||||
"""
|
||||
|
||||
assert 'display_name' in self.api_data['responsible_teams'][0]
|
||||
|
||||
|
||||
def test_api_field_type_responsible_teams_display_name(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
responsible_teams.display_name field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['responsible_teams'][0]['display_name']) is str
|
||||
|
||||
|
||||
def test_api_field_exists_responsible_teams_url(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
responsible_teams.url field must exist
|
||||
"""
|
||||
|
||||
assert 'url' in self.api_data['responsible_teams'][0]
|
||||
|
||||
|
||||
def test_api_field_type_responsible_teams_url(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
responsible_teams.url field must be str
|
||||
"""
|
||||
|
||||
assert type(self.api_data['responsible_teams'][0]['url']) is str
|
@ -88,7 +88,7 @@ class KnowledgeBaseModelTestCases(
|
||||
'unique': False,
|
||||
},
|
||||
'responsible_user': {
|
||||
'blank': False,
|
||||
'blank': True,
|
||||
'default': models.fields.NOT_PROVIDED,
|
||||
'field_type': models.ForeignKey,
|
||||
'null': True,
|
||||
@ -128,6 +128,7 @@ class KnowledgeBaseModelInheritedCases(
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_module_assistance
|
||||
class KnowledgeBaseModelPyTest(
|
||||
KnowledgeBaseModelTestCases,
|
||||
):
|
||||
|
@ -0,0 +1,259 @@
|
||||
import pytest
|
||||
|
||||
from django.db import models
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from api.tests.unit.test_unit_serializer import (
|
||||
SerializerTestCases
|
||||
)
|
||||
|
||||
from centurion.tests.abstract.mock_view import MockView
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_knowledgebase
|
||||
class KnowledgeBaseSerializerTestCases(
|
||||
SerializerTestCases
|
||||
):
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'function' )
|
||||
def created_model(self, django_db_blocker, model, model_kwargs):
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
kwargs_many_to_many = {}
|
||||
|
||||
kwargs = {}
|
||||
|
||||
for key, value in model_kwargs.items():
|
||||
|
||||
field = model._meta.get_field(key)
|
||||
|
||||
if isinstance(field, models.ManyToManyField):
|
||||
|
||||
kwargs_many_to_many.update({
|
||||
key: value
|
||||
})
|
||||
|
||||
else:
|
||||
|
||||
kwargs.update({
|
||||
key: value
|
||||
})
|
||||
|
||||
|
||||
item = model.objects.create( **kwargs )
|
||||
|
||||
for key, value in kwargs_many_to_many.items():
|
||||
|
||||
field = getattr(item, key)
|
||||
|
||||
for entry in value:
|
||||
|
||||
field.add(entry)
|
||||
|
||||
yield item
|
||||
|
||||
item.delete()
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_title(self,
|
||||
kwargs_api_create, model, model_serializer, request_user
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no title is provided a validation error occurs
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
kwargs = kwargs_api_create.copy()
|
||||
del kwargs['title']
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data = kwargs
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['title'][0] == 'required'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_both_target_team_target_user(self,
|
||||
kwargs_api_create, model, model_serializer, request_user,
|
||||
model_team, kwargs_team
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that both target user and target team raises a validation error
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
kwargs = kwargs_api_create.copy()
|
||||
kwargs.update({
|
||||
'target_user': request_user.id
|
||||
})
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data = kwargs
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['non_field_errors'][0] == 'invalid_not_both_target_team_user'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_target_team_target_user(self,
|
||||
kwargs_api_create, model, model_serializer, request_user,
|
||||
model_team, kwargs_team
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if either target user and target team is missing it raises validation error
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
kwargs = kwargs_api_create.copy()
|
||||
del kwargs['target_team']
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data = kwargs
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['non_field_errors'][0] == 'invalid_need_target_team_or_user'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_target_user(self,
|
||||
kwargs_api_create, model, model_serializer, request_user,
|
||||
model_team, kwargs_team
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item with target user is updated to include a target_team
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
kwargs = kwargs_api_create.copy()
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
self.item_has_target_user,
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data={
|
||||
"target_team": [ self.add_team.id ]
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['non_field_errors'][0] == 'invalid_not_both_target_team_user'
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_target_user(self,
|
||||
created_model,
|
||||
kwargs_api_create, model, model_serializer, request_user,
|
||||
model_team, kwargs_team
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item with target team is updated to include a target_user
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
kwargs = kwargs_api_create.copy()
|
||||
team = model_team.objects.create( **kwargs_team )
|
||||
kwargs.update({
|
||||
'target_team': [ team ]
|
||||
})
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
created_model,
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data={
|
||||
"target_user": request_user.id
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
team.delete()
|
||||
|
||||
assert err.value.get_codes()['non_field_errors'][0] == 'invalid_not_both_target_team_user'
|
||||
|
||||
|
||||
|
||||
|
||||
class KnowledgeBaseSerializerInheritedCases(
|
||||
KnowledgeBaseSerializerTestCases
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_assistance
|
||||
class KnowledgeBaseSerializerPyTest(
|
||||
KnowledgeBaseSerializerTestCases
|
||||
):
|
||||
pass
|
@ -1,44 +1,91 @@
|
||||
import pytest
|
||||
|
||||
from django.test import Client, TestCase
|
||||
|
||||
from rest_framework.reverse import reverse
|
||||
|
||||
from api.tests.unit.test_unit_common_viewset import ModelViewSetInheritedCases
|
||||
|
||||
from assistance.viewsets.knowledge_base import ViewSet
|
||||
from assistance.viewsets.knowledge_base import (
|
||||
KnowledgeBase,
|
||||
ViewSet,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.skip(reason = 'see #895, tests being refactored')
|
||||
class KnowledgeBaseViewsetList(
|
||||
@pytest.mark.model_knowledgebase
|
||||
class ViewsetTestCases(
|
||||
ModelViewSetInheritedCases,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
viewset = ViewSet
|
||||
|
||||
route_name = 'v2:_api_knowledgebase'
|
||||
@pytest.fixture( scope = 'function' )
|
||||
def viewset(self):
|
||||
return ViewSet
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. make list request
|
||||
"""
|
||||
@property
|
||||
def parameterized_class_attributes(self):
|
||||
return {
|
||||
'_model_documentation': {
|
||||
'type': type(None),
|
||||
},
|
||||
'back_url': {
|
||||
'type': type(None),
|
||||
},
|
||||
'documentation': {
|
||||
'type': type(None),
|
||||
'value': None
|
||||
},
|
||||
'filterset_fields': {
|
||||
'value': [
|
||||
'organization',
|
||||
'category',
|
||||
'target_user',
|
||||
'target_team',
|
||||
'responsible_user',
|
||||
'responsible_teams',
|
||||
'public'
|
||||
]
|
||||
},
|
||||
'model': {
|
||||
'value': KnowledgeBase
|
||||
},
|
||||
'model_documentation': {
|
||||
'type': type(None),
|
||||
},
|
||||
'queryset': {
|
||||
'type': type(None),
|
||||
},
|
||||
'serializer_class': {
|
||||
'type': type(None),
|
||||
},
|
||||
'search_fields': {
|
||||
'value': [
|
||||
'title',
|
||||
'summary',
|
||||
'content'
|
||||
]
|
||||
},
|
||||
'view_description': {
|
||||
'value': 'Information Management Knowledge Base Article(s)'
|
||||
},
|
||||
'view_name': {
|
||||
'type': type(None),
|
||||
},
|
||||
'view_serializer_name': {
|
||||
'type': type(None),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
super().setUpTestData()
|
||||
|
||||
class KnowledgeBaseViewsetInheritedCases(
|
||||
ViewsetTestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
client = Client()
|
||||
|
||||
url = reverse(
|
||||
self.route_name + '-list',
|
||||
kwargs = self.kwargs
|
||||
)
|
||||
@pytest.mark.module_assistance
|
||||
class KnowledgeBaseViewsetPyTest(
|
||||
ViewsetTestCases,
|
||||
):
|
||||
|
||||
client.force_login(self.view_user)
|
||||
|
||||
self.http_options_response_list = client.options(url)
|
||||
pass
|
||||
|
@ -17,3 +17,9 @@ def model_kwargs(request, kwargs_knowledgebasecategory):
|
||||
|
||||
if hasattr(request.cls, 'kwargs_create_item'):
|
||||
del request.cls.kwargs_create_item
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model_serializer(serializer_knowledgebasecategory):
|
||||
|
||||
yield serializer_knowledgebasecategory
|
||||
|
@ -1,481 +0,0 @@
|
||||
import django
|
||||
import pytest
|
||||
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.shortcuts import reverse
|
||||
from django.test import Client, TestCase
|
||||
|
||||
from rest_framework.relations import Hyperlink
|
||||
|
||||
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_fields import APITenancyObject
|
||||
|
||||
from assistance.models.knowledge_base_category import KnowledgeBaseCategory
|
||||
|
||||
User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
|
||||
class KnowledgeBaseCategoryAPI(
|
||||
TestCase,
|
||||
APITenancyObject
|
||||
):
|
||||
|
||||
model = KnowledgeBaseCategory
|
||||
|
||||
app_namespace = 'v2'
|
||||
|
||||
url_name = '_api_v2_knowledge_base_category'
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. Create the object
|
||||
2. create view user
|
||||
4. make api request
|
||||
"""
|
||||
|
||||
organization = Organization.objects.create(name='test_org')
|
||||
|
||||
self.organization = organization
|
||||
|
||||
different_organization = Organization.objects.create(name='test_different_organization')
|
||||
|
||||
|
||||
self.view_team = Team.objects.create(
|
||||
organization=organization,
|
||||
team_name = 'teamone',
|
||||
model_notes = 'random note'
|
||||
)
|
||||
|
||||
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,
|
||||
)
|
||||
)
|
||||
|
||||
self.view_team.permissions.set([view_permissions])
|
||||
|
||||
self.view_user = User.objects.create_user(username="test_user_view", password="password")
|
||||
|
||||
parent_category = self.model.objects.create(
|
||||
name='cat parent',
|
||||
target_user = self.view_user,
|
||||
organization=organization,
|
||||
)
|
||||
|
||||
self.item = self.model.objects.create(
|
||||
name='cat',
|
||||
model_notes='dsa',
|
||||
target_user = self.view_user,
|
||||
organization=organization,
|
||||
parent_category = parent_category,
|
||||
)
|
||||
|
||||
# self.item.target_teams.set([self.view_team])
|
||||
|
||||
self.url_view_kwargs = {'pk': self.item.id}
|
||||
|
||||
teamuser = TeamUsers.objects.create(
|
||||
team = self.view_team,
|
||||
user = self.view_user
|
||||
)
|
||||
|
||||
organization.manager = self.view_user
|
||||
|
||||
organization.save()
|
||||
|
||||
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)
|
||||
|
||||
self.api_data = response.data
|
||||
|
||||
|
||||
|
||||
# def test_api_field_exists_model_notes(self):
|
||||
# """ Test for existance of API Field
|
||||
|
||||
# model_notes field does not exist for KB articles
|
||||
# """
|
||||
|
||||
# assert 'model_notes' not in self.api_data
|
||||
|
||||
|
||||
# def test_api_field_type_model_notes(self):
|
||||
# """ Test for type for API Field
|
||||
|
||||
# model_notes does not exist for KB articles
|
||||
# """
|
||||
|
||||
# pass
|
||||
|
||||
|
||||
|
||||
def test_api_field_exists_parent_category(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
parent_category field must exist
|
||||
"""
|
||||
|
||||
assert 'parent_category' in self.api_data
|
||||
|
||||
|
||||
def test_api_field_type_parent_category(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
parent_category field must be dict
|
||||
"""
|
||||
|
||||
assert type(self.api_data['parent_category']) is dict
|
||||
|
||||
|
||||
def test_api_field_exists_parent_category_id(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
parent_category.id field must exist
|
||||
"""
|
||||
|
||||
assert 'id' in self.api_data['parent_category']
|
||||
|
||||
|
||||
def test_api_field_type_parent_category_id(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
parent_category.id field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['parent_category']['id']) is int
|
||||
|
||||
|
||||
def test_api_field_exists_parent_category_display_name(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
parent_category.display_name field must exist
|
||||
"""
|
||||
|
||||
assert 'display_name' in self.api_data['parent_category']
|
||||
|
||||
|
||||
def test_api_field_type_parent_category_display_name(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
parent_category.display_name field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['parent_category']['display_name']) is str
|
||||
|
||||
|
||||
def test_api_field_exists_parent_category_url(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
parent_category.url field must exist
|
||||
"""
|
||||
|
||||
assert 'url' in self.api_data['parent_category']
|
||||
|
||||
|
||||
def test_api_field_type_parent_category_url(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
parent_category.url field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['parent_category']['url']) is str
|
||||
|
||||
|
||||
|
||||
# def test_api_field_exists_summary(self):
|
||||
# """ Test for existance of API Field
|
||||
|
||||
# summary field must exist
|
||||
# """
|
||||
|
||||
# assert 'summary' in self.api_data
|
||||
|
||||
|
||||
# def test_api_field_type_summary(self):
|
||||
# """ Test for type for API Field
|
||||
|
||||
# summary field must be str
|
||||
# """
|
||||
|
||||
# assert type(self.api_data['summary']) is str
|
||||
|
||||
|
||||
|
||||
# def test_api_field_exists_content(self):
|
||||
# """ Test for existance of API Field
|
||||
|
||||
# content field must exist
|
||||
# """
|
||||
|
||||
# assert 'content' in self.api_data
|
||||
|
||||
|
||||
# def test_api_field_type_summary(self):
|
||||
# """ Test for type for API Field
|
||||
|
||||
# content field must be str
|
||||
# """
|
||||
|
||||
# assert type(self.api_data['content']) is str
|
||||
|
||||
|
||||
|
||||
# def test_api_field_exists_release_date(self):
|
||||
# """ Test for existance of API Field
|
||||
|
||||
# release_date field must exist
|
||||
# """
|
||||
|
||||
# assert 'release_date' in self.api_data
|
||||
|
||||
|
||||
# def test_api_field_type_release_date(self):
|
||||
# """ Test for type for API Field
|
||||
|
||||
# release_date field must be str
|
||||
# """
|
||||
|
||||
# assert type(self.api_data['release_date']) is str
|
||||
|
||||
|
||||
|
||||
# def test_api_field_exists_expiry_date(self):
|
||||
# """ Test for existance of API Field
|
||||
|
||||
# expiry_date field must exist
|
||||
# """
|
||||
|
||||
# assert 'expiry_date' in self.api_data
|
||||
|
||||
|
||||
# def test_api_field_type_expiry_date(self):
|
||||
# """ Test for type for API Field
|
||||
|
||||
# expiry_date field must be str
|
||||
# """
|
||||
|
||||
# assert type(self.api_data['expiry_date']) is str
|
||||
|
||||
|
||||
|
||||
# def test_api_field_exists_public(self):
|
||||
# """ Test for existance of API Field
|
||||
|
||||
# public field must exist
|
||||
# """
|
||||
|
||||
# assert 'public' in self.api_data
|
||||
|
||||
|
||||
# def test_api_field_type_public(self):
|
||||
# """ Test for type for API Field
|
||||
|
||||
# public field must be bool
|
||||
# """
|
||||
|
||||
# assert type(self.api_data['public']) is bool
|
||||
|
||||
|
||||
|
||||
def test_api_field_type_target_user(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
target_user field must be dict
|
||||
"""
|
||||
|
||||
assert type(self.api_data['target_user']) is dict
|
||||
|
||||
|
||||
def test_api_field_exists_target_user_id(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
target_user.id field must exist
|
||||
"""
|
||||
|
||||
assert 'id' in self.api_data['target_user']
|
||||
|
||||
|
||||
def test_api_field_type_target_user_id(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
target_user.id field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['target_user']['id']) is int
|
||||
|
||||
|
||||
def test_api_field_exists_target_user_display_name(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
target_user.display_name field must exist
|
||||
"""
|
||||
|
||||
assert 'display_name' in self.api_data['target_user']
|
||||
|
||||
|
||||
def test_api_field_type_target_user_display_name(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
target_user.display_name field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['target_user']['display_name']) is str
|
||||
|
||||
|
||||
def test_api_field_exists_target_user_url(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
target_user.url field must exist
|
||||
"""
|
||||
|
||||
assert 'url' in self.api_data['target_user']
|
||||
|
||||
|
||||
def test_api_field_type_target_user_url(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
target_user.url field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['target_user']['url']) is Hyperlink
|
||||
|
||||
|
||||
|
||||
# def test_api_field_type_responsible_user(self):
|
||||
# """ Test for type for API Field
|
||||
|
||||
# responsible_user field must be dict
|
||||
# """
|
||||
|
||||
# assert type(self.api_data['responsible_user']) is dict
|
||||
|
||||
|
||||
# def test_api_field_exists_responsible_user_id(self):
|
||||
# """ Test for existance of API Field
|
||||
|
||||
# responsible_user.id field must exist
|
||||
# """
|
||||
|
||||
# assert 'id' in self.api_data['responsible_user']
|
||||
|
||||
|
||||
# def test_api_field_type_responsible_user_id(self):
|
||||
# """ Test for type for API Field
|
||||
|
||||
# responsible_user.id field must be int
|
||||
# """
|
||||
|
||||
# assert type(self.api_data['responsible_user']['id']) is int
|
||||
|
||||
|
||||
# def test_api_field_exists_responsible_user_display_name(self):
|
||||
# """ Test for existance of API Field
|
||||
|
||||
# responsible_user.display_name field must exist
|
||||
# """
|
||||
|
||||
# assert 'display_name' in self.api_data['responsible_user']
|
||||
|
||||
|
||||
# def test_api_field_type_responsible_user_display_name(self):
|
||||
# """ Test for type for API Field
|
||||
|
||||
# responsible_user.display_name field must be int
|
||||
# """
|
||||
|
||||
# assert type(self.api_data['responsible_user']['display_name']) is str
|
||||
|
||||
|
||||
# def test_api_field_exists_responsible_user_url(self):
|
||||
# """ Test for existance of API Field
|
||||
|
||||
# responsible_user.url field must exist
|
||||
# """
|
||||
|
||||
# assert 'url' in self.api_data['responsible_user']
|
||||
|
||||
|
||||
# def test_api_field_type_responsible_user_url(self):
|
||||
# """ Test for type for API Field
|
||||
|
||||
# responsible_user.url field must be Hyperlink
|
||||
# """
|
||||
|
||||
# assert type(self.api_data['responsible_user']['url']) is Hyperlink
|
||||
|
||||
|
||||
|
||||
# def test_api_field_type_responsible_teams(self):
|
||||
# """ Test for type for API Field
|
||||
|
||||
# responsible_teams field must be list
|
||||
# """
|
||||
|
||||
# assert type(self.api_data['responsible_teams']) is list
|
||||
|
||||
|
||||
# def test_api_field_exists_responsible_teams_id(self):
|
||||
# """ Test for existance of API Field
|
||||
|
||||
# responsible_teams.id field must exist
|
||||
# """
|
||||
|
||||
# assert 'id' in self.api_data['responsible_teams'][0]
|
||||
|
||||
|
||||
# def test_api_field_type_responsible_teams_id(self):
|
||||
# """ Test for type for API Field
|
||||
|
||||
# responsible_teams.id field must be int
|
||||
# """
|
||||
|
||||
# assert type(self.api_data['responsible_teams'][0]['id']) is int
|
||||
|
||||
|
||||
# def test_api_field_exists_responsible_teams_display_name(self):
|
||||
# """ Test for existance of API Field
|
||||
|
||||
# responsible_teams.display_name field must exist
|
||||
# """
|
||||
|
||||
# assert 'display_name' in self.api_data['responsible_teams'][0]
|
||||
|
||||
|
||||
# def test_api_field_type_responsible_teams_display_name(self):
|
||||
# """ Test for type for API Field
|
||||
|
||||
# responsible_teams.display_name field must be int
|
||||
# """
|
||||
|
||||
# assert type(self.api_data['responsible_teams'][0]['display_name']) is str
|
||||
|
||||
|
||||
# def test_api_field_exists_responsible_teams_url(self):
|
||||
# """ Test for existance of API Field
|
||||
|
||||
# responsible_teams.url field must exist
|
||||
# """
|
||||
|
||||
# assert 'url' in self.api_data['responsible_teams'][0]
|
||||
|
||||
|
||||
# def test_api_field_type_responsible_teams_url(self):
|
||||
# """ Test for type for API Field
|
||||
|
||||
# responsible_teams.url field must be str
|
||||
# """
|
||||
|
||||
# assert type(self.api_data['responsible_teams'][0]['url']) is str
|
@ -77,6 +77,7 @@ class KnowledgeBaseCategoryModelInheritedCases(
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_assistance
|
||||
class KnowledgeBaseCategoryModelPyTest(
|
||||
KnowledgeBaseCategoryModelTestCases,
|
||||
):
|
||||
|
@ -0,0 +1,266 @@
|
||||
import pytest
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from api.tests.unit.test_unit_serializer import (
|
||||
SerializerTestCases
|
||||
)
|
||||
|
||||
from centurion.tests.abstract.mock_view import MockView
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_knowledgebasecategory
|
||||
class knowledgebaseCategorySerializerTestCases(
|
||||
SerializerTestCases
|
||||
):
|
||||
|
||||
@pytest.fixture( scope = 'function' )
|
||||
def created_model(self, django_db_blocker, model, model_kwargs):
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
item = model.objects.create( **model_kwargs )
|
||||
|
||||
yield item
|
||||
|
||||
item.delete()
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_name(self,
|
||||
kwargs_api_create, model, model_serializer, request_user
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no name is provided a validation error occurs
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
kwargs = kwargs_api_create.copy()
|
||||
del kwargs['name']
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data = kwargs
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['name'][0] == 'required'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_parent_category_not_self(self,
|
||||
created_model,
|
||||
model, model_serializer, request_user
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that you cant assisgn self as parent category
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
created_model,
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data={
|
||||
"parent_category": created_model.id
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['parent_category'][0] == 'parent_category_not_self'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_both_target_team_target_user(self,
|
||||
kwargs_api_create, model, model_serializer, request_user,
|
||||
model_team, kwargs_team
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that both target user and target team raises a validation error
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
kwargs = kwargs_api_create.copy()
|
||||
team = model_team.objects.create( **kwargs_team )
|
||||
kwargs.update({
|
||||
'target_team': [ team ]
|
||||
})
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data = kwargs
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
team.delete()
|
||||
assert err.value.get_codes()['non_field_errors'][0] == 'invalid_not_both_target_team_user'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_target_team_target_user(self,
|
||||
kwargs_api_create, model, model_serializer, request_user
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if either target user and target team is missing it raises validation error
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
kwargs = kwargs_api_create.copy()
|
||||
del kwargs['target_user']
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data = kwargs
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['non_field_errors'][0] == 'invalid_need_target_team_or_user'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_target_team(self,
|
||||
created_model,
|
||||
model, model_serializer, request_user,
|
||||
model_team, kwargs_team
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item with target user is updated to include a target_team
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
team = model_team.objects.create( **kwargs_team )
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
created_model,
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data={
|
||||
"target_team": [ team.id ]
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
team.delete()
|
||||
|
||||
assert err.value.get_codes()['non_field_errors'][0] == 'invalid_not_both_target_team_user'
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_target_user(self,
|
||||
created_model,
|
||||
model, model_serializer, request_user,
|
||||
model_team, kwargs_team
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item with target team is updated to include a target_user
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
team = model_team.objects.create( **kwargs_team )
|
||||
created_model.target_user = None
|
||||
created_model.target_team.add( team )
|
||||
created_model.save()
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
created_model,
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data={
|
||||
"target_user": request_user.id
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
team.delete()
|
||||
|
||||
assert err.value.get_codes()['non_field_errors'][0] == 'invalid_not_both_target_team_user'
|
||||
|
||||
|
||||
|
||||
|
||||
class knowledgebaseCategorySerializerInheritedCases(
|
||||
knowledgebaseCategorySerializerTestCases
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_assistance
|
||||
class knowledgebaseCategorySerializerPyTest(
|
||||
knowledgebaseCategorySerializerTestCases
|
||||
):
|
||||
pass
|
@ -0,0 +1,87 @@
|
||||
import pytest
|
||||
|
||||
from api.tests.unit.test_unit_common_viewset import ModelViewSetInheritedCases
|
||||
|
||||
from assistance.viewsets.knowledge_base_category import (
|
||||
KnowledgeBaseCategory,
|
||||
ViewSet,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_knowledgebasecategory
|
||||
class ViewsetTestCases(
|
||||
ModelViewSetInheritedCases,
|
||||
):
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'function' )
|
||||
def viewset(self):
|
||||
return ViewSet
|
||||
|
||||
|
||||
@property
|
||||
def parameterized_class_attributes(self):
|
||||
return {
|
||||
'_model_documentation': {
|
||||
'type': type(None),
|
||||
},
|
||||
'back_url': {
|
||||
'type': type(None),
|
||||
},
|
||||
'documentation': {
|
||||
'type': type(None),
|
||||
'value': None
|
||||
},
|
||||
'filterset_fields': {
|
||||
'value': [
|
||||
'name',
|
||||
'organization',
|
||||
'parent_category',
|
||||
'target_user',
|
||||
'target_team'
|
||||
]
|
||||
},
|
||||
'model': {
|
||||
'value': KnowledgeBaseCategory
|
||||
},
|
||||
'model_documentation': {
|
||||
'type': type(None),
|
||||
},
|
||||
'queryset': {
|
||||
'type': type(None),
|
||||
},
|
||||
'serializer_class': {
|
||||
'type': type(None),
|
||||
},
|
||||
'search_fields': {
|
||||
'value': [
|
||||
'name'
|
||||
]
|
||||
},
|
||||
'view_description': {
|
||||
'value': 'Settings, Knowledge Base Categories'
|
||||
},
|
||||
'view_name': {
|
||||
'type': type(None),
|
||||
},
|
||||
'view_serializer_name': {
|
||||
'type': type(None),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class knowledgebaseCategoryViewsetInheritedCases(
|
||||
ViewsetTestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_assistance
|
||||
class knowledgebaseCategoryViewsetPyTest(
|
||||
ViewsetTestCases,
|
||||
):
|
||||
|
||||
pass
|
@ -1,42 +0,0 @@
|
||||
from django.test import Client, TestCase
|
||||
|
||||
from rest_framework.reverse import reverse
|
||||
|
||||
from api.tests.unit.test_unit_common_viewset import ModelViewSetInheritedCases
|
||||
|
||||
from assistance.viewsets.knowledge_base_category import ViewSet
|
||||
|
||||
|
||||
|
||||
@pytest.mark.skip(reason = 'see #895, tests being refactored')
|
||||
class KnowledgeBaseViewsetList(
|
||||
ModelViewSetInheritedCases,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
viewset = ViewSet
|
||||
|
||||
route_name = 'v2:_api_v2_knowledge_base_category'
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. make list request
|
||||
"""
|
||||
|
||||
|
||||
super().setUpTestData()
|
||||
|
||||
|
||||
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)
|
@ -11,7 +11,7 @@ from assistance.viewsets.model_knowledge_base_article import ViewSet
|
||||
from itam.models.device import Device
|
||||
|
||||
|
||||
@pytest.mark.skip(reason = 'see #895, tests being refactored')
|
||||
@pytest.mark.skip(reason = 'see #895 #903, tests being refactored')
|
||||
class ModelKnowledgeBaseArticleViewsetList(
|
||||
ModelViewSetInheritedCases,
|
||||
TestCase,
|
||||
|
@ -25,7 +25,7 @@ class Index(IndexViewset):
|
||||
|
||||
return Response(
|
||||
{
|
||||
"knowledge_base": reverse('v2:_api_v2_knowledge_base-list', request=request),
|
||||
"knowledge_base": reverse('v2:_api_knowledgebase-list', request=request),
|
||||
"request": reverse('v2:_api_v2_ticket_request-list', request=request),
|
||||
}
|
||||
)
|
||||
|
@ -1,5 +1,7 @@
|
||||
import pytest
|
||||
|
||||
from django.db import models
|
||||
|
||||
|
||||
|
||||
@pytest.mark.models
|
||||
@ -23,25 +25,70 @@ class ModelTestCases:
|
||||
"""
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'function' )
|
||||
def created_model(self, django_db_blocker, model, model_kwargs):
|
||||
@pytest.fixture( scope = 'function')
|
||||
def created_model(self, request, django_db_blocker,
|
||||
model, model_kwargs, mocker, model_user, kwargs_user
|
||||
):
|
||||
|
||||
model_object = None
|
||||
item = None
|
||||
|
||||
if not model._meta.abstract:
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
model_object = model.objects.create(
|
||||
**model_kwargs
|
||||
kwargs_many_to_many = {}
|
||||
|
||||
kwargs = {}
|
||||
|
||||
for key, value in model_kwargs.items():
|
||||
|
||||
field = model._meta.get_field(key)
|
||||
|
||||
if isinstance(field, models.ManyToManyField):
|
||||
|
||||
kwargs_many_to_many.update({
|
||||
key: value
|
||||
})
|
||||
|
||||
else:
|
||||
|
||||
kwargs.update({
|
||||
key: value
|
||||
})
|
||||
|
||||
|
||||
context_user = mocker.patch.object(
|
||||
model, 'context'
|
||||
)
|
||||
user = model_user.objects.create( **kwargs_user )
|
||||
context_user.__getitem__.side_effect = {
|
||||
'logger': None,
|
||||
'user': user
|
||||
}.__getitem__
|
||||
|
||||
item = model.objects.create(
|
||||
**kwargs
|
||||
)
|
||||
|
||||
yield model_object
|
||||
for key, value in kwargs_many_to_many.items():
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
field = getattr(item, key)
|
||||
|
||||
if model_object:
|
||||
model_object.delete()
|
||||
for entry in value:
|
||||
|
||||
field.add(entry)
|
||||
|
||||
request.cls.item = item
|
||||
|
||||
yield item
|
||||
|
||||
if item:
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
item.delete()
|
||||
|
||||
user.delete()
|
||||
|
||||
|
||||
|
||||
|
@ -35,7 +35,7 @@ class ClassTestCases:
|
||||
return {}
|
||||
|
||||
|
||||
|
||||
@pytest.mark.regression
|
||||
def test_class_attribute_exists(self,
|
||||
test_class,
|
||||
parameterized, param_key_class_attributes, param_field_name, param_type
|
||||
@ -69,11 +69,11 @@ class ClassTestCases:
|
||||
|
||||
if type(getattr(test_class, param_field_name)) is property:
|
||||
|
||||
assert type( getattr(test_class, param_field_name).fget(test_class) ) is param_type
|
||||
assert type( getattr(test_class, param_field_name).fget(test_class) ) is param_type, type( getattr(test_class, param_field_name).fget(test_class) )
|
||||
|
||||
else:
|
||||
|
||||
assert type(getattr(test_class, param_field_name)) is param_type
|
||||
assert type(getattr(test_class, param_field_name)) is param_type, type(getattr(test_class, param_field_name))
|
||||
|
||||
|
||||
|
||||
|
@ -1,126 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from access.models.tenant import Tenant as Organization
|
||||
|
||||
from centurion.tests.abstract.mock_view import MockView, User
|
||||
|
||||
from config_management.serializers.config_group import ConfigGroups, ConfigGroupModelSerializer
|
||||
|
||||
|
||||
|
||||
class ConfigGroupsValidationAPI(
|
||||
TestCase,
|
||||
):
|
||||
|
||||
model = ConfigGroups
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. Create an org
|
||||
2. Create an item
|
||||
"""
|
||||
|
||||
organization = Organization.objects.create(name='test_org')
|
||||
|
||||
self.user = User.objects.create_user(username="test_user_view", password="password")
|
||||
|
||||
self.mock_view = MockView( user = self.user )
|
||||
|
||||
self.organization = organization
|
||||
|
||||
self.item_no_parent = self.model.objects.create(
|
||||
organization=organization,
|
||||
name = 'random title',
|
||||
config = { 'config_key': 'a value' }
|
||||
)
|
||||
|
||||
self.item_has_parent = self.model.objects.create(
|
||||
organization=organization,
|
||||
name = 'random title two',
|
||||
parent = self.item_no_parent
|
||||
)
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_name(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no name is provided a validation error occurs
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = ConfigGroupModelSerializer(
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"organization": self.organization.id,
|
||||
})
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['name'][0] == 'required'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_parnet_not_self(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item is assigned itself as it's parent group
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = ConfigGroupModelSerializer(
|
||||
self.item_has_parent,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"parent": self.item_has_parent.id
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['parent'][0] == 'self_not_parent'
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_invalid_config_key(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item has it's config updated with an invalid config key
|
||||
a validation exception is raised.
|
||||
"""
|
||||
|
||||
invalid_config = self.item_no_parent.config.copy()
|
||||
invalid_config.update({ 'software': 'is invalid' })
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = ConfigGroupModelSerializer(
|
||||
self.item_no_parent,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"config": invalid_config
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['config'][0] == 'invalid'
|
@ -0,0 +1,25 @@
|
||||
import pytest
|
||||
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model(model_configgroups):
|
||||
|
||||
yield model_configgroups
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class', autouse = True)
|
||||
def model_kwargs(request, kwargs_configgroups):
|
||||
|
||||
request.cls.kwargs_create_item = kwargs_configgroups.copy()
|
||||
|
||||
yield kwargs_configgroups.copy()
|
||||
|
||||
if hasattr(request.cls, 'kwargs_create_item'):
|
||||
del request.cls.kwargs_create_item
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model_serializer(serializer_configgroups):
|
||||
|
||||
yield serializer_configgroups
|
@ -0,0 +1,154 @@
|
||||
import pytest
|
||||
|
||||
from django.db import models
|
||||
|
||||
from rest_framework.relations import Hyperlink
|
||||
|
||||
from api.tests.functional.test_functional_api_fields import (
|
||||
APIFieldsInheritedCases,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_configgroups
|
||||
class ConfigGroupsAPITestCases(
|
||||
APIFieldsInheritedCases,
|
||||
):
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def second_model(self, request, django_db_blocker,
|
||||
model, model_kwargs, model_device, kwargs_device
|
||||
):
|
||||
|
||||
item = None
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
kwargs_many_to_many = {}
|
||||
|
||||
kwargs = {}
|
||||
|
||||
for key, value in model_kwargs.items():
|
||||
|
||||
field = model._meta.get_field(key)
|
||||
|
||||
if isinstance(field, models.ManyToManyField):
|
||||
|
||||
kwargs_many_to_many.update({
|
||||
key: value
|
||||
})
|
||||
|
||||
else:
|
||||
|
||||
kwargs.update({
|
||||
key: value
|
||||
})
|
||||
|
||||
device = model_device.objects.create( **kwargs_device )
|
||||
|
||||
kwargs_many_to_many.update({
|
||||
'hosts': [ device ]
|
||||
})
|
||||
|
||||
kwargs.update({
|
||||
'parent': request.cls.item
|
||||
})
|
||||
|
||||
|
||||
item_two = model.objects.create(
|
||||
**kwargs
|
||||
)
|
||||
|
||||
|
||||
for key, value in kwargs_many_to_many.items():
|
||||
|
||||
field = getattr(item_two, key)
|
||||
|
||||
for entry in value:
|
||||
|
||||
field.add(entry)
|
||||
|
||||
|
||||
request.cls.item_two = item_two
|
||||
|
||||
yield item_two
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
item_two.delete()
|
||||
|
||||
device.delete()
|
||||
|
||||
del request.cls.item_two
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class', autouse = True)
|
||||
def class_setup(self,
|
||||
create_model,
|
||||
second_model,
|
||||
make_request,
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
@property
|
||||
def parameterized_api_fields(self):
|
||||
|
||||
return {
|
||||
'parent': {
|
||||
'expected': dict
|
||||
},
|
||||
'parent.id': {
|
||||
'expected': int
|
||||
},
|
||||
'parent.display_name': {
|
||||
'expected': str
|
||||
},
|
||||
'parent.url': {
|
||||
'expected': str
|
||||
},
|
||||
'name': {
|
||||
'expected': str
|
||||
},
|
||||
'config': {
|
||||
'expected': dict
|
||||
},
|
||||
'config.key': {
|
||||
'expected': str
|
||||
},
|
||||
'config.existing': {
|
||||
'expected': str
|
||||
},
|
||||
|
||||
'hosts': {
|
||||
'expected': list
|
||||
},
|
||||
'hosts.0.id': {
|
||||
'expected': int
|
||||
},
|
||||
'hosts.0.display_name': {
|
||||
'expected': str
|
||||
},
|
||||
'hosts.0.url': {
|
||||
'expected': str
|
||||
},
|
||||
'modified': {
|
||||
'expected': str
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class ConfigGroupsAPIInheritedCases(
|
||||
ConfigGroupsAPITestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_config_management
|
||||
class ConfigGroupsAPIPyTest(
|
||||
ConfigGroupsAPITestCases,
|
||||
):
|
||||
|
||||
pass
|
@ -1,11 +1,6 @@
|
||||
import django
|
||||
import pytest
|
||||
import unittest
|
||||
import requests
|
||||
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import AnonymousUser, Permission
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.test import TestCase
|
||||
|
||||
@ -13,8 +8,6 @@ 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 APIPermissions
|
||||
from api.tests.abstract.api_serializer_viewset import SerializersTestCases
|
||||
from api.tests.abstract.test_metadata_functional import MetadataAttributesFunctional, MetaDataNavigationEntriesFunctional
|
||||
|
||||
from config_management.models.groups import ConfigGroups
|
||||
@ -25,7 +18,7 @@ User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_configgroups
|
||||
class ViewSetBase:
|
||||
|
||||
model = ConfigGroups
|
||||
@ -57,10 +50,6 @@ class ViewSetBase:
|
||||
|
||||
self.different_organization = different_organization
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
self.global_organization = Organization.objects.create(
|
||||
name = 'test_global_organization'
|
||||
)
|
||||
@ -79,9 +68,6 @@ class ViewSetBase:
|
||||
app_settings.save()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
view_permissions = Permission.objects.get(
|
||||
codename = 'view_' + self.model._meta.model_name,
|
||||
content_type = ContentType.objects.get(
|
||||
@ -98,7 +84,6 @@ class ViewSetBase:
|
||||
view_team.permissions.set([view_permissions])
|
||||
|
||||
|
||||
|
||||
add_permissions = Permission.objects.get(
|
||||
codename = 'add_' + self.model._meta.model_name,
|
||||
content_type = ContentType.objects.get(
|
||||
@ -115,7 +100,6 @@ class ViewSetBase:
|
||||
add_team.permissions.set([add_permissions])
|
||||
|
||||
|
||||
|
||||
change_permissions = Permission.objects.get(
|
||||
codename = 'change_' + self.model._meta.model_name,
|
||||
content_type = ContentType.objects.get(
|
||||
@ -219,26 +203,7 @@ class ViewSetBase:
|
||||
|
||||
|
||||
|
||||
class ConfigGroupsPermissionsAPI(
|
||||
ViewSetBase,
|
||||
APIPermissions,
|
||||
TestCase
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class ConfigGroupsViewSet(
|
||||
ViewSetBase,
|
||||
SerializersTestCases,
|
||||
TestCase
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_config_management
|
||||
class ConfigGroupsMetadata(
|
||||
ViewSetBase,
|
||||
MetadataAttributesFunctional,
|
@ -0,0 +1,28 @@
|
||||
import pytest
|
||||
|
||||
from core.tests.functional.centurion_abstract.test_functional_centurion_abstract_model import (
|
||||
CenturionAbstractModelInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_configgroups
|
||||
class ConfigGroupsModelTestCases(
|
||||
CenturionAbstractModelInheritedCases
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class ConfigGroupsModelInheritedCases(
|
||||
ConfigGroupsModelTestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_config_management
|
||||
class ConfigGroupsModelPyTest(
|
||||
ConfigGroupsModelTestCases,
|
||||
):
|
||||
pass
|
@ -13,6 +13,8 @@ from itam.models.software import Software, SoftwareVersion
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_configgroupsoftware
|
||||
@pytest.mark.module_config_management
|
||||
class ConfigGroupSoftwareValidationAPI(
|
||||
TestCase,
|
||||
):
|
@ -1,11 +1,7 @@
|
||||
import django
|
||||
import pytest
|
||||
import unittest
|
||||
import requests
|
||||
import django
|
||||
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import AnonymousUser, Permission
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.test import TestCase
|
||||
|
||||
@ -13,8 +9,6 @@ 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 APIPermissions
|
||||
from api.tests.abstract.api_serializer_viewset import SerializersTestCases
|
||||
from api.tests.abstract.test_metadata_functional import MetadataAttributesFunctional
|
||||
|
||||
from config_management.models.groups import ConfigGroups, ConfigGroupSoftware, Software, SoftwareVersion
|
||||
@ -23,6 +17,7 @@ User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_configgroupsoftware
|
||||
class ViewSetBase:
|
||||
|
||||
model = ConfigGroupSoftware
|
||||
@ -236,37 +231,7 @@ class ViewSetBase:
|
||||
|
||||
|
||||
|
||||
class ConfigGroupSoftwarePermissionsAPI(
|
||||
ViewSetBase,
|
||||
APIPermissions,
|
||||
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.
|
||||
This model is not a tenancy model making this test not-applicable.
|
||||
|
||||
Items returned from the query Must be from the users organization and
|
||||
global ONLY!
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
class ConfigGroupSoftwareViewSet(
|
||||
ViewSetBase,
|
||||
SerializersTestCases,
|
||||
TestCase
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_config_management
|
||||
class ConfigGroupSoftwareMetadata(
|
||||
ViewSetBase,
|
||||
MetadataAttributesFunctional,
|
@ -1,103 +0,0 @@
|
||||
|
||||
from django.shortcuts import reverse
|
||||
from django.test import Client, TestCase
|
||||
|
||||
from core.tests.abstract.test_item_ticket_api_v2 import ItemTicketAPI
|
||||
|
||||
from core.models.ticket.ticket_linked_items import TicketLinkedItem
|
||||
|
||||
from config_management.models.groups import ConfigGroups
|
||||
|
||||
|
||||
|
||||
class ConfigGroupsTicketAPI(
|
||||
ItemTicketAPI,
|
||||
TestCase,
|
||||
):
|
||||
"""Test Cases for Item Tickets
|
||||
|
||||
Args:
|
||||
APITenancyObject (class): Base class for ALL field checks
|
||||
"""
|
||||
|
||||
item_type = TicketLinkedItem.Modules.CONFIG_GROUP
|
||||
|
||||
item_class = 'config_group'
|
||||
|
||||
item_model = ConfigGroups
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. Create an organization for user and item
|
||||
2. Create an item
|
||||
|
||||
"""
|
||||
|
||||
super().setUpTestData()
|
||||
|
||||
|
||||
self.linked_item = self.item_model.objects.create(
|
||||
organization = self.organization,
|
||||
name = 'dev'
|
||||
)
|
||||
|
||||
|
||||
|
||||
self.item = self.model.objects.create(
|
||||
organization = self.organization,
|
||||
ticket = self.ticket,
|
||||
|
||||
# Item attributes
|
||||
|
||||
item = self.linked_item.id,
|
||||
item_type = self.item_type,
|
||||
)
|
||||
|
||||
|
||||
|
||||
self.url_view_kwargs = {
|
||||
'item_class': self.item_class,
|
||||
'item_id': self.item.id,
|
||||
'pk': self.item.id,
|
||||
}
|
||||
|
||||
|
||||
client = Client()
|
||||
url = reverse('v2:_api_v2_item_tickets-detail', kwargs=self.url_view_kwargs)
|
||||
|
||||
|
||||
client.force_login(self.view_user)
|
||||
response = client.get(url)
|
||||
|
||||
self.api_data = response.data
|
||||
|
||||
|
||||
|
||||
def test_api_field_value_item_id(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
item.id field must exist
|
||||
"""
|
||||
|
||||
assert self.api_data['item']['id'] == self.linked_item.id
|
||||
|
||||
|
||||
|
||||
def test_api_field_value_item_type(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
item_type field must be int
|
||||
"""
|
||||
|
||||
assert self.api_data['item_type'] == self.item_type
|
||||
|
||||
def test_api_field_type_item_url(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
item.url field must be str
|
||||
"""
|
||||
|
||||
assert type(self.api_data['item']['url']) is str
|
@ -1,196 +0,0 @@
|
||||
import django
|
||||
import pytest
|
||||
import unittest
|
||||
|
||||
from django.contrib.auth.models import 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_fields import APITenancyObject
|
||||
|
||||
from config_management.models.groups import ConfigGroups
|
||||
|
||||
User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
|
||||
class ConfigGroupsAPI(
|
||||
TestCase,
|
||||
APITenancyObject
|
||||
):
|
||||
|
||||
model = ConfigGroups
|
||||
|
||||
@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',
|
||||
config = dict({"key": "one", "existing": "dont_over_write"})
|
||||
)
|
||||
|
||||
self.second_item = self.model.objects.create(
|
||||
organization = self.organization,
|
||||
name = 'one_two',
|
||||
model_notes = 'stuff',
|
||||
config = dict({"key": "two"}),
|
||||
parent = self.item
|
||||
)
|
||||
|
||||
self.url_view_kwargs = {'pk': self.second_item.id}
|
||||
|
||||
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 = self.organization,
|
||||
)
|
||||
|
||||
view_team.permissions.set([view_permissions])
|
||||
|
||||
self.view_user = User.objects.create_user(username="test_user_view", password="password")
|
||||
teamuser = TeamUsers.objects.create(
|
||||
team = view_team,
|
||||
user = self.view_user
|
||||
)
|
||||
|
||||
client = Client()
|
||||
url = reverse('v2:_api_configgroups-detail', kwargs=self.url_view_kwargs)
|
||||
|
||||
|
||||
client.force_login(self.view_user)
|
||||
response = client.get(url)
|
||||
|
||||
self.api_data = response.data
|
||||
|
||||
|
||||
|
||||
def test_api_field_exists_config(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
config field must exist
|
||||
"""
|
||||
|
||||
assert 'config' in self.api_data
|
||||
|
||||
|
||||
def test_api_field_type_config(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
config field must be dict
|
||||
"""
|
||||
|
||||
assert type(self.api_data['config']) is dict
|
||||
|
||||
|
||||
|
||||
def test_api_field_exists_parent(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
parent field must exist
|
||||
"""
|
||||
|
||||
assert 'parent' in self.api_data
|
||||
|
||||
|
||||
def test_api_field_type_parent(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
parent field must be dict
|
||||
"""
|
||||
|
||||
assert type(self.api_data['parent']) is dict
|
||||
|
||||
|
||||
def test_api_field_exists_parent_id(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
parent.id field must exist
|
||||
"""
|
||||
|
||||
assert 'id' in self.api_data['parent']
|
||||
|
||||
|
||||
def test_api_field_type_parent_id(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
parent.id field must be int
|
||||
"""
|
||||
|
||||
assert type(self.api_data['parent']['id']) is int
|
||||
|
||||
|
||||
def test_api_field_exists_parent_display_name(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
parent.display_name field must exist
|
||||
"""
|
||||
|
||||
assert 'display_name' in self.api_data['parent']
|
||||
|
||||
|
||||
def test_api_field_type_parent_display_name(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
parent.display_name field must be str
|
||||
"""
|
||||
|
||||
assert type(self.api_data['parent']['display_name']) is str
|
||||
|
||||
|
||||
def test_api_field_exists_parent_url(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
parent.url field must exist
|
||||
"""
|
||||
|
||||
assert 'url' in self.api_data['parent']
|
||||
|
||||
|
||||
def test_api_field_type_parent_url(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
parent.url field must be str
|
||||
"""
|
||||
|
||||
assert type(self.api_data['parent']['url']) is str
|
||||
|
||||
|
||||
|
||||
def test_api_field_exists_urls_tickets(self):
|
||||
""" Test for existance of API Field
|
||||
|
||||
_urls.tickets field must exist
|
||||
"""
|
||||
|
||||
assert 'tickets' in self.api_data['_urls']
|
||||
|
||||
|
||||
def test_api_field_type_urls_tickets(self):
|
||||
""" Test for type for API Field
|
||||
|
||||
_urls.tickets field must be str
|
||||
"""
|
||||
|
||||
assert type(self.api_data['_urls']['tickets']) is str
|
@ -17,3 +17,9 @@ def model_kwargs(request, kwargs_configgroups):
|
||||
|
||||
if hasattr(request.cls, 'kwargs_create_item'):
|
||||
del request.cls.kwargs_create_item
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model_serializer(serializer_configgroups):
|
||||
|
||||
yield serializer_configgroups
|
||||
|
@ -2,90 +2,12 @@ import pytest
|
||||
|
||||
from django.db import models
|
||||
|
||||
|
||||
from core.tests.unit.centurion_abstract.test_unit_centurion_abstract_model import (
|
||||
CenturionAbstractModelInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
# class Old:
|
||||
|
||||
# kwargs_item_create = {
|
||||
# 'name': 'one',
|
||||
# 'config': dict({"key": "one", "existing": "dont_over_write"})
|
||||
# }
|
||||
|
||||
# model = ConfigGroups
|
||||
|
||||
# @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')
|
||||
|
||||
# super().setUpTestData()
|
||||
|
||||
|
||||
# self.second_item = self.model.objects.create(
|
||||
# organization = self.organization,
|
||||
# name = 'one_two',
|
||||
# config = dict({"key": "two"}),
|
||||
# parent = self.item
|
||||
# )
|
||||
|
||||
# def test_config_groups_count_child_groups(self):
|
||||
# """ Test function count_children """
|
||||
|
||||
# assert self.item.count_children() == 1
|
||||
|
||||
|
||||
# def test_config_groups_rendered_config_not_empty(self):
|
||||
# """ Rendered Config must be returned """
|
||||
|
||||
# assert self.item.config is not None
|
||||
|
||||
|
||||
# def test_config_groups_rendered_config_is_dict(self):
|
||||
# """ Rendered Config is a string """
|
||||
|
||||
# assert type(self.item.render_config()) is dict
|
||||
|
||||
|
||||
# def test_config_groups_rendered_config_is_correct(self):
|
||||
# """ Rendered Config is correct """
|
||||
|
||||
# assert self.item.config['key'] == 'one'
|
||||
|
||||
|
||||
# def test_config_groups_rendered_config_inheritence_overwrite(self):
|
||||
# """ rendered config from parent group merged correctly """
|
||||
|
||||
# assert self.second_item.config['key'] == 'two'
|
||||
|
||||
|
||||
# def test_config_groups_rendered_config_inheritence_existing_key_present(self):
|
||||
# """ rendered config from parent group merge existing key present
|
||||
|
||||
# during merge, a key that doesn't exist in the child group that exists in the
|
||||
# parent group should be within the child groups rendered config
|
||||
# """
|
||||
|
||||
# assert self.second_item.config['key'] == 'two'
|
||||
|
||||
|
||||
# @pytest.mark.skip(reason="to be written")
|
||||
# def test_config_groups_config_keys_valid_ansible_variable():
|
||||
# """ All config keys must be valid ansible variables """
|
||||
# pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_configgroups
|
||||
class ConfigGroupModelTestCases(
|
||||
CenturionAbstractModelInheritedCases
|
||||
@ -146,6 +68,52 @@ class ConfigGroupModelTestCases(
|
||||
}
|
||||
|
||||
|
||||
# def test_config_groups_count_child_groups(self):
|
||||
# """ Test function count_children """
|
||||
|
||||
# assert self.item.count_children() == 1
|
||||
|
||||
|
||||
# def test_config_groups_rendered_config_not_empty(self):
|
||||
# """ Rendered Config must be returned """
|
||||
|
||||
# assert self.item.config is not None
|
||||
|
||||
|
||||
# def test_config_groups_rendered_config_is_dict(self):
|
||||
# """ Rendered Config is a string """
|
||||
|
||||
# assert type(self.item.render_config()) is dict
|
||||
|
||||
|
||||
# def test_config_groups_rendered_config_is_correct(self):
|
||||
# """ Rendered Config is correct """
|
||||
|
||||
# assert self.item.config['key'] == 'one'
|
||||
|
||||
|
||||
# def test_config_groups_rendered_config_inheritence_overwrite(self):
|
||||
# """ rendered config from parent group merged correctly """
|
||||
|
||||
# assert self.second_item.config['key'] == 'two'
|
||||
|
||||
|
||||
# def test_config_groups_rendered_config_inheritence_existing_key_present(self):
|
||||
# """ rendered config from parent group merge existing key present
|
||||
|
||||
# during merge, a key that doesn't exist in the child group that exists in the
|
||||
# parent group should be within the child groups rendered config
|
||||
# """
|
||||
|
||||
# assert self.second_item.config['key'] == 'two'
|
||||
|
||||
|
||||
# @pytest.mark.skip(reason="to be written")
|
||||
# def test_config_groups_config_keys_valid_ansible_variable():
|
||||
# """ All config keys must be valid ansible variables """
|
||||
# pass
|
||||
|
||||
|
||||
|
||||
class ConfigGroupModelInheritedCases(
|
||||
ConfigGroupModelTestCases,
|
||||
|
@ -0,0 +1,179 @@
|
||||
import pytest
|
||||
|
||||
from django.db import models
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from api.tests.unit.test_unit_serializer import (
|
||||
SerializerTestCases
|
||||
)
|
||||
|
||||
from centurion.tests.abstract.mock_view import MockView
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_configgroups
|
||||
class ConfigGroupsSerializerTestCases(
|
||||
SerializerTestCases
|
||||
):
|
||||
pass
|
||||
|
||||
@pytest.fixture( scope = 'function' )
|
||||
def created_model(self, django_db_blocker, model, model_kwargs):
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
kwargs_many_to_many = {}
|
||||
|
||||
kwargs = {}
|
||||
|
||||
for key, value in model_kwargs.items():
|
||||
|
||||
field = model._meta.get_field(key)
|
||||
|
||||
if isinstance(field, models.ManyToManyField):
|
||||
|
||||
kwargs_many_to_many.update({
|
||||
key: value
|
||||
})
|
||||
|
||||
else:
|
||||
|
||||
kwargs.update({
|
||||
key: value
|
||||
})
|
||||
|
||||
|
||||
item = model.objects.create( **kwargs )
|
||||
|
||||
for key, value in kwargs_many_to_many.items():
|
||||
|
||||
field = getattr(item, key)
|
||||
|
||||
for entry in value:
|
||||
|
||||
field.add(entry)
|
||||
|
||||
yield item
|
||||
|
||||
item.delete()
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_name(self,
|
||||
kwargs_api_create, model, model_serializer, request_user
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no name is provided a validation error occurs
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
kwargs = kwargs_api_create.copy()
|
||||
del kwargs['name']
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data = kwargs,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['name'][0] == 'required'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_parnet_not_self(self,
|
||||
created_model,
|
||||
kwargs_api_create, model, model_serializer, request_user
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item is assigned itself as it's parent group
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
kwargs = kwargs_api_create.copy()
|
||||
kwargs['parent'] = created_model.id
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
created_model,
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data = kwargs,
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['parent'][0] == 'self_not_parent'
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_invalid_config_key(self,
|
||||
created_model,
|
||||
model, model_serializer, request_user
|
||||
):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item has it's config updated with an invalid config key
|
||||
a validation exception is raised.
|
||||
"""
|
||||
|
||||
mock_view = MockView(
|
||||
user = request_user,
|
||||
model = model,
|
||||
action = 'create',
|
||||
)
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = model_serializer['model'](
|
||||
created_model,
|
||||
context = {
|
||||
'request': mock_view.request,
|
||||
'view': mock_view,
|
||||
},
|
||||
data={
|
||||
"config": {'software': 'is invalid'}
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['config'][0] == 'invalid'
|
||||
|
||||
|
||||
|
||||
class ConfigGroupsSerializerInheritedCases(
|
||||
ConfigGroupsSerializerTestCases
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_config_management
|
||||
class ConfigGroupsSerializerPyTest(
|
||||
ConfigGroupsSerializerTestCases
|
||||
):
|
||||
pass
|
@ -0,0 +1,99 @@
|
||||
import pytest
|
||||
|
||||
from api.tests.unit.test_unit_common_viewset import ModelViewSetInheritedCases
|
||||
|
||||
from config_management.viewsets.config_group import (
|
||||
ConfigGroups,
|
||||
ViewSet,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_configgroups
|
||||
class ViewsetTestCases(
|
||||
ModelViewSetInheritedCases,
|
||||
):
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'function' )
|
||||
def viewset(self):
|
||||
return ViewSet
|
||||
|
||||
|
||||
@property
|
||||
def parameterized_class_attributes(self):
|
||||
return {
|
||||
'_model_documentation': {
|
||||
'type': type(None),
|
||||
},
|
||||
'back_url': {
|
||||
'type': type(None),
|
||||
},
|
||||
'documentation': {
|
||||
'type': type(None),
|
||||
'value': None
|
||||
},
|
||||
'filterset_fields': {
|
||||
'value': [
|
||||
'organization',
|
||||
'parent'
|
||||
]
|
||||
},
|
||||
'model': {
|
||||
'value': ConfigGroups
|
||||
},
|
||||
'model_documentation': {
|
||||
'type': type(None),
|
||||
},
|
||||
'queryset': {
|
||||
'type': type(None),
|
||||
},
|
||||
'serializer_class': {
|
||||
'type': type(None),
|
||||
},
|
||||
'search_fields': {
|
||||
'value': [
|
||||
'name',
|
||||
'config'
|
||||
]
|
||||
},
|
||||
'view_description': {
|
||||
'value': 'Configuration Groups'
|
||||
},
|
||||
'view_name': {
|
||||
'type': type(None),
|
||||
},
|
||||
'view_serializer_name': {
|
||||
'type': type(None),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def test_view_func_get_queryset_cache_result_used(self, mocker, viewset, viewset_mock_request):
|
||||
|
||||
qs = mocker.spy(viewset_mock_request.model, 'objects')
|
||||
|
||||
viewset_mock_request.get_queryset() # Initial QuerySet fetch/filter and cache
|
||||
|
||||
assert len(qs.method_calls) == 1 # one call to .all()
|
||||
assert len(qs.mock_calls) == 3 # calls = .all(), all().filter()
|
||||
|
||||
viewset_mock_request.get_queryset() # Use Cached results, dont re-fetch QuerySet
|
||||
|
||||
assert len(qs.method_calls) == 1
|
||||
assert len(qs.mock_calls) == 3
|
||||
|
||||
|
||||
class KnowledgeBaseViewsetInheritedCases(
|
||||
ViewsetTestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_config_management
|
||||
class KnowledgeBaseViewsetPyTest(
|
||||
ViewsetTestCases,
|
||||
):
|
||||
|
||||
pass
|
@ -1,42 +0,0 @@
|
||||
from django.test import Client, TestCase
|
||||
|
||||
from rest_framework.reverse import reverse
|
||||
|
||||
from api.tests.unit.test_unit_common_viewset import ModelViewSetInheritedCases
|
||||
|
||||
from config_management.viewsets.config_group import ViewSet
|
||||
|
||||
|
||||
|
||||
@pytest.mark.skip(reason = 'see #895, tests being refactored')
|
||||
class ConfigGroupsViewsetList(
|
||||
ModelViewSetInheritedCases,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
viewset = ViewSet
|
||||
|
||||
route_name = 'v2:_api_configgroups'
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. make list request
|
||||
"""
|
||||
|
||||
|
||||
super().setUpTestData()
|
||||
|
||||
|
||||
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)
|
@ -20,6 +20,8 @@ User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_configgroupsoftware
|
||||
@pytest.mark.module_config_management
|
||||
class ConfigGroupsAPI(
|
||||
TestCase,
|
||||
APITenancyObject
|
@ -72,6 +72,9 @@ class ConfigGroupSoftwareModelTestCases(
|
||||
def parameterized_class_attributes(self):
|
||||
|
||||
return {
|
||||
'_notes_enabled': {
|
||||
'value': False,
|
||||
},
|
||||
'model_tag': {
|
||||
'type': models.NOT_PROVIDED,
|
||||
'value': models.NOT_PROVIDED,
|
||||
|
@ -0,0 +1,83 @@
|
||||
import pytest
|
||||
|
||||
from api.tests.unit.test_unit_common_viewset import ModelViewSetInheritedCases
|
||||
|
||||
|
||||
from config_management.viewsets.config_group_software import (
|
||||
ConfigGroupSoftware,
|
||||
ViewSet,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_configgroupsoftware
|
||||
class ViewsetTestCases(
|
||||
ModelViewSetInheritedCases,
|
||||
):
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'function' )
|
||||
def viewset(self):
|
||||
return ViewSet
|
||||
|
||||
|
||||
@property
|
||||
def parameterized_class_attributes(self):
|
||||
return {
|
||||
'_model_documentation': {
|
||||
'type': type(None),
|
||||
},
|
||||
'back_url': {
|
||||
'type': type(None),
|
||||
},
|
||||
'documentation': {
|
||||
'type': type(None),
|
||||
'value': None
|
||||
},
|
||||
'filterset_fields': {
|
||||
'value': [
|
||||
'organization',
|
||||
'software'
|
||||
]
|
||||
},
|
||||
'model': {
|
||||
'value': ConfigGroupSoftware
|
||||
},
|
||||
'model_documentation': {
|
||||
'type': type(None),
|
||||
},
|
||||
'queryset': {
|
||||
'type': type(None),
|
||||
},
|
||||
'serializer_class': {
|
||||
'type': type(None),
|
||||
},
|
||||
'search_fields': {
|
||||
'value': []
|
||||
},
|
||||
'view_description': {
|
||||
'value': 'Software for a config group'
|
||||
},
|
||||
'view_name': {
|
||||
'type': type(None),
|
||||
},
|
||||
'view_serializer_name': {
|
||||
'type': type(None),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class ConfigGroupsViewsetInheritedCases(
|
||||
ViewsetTestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_config_management
|
||||
class ConfigGroupsViewsetPyTest(
|
||||
ViewsetTestCases,
|
||||
):
|
||||
|
||||
pass
|
@ -1,52 +0,0 @@
|
||||
from django.test import Client, TestCase
|
||||
|
||||
from rest_framework.reverse import reverse
|
||||
|
||||
from api.tests.unit.test_unit_common_viewset import ModelViewSetInheritedCases
|
||||
|
||||
from config_management.models.groups import ConfigGroups
|
||||
from config_management.viewsets.config_group_software import ViewSet
|
||||
|
||||
|
||||
|
||||
@pytest.mark.skip(reason = 'see #895, tests being refactored')
|
||||
class ConfigGroupsSoftwareViewsetList(
|
||||
ModelViewSetInheritedCases,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
viewset = ViewSet
|
||||
|
||||
route_name = 'v2:_api_configgroupsoftware'
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. make list request
|
||||
"""
|
||||
|
||||
|
||||
super().setUpTestData()
|
||||
|
||||
cg = ConfigGroups.objects.create(
|
||||
organization = self.organization,
|
||||
name = 'cg'
|
||||
)
|
||||
|
||||
self.kwargs = {
|
||||
'config_group_id': cg.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)
|
@ -74,7 +74,7 @@ class ViewSet( ModelViewSet ):
|
||||
|
||||
model = ConfigGroups
|
||||
|
||||
view_description = 'Information Management Knowledge Base Article(s)'
|
||||
view_description = 'Configuration Groups'
|
||||
|
||||
|
||||
def get_queryset(self):
|
||||
@ -105,12 +105,13 @@ class ViewSet( ModelViewSet ):
|
||||
or self.action == 'retrieve'
|
||||
):
|
||||
|
||||
self.serializer_class = globals()[str( self.model._meta.verbose_name).replace(' ' , '') + 'ViewSerializer']
|
||||
self.serializer_class = globals()[str(
|
||||
self.model._meta.verbose_name).replace(' ' , '') + 'ViewSerializer']
|
||||
|
||||
else:
|
||||
|
||||
self.serializer_class = globals()[str( self.model._meta.verbose_name).replace(' ' , '') + 'ModelSerializer']
|
||||
self.serializer_class = globals()[str(
|
||||
self.model._meta.verbose_name).replace(' ' , '') + 'ModelSerializer']
|
||||
|
||||
|
||||
return self.serializer_class
|
||||
|
||||
|
@ -262,6 +262,15 @@ def pytest_generate_tests(metafunc):
|
||||
|
||||
if len(arg_values) > 0:
|
||||
|
||||
# Get the test method
|
||||
test_func = getattr(metafunc.cls, metafunc.definition.name, None)
|
||||
|
||||
# Remove previous xfail mark if present
|
||||
if test_func and hasattr(test_func, 'pytestmark'):
|
||||
test_func.pytestmark = [
|
||||
mark for mark in test_func.pytestmark if mark.name != 'xfail'
|
||||
]
|
||||
|
||||
metafunc.parametrize(
|
||||
argnames = [
|
||||
*fixture_parameters
|
||||
@ -685,7 +694,11 @@ def recursearray() -> dict[dict, str, any]:
|
||||
|
||||
print( f'Index {keys[1]} does not exist. List had a length of {len(v)}', file = sys.stderr )
|
||||
|
||||
return None
|
||||
return {
|
||||
'obj': obj,
|
||||
'key': key,
|
||||
'value': '-value-does_not-exist-'
|
||||
}
|
||||
|
||||
except ValueError:
|
||||
|
||||
|
@ -39,7 +39,7 @@ class CenturionAbstractModelTestCases(
|
||||
assert history_model.__name__ == model().get_history_model_name()
|
||||
|
||||
|
||||
|
||||
@pytest.mark.skip( reason = 'test must be as part of serializer and viewset tests, not model' )
|
||||
def test_model_create_has_history_entry(self, model_contenttype, created_model, model):
|
||||
"""Model Created
|
||||
|
||||
|
@ -1,11 +1,7 @@
|
||||
import django
|
||||
import pytest
|
||||
import unittest
|
||||
import requests
|
||||
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import AnonymousUser, Permission
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.test import TestCase
|
||||
|
||||
@ -13,8 +9,6 @@ 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 APIPermissions
|
||||
from api.tests.abstract.api_serializer_viewset import SerializersTestCases
|
||||
from api.tests.abstract.test_metadata_functional import MetadataAttributesFunctional
|
||||
|
||||
from core.models.manufacturer import Manufacturer
|
||||
@ -26,6 +20,7 @@ User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_manufacturer
|
||||
class ViewSetBase:
|
||||
|
||||
model = Manufacturer
|
||||
@ -220,26 +215,7 @@ class ViewSetBase:
|
||||
|
||||
|
||||
|
||||
class ManufacturerPermissionsAPI(
|
||||
ViewSetBase,
|
||||
APIPermissions,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class ManufacturerViewSet(
|
||||
ViewSetBase,
|
||||
SerializersTestCases,
|
||||
TestCase
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_core
|
||||
class ManufacturerMetadata(
|
||||
ViewSetBase,
|
||||
MetadataAttributesFunctional,
|
@ -10,6 +10,8 @@ from core.serializers.manufacturer import Manufacturer, ManufacturerModelSeriali
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_manufacturer
|
||||
@pytest.mark.module_core
|
||||
class ManufacturerValidationAPI(
|
||||
TestCase,
|
||||
):
|
@ -1,6 +1,5 @@
|
||||
import django
|
||||
import pytest
|
||||
import unittest
|
||||
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
@ -19,6 +18,8 @@ User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_manufacturer
|
||||
@pytest.mark.module_core
|
||||
class ManufacturerAPI(
|
||||
TestCase,
|
||||
APITenancyObject
|
@ -0,0 +1,84 @@
|
||||
|
||||
import pytest
|
||||
|
||||
from api.tests.unit.test_unit_common_viewset import ModelViewSetInheritedCases
|
||||
|
||||
from core.viewsets.manufacturer import (
|
||||
Manufacturer,
|
||||
ViewSet,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_manufacturer
|
||||
class ViewsetTestCases(
|
||||
ModelViewSetInheritedCases,
|
||||
):
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'function' )
|
||||
def viewset(self):
|
||||
return ViewSet
|
||||
|
||||
|
||||
@property
|
||||
def parameterized_class_attributes(self):
|
||||
return {
|
||||
'_model_documentation': {
|
||||
'type': type(None),
|
||||
},
|
||||
'back_url': {
|
||||
'type': type(None),
|
||||
},
|
||||
'documentation': {
|
||||
'type': type(None),
|
||||
'value': None
|
||||
},
|
||||
'filterset_fields': {
|
||||
'value': [
|
||||
'organization'
|
||||
]
|
||||
},
|
||||
'model': {
|
||||
'value': Manufacturer
|
||||
},
|
||||
'model_documentation': {
|
||||
'type': type(None),
|
||||
},
|
||||
'queryset': {
|
||||
'type': type(None),
|
||||
},
|
||||
'serializer_class': {
|
||||
'type': type(None),
|
||||
},
|
||||
'search_fields': {
|
||||
'value': [
|
||||
'name'
|
||||
]
|
||||
},
|
||||
'view_description': {
|
||||
'value': 'Manufacturer(s) / Publishers'
|
||||
},
|
||||
'view_name': {
|
||||
'type': type(None),
|
||||
},
|
||||
'view_serializer_name': {
|
||||
'type': type(None),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class KnowledgeBaseViewsetInheritedCases(
|
||||
ViewsetTestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_core
|
||||
class KnowledgeBaseViewsetPyTest(
|
||||
ViewsetTestCases,
|
||||
):
|
||||
|
||||
pass
|
@ -1,27 +0,0 @@
|
||||
from django.test import TestCase
|
||||
|
||||
from centurion.tests.unit.test_unit_models import (
|
||||
TenancyObjectInheritedCases
|
||||
)
|
||||
|
||||
from core.models.manufacturer import Manufacturer
|
||||
|
||||
|
||||
class ManufacturerModelTestCases(
|
||||
TenancyObjectInheritedCases,
|
||||
):
|
||||
|
||||
kwargs_item_create = {
|
||||
'name': 'man'
|
||||
}
|
||||
|
||||
model = Manufacturer
|
||||
|
||||
|
||||
|
||||
class ManufacturerModelTest(
|
||||
ManufacturerModelTestCases,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
pass
|
@ -1,42 +0,0 @@
|
||||
from django.test import Client, TestCase
|
||||
|
||||
from rest_framework.reverse import reverse
|
||||
|
||||
from api.tests.unit.test_unit_common_viewset import ModelViewSetInheritedCases
|
||||
|
||||
from core.viewsets.manufacturer import ViewSet
|
||||
|
||||
|
||||
|
||||
@pytest.mark.skip(reason = 'see #895, tests being refactored')
|
||||
class ManufacturerViewsetList(
|
||||
ModelViewSetInheritedCases,
|
||||
TestCase,
|
||||
):
|
||||
|
||||
viewset = ViewSet
|
||||
|
||||
route_name = 'v2:_api_manufacturer'
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. make list request
|
||||
"""
|
||||
|
||||
|
||||
super().setUpTestData()
|
||||
|
||||
|
||||
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)
|
@ -262,7 +262,13 @@ class TicketBaseModelTestCases(
|
||||
|
||||
ticket = model.objects.create( **kwargs )
|
||||
|
||||
project_one = model_project.objects.create( **kwargs_project )
|
||||
kwargs_proj = kwargs_project.copy()
|
||||
team_members = kwargs_proj['team_members']
|
||||
del kwargs_proj['team_members']
|
||||
del kwargs_proj['code']
|
||||
|
||||
project_one = model_project.objects.create( **kwargs_proj )
|
||||
project_one.team_members.add( team_members[0] )
|
||||
|
||||
|
||||
kwargs = kwargs_projectmilestone
|
||||
@ -271,7 +277,12 @@ class TicketBaseModelTestCases(
|
||||
|
||||
kwargs = kwargs_project
|
||||
kwargs['name'] = 'project_two'
|
||||
team_members = kwargs['team_members']
|
||||
del kwargs['team_members']
|
||||
del kwargs['code']
|
||||
|
||||
project_two = model_project.objects.create( **kwargs )
|
||||
project_two.team_members.add( team_members[0] )
|
||||
|
||||
kwargs = kwargs_projectmilestone
|
||||
kwargs['name'] = 'two'
|
||||
|
@ -148,7 +148,7 @@ class TicketCommentBaseModelTestCases(
|
||||
"is_closed": {
|
||||
'blank': False,
|
||||
'default': False,
|
||||
'field_type': models.fields.IntegerField,
|
||||
'field_type': models.fields.BooleanField,
|
||||
'null': False,
|
||||
'unique': False,
|
||||
},
|
||||
|
@ -16,17 +16,6 @@ class GitGroupModelTestCases(
|
||||
):
|
||||
|
||||
|
||||
@pytest.mark.skip( reason = 'test must be as part of serializer and viewset tests, not model' )
|
||||
def test_model_create_has_history_entry(self, model_contenttype, created_model, model):
|
||||
"""Model Created
|
||||
|
||||
Ensure that the model when created, added a `create` Audit History
|
||||
entry.
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def test_model_create_with_parent_sets_tenancy(self, created_model, model, model_kwargs):
|
||||
"""Model Created
|
||||
|
||||
|
@ -54,7 +54,7 @@ class GitGroupModelTestCases(
|
||||
'name': {
|
||||
'blank': False,
|
||||
'default': models.fields.NOT_PROVIDED,
|
||||
'field_type': models.IntegerField,
|
||||
'field_type': models.CharField,
|
||||
'length': 80,
|
||||
'null': False,
|
||||
'unique': False,
|
||||
@ -62,7 +62,7 @@ class GitGroupModelTestCases(
|
||||
'path': {
|
||||
'blank': False,
|
||||
'default': models.fields.NOT_PROVIDED,
|
||||
'field_type': models.IntegerField,
|
||||
'field_type': models.CharField,
|
||||
'length': 80,
|
||||
'null': False,
|
||||
'unique': False,
|
||||
@ -70,7 +70,7 @@ class GitGroupModelTestCases(
|
||||
'description': {
|
||||
'blank': True,
|
||||
'default': models.fields.NOT_PROVIDED,
|
||||
'field_type': models.IntegerField,
|
||||
'field_type': models.TextField,
|
||||
'max_length': 80,
|
||||
'null': True,
|
||||
'unique': False,
|
||||
|
@ -25,7 +25,7 @@ class GitLabRepositoryBaseModelTestCases(
|
||||
'visibility': {
|
||||
'blank': False,
|
||||
'default': models.NOT_PROVIDED,
|
||||
'field_type': models.BooleanField,
|
||||
'field_type': models.IntegerField,
|
||||
'null': False,
|
||||
'unique': False,
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ class SoftwareEnableFeatureFlagModelTestCases(
|
||||
'enabled': {
|
||||
'blank': False,
|
||||
'default': False,
|
||||
'field_type': models.IntegerField,
|
||||
'field_type': models.BooleanField,
|
||||
'null': False,
|
||||
'unique': False,
|
||||
},
|
||||
|
@ -1,40 +0,0 @@
|
||||
from access.tests.functional.contact.test_functional_contact_permission import (
|
||||
ContactPermissionsAPIInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
class EmployeePermissionsAPITestCases(
|
||||
ContactPermissionsAPIInheritedCases,
|
||||
):
|
||||
|
||||
add_data: dict = {
|
||||
'employee_number': 123456,
|
||||
}
|
||||
|
||||
kwargs_create_item: dict = {
|
||||
'employee_number': 1234568,
|
||||
}
|
||||
|
||||
kwargs_create_item_diff_org: dict = {
|
||||
'employee_number': 1234567,
|
||||
}
|
||||
|
||||
|
||||
|
||||
class EmployeePermissionsAPIInheritedCases(
|
||||
EmployeePermissionsAPITestCases,
|
||||
):
|
||||
|
||||
add_data: dict = None
|
||||
|
||||
kwargs_create_item: dict = None
|
||||
|
||||
kwargs_create_item_diff_org: dict = None
|
||||
|
||||
|
||||
class EmployeePermissionsAPIPyTest(
|
||||
EmployeePermissionsAPITestCases,
|
||||
):
|
||||
|
||||
pass
|
@ -1,45 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from access.tests.functional.contact.test_functional_contact_serializer import (
|
||||
ContactSerializerInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
class EmployeeSerializerTestCases(
|
||||
ContactSerializerInheritedCases
|
||||
):
|
||||
|
||||
|
||||
parameterized_test_data: dict = {
|
||||
"employee_number": {
|
||||
'will_create': False,
|
||||
'exception_key': 'required'
|
||||
}
|
||||
}
|
||||
|
||||
valid_data: dict = {
|
||||
'employee_number': 123456,
|
||||
}
|
||||
"""Valid data used by serializer to create object"""
|
||||
|
||||
|
||||
|
||||
class EmployeeSerializerInheritedCases(
|
||||
EmployeeSerializerTestCases,
|
||||
):
|
||||
|
||||
parameterized_test_data: dict = None
|
||||
|
||||
valid_data: dict = None
|
||||
"""Valid data used by serializer to create object"""
|
||||
|
||||
|
||||
|
||||
class EmployeeSerializerPyTest(
|
||||
EmployeeSerializerTestCases,
|
||||
):
|
||||
|
||||
parameterized_test_data: dict = None
|
@ -1,60 +0,0 @@
|
||||
from django.test import TestCase
|
||||
|
||||
|
||||
from access.tests.functional.contact.test_functional_contact_viewset import (
|
||||
ContactViewSetInheritedCases
|
||||
)
|
||||
|
||||
from human_resources.models.employee import Employee
|
||||
|
||||
|
||||
|
||||
class ViewSetTestCases(
|
||||
ContactViewSetInheritedCases,
|
||||
):
|
||||
|
||||
add_data: dict = {
|
||||
'employee_number': 123,
|
||||
}
|
||||
|
||||
kwargs_create_item: dict = {
|
||||
'employee_number': 456,
|
||||
}
|
||||
|
||||
kwargs_create_item_diff_org: dict = {
|
||||
'employee_number': 789,
|
||||
}
|
||||
|
||||
model = Employee
|
||||
|
||||
|
||||
|
||||
class EmployeeViewSetInheritedCases(
|
||||
ViewSetTestCases,
|
||||
):
|
||||
|
||||
model = None
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
|
||||
self.kwargs_create_item = {
|
||||
**super().kwargs_create_item,
|
||||
**self.kwargs_create_item
|
||||
}
|
||||
|
||||
self.kwargs_create_item_diff_org = {
|
||||
**super().kwargs_create_item_diff_org,
|
||||
**self.kwargs_create_item_diff_org
|
||||
}
|
||||
|
||||
super().setUpTestData()
|
||||
|
||||
|
||||
|
||||
class EmployeeViewSetTest(
|
||||
ViewSetTestCases,
|
||||
TestCase,
|
||||
):
|
||||
pass
|
@ -9,7 +9,7 @@ from access.tests.unit.contact.test_unit_contact_viewset import (
|
||||
from human_resources.models.employee import Employee
|
||||
|
||||
|
||||
|
||||
@pytest.mark.skip(reason = 'see #895, tests being refactored')
|
||||
@pytest.mark.model_employee
|
||||
class ViewsetTestCases(
|
||||
ContactViewsetInheritedCases,
|
||||
|
170
app/itam/tests/functional/additional_device_permissions_api.py
Normal file
170
app/itam/tests/functional/additional_device_permissions_api.py
Normal file
@ -0,0 +1,170 @@
|
||||
import pytest
|
||||
import random
|
||||
|
||||
from django.test import Client
|
||||
from django.urls.exceptions import NoReverseMatch
|
||||
|
||||
from rest_framework.permissions import (
|
||||
IsAuthenticatedOrReadOnly
|
||||
)
|
||||
|
||||
|
||||
|
||||
class AdditionalTestCases:
|
||||
|
||||
|
||||
# @pytest.fixture( scope = 'function', autouse = True )
|
||||
# def reset_model_kwargs(request, django_db_blocker, kwargs_ticketcommentsolution,
|
||||
# model_ticketbase, kwargs_ticketbase
|
||||
# ):
|
||||
|
||||
# kwargs = kwargs_ticketbase
|
||||
# kwargs['title'] = 'cust_mk_' + str(random.randint(5000,9999))
|
||||
|
||||
# if kwargs.get('external_system', None):
|
||||
# del kwargs['external_system']
|
||||
# if kwargs.get('external_ref', None):
|
||||
# del kwargs['external_ref']
|
||||
|
||||
# with django_db_blocker.unblock():
|
||||
|
||||
# ticket = model_ticketbase.objects.create( **kwargs )
|
||||
|
||||
|
||||
|
||||
# kwargs = kwargs_ticketcommentsolution.copy()
|
||||
# kwargs['ticket'] = ticket
|
||||
|
||||
# request.kwargs_create_item = kwargs
|
||||
|
||||
# yield kwargs
|
||||
|
||||
# with django_db_blocker.unblock():
|
||||
|
||||
# for comment in ticket.ticketcommentbase_set.all():
|
||||
# comment.delete()
|
||||
|
||||
# ticket.delete()
|
||||
|
||||
|
||||
|
||||
def test_permission_add(self, model_instance, api_request_permissions,
|
||||
model_kwargs, kwargs_api_create
|
||||
):
|
||||
""" Check correct permission for add
|
||||
|
||||
Attempt to add as user with permission
|
||||
"""
|
||||
|
||||
client = Client()
|
||||
|
||||
client.force_login( api_request_permissions['user']['add'] )
|
||||
|
||||
the_model = model_instance( kwargs_create = self.kwargs_create_item )
|
||||
|
||||
# self.kwargs_create_item['ticket'].status = 2
|
||||
# self.kwargs_create_item['ticket'].save()
|
||||
|
||||
url = the_model.get_url( many = True )
|
||||
|
||||
kwargs = kwargs_api_create.copy()
|
||||
kwargs['name'] = 'fn-name-01'
|
||||
kwargs['serial_number'] = 'fn_sn_123'
|
||||
kwargs['uuid'] = '039d1b53-d776-49f9-8b8e-a71550317eaf'
|
||||
|
||||
response = client.post(
|
||||
path = url,
|
||||
data = kwargs,
|
||||
content_type = 'application/json'
|
||||
)
|
||||
|
||||
assert response.status_code == 201, response.content
|
||||
|
||||
|
||||
|
||||
def test_returned_results_only_user_orgs(self, model_instance, model_kwargs, api_request_permissions):
|
||||
"""Returned results check
|
||||
|
||||
Ensure that a query to the viewset endpoint does not return
|
||||
items that are not part of the users organizations.
|
||||
"""
|
||||
|
||||
if model_kwargs.get('organization', None) is None:
|
||||
pytest.xfail( reason = 'Model lacks organization field. test is n/a' )
|
||||
|
||||
|
||||
client = Client()
|
||||
|
||||
viewable_organizations = [
|
||||
api_request_permissions['tenancy']['user'].id,
|
||||
]
|
||||
|
||||
if getattr(self, 'global_organization', None):
|
||||
# Cater for above test that also has global org
|
||||
|
||||
viewable_organizations += [ api_request_permissions['tenancy']['global'] ]
|
||||
|
||||
|
||||
client.force_login( api_request_permissions['user']['view'] )
|
||||
|
||||
kwargs = self.kwargs_create_item
|
||||
kwargs['uuid'] = '039d1b53-d776-49f9-8b8e-a71550317ea1'
|
||||
kwargs.update({
|
||||
'organization': api_request_permissions['tenancy']['different']
|
||||
})
|
||||
|
||||
model_instance(
|
||||
kwargs_create = kwargs
|
||||
)
|
||||
|
||||
kwargs = self.kwargs_create_item
|
||||
kwargs['uuid'] = '039d1b53-d776-49f9-8b8e-a71550317ea2'
|
||||
kwargs.update({
|
||||
'organization': api_request_permissions['tenancy']['global']
|
||||
})
|
||||
|
||||
model_instance(
|
||||
kwargs_create = kwargs
|
||||
)
|
||||
|
||||
kwargs = self.kwargs_create_item
|
||||
kwargs['uuid'] = '039d1b53-d776-49f9-8b8e-a71550317ea3'
|
||||
the_model = model_instance( kwargs_create = kwargs )
|
||||
|
||||
response = client.get(
|
||||
path = the_model.get_url( many = True )
|
||||
)
|
||||
|
||||
if response.status_code == 405:
|
||||
pytest.xfail( reason = 'ViewSet does not have this request method.' )
|
||||
|
||||
elif IsAuthenticatedOrReadOnly in response.renderer_context['view'].permission_classes:
|
||||
|
||||
pytest.xfail( reason = 'ViewSet is public viewable, test is N/A' )
|
||||
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
contains_different_org: bool = False
|
||||
|
||||
for item in response.data['results']:
|
||||
|
||||
if 'organization' not in item:
|
||||
pytest.xfail( reason = 'Model lacks organization field. test is n/a' )
|
||||
|
||||
if(
|
||||
int(item['organization']['id']) not in viewable_organizations
|
||||
and
|
||||
int(item['organization']['id']) != api_request_permissions['tenancy']['global'].id
|
||||
):
|
||||
|
||||
contains_different_org = True
|
||||
print(f'Failed returned row was: {item}')
|
||||
|
||||
assert not contains_different_org
|
||||
|
||||
|
||||
def test_returned_data_from_user_and_global_organizations_only(
|
||||
self, model_instance, model_kwargs, api_request_permissions
|
||||
):
|
||||
pytest.xfail( reason = 'model is not a global object' )
|
25
app/itam/tests/functional/device/conftest.py
Normal file
25
app/itam/tests/functional/device/conftest.py
Normal file
@ -0,0 +1,25 @@
|
||||
import pytest
|
||||
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model(model_device):
|
||||
|
||||
yield model_device
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class', autouse = True)
|
||||
def model_kwargs(request, kwargs_device):
|
||||
|
||||
request.cls.kwargs_create_item = kwargs_device.copy()
|
||||
|
||||
yield kwargs_device.copy()
|
||||
|
||||
if hasattr(request.cls, 'kwargs_create_item'):
|
||||
del request.cls.kwargs_create_item
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model_serializer(serializer_device):
|
||||
|
||||
yield serializer_device
|
@ -1,410 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from access.models.tenant import Tenant as Organization
|
||||
|
||||
from centurion.tests.abstract.mock_view import MockView, User
|
||||
|
||||
from itam.serializers.device import Device, DeviceModelSerializer
|
||||
|
||||
|
||||
|
||||
class DeviceValidationAPI(
|
||||
TestCase,
|
||||
):
|
||||
|
||||
model = Device
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. Create an org
|
||||
2. Create an item
|
||||
"""
|
||||
|
||||
organization = Organization.objects.create(name='test_org')
|
||||
|
||||
self.user = User.objects.create_user(username="test_user_view", password="password")
|
||||
|
||||
self.mock_view = MockView( user = self.user )
|
||||
|
||||
self.organization = organization
|
||||
|
||||
self.item = self.model.objects.create(
|
||||
organization=organization,
|
||||
name = 'valid-hostname',
|
||||
)
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_no_name(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no name is provided a validation error occurs
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = DeviceModelSerializer(
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"organization": self.organization.id,
|
||||
})
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['name'][0] == 'required'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_invalid_name_starts_with_digit(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item is given an invalid name 'starts with digit'
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = DeviceModelSerializer(
|
||||
self.item,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"name": '0-start-with-number'
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['name'][0] == 'invalid_hostname'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_invalid_name_contains_hyphon(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item is given an invalid name 'contains hyphon'
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = DeviceModelSerializer(
|
||||
self.item,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"name": 'has_a_hyphon'
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['name'][0] == 'invalid_hostname'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_invalid_name_ends_with_dash(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item is given an invalid name 'ends with dash'
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = DeviceModelSerializer(
|
||||
self.item,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"name": 'ends-with-dash-'
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['name'][0] == 'invalid_hostname'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_invalid_uuid_first_octet(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item is given an invalid uuid 'first octet not hex'
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = DeviceModelSerializer(
|
||||
self.item,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"uuid": 'g0000000-0000-0000-0000-000000000000'
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['uuid'][0] == 'invalid_uuid'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_invalid_uuid_first_octet_wrong_length(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item is given an invalid uuid 'first octet wrong length'
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = DeviceModelSerializer(
|
||||
self.item,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"uuid": '0000000-0000-0000-0000-000000000000'
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['uuid'][0] == 'invalid_uuid'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_invalid_uuid_second_octet(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item is given an invalid uuid 'second octet not hex'
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = DeviceModelSerializer(
|
||||
self.item,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"uuid": '00000000-g000-0000-0000-000000000000'
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['uuid'][0] == 'invalid_uuid'
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_invalid_uuid_second_octet_wrong_length(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item is given an invalid uuid 'second octet wrong length'
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = DeviceModelSerializer(
|
||||
self.item,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"uuid": '00000000-000-0000-0000-000000000000'
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['uuid'][0] == 'invalid_uuid'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_invalid_uuid_third_octet(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item is given an invalid uuid 'third octet not hex'
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = DeviceModelSerializer(
|
||||
self.item,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"uuid": '00000000-0000-g000-0000-000000000000'
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['uuid'][0] == 'invalid_uuid'
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_invalid_uuid_third_octet_wrong_length(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item is given an invalid uuid 'third octet wrong length'
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = DeviceModelSerializer(
|
||||
self.item,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"uuid": '00000000-0000-000-0000-000000000000'
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['uuid'][0] == 'invalid_uuid'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_invalid_uuid_fourth_octet(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item is given an invalid uuid 'fourth octet not hex'
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = DeviceModelSerializer(
|
||||
self.item,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"uuid": '00000000-0000-0000-g000-000000000000'
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['uuid'][0] == 'invalid_uuid'
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_invalid_uuid_fourth_octet_wrong_length(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item is given an invalid uuid 'fourth octet wrong length'
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = DeviceModelSerializer(
|
||||
self.item,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"uuid": '00000000-0000-0000-000-000000000000'
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['uuid'][0] == 'invalid_uuid'
|
||||
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_invalid_uuid_fifth_octet(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item is given an invalid uuid 'fifth octet not hex'
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = DeviceModelSerializer(
|
||||
self.item,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"uuid": '00000000-0000-0000-0000-g00000000000'
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['uuid'][0] == 'invalid_uuid'
|
||||
|
||||
|
||||
def test_serializer_validation_update_existing_invalid_uuid_fifth_octet_wrong_length(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if an existing item is given an invalid uuid 'fifth octet wrong length'
|
||||
it raises a validation error
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = DeviceModelSerializer(
|
||||
self.item,
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"uuid": '00000000-0000-0000-0000-00000000000'
|
||||
},
|
||||
partial=True,
|
||||
)
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['uuid'][0] == 'invalid_uuid'
|
@ -0,0 +1,156 @@
|
||||
import pytest
|
||||
|
||||
# from django.db import models
|
||||
|
||||
from rest_framework.relations import Hyperlink
|
||||
|
||||
from api.tests.functional.test_functional_api_fields import (
|
||||
APIFieldsInheritedCases,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_device
|
||||
class DeviceAPITestCases(
|
||||
APIFieldsInheritedCases,
|
||||
):
|
||||
|
||||
# @pytest.fixture( scope = 'class')
|
||||
# def second_model(self, request, django_db_blocker,
|
||||
# model, model_kwargs
|
||||
# ):
|
||||
|
||||
# item = None
|
||||
|
||||
# with django_db_blocker.unblock():
|
||||
|
||||
# kwargs_many_to_many = {}
|
||||
|
||||
# kwargs = {}
|
||||
|
||||
# for key, value in model_kwargs.items():
|
||||
|
||||
# field = model._meta.get_field(key)
|
||||
|
||||
# if isinstance(field, models.ManyToManyField):
|
||||
|
||||
# kwargs_many_to_many.update({
|
||||
# key: value
|
||||
# })
|
||||
|
||||
# else:
|
||||
|
||||
# kwargs.update({
|
||||
# key: value
|
||||
# })
|
||||
|
||||
|
||||
# # Switch model fields so all fields can be checked
|
||||
# kwargs_many_to_many.update({ 'responsible_teams': kwargs_many_to_many['target_team']})
|
||||
# del kwargs_many_to_many['target_team']
|
||||
|
||||
# kwargs.update({ 'target_user': kwargs['responsible_user']})
|
||||
# del kwargs['responsible_user']
|
||||
|
||||
|
||||
# item_two = model.objects.create(
|
||||
# **kwargs
|
||||
# )
|
||||
|
||||
|
||||
# for key, value in kwargs_many_to_many.items():
|
||||
|
||||
# field = getattr(item_two, key)
|
||||
|
||||
# for entry in value:
|
||||
|
||||
# field.add(entry)
|
||||
|
||||
|
||||
# request.cls.item_two = item_two
|
||||
|
||||
# yield item_two
|
||||
|
||||
# with django_db_blocker.unblock():
|
||||
|
||||
# item_two.delete()
|
||||
|
||||
# del request.cls.item_two
|
||||
|
||||
|
||||
# @pytest.fixture( scope = 'class', autouse = True)
|
||||
# def class_setup(self,
|
||||
# create_model,
|
||||
# second_model,
|
||||
# make_request,
|
||||
# ):
|
||||
|
||||
# pass
|
||||
|
||||
@property
|
||||
def parameterized_api_fields(self):
|
||||
|
||||
return {
|
||||
'name': {
|
||||
'expected': str
|
||||
},
|
||||
'serial_number': {
|
||||
'expected': str
|
||||
},
|
||||
'uuid': {
|
||||
'expected': str
|
||||
},
|
||||
'device_model': {
|
||||
'expected': dict
|
||||
},
|
||||
'device_model.id': {
|
||||
'expected': int
|
||||
},
|
||||
'device_model.display_name': {
|
||||
'expected': str
|
||||
},
|
||||
'device_model.url': {
|
||||
'expected': Hyperlink
|
||||
},
|
||||
|
||||
'device_type': {
|
||||
'expected': dict
|
||||
},
|
||||
'device_type.id': {
|
||||
'expected': int
|
||||
},
|
||||
'device_type.display_name': {
|
||||
'expected': str
|
||||
},
|
||||
'device_type.url': {
|
||||
'expected': Hyperlink
|
||||
},
|
||||
'config': {
|
||||
'expected': dict
|
||||
},
|
||||
'inventorydate': {
|
||||
'expected': str
|
||||
},
|
||||
'is_virtual': {
|
||||
'expected': bool
|
||||
},
|
||||
'modified': {
|
||||
'expected': str
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class DeviceAPIInheritedCases(
|
||||
DeviceAPITestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_itam
|
||||
class DeviceAPIPyTest(
|
||||
DeviceAPITestCases,
|
||||
):
|
||||
|
||||
pass
|
@ -1,18 +1,18 @@
|
||||
import django
|
||||
import pytest
|
||||
|
||||
from django.contrib.auth.models import AnonymousUser, Permission
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.shortcuts import reverse
|
||||
from django.test import Client, TestCase
|
||||
from django.test import 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_serializer_viewset import SerializersTestCases
|
||||
from api.tests.abstract.api_permissions_viewset import APIPermissions
|
||||
from api.tests.abstract.test_metadata_functional import MetadataAttributesFunctional, MetaDataNavigationEntriesFunctional
|
||||
from api.tests.abstract.test_metadata_functional import (
|
||||
MetadataAttributesFunctional,
|
||||
MetaDataNavigationEntriesFunctional
|
||||
)
|
||||
|
||||
from itam.models.device import Device
|
||||
|
||||
@ -22,6 +22,7 @@ User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_device
|
||||
class ViewSetBase:
|
||||
|
||||
model = Device
|
||||
@ -61,7 +62,7 @@ class ViewSetBase:
|
||||
|
||||
self.global_org_item = self.model.objects.create(
|
||||
organization = self.global_organization,
|
||||
name = 'global_item'
|
||||
name = 'global-item'
|
||||
)
|
||||
|
||||
app_settings = AppSettings.objects.get(
|
||||
@ -142,7 +143,8 @@ class ViewSetBase:
|
||||
delete_team.permissions.set([delete_permissions])
|
||||
|
||||
|
||||
self.no_permissions_user = User.objects.create_user(username="test_no_permissions", password="password")
|
||||
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")
|
||||
@ -159,7 +161,7 @@ class ViewSetBase:
|
||||
|
||||
self.other_org_item = self.model.objects.create(
|
||||
organization = different_organization,
|
||||
name = 'other_item'
|
||||
name = 'other-item'
|
||||
)
|
||||
|
||||
|
||||
@ -177,20 +179,23 @@ class ViewSetBase:
|
||||
user = self.add_user
|
||||
)
|
||||
|
||||
self.change_user = User.objects.create_user(username="test_user_change", password="password")
|
||||
self.change_user = User.objects.create_user(
|
||||
username="test_user_change", password="password")
|
||||
teamuser = TeamUsers.objects.create(
|
||||
team = change_team,
|
||||
user = self.change_user
|
||||
)
|
||||
|
||||
self.delete_user = User.objects.create_user(username="test_user_delete", password="password")
|
||||
self.delete_user = User.objects.create_user(
|
||||
username="test_user_delete", password="password")
|
||||
teamuser = 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")
|
||||
self.different_organization_user = User.objects.create_user(
|
||||
username="test_different_organization_user", password="password")
|
||||
|
||||
|
||||
different_organization_team = Team.objects.create(
|
||||
@ -212,25 +217,7 @@ class ViewSetBase:
|
||||
|
||||
|
||||
|
||||
class DevicePermissionsAPI(
|
||||
ViewSetBase,
|
||||
APIPermissions,
|
||||
TestCase
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class DeviceViewSet(
|
||||
ViewSetBase,
|
||||
SerializersTestCases,
|
||||
TestCase
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_itam
|
||||
class DeviceMetadata(
|
||||
ViewSetBase,
|
||||
MetadataAttributesFunctional,
|
@ -0,0 +1,28 @@
|
||||
import pytest
|
||||
|
||||
from core.tests.functional.centurion_abstract.test_functional_centurion_abstract_model import (
|
||||
CenturionAbstractModelInheritedCases
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_device
|
||||
class DeviceModelTestCases(
|
||||
CenturionAbstractModelInheritedCases
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class DeviceModelInheritedCases(
|
||||
DeviceModelTestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_itam
|
||||
class DeviceModelPyTest(
|
||||
DeviceModelTestCases,
|
||||
):
|
||||
pass
|
25
app/itam/tests/functional/device_model/conftest.py
Normal file
25
app/itam/tests/functional/device_model/conftest.py
Normal file
@ -0,0 +1,25 @@
|
||||
import pytest
|
||||
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model(model_devicemodel):
|
||||
|
||||
yield model_devicemodel
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class', autouse = True)
|
||||
def model_kwargs(request, kwargs_devicemodel):
|
||||
|
||||
request.cls.kwargs_create_item = kwargs_devicemodel.copy()
|
||||
|
||||
yield kwargs_devicemodel.copy()
|
||||
|
||||
if hasattr(request.cls, 'kwargs_create_item'):
|
||||
del request.cls.kwargs_create_item
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model_serializer(serializer_devicemodel):
|
||||
|
||||
yield serializer_devicemodel
|
@ -1,57 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
from access.models.tenant import Tenant as Organization
|
||||
|
||||
from centurion.tests.abstract.mock_view import MockView, User
|
||||
|
||||
from itam.serializers.device_model import DeviceModel, DeviceModelModelSerializer
|
||||
|
||||
|
||||
|
||||
class DeviceModelValidationAPI(
|
||||
TestCase,
|
||||
):
|
||||
|
||||
model = DeviceModel
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. Create an org
|
||||
2. Create an item
|
||||
"""
|
||||
|
||||
organization = Organization.objects.create(name='test_org')
|
||||
|
||||
self.user = User.objects.create_user(username="test_user_view", password="password")
|
||||
|
||||
self.mock_view = MockView( user = self.user )
|
||||
|
||||
self.organization = organization
|
||||
|
||||
|
||||
def test_serializer_validation_no_name(self):
|
||||
"""Serializer Validation Check
|
||||
|
||||
Ensure that if creating and no name is provided a validation error occurs
|
||||
"""
|
||||
|
||||
with pytest.raises(ValidationError) as err:
|
||||
|
||||
serializer = DeviceModelModelSerializer(
|
||||
context = {
|
||||
'request': self.mock_view.request,
|
||||
'view': self.mock_view,
|
||||
},
|
||||
data={
|
||||
"organization": self.organization.id,
|
||||
})
|
||||
|
||||
serializer.is_valid(raise_exception = True)
|
||||
|
||||
assert err.value.get_codes()['name'][0] == 'required'
|
@ -0,0 +1,54 @@
|
||||
import pytest
|
||||
|
||||
from rest_framework.relations import Hyperlink
|
||||
|
||||
from api.tests.functional.test_functional_api_fields import (
|
||||
APIFieldsInheritedCases,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_device
|
||||
class DeviceModelAPITestCases(
|
||||
APIFieldsInheritedCases,
|
||||
):
|
||||
|
||||
@property
|
||||
def parameterized_api_fields(self):
|
||||
|
||||
return {
|
||||
'name': {
|
||||
'expected': str
|
||||
},
|
||||
'manufacturer': {
|
||||
'expected': dict
|
||||
},
|
||||
'manufacturer.id': {
|
||||
'expected': int
|
||||
},
|
||||
'manufacturer.display_name': {
|
||||
'expected': str
|
||||
},
|
||||
'manufacturer.url': {
|
||||
'expected': Hyperlink
|
||||
},
|
||||
'modified': {
|
||||
'expected': str
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class DeviceModelAPIInheritedCases(
|
||||
DeviceModelAPITestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_itam
|
||||
class DeviceModelAPIPyTest(
|
||||
DeviceModelAPITestCases,
|
||||
):
|
||||
|
||||
pass
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user