Add tests
This commit is contained in:
parent
31d85be949
commit
466f2565b5
@ -24,8 +24,8 @@ class AuditEventQuery(Query):
|
|||||||
|
|
||||||
class AuditLog(object):
|
class AuditLog(object):
|
||||||
@classmethod
|
@classmethod
|
||||||
def log_system_event(cls, resource, action):
|
def log_system_event(cls, resource, action, workspace=None):
|
||||||
return cls._log(resource=resource, action=action)
|
return cls._log(resource=resource, action=action, workspace=workspace)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_all_events(cls, user, pagination_opts=None):
|
def get_all_events(cls, user, pagination_opts=None):
|
||||||
@ -58,9 +58,10 @@ class AuditLog(object):
|
|||||||
return type(resource).__name__.lower()
|
return type(resource).__name__.lower()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _log(cls, user=None, workspace_id=None, resource=None, action=None):
|
def _log(cls, user=None, workspace=None, resource=None, action=None):
|
||||||
resource_id = resource.id if resource else None
|
resource_id = resource.id if resource else None
|
||||||
resource_type = cls._resource_type(resource) if resource else None
|
resource_type = cls._resource_type(resource) if resource else None
|
||||||
|
workspace_id = workspace.id if workspace else None
|
||||||
|
|
||||||
audit_event = AuditEventQuery.create(
|
audit_event = AuditEventQuery.create(
|
||||||
user=user,
|
user=user,
|
||||||
|
@ -2,7 +2,14 @@ import pytest
|
|||||||
|
|
||||||
from atst.domain.audit_log import AuditLog
|
from atst.domain.audit_log import AuditLog
|
||||||
from atst.domain.exceptions import UnauthorizedError
|
from atst.domain.exceptions import UnauthorizedError
|
||||||
from tests.factories import UserFactory
|
from atst.domain.roles import Roles
|
||||||
|
from atst.models.workspace_role import Status as WorkspaceRoleStatus
|
||||||
|
from tests.factories import (
|
||||||
|
UserFactory,
|
||||||
|
WorkspaceFactory,
|
||||||
|
WorkspaceRoleFactory,
|
||||||
|
ProjectFactory,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="function")
|
@pytest.fixture(scope="function")
|
||||||
@ -31,3 +38,65 @@ def test_paginate_audit_log(ccpo):
|
|||||||
|
|
||||||
events = AuditLog.get_all_events(ccpo, pagination_opts={"per_page": 25, "page": 2})
|
events = AuditLog.get_all_events(ccpo, pagination_opts={"per_page": 25, "page": 2})
|
||||||
assert len(events) == 25
|
assert len(events) == 25
|
||||||
|
|
||||||
|
|
||||||
|
def test_ccpo_can_view_ws_audit_log(ccpo):
|
||||||
|
workspace = WorkspaceFactory.create()
|
||||||
|
AuditLog.get_workspace_events(ccpo, workspace)
|
||||||
|
|
||||||
|
|
||||||
|
def test_ws_admin_can_view_ws_audit_log():
|
||||||
|
workspace = WorkspaceFactory.create()
|
||||||
|
admin = UserFactory.create()
|
||||||
|
WorkspaceRoleFactory.create(
|
||||||
|
workspace=workspace,
|
||||||
|
user=admin,
|
||||||
|
role=Roles.get("admin"),
|
||||||
|
status=WorkspaceRoleStatus.ACTIVE,
|
||||||
|
)
|
||||||
|
AuditLog.get_workspace_events(admin, workspace)
|
||||||
|
|
||||||
|
|
||||||
|
def test_ws_owner_can_view_ws_audit_log():
|
||||||
|
workspace = WorkspaceFactory.create()
|
||||||
|
AuditLog.get_workspace_events(workspace.owner, workspace)
|
||||||
|
|
||||||
|
|
||||||
|
def test_other_users_cannot_view_ws_audit_log():
|
||||||
|
with pytest.raises(UnauthorizedError):
|
||||||
|
workspace = WorkspaceFactory.create()
|
||||||
|
dev = UserFactory.create()
|
||||||
|
WorkspaceRoleFactory.create(
|
||||||
|
workspace=workspace,
|
||||||
|
user=dev,
|
||||||
|
role=Roles.get("developer"),
|
||||||
|
status=WorkspaceRoleStatus.ACTIVE,
|
||||||
|
)
|
||||||
|
AuditLog.get_workspace_events(dev, workspace)
|
||||||
|
|
||||||
|
|
||||||
|
def test_paginate_ws_audit_log():
|
||||||
|
workspace = WorkspaceFactory.create()
|
||||||
|
project = ProjectFactory.create(workspace=workspace)
|
||||||
|
for _ in range(100):
|
||||||
|
AuditLog.log_system_event(
|
||||||
|
resource=project, action="create", workspace=workspace
|
||||||
|
)
|
||||||
|
|
||||||
|
events = AuditLog.get_workspace_events(
|
||||||
|
workspace.owner, workspace, pagination_opts={"per_page": 25, "page": 2}
|
||||||
|
)
|
||||||
|
assert len(events) == 25
|
||||||
|
|
||||||
|
|
||||||
|
def test_ws_audit_log_only_includes_current_ws_events():
|
||||||
|
owner = UserFactory.create()
|
||||||
|
workspace = WorkspaceFactory.create(owner=owner)
|
||||||
|
other_workspace = WorkspaceFactory.create(owner=owner)
|
||||||
|
# Add some audit events
|
||||||
|
project_1 = ProjectFactory.create(workspace=workspace)
|
||||||
|
project_2 = ProjectFactory.create(workspace=other_workspace)
|
||||||
|
|
||||||
|
events = AuditLog.get_workspace_events(workspace.owner, workspace)
|
||||||
|
for event in events:
|
||||||
|
assert event.workspace_id == workspace.id
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from flask import url_for
|
from flask import url_for
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
from tests.factories import (
|
from tests.factories import (
|
||||||
UserFactory,
|
UserFactory,
|
||||||
WorkspaceFactory,
|
WorkspaceFactory,
|
||||||
@ -8,8 +9,12 @@ from tests.factories import (
|
|||||||
EnvironmentFactory,
|
EnvironmentFactory,
|
||||||
ProjectFactory,
|
ProjectFactory,
|
||||||
)
|
)
|
||||||
|
=======
|
||||||
|
from tests.factories import UserFactory, WorkspaceFactory, WorkspaceRoleFactory
|
||||||
|
>>>>>>> Add tests
|
||||||
from atst.domain.projects import Projects
|
from atst.domain.projects import Projects
|
||||||
from atst.domain.workspaces import Workspaces
|
from atst.domain.workspaces import Workspaces
|
||||||
|
from atst.domain.roles import Roles
|
||||||
from atst.models.workspace_role import Status as WorkspaceRoleStatus
|
from atst.models.workspace_role import Status as WorkspaceRoleStatus
|
||||||
|
|
||||||
|
|
||||||
@ -36,6 +41,55 @@ def test_user_without_permission_has_no_budget_report_link(client, user_session)
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_user_with_permission_has_activity_log_link(client, user_session):
|
||||||
|
workspace = WorkspaceFactory.create()
|
||||||
|
ccpo = UserFactory.from_atat_role("ccpo")
|
||||||
|
admin = UserFactory.create()
|
||||||
|
WorkspaceRoleFactory.create(
|
||||||
|
workspace=workspace,
|
||||||
|
user=admin,
|
||||||
|
role=Roles.get("admin"),
|
||||||
|
status=WorkspaceRoleStatus.ACTIVE,
|
||||||
|
)
|
||||||
|
|
||||||
|
user_session(workspace.owner)
|
||||||
|
response = client.get("/workspaces/{}/projects".format(workspace.id))
|
||||||
|
assert (
|
||||||
|
'href="/workspaces/{}/activity"'.format(workspace.id).encode() in response.data
|
||||||
|
)
|
||||||
|
|
||||||
|
# logs out previous user before creating a new session
|
||||||
|
user_session(admin)
|
||||||
|
response = client.get("/workspaces/{}/projects".format(workspace.id))
|
||||||
|
assert (
|
||||||
|
'href="/workspaces/{}/activity"'.format(workspace.id).encode() in response.data
|
||||||
|
)
|
||||||
|
|
||||||
|
user_session(ccpo)
|
||||||
|
response = client.get("/workspaces/{}/projects".format(workspace.id))
|
||||||
|
assert (
|
||||||
|
'href="/workspaces/{}/activity"'.format(workspace.id).encode() in response.data
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_user_without_permission_has_no_activity_log_link(client, user_session):
|
||||||
|
workspace = WorkspaceFactory.create()
|
||||||
|
developer = UserFactory.create()
|
||||||
|
WorkspaceRoleFactory.create(
|
||||||
|
workspace=workspace,
|
||||||
|
user=developer,
|
||||||
|
role=Roles.get("developer"),
|
||||||
|
status=WorkspaceRoleStatus.ACTIVE,
|
||||||
|
)
|
||||||
|
|
||||||
|
user_session(developer)
|
||||||
|
response = client.get("/workspaces/{}/projects".format(workspace.id))
|
||||||
|
assert (
|
||||||
|
'href="/workspaces/{}/activity"'.format(workspace.id).encode()
|
||||||
|
not in response.data
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_user_with_permission_has_add_project_link(client, user_session):
|
def test_user_with_permission_has_add_project_link(client, user_session):
|
||||||
workspace = WorkspaceFactory.create()
|
workspace = WorkspaceFactory.create()
|
||||||
user_session(workspace.owner)
|
user_session(workspace.owner)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user