test(config_management): Config Groups API ViewSet permission checks
ref: #15 #248 #353
This commit is contained in:
@ -2,6 +2,16 @@
|
|||||||
|
|
||||||
API redesign in preparation for moving the UI out of centurion to it's [own project](https://github.com/nofusscomputing/centurion_erp_ui). This release introduces a **Feature freeze** to the current UI. Only bug fixes will be done for the current UI.
|
API redesign in preparation for moving the UI out of centurion to it's [own project](https://github.com/nofusscomputing/centurion_erp_ui). This release introduces a **Feature freeze** to the current UI. Only bug fixes will be done for the current UI.
|
||||||
|
|
||||||
|
- A large emphasis is being placed upon API stability. This is being achieved by ensuring the following:
|
||||||
|
|
||||||
|
- Actions can only be carried out by users whom have the correct permissions
|
||||||
|
|
||||||
|
- fields are of the correct type and visible when required as part of the API response
|
||||||
|
|
||||||
|
- Data validations work and notify the user of any issue
|
||||||
|
|
||||||
|
We are make the above possible by ensuring a more stringent test policy.
|
||||||
|
|
||||||
- New API will be at path `api/v2` and will remain until v2.0.0 release of Centurion on which the `api/v2` path will be moved to `api`
|
- New API will be at path `api/v2` and will remain until v2.0.0 release of Centurion on which the `api/v2` path will be moved to `api`
|
||||||
|
|
||||||
- API v1 is now **Feature frozen** with only bug fixes being completed. It's recommended that you move to and start using API v2 as this has feature parity with API v1.
|
- API v1 is now **Feature frozen** with only bug fixes being completed. It's recommended that you move to and start using API v2 as this has feature parity with API v1.
|
||||||
|
@ -0,0 +1,181 @@
|
|||||||
|
import pytest
|
||||||
|
import unittest
|
||||||
|
import requests
|
||||||
|
|
||||||
|
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
from django.contrib.auth.models import AnonymousUser, User
|
||||||
|
from django.contrib.contenttypes.models import ContentType
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from access.models import Organization, Team, TeamUsers, Permission
|
||||||
|
|
||||||
|
from api.tests.abstract.api_permissions_viewset import APIPermissions
|
||||||
|
|
||||||
|
from config_management.models.groups import ConfigGroups
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigGroupsPermissionsAPI(TestCase, APIPermissions):
|
||||||
|
|
||||||
|
model = ConfigGroups
|
||||||
|
|
||||||
|
app_namespace = 'API'
|
||||||
|
|
||||||
|
url_name = '_api_v2_config_group'
|
||||||
|
|
||||||
|
change_data = {'name': 'device'}
|
||||||
|
|
||||||
|
delete_data = {}
|
||||||
|
|
||||||
|
@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
|
||||||
|
|
||||||
|
different_organization = Organization.objects.create(name='test_different_organization')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# self.url_kwargs = {}
|
||||||
|
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
|
||||||
|
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.item = self.model.objects.create(
|
||||||
|
organization = self.organization,
|
||||||
|
name = 'one'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
self.url_view_kwargs = {'pk': self.item.id}
|
||||||
|
|
||||||
|
self.add_data = {
|
||||||
|
'name': 'team_post',
|
||||||
|
'organization': self.organization.id,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
self.add_user = User.objects.create_user(username="test_user_add", password="password")
|
||||||
|
teamuser = TeamUsers.objects.create(
|
||||||
|
team = add_team,
|
||||||
|
user = self.add_user
|
||||||
|
)
|
||||||
|
|
||||||
|
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")
|
||||||
|
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")
|
||||||
|
|
||||||
|
|
||||||
|
different_organization_team = Team.objects.create(
|
||||||
|
team_name = 'different_organization_team',
|
||||||
|
organization = 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
|
||||||
|
)
|
Reference in New Issue
Block a user