feat: 日历分享改为按手机号+好友关系校验
This commit is contained in:
@@ -1,17 +1,18 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from typing import cast
|
||||
from typing import Any, cast
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from core.http.errors import ApiProblemError
|
||||
from core.auth.models import CurrentUser
|
||||
from models.inbox_messages import InboxMessage, InboxMessageType
|
||||
from models.schedule_items import ScheduleItem
|
||||
from schemas.enums import FriendshipStatus
|
||||
from v1.auth.schemas import UserByPhoneResponse
|
||||
from v1.schedule_items.repository import ScheduleItemRepository
|
||||
from v1.schedule_items.schemas import ScheduleItemShareRequest
|
||||
@@ -79,6 +80,14 @@ class AuthGatewayStub:
|
||||
phone_confirmed_at=None,
|
||||
)
|
||||
|
||||
async def get_user_by_id(self, user_id: str) -> UserByPhoneResponse:
|
||||
return UserByPhoneResponse(
|
||||
id=user_id,
|
||||
phone="+8613810000000",
|
||||
created_at="2026-02-28T10:00:00Z",
|
||||
phone_confirmed_at=None,
|
||||
)
|
||||
|
||||
|
||||
class InboxRepoStub:
|
||||
async def create(self, data: dict[str, object]) -> InboxMessage:
|
||||
@@ -127,6 +136,26 @@ class AuthGatewayInvalidIdStub:
|
||||
phone_confirmed_at=None,
|
||||
)
|
||||
|
||||
async def get_user_by_id(self, user_id: str) -> UserByPhoneResponse:
|
||||
return UserByPhoneResponse(
|
||||
id=user_id,
|
||||
phone="+8613810000000",
|
||||
created_at="2026-02-28T10:00:00Z",
|
||||
phone_confirmed_at=None,
|
||||
)
|
||||
|
||||
|
||||
class FriendshipRepoStub:
|
||||
def __init__(self, accepted: bool = True) -> None:
|
||||
self._accepted = accepted
|
||||
|
||||
async def get_friendship_between_users(self, user_id_1: UUID, user_id_2: UUID):
|
||||
if not self._accepted:
|
||||
return None
|
||||
friendship = MagicMock()
|
||||
friendship.status = FriendshipStatus.ACCEPTED
|
||||
return friendship
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_share_forbidden_when_not_owner() -> None:
|
||||
@@ -140,11 +169,12 @@ async def test_share_forbidden_when_not_owner() -> None:
|
||||
),
|
||||
session=AsyncMock(),
|
||||
current_user=CurrentUser(id=requester_id),
|
||||
auth_gateway=AuthGatewayStub(),
|
||||
auth_gateway=cast(Any, AuthGatewayStub()),
|
||||
inbox_repository=InboxRepoStub(),
|
||||
friendship_repository=cast(Any, FriendshipRepoStub()),
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
with pytest.raises(ApiProblemError) as exc_info:
|
||||
await service.share(
|
||||
item_id,
|
||||
ScheduleItemShareRequest(
|
||||
@@ -171,8 +201,9 @@ async def test_share_success_creates_calendar_invitation_message() -> None:
|
||||
),
|
||||
session=session,
|
||||
current_user=CurrentUser(id=owner_id),
|
||||
auth_gateway=AuthGatewayStub(),
|
||||
auth_gateway=cast(Any, AuthGatewayStub()),
|
||||
inbox_repository=InboxRepoStub(),
|
||||
friendship_repository=cast(Any, FriendshipRepoStub()),
|
||||
)
|
||||
|
||||
result = await service.share(
|
||||
@@ -203,11 +234,12 @@ async def test_share_returns_not_found_when_item_missing() -> None:
|
||||
repository=cast(ScheduleItemRepository, ShareRepo(None)),
|
||||
session=AsyncMock(),
|
||||
current_user=CurrentUser(id=requester_id),
|
||||
auth_gateway=AuthGatewayStub(),
|
||||
auth_gateway=cast(Any, AuthGatewayStub()),
|
||||
inbox_repository=InboxRepoStub(),
|
||||
friendship_repository=cast(Any, FriendshipRepoStub()),
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
with pytest.raises(ApiProblemError) as exc_info:
|
||||
await service.share(
|
||||
uuid4(),
|
||||
ScheduleItemShareRequest(
|
||||
@@ -233,11 +265,12 @@ async def test_share_invalid_auth_user_id_returns_503() -> None:
|
||||
),
|
||||
session=session,
|
||||
current_user=CurrentUser(id=owner_id),
|
||||
auth_gateway=AuthGatewayInvalidIdStub(),
|
||||
auth_gateway=cast(Any, AuthGatewayInvalidIdStub()),
|
||||
inbox_repository=InboxRepoStub(),
|
||||
friendship_repository=cast(Any, FriendshipRepoStub()),
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
with pytest.raises(ApiProblemError) as exc_info:
|
||||
await service.share(
|
||||
item_id,
|
||||
ScheduleItemShareRequest(
|
||||
@@ -266,11 +299,12 @@ async def test_share_sqlalchemy_error_rolls_back() -> None:
|
||||
),
|
||||
session=session,
|
||||
current_user=CurrentUser(id=owner_id),
|
||||
auth_gateway=AuthGatewayStub(),
|
||||
auth_gateway=cast(Any, AuthGatewayStub()),
|
||||
inbox_repository=InboxRepoStub(),
|
||||
friendship_repository=cast(Any, FriendshipRepoStub()),
|
||||
)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
with pytest.raises(ApiProblemError) as exc_info:
|
||||
await service.share(
|
||||
item_id,
|
||||
ScheduleItemShareRequest(
|
||||
@@ -284,3 +318,34 @@ async def test_share_sqlalchemy_error_rolls_back() -> None:
|
||||
assert exc_info.value.status_code == 503
|
||||
assert exc_info.value.detail == "Schedule item store unavailable"
|
||||
session.rollback.assert_awaited_once()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_share_returns_forbidden_when_target_is_not_friend() -> None:
|
||||
owner_id = UUID("00000000-0000-0000-0000-000000000001")
|
||||
item_id = uuid4()
|
||||
service = ScheduleItemService(
|
||||
repository=cast(
|
||||
ScheduleItemRepository,
|
||||
ShareRepo(_build_item(item_id=item_id, owner_id=owner_id)),
|
||||
),
|
||||
session=AsyncMock(),
|
||||
current_user=CurrentUser(id=owner_id),
|
||||
auth_gateway=cast(Any, AuthGatewayStub()),
|
||||
inbox_repository=InboxRepoStub(),
|
||||
friendship_repository=cast(Any, FriendshipRepoStub(accepted=False)),
|
||||
)
|
||||
|
||||
with pytest.raises(ApiProblemError) as exc_info:
|
||||
await service.share(
|
||||
item_id,
|
||||
ScheduleItemShareRequest(
|
||||
phone="+8613810000000",
|
||||
permission_view=True,
|
||||
permission_edit=False,
|
||||
permission_invite=False,
|
||||
),
|
||||
)
|
||||
|
||||
assert exc_info.value.status_code == 403
|
||||
assert exc_info.value.detail == "You can only share calendar with accepted friends"
|
||||
|
||||
Reference in New Issue
Block a user