Skip to content

Commit 51b8a57

Browse files
committed
MPT-16512 E2E for Notification batch attachments
1 parent 359d60e commit 51b8a57

6 files changed

Lines changed: 73 additions & 20 deletions

File tree

e2e_config.test.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"commerce.subscription.agreement.id": "AGR-2473-3299-1721",
4747
"commerce.subscription.id": "SUB-3678-1831-2188",
4848
"commerce.subscription.product.item.id": "ITM-1767-7355-0001",
49+
"notifications.batch.attachment.id": "ATT-7183-1965-7758",
4950
"notifications.batch.id": "MST-3638-2460-4825",
5051
"notifications.category.id": "NTC-6157-0397",
5152
"notifications.subscriber.id": "NTS-0829-7123-7123",

mpt_api_client/resources/notifications/batches.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class BatchesService(
3333
):
3434
"""Notifications Batches service."""
3535

36-
def get_batch_attachment(self, batch_id: str, attachment_id: str) -> FileModel:
37-
"""Get batch attachment.
36+
def download_attachment(self, batch_id: str, attachment_id: str) -> FileModel:
37+
"""Download batch attachment.
3838
3939
Args:
4040
batch_id: Batch ID.
@@ -59,8 +59,8 @@ class AsyncBatchesService(
5959
):
6060
"""Async Notifications Batches service."""
6161

62-
async def get_batch_attachment(self, batch_id: str, attachment_id: str) -> FileModel:
63-
"""Get batch attachment.
62+
async def download_attachment(self, batch_id: str, attachment_id: str) -> FileModel:
63+
"""Download batch attachment.
6464
6565
Args:
6666
batch_id: Batch ID.

tests/e2e/notifications/batch/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ def batch_data(category_id, short_uuid):
2424
"body": "Hello world",
2525
"contacts": [{"email": f"{short_uuid}@example.com"}],
2626
}
27+
28+
29+
@pytest.fixture
30+
def batch_attachment_id(e2e_config):
31+
return e2e_config["notifications.batch.attachment.id"]

tests/e2e/notifications/batch/test_async_batches.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ async def test_create_batch(async_batch_service, batch_data):
1111

1212

1313
async def test_get_batch(async_batch_service, batch_id):
14-
result = await async_batch_service.get(batch_id)
14+
result = await async_batch_service.get(batch_id, select=["attachments"])
1515

1616
assert result.id == batch_id
1717

@@ -30,7 +30,7 @@ async def test_create_batch_with_file(async_batch_service, batch_data, logo_fd):
3030
assert result is not None
3131

3232

33-
@pytest.mark.skip(reason="Batches attachments not implemented")
34-
async def test_download_attachment():
35-
# TODO - Implement get and download E2E tests for attachments
36-
raise NotImplementedError
33+
async def test_download_attachment(async_batch_service, batch_id, batch_attachment_id):
34+
result = await async_batch_service.download_attachment(batch_id, batch_attachment_id)
35+
36+
assert result.filename == "logo.png"

tests/e2e/notifications/batch/test_batches.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def test_create_batch(batch_service, batch_data):
1111

1212

1313
def test_get_batch(batch_service, batch_id):
14-
result = batch_service.get(batch_id)
14+
result = batch_service.get(batch_id, select=["attachments"])
1515

1616
assert result.id == batch_id
1717

@@ -30,7 +30,7 @@ def test_create_batch_with_file(batch_service, batch_data, logo_fd):
3030
assert result is not None
3131

3232

33-
@pytest.mark.skip(reason="Batches attachments not implemented") # noqa: AAA01
34-
def test_download_attachment():
35-
# TODO - Implement get and download E2E tests for attachment
36-
raise NotImplementedError
33+
def test_download_attachment(batch_service, batch_id, batch_attachment_id):
34+
result = batch_service.download_attachment(batch_id, batch_attachment_id)
35+
36+
assert result.filename == "logo.png"
Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,77 @@
11
import pytest
22

3+
from mpt_api_client.http import AsyncHTTPClient, HTTPClient
4+
from mpt_api_client.http.types import Response
5+
from mpt_api_client.models import FileModel
36
from mpt_api_client.resources.notifications.batches import (
47
AsyncBatchesService,
58
BatchesService,
69
)
710

811

912
@pytest.fixture
10-
def batches_service(http_client):
13+
def batches_service(http_client: HTTPClient) -> BatchesService:
1114
return BatchesService(http_client=http_client)
1215

1316

1417
@pytest.fixture
15-
def async_batches_service(async_http_client):
18+
def async_batches_service(async_http_client: AsyncHTTPClient) -> AsyncBatchesService:
1619
return AsyncBatchesService(http_client=async_http_client)
1720

1821

19-
@pytest.mark.parametrize("method", ["get", "create", "iterate", "get_batch_attachment"])
20-
def test_sync_batches_service_methods(batches_service, method):
22+
@pytest.fixture
23+
def attachment_response() -> Response:
24+
return Response(
25+
headers={"content-disposition": 'attachment; filename="test.csv"'},
26+
status_code=200,
27+
content=b"content",
28+
)
29+
30+
31+
@pytest.mark.parametrize("method", ["get", "create", "iterate", "download_attachment"])
32+
def test_sync_batches_service_methods(batches_service: BatchesService, method: str) -> None:
2133
result = hasattr(batches_service, method)
2234

2335
assert result is True
2436

2537

26-
@pytest.mark.parametrize("method", ["get", "create", "iterate", "get_batch_attachment"])
27-
def test_async_batches_service_methods(async_batches_service, method):
38+
@pytest.mark.parametrize("method", ["get", "create", "iterate", "download_attachment"])
39+
def test_async_batches_service_methods(
40+
async_batches_service: AsyncBatchesService, method: str
41+
) -> None:
2842
result = hasattr(async_batches_service, method)
2943

3044
assert result is True
45+
46+
47+
def test_download_attachment(mocker, batches_service, attachment_response):
48+
batch_id = "B-123"
49+
attachment_id = "A-123"
50+
mock_request = mocker.patch.object(
51+
batches_service.http_client, "request", return_value=attachment_response
52+
)
53+
54+
result = batches_service.download_attachment(batch_id, attachment_id)
55+
56+
assert isinstance(result, FileModel)
57+
mock_request.assert_called_once_with(
58+
"get", f"/public/v1/notifications/batches/{batch_id}/attachments/{attachment_id}"
59+
)
60+
61+
62+
async def test_async_download_attachment( # noqa: WPS210
63+
mocker, async_batches_service, attachment_response
64+
):
65+
batch_id = "B-123"
66+
attachment_id = "A-123"
67+
mock_request = mocker.patch.object(
68+
async_batches_service.http_client, "request", return_value=attachment_response
69+
)
70+
71+
result = await async_batches_service.download_attachment(batch_id, attachment_id)
72+
73+
assert isinstance(result, FileModel)
74+
75+
mock_request.assert_called_once_with(
76+
"get", f"/public/v1/notifications/batches/{batch_id}/attachments/{attachment_id}"
77+
)

0 commit comments

Comments
 (0)