fix(content): allow entitled users to access content of inactive modules #28
|
|
@ -152,16 +152,16 @@ class ContentService:
|
||||||
session=self.session,
|
session=self.session,
|
||||||
module_id=request.module_id,
|
module_id=request.module_id,
|
||||||
module_code=request.module_code,
|
module_code=request.module_code,
|
||||||
active_only=True,
|
active_only=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
if module is None:
|
if module is None:
|
||||||
raise HTTPException(status_code=404, detail="Module not found")
|
raise HTTPException(status_code=404, detail="Module not found")
|
||||||
|
|
||||||
allowed_module_statuses = (
|
allowed_module_statuses = (
|
||||||
{ModuleStatus.ACTIVE, ModuleStatus.DRAFT}
|
{ModuleStatus.ACTIVE, ModuleStatus.DRAFT, ModuleStatus.INACTIVE}
|
||||||
if preview
|
if preview
|
||||||
else {ModuleStatus.ACTIVE}
|
else {ModuleStatus.ACTIVE, ModuleStatus.INACTIVE}
|
||||||
)
|
)
|
||||||
if module.status not in allowed_module_statuses:
|
if module.status not in allowed_module_statuses:
|
||||||
raise HTTPException(status_code=404, detail="Module not found")
|
raise HTTPException(status_code=404, detail="Module not found")
|
||||||
|
|
@ -205,16 +205,16 @@ class ContentService:
|
||||||
session=self.session,
|
session=self.session,
|
||||||
module_id=request.module_id,
|
module_id=request.module_id,
|
||||||
module_code=request.module_code,
|
module_code=request.module_code,
|
||||||
active_only=True,
|
active_only=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
if module is None:
|
if module is None:
|
||||||
raise HTTPException(status_code=404, detail="Module not found")
|
raise HTTPException(status_code=404, detail="Module not found")
|
||||||
|
|
||||||
allowed_module_statuses = (
|
allowed_module_statuses = (
|
||||||
{ModuleStatus.ACTIVE, ModuleStatus.DRAFT}
|
{ModuleStatus.ACTIVE, ModuleStatus.DRAFT, ModuleStatus.INACTIVE}
|
||||||
if preview
|
if preview
|
||||||
else {ModuleStatus.ACTIVE}
|
else {ModuleStatus.ACTIVE, ModuleStatus.INACTIVE}
|
||||||
)
|
)
|
||||||
if module.status not in allowed_module_statuses:
|
if module.status not in allowed_module_statuses:
|
||||||
raise HTTPException(status_code=404, detail="Module not found")
|
raise HTTPException(status_code=404, detail="Module not found")
|
||||||
|
|
@ -270,16 +270,16 @@ class ContentService:
|
||||||
session=self.session,
|
session=self.session,
|
||||||
module_id=request.module_id,
|
module_id=request.module_id,
|
||||||
module_code=request.module_code,
|
module_code=request.module_code,
|
||||||
active_only=True,
|
active_only=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
if module is None:
|
if module is None:
|
||||||
raise HTTPException(status_code=404, detail="Module not found")
|
raise HTTPException(status_code=404, detail="Module not found")
|
||||||
|
|
||||||
allowed_module_statuses = (
|
allowed_module_statuses = (
|
||||||
{ModuleStatus.ACTIVE, ModuleStatus.DRAFT}
|
{ModuleStatus.ACTIVE, ModuleStatus.DRAFT, ModuleStatus.INACTIVE}
|
||||||
if preview
|
if preview
|
||||||
else {ModuleStatus.ACTIVE}
|
else {ModuleStatus.ACTIVE, ModuleStatus.INACTIVE}
|
||||||
)
|
)
|
||||||
if module.status not in allowed_module_statuses:
|
if module.status not in allowed_module_statuses:
|
||||||
raise HTTPException(status_code=404, detail="Module not found")
|
raise HTTPException(status_code=404, detail="Module not found")
|
||||||
|
|
@ -572,16 +572,16 @@ class ContentService:
|
||||||
session=self.session,
|
session=self.session,
|
||||||
module_id=request.module_id,
|
module_id=request.module_id,
|
||||||
module_code=request.module_code,
|
module_code=request.module_code,
|
||||||
active_only=True,
|
active_only=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
if module is None:
|
if module is None:
|
||||||
raise HTTPException(status_code=404, detail="Module not found")
|
raise HTTPException(status_code=404, detail="Module not found")
|
||||||
|
|
||||||
allowed_module_statuses = (
|
allowed_module_statuses = (
|
||||||
{ModuleStatus.ACTIVE, ModuleStatus.DRAFT}
|
{ModuleStatus.ACTIVE, ModuleStatus.DRAFT, ModuleStatus.INACTIVE}
|
||||||
if preview
|
if preview
|
||||||
else {ModuleStatus.ACTIVE}
|
else {ModuleStatus.ACTIVE, ModuleStatus.INACTIVE}
|
||||||
)
|
)
|
||||||
if module.status not in allowed_module_statuses:
|
if module.status not in allowed_module_statuses:
|
||||||
raise HTTPException(status_code=404, detail="Module not found")
|
raise HTTPException(status_code=404, detail="Module not found")
|
||||||
|
|
@ -1084,10 +1084,18 @@ class ContentService:
|
||||||
module = await self.repository.resolve_module(
|
module = await self.repository.resolve_module(
|
||||||
session=self.session,
|
session=self.session,
|
||||||
module_code=module_code,
|
module_code=module_code,
|
||||||
active_only=True,
|
active_only=False,
|
||||||
)
|
)
|
||||||
if module is None:
|
if module is None:
|
||||||
raise ModuleNotFoundError("Requested active module was not found")
|
raise ModuleNotFoundError("Requested module was not found")
|
||||||
|
module_status = (
|
||||||
|
module.status.value
|
||||||
|
if hasattr(module.status, "value")
|
||||||
|
else str(module.status)
|
||||||
|
)
|
||||||
|
allowed_module_statuses = {"active", "inactive"}
|
||||||
|
if module_status not in allowed_module_statuses:
|
||||||
|
raise ModuleNotFoundError("Requested module is not available")
|
||||||
|
|
||||||
definitions = await self.repository.get_module_search_definitions(
|
definitions = await self.repository.get_module_search_definitions(
|
||||||
module_id=module.id,
|
module_id=module.id,
|
||||||
|
|
@ -1147,10 +1155,18 @@ class ContentService:
|
||||||
session=self.session,
|
session=self.session,
|
||||||
module_id=request.module_id,
|
module_id=request.module_id,
|
||||||
module_code=request.module_code,
|
module_code=request.module_code,
|
||||||
active_only=True,
|
active_only=False,
|
||||||
)
|
)
|
||||||
if module is None:
|
if module is None:
|
||||||
raise ModuleNotFoundError("Requested active module was not found")
|
raise ModuleNotFoundError("Requested module was not found")
|
||||||
|
module_status = (
|
||||||
|
module.status.value
|
||||||
|
if hasattr(module.status, "value")
|
||||||
|
else str(module.status)
|
||||||
|
)
|
||||||
|
allowed_module_statuses = {"active", "inactive"}
|
||||||
|
if module_status not in allowed_module_statuses:
|
||||||
|
raise ModuleNotFoundError("Requested module is not available")
|
||||||
|
|
||||||
definitions = await self.repository.get_module_search_definitions(
|
definitions = await self.repository.get_module_search_definitions(
|
||||||
module_id=module.id,
|
module_id=module.id,
|
||||||
|
|
@ -1189,16 +1205,20 @@ class ContentService:
|
||||||
session=self.session,
|
session=self.session,
|
||||||
module_id=request.module_id,
|
module_id=request.module_id,
|
||||||
module_code=request.module_code,
|
module_code=request.module_code,
|
||||||
active_only=True,
|
active_only=False,
|
||||||
)
|
)
|
||||||
if module is None:
|
if module is None:
|
||||||
raise ModuleNotFoundError("Requested active module was not found")
|
raise ModuleNotFoundError("Requested module was not found")
|
||||||
module_status = (
|
module_status = (
|
||||||
module.status.value
|
module.status.value
|
||||||
if hasattr(module.status, "value")
|
if hasattr(module.status, "value")
|
||||||
else str(module.status)
|
else str(module.status)
|
||||||
)
|
)
|
||||||
allowed_module_statuses = {"active", "draft"} if preview else {"active"}
|
allowed_module_statuses = (
|
||||||
|
{"active", "draft", "inactive"}
|
||||||
|
if preview
|
||||||
|
else {"active", "inactive"}
|
||||||
|
)
|
||||||
if module_status not in allowed_module_statuses:
|
if module_status not in allowed_module_statuses:
|
||||||
raise ModuleNotFoundError("Requested module is not available")
|
raise ModuleNotFoundError("Requested module is not available")
|
||||||
definition_set = (
|
definition_set = (
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,168 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from unittest.mock import AsyncMock, MagicMock
|
||||||
|
from uuid import uuid4
|
||||||
|
import pytest
|
||||||
|
from fastapi import HTTPException
|
||||||
|
|
||||||
|
from app.db.enums import ModuleStatus
|
||||||
|
from app.features.content.service import ContentService
|
||||||
|
from app.features.content.errors import ModuleNotFoundError
|
||||||
|
from app.features.content.schema import (
|
||||||
|
ModuleRequest,
|
||||||
|
ContentSearchRequest,
|
||||||
|
ModuleContentSearchRequest,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_repository() -> MagicMock:
|
||||||
|
return MagicMock()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_session() -> AsyncMock:
|
||||||
|
return AsyncMock()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_cache() -> MagicMock:
|
||||||
|
cache = MagicMock()
|
||||||
|
# By default, pass through loader
|
||||||
|
async def fake_cached(*args, loader=None, **kwargs):
|
||||||
|
if loader:
|
||||||
|
return await loader()
|
||||||
|
return None
|
||||||
|
cache.get_or_set = AsyncMock(side_effect=fake_cached)
|
||||||
|
cache.module_contents = AsyncMock(side_effect=fake_cached)
|
||||||
|
cache.template_contents = AsyncMock(side_effect=fake_cached)
|
||||||
|
return cache
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def content_service(mock_repository: MagicMock, mock_session: AsyncMock, mock_cache: MagicMock) -> ContentService:
|
||||||
|
service = ContentService(
|
||||||
|
session=mock_session,
|
||||||
|
repository=mock_repository,
|
||||||
|
cache=mock_cache,
|
||||||
|
invalidator=MagicMock(),
|
||||||
|
search_projection_writer=MagicMock(),
|
||||||
|
)
|
||||||
|
return service
|
||||||
|
|
||||||
|
|
||||||
|
def create_mock_module(status: ModuleStatus, is_active: bool = False) -> MagicMock:
|
||||||
|
module = MagicMock()
|
||||||
|
module.id = uuid4()
|
||||||
|
module.code = "MOD_TEST"
|
||||||
|
module.slug = "test-module"
|
||||||
|
module.name = "Test Module"
|
||||||
|
module.status = status
|
||||||
|
module.is_active = is_active
|
||||||
|
module.category = "clinical"
|
||||||
|
module.preview_enabled = False
|
||||||
|
module.preview_item_limit = 0
|
||||||
|
module.default_template_code = "MOD_TEST_T01"
|
||||||
|
return module
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_module_contents_allows_inactive_module(
|
||||||
|
content_service: ContentService,
|
||||||
|
mock_repository: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
inactive_module = create_mock_module(status=ModuleStatus.INACTIVE, is_active=False)
|
||||||
|
mock_repository.resolve_module = AsyncMock(return_value=inactive_module)
|
||||||
|
mock_repository.get_module_contents = AsyncMock(return_value=[])
|
||||||
|
|
||||||
|
req = ModuleRequest(module_code="MOD_TEST")
|
||||||
|
res = await content_service.get_module_contents(request=req)
|
||||||
|
|
||||||
|
mock_repository.resolve_module.assert_awaited_once_with(
|
||||||
|
session=content_service.session,
|
||||||
|
module_id=None,
|
||||||
|
module_code="MOD_TEST",
|
||||||
|
active_only=False,
|
||||||
|
)
|
||||||
|
assert res.module.code == "MOD_TEST"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_module_contents_rejects_archived_module(
|
||||||
|
content_service: ContentService,
|
||||||
|
mock_repository: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
archived_module = create_mock_module(status=ModuleStatus.ARCHIVED, is_active=False)
|
||||||
|
mock_repository.resolve_module = AsyncMock(return_value=archived_module)
|
||||||
|
|
||||||
|
req = ModuleRequest(module_code="MOD_TEST")
|
||||||
|
with pytest.raises(HTTPException) as exc_info:
|
||||||
|
await content_service.get_module_contents(request=req)
|
||||||
|
|
||||||
|
assert exc_info.value.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_module_search_contract_allows_inactive_module(
|
||||||
|
content_service: ContentService,
|
||||||
|
mock_repository: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
inactive_module = create_mock_module(status=ModuleStatus.INACTIVE, is_active=False)
|
||||||
|
mock_repository.resolve_module = AsyncMock(return_value=inactive_module)
|
||||||
|
mock_repository.get_module_search_definitions = AsyncMock(return_value=[])
|
||||||
|
|
||||||
|
res = await content_service.get_module_search_contract(module_code="MOD_TEST")
|
||||||
|
|
||||||
|
mock_repository.resolve_module.assert_awaited_once_with(
|
||||||
|
session=content_service.session,
|
||||||
|
module_code="MOD_TEST",
|
||||||
|
active_only=False,
|
||||||
|
)
|
||||||
|
assert res.module_code == "MOD_TEST"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_search_contents_allows_inactive_module(
|
||||||
|
content_service: ContentService,
|
||||||
|
mock_repository: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
inactive_module = create_mock_module(status=ModuleStatus.INACTIVE, is_active=False)
|
||||||
|
mock_repository.resolve_module = AsyncMock(return_value=inactive_module)
|
||||||
|
|
||||||
|
mock_def_set = MagicMock()
|
||||||
|
mock_def_set.id = uuid4()
|
||||||
|
mock_def_set.code = "MOD_TEST_T01"
|
||||||
|
mock_repository.resolve_definition_set = AsyncMock(return_value=mock_def_set)
|
||||||
|
mock_repository.assert_template_has_module_content = AsyncMock()
|
||||||
|
mock_repository.get_active_presentation = AsyncMock()
|
||||||
|
mock_repository.load_active_definitions = AsyncMock(return_value=[])
|
||||||
|
mock_repository.search = AsyncMock(return_value=(0, []))
|
||||||
|
mock_repository.build_attributes_for_content_ids = AsyncMock(return_value={})
|
||||||
|
mock_repository.get_content_media_for_content_ids = AsyncMock(return_value={})
|
||||||
|
|
||||||
|
content_service._derive_module_code = MagicMock(return_value="MOD_TEST")
|
||||||
|
|
||||||
|
req = ContentSearchRequest(module_code="MOD_TEST", template_code="MOD_TEST_T01")
|
||||||
|
res = await content_service.search_contents(req)
|
||||||
|
|
||||||
|
mock_repository.resolve_module.assert_awaited_once_with(
|
||||||
|
session=content_service.session,
|
||||||
|
module_id=None,
|
||||||
|
module_code="MOD_TEST",
|
||||||
|
active_only=False,
|
||||||
|
)
|
||||||
|
assert res.total == 0
|
||||||
|
assert res.items == []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_search_contents_rejects_archived_module(
|
||||||
|
content_service: ContentService,
|
||||||
|
mock_repository: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
archived_module = create_mock_module(status=ModuleStatus.ARCHIVED, is_active=False)
|
||||||
|
mock_repository.resolve_module = AsyncMock(return_value=archived_module)
|
||||||
|
|
||||||
|
req = ContentSearchRequest(module_code="MOD_TEST", template_code="MOD_TEST_T01")
|
||||||
|
with pytest.raises(ModuleNotFoundError):
|
||||||
|
await content_service.search_contents(req)
|
||||||
Loading…
Reference in New Issue