|
| 1 | +from http import HTTPStatus |
| 2 | +from uuid import UUID |
| 3 | + |
| 4 | +import httpx |
| 5 | +import pytest |
| 6 | +from respx.router import MockRouter |
| 7 | + |
| 8 | +from pybotx import ( |
| 9 | + Bot, |
| 10 | + BotAccountWithSecret, |
| 11 | + HandlerCollector, |
| 12 | + ThreadCreationError, |
| 13 | + ThreadCreationEventNotFoundError, |
| 14 | + ThreadCreationProhibitedError, |
| 15 | + lifespan_wrapper, |
| 16 | +) |
| 17 | + |
| 18 | +pytestmark = [ |
| 19 | + pytest.mark.asyncio, |
| 20 | + pytest.mark.mock_authorization, |
| 21 | + pytest.mark.usefixtures("respx_mock"), |
| 22 | +] |
| 23 | + |
| 24 | +ENDPOINT = "api/v3/botx/chats/create_thread" |
| 25 | + |
| 26 | + |
| 27 | +async def test__create_chat__succeed( |
| 28 | + respx_mock: MockRouter, |
| 29 | + host: str, |
| 30 | + bot_id: UUID, |
| 31 | + bot_account: BotAccountWithSecret, |
| 32 | +) -> None: |
| 33 | + # - Arrange - |
| 34 | + sync_id = "21a9ec9e-f21f-4406-ac44-1a78d2ccf9e3" |
| 35 | + thread_id = "2a8c0d1e-c4d1-4308-b024-6e1a9f4a4b6d" |
| 36 | + endpoint = respx_mock.post( |
| 37 | + f"https://{host}/{ENDPOINT}", |
| 38 | + headers={"Authorization": "Bearer token", "Content-Type": "application/json"}, |
| 39 | + json={"sync_id": sync_id}, |
| 40 | + ).mock( |
| 41 | + return_value=httpx.Response( |
| 42 | + HTTPStatus.OK, |
| 43 | + json={ |
| 44 | + "status": "ok", |
| 45 | + "result": {"thread_id": thread_id}, |
| 46 | + }, |
| 47 | + ), |
| 48 | + ) |
| 49 | + |
| 50 | + built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account]) |
| 51 | + |
| 52 | + # - Act - |
| 53 | + async with lifespan_wrapper(built_bot) as bot: |
| 54 | + created_thread_id = await bot.create_thread(bot_id=bot_id, sync_id=UUID(sync_id)) |
| 55 | + |
| 56 | + # - Assert - |
| 57 | + assert str(created_thread_id) == thread_id |
| 58 | + assert endpoint.called |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.parametrize( |
| 62 | + "return_json, response_status, expected_exc_type", |
| 63 | + ( |
| 64 | + ( |
| 65 | + { |
| 66 | + "status": "error", |
| 67 | + "reason": "thread_creation_is_prohibited", |
| 68 | + "errors": ["This bot is not allowed to create thread"], |
| 69 | + "error_data": {"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46"}, |
| 70 | + }, |
| 71 | + HTTPStatus.FORBIDDEN, |
| 72 | + ThreadCreationProhibitedError, |
| 73 | + ), |
| 74 | + ( |
| 75 | + { |
| 76 | + "status": "error", |
| 77 | + "reason": "threads_not_enabled", |
| 78 | + "errors": ["Threads not enabled for this chat"], |
| 79 | + "error_data": {"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46"}, |
| 80 | + }, |
| 81 | + HTTPStatus.FORBIDDEN, |
| 82 | + ThreadCreationProhibitedError, |
| 83 | + ), |
| 84 | + ( |
| 85 | + { |
| 86 | + "status": "error", |
| 87 | + "reason": "bot_is_not_a_chat_member", |
| 88 | + "errors": ["This bot is not a chat member"], |
| 89 | + "error_data": {"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46"}, |
| 90 | + }, |
| 91 | + HTTPStatus.FORBIDDEN, |
| 92 | + ThreadCreationProhibitedError, |
| 93 | + ), |
| 94 | + ( |
| 95 | + { |
| 96 | + "status": "error", |
| 97 | + "reason": "can_not_create_for_personal_chat", |
| 98 | + "errors": ["This is personal chat"], |
| 99 | + "error_data": {"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46"}, |
| 100 | + }, |
| 101 | + HTTPStatus.FORBIDDEN, |
| 102 | + ThreadCreationProhibitedError, |
| 103 | + ), |
| 104 | + ( |
| 105 | + { |
| 106 | + "status": "error", |
| 107 | + "reason": "unsupported_event_type", |
| 108 | + "errors": ["This event type is unsupported"], |
| 109 | + "error_data": {"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46"}, |
| 110 | + }, |
| 111 | + HTTPStatus.FORBIDDEN, |
| 112 | + ThreadCreationProhibitedError, |
| 113 | + ), |
| 114 | + ( |
| 115 | + { |
| 116 | + "status": "error", |
| 117 | + "reason": "unsupported_chat_type", |
| 118 | + "errors": ["This chat type is unsupported"], |
| 119 | + "error_data": {"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46"}, |
| 120 | + }, |
| 121 | + HTTPStatus.FORBIDDEN, |
| 122 | + ThreadCreationProhibitedError, |
| 123 | + ), |
| 124 | + ( |
| 125 | + { |
| 126 | + "status": "error", |
| 127 | + "reason": "thread_already_created", |
| 128 | + "errors": ["Thread already created"], |
| 129 | + "error_data": {"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46"}, |
| 130 | + }, |
| 131 | + HTTPStatus.FORBIDDEN, |
| 132 | + ThreadCreationProhibitedError, |
| 133 | + ), |
| 134 | + ( |
| 135 | + { |
| 136 | + "status": "error", |
| 137 | + "reason": "no_access_for_message", |
| 138 | + "errors": ["There is no access for this message"], |
| 139 | + "error_data": {"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46"}, |
| 140 | + }, |
| 141 | + HTTPStatus.FORBIDDEN, |
| 142 | + ThreadCreationProhibitedError, |
| 143 | + ), |
| 144 | + ( |
| 145 | + { |
| 146 | + "status": "error", |
| 147 | + "reason": "event_in_stealth_mode", |
| 148 | + "errors": ["This event is in stealth mode"], |
| 149 | + "error_data": {"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46"}, |
| 150 | + }, |
| 151 | + HTTPStatus.FORBIDDEN, |
| 152 | + ThreadCreationProhibitedError, |
| 153 | + ), |
| 154 | + ( |
| 155 | + { |
| 156 | + "status": "error", |
| 157 | + "reason": "event_already_deleted", |
| 158 | + "errors": ["This event already deleted"], |
| 159 | + "error_data": {"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46"}, |
| 160 | + }, |
| 161 | + HTTPStatus.FORBIDDEN, |
| 162 | + ThreadCreationProhibitedError, |
| 163 | + ), |
| 164 | + ( |
| 165 | + { |
| 166 | + "status": "error", |
| 167 | + "reason": "event_not_found", |
| 168 | + "errors": ["Event not found"], |
| 169 | + "error_data": {"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46"}, |
| 170 | + }, |
| 171 | + HTTPStatus.NOT_FOUND, |
| 172 | + ThreadCreationEventNotFoundError, |
| 173 | + ), |
| 174 | + ( |
| 175 | + { |
| 176 | + "status": "error", |
| 177 | + "reason": "|specified reason|", |
| 178 | + "errors": ["|specified errors|"], |
| 179 | + "error_data": {}, |
| 180 | + }, |
| 181 | + HTTPStatus.UNPROCESSABLE_ENTITY, |
| 182 | + ThreadCreationError, |
| 183 | + ), |
| 184 | + ), |
| 185 | +) |
| 186 | +async def test__create_thread__botx_error_raised( |
| 187 | + respx_mock: MockRouter, |
| 188 | + host: str, |
| 189 | + bot_id: UUID, |
| 190 | + bot_account: BotAccountWithSecret, |
| 191 | + return_json, |
| 192 | + response_status, |
| 193 | + expected_exc_type, |
| 194 | +) -> None: |
| 195 | + # - Arrange - |
| 196 | + sync_id = "21a9ec9e-f21f-4406-ac44-1a78d2ccf9e3" |
| 197 | + endpoint = respx_mock.post( |
| 198 | + f"https://{host}/{ENDPOINT}", |
| 199 | + headers={"Authorization": "Bearer token", "Content-Type": "application/json"}, |
| 200 | + json={"sync_id": sync_id}, |
| 201 | + ).mock(return_value=httpx.Response(response_status, json=return_json)) |
| 202 | + |
| 203 | + built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account]) |
| 204 | + |
| 205 | + # - Act - |
| 206 | + async with lifespan_wrapper(built_bot) as bot: |
| 207 | + with pytest.raises(expected_exc_type) as exc: |
| 208 | + await bot.create_thread( |
| 209 | + bot_id=bot_id, sync_id=UUID(sync_id) |
| 210 | + ) |
| 211 | + |
| 212 | + # - Assert - |
| 213 | + assert endpoint.called |
| 214 | + assert return_json["reason"] in str(exc.value) |
0 commit comments