refactor(settings): Update Test Suite for ExternalLink model
ref: #833 #836
This commit is contained in:
@ -100,9 +100,8 @@ CREATE TABLE IF NOT EXISTS "devops_git_group_history" ("modelhistory_ptr_id" int
|
||||
CREATE TABLE IF NOT EXISTS "devops_github_repository_notes" ("modelnotes_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_model_notes" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "devops_githubrepository" ("gitrepository_ptr_id") DEFERRABLE INITIALLY DEFERRED);
|
||||
CREATE TABLE IF NOT EXISTS "devops_gitlab_repository_notes" ("modelnotes_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_model_notes" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "devops_gitlabrepository" ("gitrepository_ptr_id") DEFERRABLE INITIALLY DEFERRED);
|
||||
CREATE TABLE IF NOT EXISTS "devops_git_group_notes" ("modelnotes_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_model_notes" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "devops_gitgroup" ("id") DEFERRABLE INITIALLY DEFERRED);
|
||||
CREATE TABLE IF NOT EXISTS "access_organization_notes" ("modelnotes_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_model_notes" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED);
|
||||
CREATE TABLE IF NOT EXISTS "access_organization_history" ("modelhistory_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_model_history" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED);
|
||||
CREATE TABLE IF NOT EXISTS "settings_usersettings" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "created" datetime NOT NULL, "modified" datetime NOT NULL, "default_organization_id" integer NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED, "user_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED, "timezone" varchar(32) NOT NULL, "browser_mode" integer NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS "access_organization_notes" ("modelnotes_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_model_notes" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED);
|
||||
CREATE TABLE IF NOT EXISTS "access_company" ("entity_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "access_entity" ("id") DEFERRABLE INITIALLY DEFERRED, "name" varchar(80) NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS "access_entity" ("is_global" bool NOT NULL, "model_notes" text NULL, "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "created" datetime NOT NULL, "modified" datetime NOT NULL, "organization_id" integer NOT NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED, "entity_type" varchar(30) NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS "access_person" ("entity_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "access_entity" ("id") DEFERRABLE INITIALLY DEFERRED, "f_name" varchar(64) NOT NULL, "l_name" varchar(64) NOT NULL, "dob" date NULL, "m_name" varchar(100) NULL);
|
||||
@ -223,13 +222,14 @@ CREATE TABLE IF NOT EXISTS "settings_appsettings_audithistory" ("centurionaudit_
|
||||
CREATE TABLE IF NOT EXISTS "settings_externallink" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(30) NOT NULL UNIQUE, "template" varchar(180) NOT NULL, "colour" varchar(80) NULL, "devices" bool NOT NULL, "software" bool NOT NULL, "created" datetime NOT NULL, "modified" datetime NOT NULL, "organization_id" integer NOT NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED, "cluster" bool NOT NULL, "service" bool NOT NULL, "button_text" varchar(30) NULL UNIQUE, "model_notes" text NULL);
|
||||
CREATE TABLE IF NOT EXISTS "settings_externallink_audithistory" ("centurionaudit_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_audithistory" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "settings_externallink" ("id") DEFERRABLE INITIALLY DEFERRED);
|
||||
CREATE TABLE IF NOT EXISTS "settings_externallink_centurionmodelnote" ("centurionmodelnote_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "core_centurionmodelnote" ("id") DEFERRABLE INITIALLY DEFERRED, "model_id" integer NOT NULL REFERENCES "settings_externallink" ("id") DEFERRABLE INITIALLY DEFERRED);
|
||||
CREATE TABLE IF NOT EXISTS "settings_usersettings" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "created" datetime NOT NULL, "modified" datetime NOT NULL, "user_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED, "timezone" varchar(32) NOT NULL, "browser_mode" integer NOT NULL, "default_organization_id" integer NULL REFERENCES "access_tenant" ("id") DEFERRABLE INITIALLY DEFERRED);
|
||||
CREATE TABLE IF NOT EXISTS "social_auth_association" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "server_url" varchar(255) NOT NULL, "handle" varchar(255) NOT NULL, "secret" varchar(255) NOT NULL, "issued" integer NOT NULL, "lifetime" integer NOT NULL, "assoc_type" varchar(64) NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS "social_auth_code" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "email" varchar(254) NOT NULL, "code" varchar(32) NOT NULL, "verified" bool NOT NULL, "timestamp" datetime NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS "social_auth_nonce" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "server_url" varchar(255) NOT NULL, "timestamp" integer NOT NULL, "salt" varchar(65) NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS "social_auth_usersocialauth" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "provider" varchar(32) NOT NULL, "uid" varchar(255) NOT NULL, "user_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED, "created" datetime NOT NULL, "modified" datetime NOT NULL, "extra_data" text NOT NULL CHECK ((JSON_VALID("extra_data") OR "extra_data" IS NULL)));
|
||||
CREATE TABLE IF NOT EXISTS "social_auth_partial" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "token" varchar(32) NOT NULL, "next_step" smallint unsigned NOT NULL CHECK ("next_step" >= 0), "backend" varchar(32) NOT NULL, "timestamp" datetime NOT NULL, "data" text NOT NULL CHECK ((JSON_VALID("data") OR "data" IS NULL)));
|
||||
DELETE FROM sqlite_sequence;
|
||||
INSERT INTO sqlite_sequence VALUES('django_migrations',216);
|
||||
INSERT INTO sqlite_sequence VALUES('django_migrations',217);
|
||||
INSERT INTO sqlite_sequence VALUES('django_content_type',208);
|
||||
INSERT INTO sqlite_sequence VALUES('auth_permission',877);
|
||||
INSERT INTO sqlite_sequence VALUES('auth_group',0);
|
||||
@ -239,7 +239,6 @@ INSERT INTO sqlite_sequence VALUES('core_relatedtickets',0);
|
||||
INSERT INTO sqlite_sequence VALUES('core_ticket',0);
|
||||
INSERT INTO sqlite_sequence VALUES('core_ticketcomment',0);
|
||||
INSERT INTO sqlite_sequence VALUES('core_ticketlinkeditem',0);
|
||||
INSERT INTO sqlite_sequence VALUES('settings_usersettings',0);
|
||||
INSERT INTO sqlite_sequence VALUES('access_entity',0);
|
||||
INSERT INTO sqlite_sequence VALUES('core_ticketbase',0);
|
||||
INSERT INTO sqlite_sequence VALUES('core_ticketcommentbase',0);
|
||||
@ -286,6 +285,7 @@ INSERT INTO sqlite_sequence VALUES('project_management_projectstate',0);
|
||||
INSERT INTO sqlite_sequence VALUES('project_management_projecttype',0);
|
||||
INSERT INTO sqlite_sequence VALUES('settings_appsettings',1);
|
||||
INSERT INTO sqlite_sequence VALUES('settings_externallink',0);
|
||||
INSERT INTO sqlite_sequence VALUES('settings_usersettings',0);
|
||||
INSERT INTO sqlite_sequence VALUES('social_auth_association',0);
|
||||
INSERT INTO sqlite_sequence VALUES('social_auth_code',0);
|
||||
INSERT INTO sqlite_sequence VALUES('social_auth_nonce',0);
|
||||
|
@ -1,3 +1,5 @@
|
||||
import pytest
|
||||
|
||||
from django.shortcuts import reverse
|
||||
from django.test import Client, TestCase
|
||||
|
||||
@ -7,6 +9,7 @@ from itim.viewsets.index import Index
|
||||
|
||||
|
||||
|
||||
@pytest.mark.module_settings
|
||||
class SettingsViewset(
|
||||
IndexViewsetInheritedCases,
|
||||
TestCase,
|
||||
|
19
app/settings/tests/unit/user_settings/conftest.py
Normal file
19
app/settings/tests/unit/user_settings/conftest.py
Normal file
@ -0,0 +1,19 @@
|
||||
import pytest
|
||||
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model(model_usersettings):
|
||||
|
||||
yield model_usersettings
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class', autouse = True)
|
||||
def model_kwargs(request, kwargs_usersettings):
|
||||
|
||||
request.cls.kwargs_create_item = kwargs_usersettings.copy()
|
||||
|
||||
yield kwargs_usersettings.copy()
|
||||
|
||||
if hasattr(request.cls, 'kwargs_create_item'):
|
||||
del request.cls.kwargs_create_item
|
@ -1,168 +1,121 @@
|
||||
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.contenttypes.models import ContentType
|
||||
from django.shortcuts import reverse
|
||||
from django.test import TestCase, Client
|
||||
from django.db import models
|
||||
|
||||
from access.models.tenant import Tenant as Organization
|
||||
from access.models.team import Team
|
||||
from access.models.team_user import TeamUsers
|
||||
|
||||
from core.tests.unit.centurion_abstract.test_unit_centurion_abstract_model import (
|
||||
CenturionAbstractModelInheritedCases
|
||||
)
|
||||
|
||||
from settings.models.user_settings import UserSettings
|
||||
|
||||
User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
@pytest.mark.model_usersettings
|
||||
class AppSettingsModelTestCases(
|
||||
CenturionAbstractModelInheritedCases
|
||||
):
|
||||
|
||||
|
||||
@property
|
||||
def parameterized_class_attributes(self):
|
||||
|
||||
return {
|
||||
'_audit_enabled': {
|
||||
'value': False
|
||||
},
|
||||
'_notes_enabled': {
|
||||
'value': False
|
||||
},
|
||||
'model_tag': {
|
||||
'type': models.fields.NOT_PROVIDED,
|
||||
'value': models.fields.NOT_PROVIDED
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@property
|
||||
def parameterized_model_fields(self):
|
||||
|
||||
return {
|
||||
'model_notes': {
|
||||
'blank': models.fields.NOT_PROVIDED,
|
||||
'default': models.fields.NOT_PROVIDED,
|
||||
'field_type': models.CharField,
|
||||
'null': models.fields.NOT_PROVIDED,
|
||||
'unique': models.fields.NOT_PROVIDED,
|
||||
},
|
||||
'organization': {
|
||||
'blank': models.fields.NOT_PROVIDED,
|
||||
'default': models.fields.NOT_PROVIDED,
|
||||
'field_type': models.CharField,
|
||||
'null': models.fields.NOT_PROVIDED,
|
||||
'unique': models.fields.NOT_PROVIDED,
|
||||
},
|
||||
'user': {
|
||||
'blank': False,
|
||||
'default': models.fields.NOT_PROVIDED,
|
||||
'field_type': models.ForeignKey,
|
||||
'null': False,
|
||||
'unique': False,
|
||||
},
|
||||
'browser_mode': {
|
||||
'blank': False,
|
||||
'default': UserSettings.BrowserMode.AUTO,
|
||||
'field_type': models.IntegerField,
|
||||
'null': False,
|
||||
'unique': False,
|
||||
},
|
||||
'default_organization': {
|
||||
'blank': True,
|
||||
'default': models.fields.NOT_PROVIDED,
|
||||
'field_type': models.ForeignKey,
|
||||
'null': True,
|
||||
'unique': False,
|
||||
},
|
||||
'timezone': {
|
||||
'blank': False,
|
||||
'default': 'UTC',
|
||||
'field_type': models.CharField,
|
||||
'length': 32,
|
||||
'null': False,
|
||||
'unique': False,
|
||||
},
|
||||
'modified': {
|
||||
'blank': False,
|
||||
'default': models.fields.NOT_PROVIDED,
|
||||
'field_type': models.DateTimeField,
|
||||
'null': False,
|
||||
'unique': False,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
|
||||
class UserSettings(TestCase):
|
||||
class AppSettingsModelInheritedCases(
|
||||
AppSettingsModelTestCases,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
model = UserSettings
|
||||
|
||||
model_name = 'usersettings'
|
||||
app_label = 'settings'
|
||||
@pytest.mark.module_settings
|
||||
class AppSettingsModelPyTest(
|
||||
AppSettingsModelTestCases,
|
||||
):
|
||||
|
||||
def test_model_tag_defined(self, model):
|
||||
""" Model Tag
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
||||
"""Setup Test
|
||||
|
||||
1. Create an organization for user and item
|
||||
. create an organization that is different to item
|
||||
2. Create a device
|
||||
3. create teams with each permission: view, add, change, delete
|
||||
4. create a user per team
|
||||
Ensure that the model has a tag defined.
|
||||
"""
|
||||
|
||||
organization = Organization.objects.create(name='test_org')
|
||||
pytest.xfail( reason = 'Model does not require tag' )
|
||||
|
||||
self.organization = organization
|
||||
def test_method_value_not_default___str__(self, model, model_instance ):
|
||||
"""Test Method
|
||||
|
||||
different_organization = Organization.objects.create(name='test_different_organization')
|
||||
|
||||
self.different_organization = different_organization
|
||||
|
||||
|
||||
view_permissions = Permission.objects.get(
|
||||
codename = 'view_' + self.model_name,
|
||||
content_type = ContentType.objects.get(
|
||||
app_label = self.app_label,
|
||||
model = self.model_name,
|
||||
)
|
||||
)
|
||||
|
||||
view_team = Team.objects.create(
|
||||
team_name = 'view_team',
|
||||
organization = organization,
|
||||
)
|
||||
|
||||
view_team.permissions.set([view_permissions])
|
||||
|
||||
|
||||
self.no_permissions_user = User.objects.create_user(username="test_no_permissions", password="password")
|
||||
|
||||
|
||||
self.view_user = User.objects.create_user(username="test_user_view", password="password")
|
||||
teamuser = TeamUsers.objects.create(
|
||||
team = view_team,
|
||||
user = self.view_user
|
||||
)
|
||||
|
||||
|
||||
self.different_organization_user = User.objects.create_user(username="test_different_organization_user", password="password")
|
||||
|
||||
|
||||
different_organization_team = Team.objects.create(
|
||||
team_name = 'different_organization_team',
|
||||
organization = different_organization,
|
||||
)
|
||||
|
||||
different_organization_team.permissions.set([
|
||||
view_permissions,
|
||||
])
|
||||
|
||||
TeamUsers.objects.create(
|
||||
team = different_organization_team,
|
||||
user = self.different_organization_user
|
||||
)
|
||||
|
||||
|
||||
self.item = self.model.objects.get(
|
||||
user=self.view_user,
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
def test_user_settings_exist(self):
|
||||
""" User Settings must exist for user
|
||||
|
||||
User settings a created if they dont exist on attempting to access
|
||||
Ensure method `__str__` does not return the default value.
|
||||
"""
|
||||
|
||||
assert self.item
|
||||
|
||||
|
||||
def test_user_settings_organization_is_none(self):
|
||||
""" User Settings value 'organization' is none
|
||||
|
||||
When row is created the organization must not be set
|
||||
"""
|
||||
|
||||
assert self.item.default_organization_id is None
|
||||
|
||||
|
||||
def test_user_settings_organization_edit_correct(self):
|
||||
""" User Settings value 'organization' is none
|
||||
|
||||
When row is created the organization must not be set
|
||||
"""
|
||||
|
||||
self.item.default_organization_id = self.different_organization.id
|
||||
|
||||
self.item.save()
|
||||
|
||||
assert self.item.default_organization_id == self.different_organization.id
|
||||
|
||||
|
||||
# @pytest.mark.skip(reason="to be written")
|
||||
def test_user_settings_on_delete_of_user_settings_removed(self):
|
||||
""" On Delete of a user their settings are removed """
|
||||
|
||||
test_user = User.objects.create_user(username="test_user_single_use", password="password")
|
||||
|
||||
assert self.model.objects.get(
|
||||
user=test_user,
|
||||
)
|
||||
|
||||
test_user.delete()
|
||||
|
||||
|
||||
settings = self.model.objects.filter(
|
||||
user_id=test_user.id,
|
||||
)
|
||||
|
||||
assert settings.exists() == False
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="to be written")
|
||||
def test_user_settings_on_delete_organization_default_organization(self):
|
||||
""" On Delete of an organization, users default organization set to null """
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="to be written")
|
||||
def test_user_settings_on_delete_organization_user_settings_not_deleted(self):
|
||||
""" On Delete of an organization, users settings are not deleted """
|
||||
pass
|
||||
|
||||
|
||||
def test_user_settings_browser_mode(self):
|
||||
"""User Settings value 'browser_mode' exists"""
|
||||
|
||||
assert self.item.browser_mode
|
||||
pytest.xfail( reason = 'Model does not require this function' )
|
||||
|
@ -1,3 +1,5 @@
|
||||
import pytest
|
||||
|
||||
from django.test import Client, TestCase
|
||||
|
||||
from rest_framework.reverse import reverse
|
||||
@ -8,6 +10,8 @@ from settings.viewsets.user_settings import ViewSet
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_usersettings
|
||||
@pytest.mark.module_settings
|
||||
class UserSettingsViewsetList(
|
||||
ModelRetrieveUpdateViewSetInheritedCases,
|
||||
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
|
||||
@ -15,13 +14,14 @@ from access.models.team_user import TeamUsers
|
||||
|
||||
from api.tests.abstract.api_fields import APICommonFields
|
||||
|
||||
from settings.models.app_settings import AppSettings
|
||||
from settings.models.user_settings import UserSettings
|
||||
|
||||
User = django.contrib.auth.get_user_model()
|
||||
|
||||
|
||||
|
||||
@pytest.mark.model_usersettings
|
||||
@pytest.mark.module_settings
|
||||
class UserSettingsAPI(
|
||||
TestCase,
|
||||
APICommonFields
|
||||
|
5
app/tests/fixtures/__init__.py
vendored
5
app/tests/fixtures/__init__.py
vendored
@ -257,3 +257,8 @@ from .model_user import (
|
||||
kwargs_user,
|
||||
model_user,
|
||||
)
|
||||
|
||||
from .model_usersettings import (
|
||||
kwargs_usersettings,
|
||||
model_usersettings,
|
||||
)
|
||||
|
41
app/tests/fixtures/model_usersettings.py
vendored
Normal file
41
app/tests/fixtures/model_usersettings.py
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
import datetime
|
||||
import pytest
|
||||
|
||||
from settings.models.user_settings import UserSettings
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model_usersettings():
|
||||
|
||||
yield UserSettings
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def kwargs_usersettings( django_db_blocker, model_user ):
|
||||
|
||||
random_str = str(datetime.datetime.now(tz=datetime.timezone.utc))
|
||||
random_str = str(random_str).replace(
|
||||
' ', '').replace(':', '').replace('+', '').replace('.', '')
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
user = model_user.objects.create(
|
||||
username = 'a' + random_str,
|
||||
password = 'password'
|
||||
)
|
||||
|
||||
# user_settings = UserSettings.objects.get(
|
||||
# user = user
|
||||
# )
|
||||
|
||||
# user_settings.delete() # Remove default created
|
||||
|
||||
|
||||
kwargs = {
|
||||
'user': user,
|
||||
}
|
||||
|
||||
yield kwargs.copy()
|
||||
|
||||
with django_db_blocker.unblock():
|
||||
|
||||
user.delete()
|
Reference in New Issue
Block a user