Skip to content

Commit

Permalink
Merge pull request #70 from intuitem/CA-203-API-tests-for-user-permis…
Browse files Browse the repository at this point in the history
…sions-and-roles

Ca 203 api tests for user permissions and roles
  • Loading branch information
nas-tabchiche authored Feb 27, 2024
2 parents 1622a77 + 1a8ee48 commit 6fd6073
Show file tree
Hide file tree
Showing 26 changed files with 1,041 additions and 693 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/backend-api-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,10 @@ jobs:
working-directory: ${{env.working-directory}}
run: |
export $(grep -v '^#' .env | xargs)
pytest app_tests/api
pytest app_tests/api --html=pytest-report.html --self-contained-html
- uses: actions/upload-artifact@v4
if: always()
with:
name: api-tests-report
path: ${{ env.working-directory }}/pytest-report.html
retention-days: 5
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ db/django_secret_key
db/pg_password.txt
./db/
.coverage
pytest-report.html
59 changes: 33 additions & 26 deletions backend/app_tests/api/test_api_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from core.models import Asset
from iam.models import Folder

from test_api import EndpointTestsQueries
from test_vars import GROUPS_PERMISSIONS
from test_utils import EndpointTestsQueries

# Generic asset data for tests
ASSET_NAME = "Test Asset"
Expand Down Expand Up @@ -78,31 +79,33 @@ def test_delete_assets(self):


@pytest.mark.django_db
@pytest.mark.parametrize("test", GROUPS_PERMISSIONS.keys(), ids=[GROUPS_PERMISSIONS[key]["name"] for key in GROUPS_PERMISSIONS.keys()], indirect=True)
class TestAssetsAuthenticated:
"""Perform tests on Assets API endpoint with authentication"""

def test_get_assets(self, authenticated_client):
def test_get_assets(self, test):
"""test to get assets from the API with authentication"""

EndpointTestsQueries.Auth.get_object(
authenticated_client,
test.client,
"Assets",
Asset,
{
"name": ASSET_NAME,
"description": ASSET_DESCRIPTION,
"business_value": ASSET_BUSINESS_VALUE,
"type": ASSET_TYPE[0],
"folder": Folder.get_root_folder(),
"folder": test.folder,
},
{"folder": {"str": Folder.get_root_folder().name}, "type": ASSET_TYPE[1]},
{"folder": {"id": str(test.folder.id), "str": test.folder.name}, "type": ASSET_TYPE[1]},
user_group=test.user_group,
)

def test_create_assets(self, authenticated_client):
"""test to create assets with the API with authentication"""
def test_create_assets(self, test):
"""test to create assets without a parent asset the API with authentication"""

EndpointTestsQueries.Auth.create_object(
authenticated_client,
test.client,
"Assets",
Asset,
{
Expand All @@ -111,23 +114,24 @@ def test_create_assets(self, authenticated_client):
"business_value": ASSET_BUSINESS_VALUE,
"type": ASSET_TYPE[0],
"parent_assets": [],
"folder": str(Folder.get_root_folder().id),
"folder": str(test.folder.id),
},
{"folder": {"str": Folder.get_root_folder().name}, "type": ASSET_TYPE[1]},
{"folder": {"id": str(test.folder.id), "str": test.folder.name}, "type": ASSET_TYPE[1]},
user_group=test.user_group,
)

def test_create_assets2(self, authenticated_client):
"""test to create assets with the API with authentication"""
def test_create_assets_with_parent(self, test):
"""test to create assets with a parent asset with the API with authentication"""

root_asset = Asset.objects.create(
name="root",
description=ASSET_DESCRIPTION,
type=ASSET_TYPE[0],
folder=Folder.get_root_folder(),
folder=test.folder,
)

EndpointTestsQueries.Auth.create_object(
authenticated_client,
test.client,
"Assets",
Asset,
{
Expand All @@ -136,31 +140,32 @@ def test_create_assets2(self, authenticated_client):
"business_value": ASSET_BUSINESS_VALUE,
"type": ASSET_TYPE2[0],
"parent_assets": [str(root_asset.id)],
"folder": str(Folder.get_root_folder().id),
"folder": str(test.folder.id),
},
{
"folder": {"str": Folder.get_root_folder().name},
"folder": {"id": str(test.folder.id), "str": test.folder.name},
"type": ASSET_TYPE2[1],
"parent_assets": [{"id": str(root_asset.id), "str": root_asset.name}],
},
base_count=1,
user_group=test.user_group,
)

def test_update_assets(self, authenticated_client):
def test_update_assets(self, test):
"""test to update assets with the API with authentication"""

folder = Folder.objects.create(name="test")
folder = Folder.objects.create(name="test2")

EndpointTestsQueries.Auth.update_object(
authenticated_client,
test.client,
"Assets",
Asset,
{
"name": ASSET_NAME,
"description": ASSET_DESCRIPTION,
"business_value": ASSET_BUSINESS_VALUE,
"type": ASSET_TYPE[0],
"folder": Folder.get_root_folder(),
"folder": test.folder,
},
{
"name": "new " + ASSET_NAME,
Expand All @@ -169,22 +174,24 @@ def test_update_assets(self, authenticated_client):
"type": ASSET_TYPE2[0],
"folder": str(folder.id),
},
{"folder": {"str": Folder.get_root_folder().name}, "type": ASSET_TYPE[1]},
{"folder": {"id": str(test.folder.id), "str": test.folder.name}, "type": ASSET_TYPE[1]},
user_group=test.user_group,
)

