1import pytest
2
3from threema.gateway import e2e
4
5
6class TestCallback:
7    @pytest.mark.asyncio
8    def test_invalid_message(self, connection, callback_send, raw_message):
9        outgoing = raw_message(
10            connection=connection,
11            to_id=pytest.msgapi.id,
12            nonce=b'0' * 24,
13            message=b'1' * 200
14        )
15        response = yield from callback_send(outgoing)
16        assert response.status == 400
17        yield from response.release()
18
19    @pytest.mark.asyncio
20    def test_delivery_receipt(self, connection, callback_send, callback_receive):
21        outgoing = e2e.DeliveryReceipt(
22            connection=connection,
23            to_id=pytest.msgapi.id,
24            receipt_type=e2e.DeliveryReceipt.ReceiptType.read,
25            message_ids=[b'0' * 8, b'1' * 8],
26        )
27        response = yield from callback_send(outgoing)
28        yield from response.release()
29        incoming = yield from callback_receive()
30        assert outgoing.from_id == incoming.from_id
31        assert outgoing.to_id == incoming.to_id
32        assert outgoing.receipt_type == incoming.receipt_type
33        assert outgoing.message_ids == incoming.message_ids
34
35    @pytest.mark.asyncio
36    def test_text_message(self, connection, callback_send, callback_receive):
37        outgoing = e2e.TextMessage(
38            connection,
39            to_id=pytest.msgapi.id,
40            text='私はガラスを食べられます。それは私を傷つけません。!',
41        )
42        response = yield from callback_send(outgoing)
43        yield from response.release()
44        incoming = yield from callback_receive()
45        assert outgoing.from_id == incoming.from_id
46        assert outgoing.to_id == incoming.to_id
47        assert outgoing.text == incoming.text
48
49    @pytest.mark.asyncio
50    def test_image_message(self, connection, callback_send, callback_receive, server):
51        outgoing = e2e.ImageMessage(
52            connection,
53            to_id=pytest.msgapi.id,
54            image_path=server.threema_jpg,
55        )
56        response = yield from callback_send(outgoing)
57        yield from response.release()
58        incoming = yield from callback_receive()
59        assert outgoing.from_id == incoming.from_id
60        assert outgoing.to_id == incoming.to_id
61        assert outgoing.image == incoming.image
62
63    @pytest.mark.asyncio
64    def test_file_message(self, connection, callback_send, callback_receive, server):
65        outgoing = e2e.FileMessage(
66            connection,
67            to_id=pytest.msgapi.id,
68            file_path=server.threema_jpg,
69        )
70        response = yield from callback_send(outgoing)
71        yield from response.release()
72        incoming = yield from callback_receive()
73        assert outgoing.from_id == incoming.from_id
74        assert outgoing.to_id == incoming.to_id
75        assert outgoing.file_content == incoming.file_content
76
77    @pytest.mark.asyncio
78    def test_file_message_thumb(
79            self, connection, callback_send, callback_receive, server
80    ):
81        outgoing = e2e.FileMessage(
82            connection,
83            to_id=pytest.msgapi.id,
84            file_path=server.threema_jpg,
85            thumbnail_path=server.threema_jpg,
86        )
87        response = yield from callback_send(outgoing)
88        yield from response.release()
89        incoming = yield from callback_receive()
90        assert outgoing.from_id == incoming.from_id
91        assert outgoing.to_id == incoming.to_id
92        assert outgoing.file_content == incoming.file_content
93        assert outgoing.thumbnail_content == incoming.thumbnail_content
94