|
| 1 | +"""Integration tests for safety / moderation: block / unblock / list_blocked. |
| 2 | +
|
| 3 | +Uses the secondary test account as the block target so each run is |
| 4 | +self-contained — no hard-coded user IDs. |
| 5 | +
|
| 6 | +Report endpoints are exercised via the unit tests in ``test_client.py`` |
| 7 | +rather than here, because submitting real moderation reports against the |
| 8 | +secondary test account would generate operator-side noise on each run. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import contextlib |
| 14 | + |
| 15 | +from colony_sdk import ColonyAPIError, ColonyClient |
| 16 | + |
| 17 | +from .conftest import raises_status |
| 18 | + |
| 19 | + |
| 20 | +def _target_in_blocked(blocked_response: object, target_id: str) -> bool: |
| 21 | + """Loose check that target_id appears in a list_blocked() response. |
| 22 | +
|
| 23 | + Accepts either ``{items: [...]}`` or a raw list shape, since the exact |
| 24 | + envelope shape is not pinned in the SDK type yet. |
| 25 | + """ |
| 26 | + if isinstance(blocked_response, dict): |
| 27 | + items = blocked_response.get("items") |
| 28 | + members = items if isinstance(items, list) else [] |
| 29 | + elif isinstance(blocked_response, list): |
| 30 | + members = blocked_response |
| 31 | + else: |
| 32 | + members = [] |
| 33 | + for m in members: |
| 34 | + if isinstance(m, dict) and m.get("id") == target_id: |
| 35 | + return True |
| 36 | + if isinstance(m, str) and m == target_id: |
| 37 | + return True |
| 38 | + return False |
| 39 | + |
| 40 | + |
| 41 | +class TestBlockUser: |
| 42 | + """Focused tests for ``block_user`` against the live API.""" |
| 43 | + |
| 44 | + def test_block_user_adds_to_blocked_list(self, client: ColonyClient, second_me: dict) -> None: |
| 45 | + target_id = second_me["id"] |
| 46 | + |
| 47 | + # Best-effort cleanup from a previous failed run. |
| 48 | + with contextlib.suppress(ColonyAPIError): |
| 49 | + client.unblock_user(target_id) |
| 50 | + |
| 51 | + try: |
| 52 | + client.block_user(target_id) |
| 53 | + assert _target_in_blocked(client.list_blocked(), target_id) |
| 54 | + finally: |
| 55 | + with contextlib.suppress(ColonyAPIError): |
| 56 | + client.unblock_user(target_id) |
| 57 | + |
| 58 | + |
| 59 | +class TestListBlocked: |
| 60 | + """Focused tests for ``list_blocked`` against the live API.""" |
| 61 | + |
| 62 | + def test_list_blocked_returns_collection(self, client: ColonyClient) -> None: |
| 63 | + result = client.list_blocked() |
| 64 | + # The endpoint should return either {items: [...]} or a list — both |
| 65 | + # shapes are accepted by the SDK type. Validate it's one of them. |
| 66 | + if isinstance(result, dict): |
| 67 | + assert "items" in result or "total" in result |
| 68 | + else: |
| 69 | + assert isinstance(result, list) |
| 70 | + |
| 71 | + |
| 72 | +class TestUnblockUser: |
| 73 | + """Focused tests for ``unblock_user`` against the live API.""" |
| 74 | + |
| 75 | + def test_unblock_user_removes_from_blocked_list(self, client: ColonyClient, second_me: dict) -> None: |
| 76 | + target_id = second_me["id"] |
| 77 | + |
| 78 | + # Make sure the user is currently blocked. |
| 79 | + with contextlib.suppress(ColonyAPIError): |
| 80 | + client.block_user(target_id) |
| 81 | + |
| 82 | + client.unblock_user(target_id) |
| 83 | + assert not _target_in_blocked(client.list_blocked(), target_id) |
| 84 | + |
| 85 | + |
| 86 | +class TestBlockUnblockRoundTrip: |
| 87 | + def test_block_then_unblock(self, client: ColonyClient, second_me: dict) -> None: |
| 88 | + target_id = second_me["id"] |
| 89 | + |
| 90 | + # Best-effort cleanup from a previous failed run. |
| 91 | + with contextlib.suppress(ColonyAPIError): |
| 92 | + client.unblock_user(target_id) |
| 93 | + |
| 94 | + client.block_user(target_id) |
| 95 | + try: |
| 96 | + blocked = client.list_blocked() |
| 97 | + assert _target_in_blocked(blocked, target_id) |
| 98 | + finally: |
| 99 | + client.unblock_user(target_id) |
| 100 | + |
| 101 | + blocked_after = client.list_blocked() |
| 102 | + assert not _target_in_blocked(blocked_after, target_id) |
| 103 | + |
| 104 | + def test_block_is_idempotent(self, client: ColonyClient, second_me: dict) -> None: |
| 105 | + target_id = second_me["id"] |
| 106 | + |
| 107 | + with contextlib.suppress(ColonyAPIError): |
| 108 | + client.unblock_user(target_id) |
| 109 | + |
| 110 | + try: |
| 111 | + client.block_user(target_id) |
| 112 | + # Second block on the same target should not raise — block is |
| 113 | + # idempotent server-side. |
| 114 | + client.block_user(target_id) |
| 115 | + finally: |
| 116 | + with contextlib.suppress(ColonyAPIError): |
| 117 | + client.unblock_user(target_id) |
| 118 | + |
| 119 | + def test_unblock_when_not_blocked_raises(self, client: ColonyClient, second_me: dict) -> None: |
| 120 | + target_id = second_me["id"] |
| 121 | + |
| 122 | + # Ensure not currently blocked. |
| 123 | + with contextlib.suppress(ColonyAPIError): |
| 124 | + client.unblock_user(target_id) |
| 125 | + |
| 126 | + with raises_status(404, 409): |
| 127 | + client.unblock_user(target_id) |
| 128 | + |
| 129 | + |
| 130 | +class TestReportSmoke: |
| 131 | + """Smoke check that the report_* methods are reachable. |
| 132 | +
|
| 133 | + We intentionally do NOT submit a real report against the secondary |
| 134 | + account in CI — that would generate operator-side moderation noise |
| 135 | + on every run. The unit tests in ``tests/test_api_methods.py`` |
| 136 | + exercise the request construction; this just confirms the methods |
| 137 | + are wired on the live client without invoking them. |
| 138 | + """ |
| 139 | + |
| 140 | + def test_report_methods_are_present_on_live_client(self, client: ColonyClient) -> None: |
| 141 | + assert callable(client.report_user) |
| 142 | + assert callable(client.report_message) |
| 143 | + assert callable(client.report_post) |
| 144 | + assert callable(client.report_comment) |
0 commit comments