def test_delete_assets(self, authenticated_client):
def test_delete_assets(self, test):
"""test to delete assets with the API with authentication"""

EndpointTestsQueries.Auth.delete_object(
authenticated_client,
test.client,
"Assets",
Asset,
{"name": ASSET_NAME, "folder": Folder.get_root_folder()},
{"name": ASSET_NAME, "folder": test.folder},
user_group=test.user_group,
)

def test_get_type_choices(self, authenticated_client):
def test_get_type_choices(self, test):
"""test to get type choices from the API with authentication"""

EndpointTestsQueries.Auth.get_object_options(
authenticated_client, "Assets", "type", Asset.Type.choices
test.client, "Assets", "type", Asset.Type.choices
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from core.models import Project
from iam.models import Folder

from test_api import EndpointTestsQueries
from test_vars import GROUPS_PERMISSIONS
from test_utils import EndpointTestsQueries

# Generic compliance assessment data for tests
COMPLIANCE_ASSESSMENT_NAME = "Test Compliance Assessment"
Expand Down Expand Up @@ -96,19 +97,20 @@ def test_delete_compliance_assessments(self, authenticated_client):


@pytest.mark.django_db
@pytest.mark.parametrize("test", GROUPS_PERMISSIONS.keys(), ids=[GROUPS_PERMISSIONS[key]["name"] for key in GROUPS_PERMISSIONS.keys()], indirect=True)
class TestComplianceAssessmentsAuthenticated:
"""Perform tests on ComplianceAssessments API endpoint with authentication"""

def test_get_compliance_assessments(self, authenticated_client):
def test_get_compliance_assessments(self, test):
"""test to get compliance assessments from the API with authentication"""

EndpointTestsQueries.Auth.import_object(authenticated_client, "Framework")
EndpointTestsQueries.Auth.import_object(test.admin_client, "Framework")
project = Project.objects.create(
name="test", folder=Folder.objects.create(name="test")
name="test", folder=test.folder
)

EndpointTestsQueries.Auth.get_object(
authenticated_client,
test.client,
"Compliance Assessments",
ComplianceAssessment,
{
Expand All @@ -125,18 +127,19 @@ def test_get_compliance_assessments(self, authenticated_client):
"str": str(Framework.objects.all()[0]),
},
},
user_group=test.user_group,
)

def test_create_compliance_assessments(self, authenticated_client):
def test_create_compliance_assessments(self, test):
"""test to create compliance assessments with the API with authentication"""

EndpointTestsQueries.Auth.import_object(authenticated_client, "Framework")
EndpointTestsQueries.Auth.import_object(test.admin_client, "Framework")
project = Project.objects.create(
name="test", folder=Folder.objects.create(name="test")
name="test", folder=test.folder
)

EndpointTestsQueries.Auth.create_object(
authenticated_client,
test.client,
"Compliance Assessments",
ComplianceAssessment,
{
Expand All @@ -153,23 +156,24 @@ def test_create_compliance_assessments(self, authenticated_client):
"str": str(Framework.objects.all()[0]),
},
},
user_group=test.user_group,
)

def test_update_compliance_assessments(self, authenticated_client):
def test_update_compliance_assessments(self, test):
"""test to update compliance assessments with the API with authentication"""

EndpointTestsQueries.Auth.import_object(authenticated_client, "Documents")
EndpointTestsQueries.Auth.import_object(authenticated_client, "Framework")
EndpointTestsQueries.Auth.import_object(authenticated_client, "Framework2")
EndpointTestsQueries.Auth.import_object(test.admin_client, "Documents")
EndpointTestsQueries.Auth.import_object(test.admin_client, "Framework")
EndpointTestsQueries.Auth.import_object(test.admin_client, "Framework2")
project = Project.objects.create(
name="test", folder=Folder.objects.create(name="test")
name="test", folder=test.folder
)
project2 = Project.objects.create(
name="test2", folder=Folder.objects.create(name="test2")
)

EndpointTestsQueries.Auth.update_object(
authenticated_client,
test.client,
"Compliance Assessments",
ComplianceAssessment,
{
Expand All @@ -193,23 +197,25 @@ def test_update_compliance_assessments(self, authenticated_client):
"str": str(Framework.objects.all()[0]),
},
},
user_group=test.user_group,
)

def test_delete_compliance_assessments(self, authenticated_client):
def test_delete_compliance_assessments(self, test):
"""test to delete compliance assessments with the API with authentication"""

EndpointTestsQueries.Auth.import_object(authenticated_client, "Framework")
EndpointTestsQueries.Auth.import_object(test.admin_client, "Framework")
project = Project.objects.create(
name="test", folder=Folder.objects.create(name="test")
name="test", folder=test.folder
)

EndpointTestsQueries.Auth.delete_object(
authenticated_client,
test.client,
"Compliance Assessments",
ComplianceAssessment,
{
"name": COMPLIANCE_ASSESSMENT_NAME,
"project": project,
"framework": Framework.objects.all()[0],
},
user_group=test.user_group,
)
Loading

0 comments on commit 6fd6073

Please sign in to comment.