~aleteoryx/muditaos

ref: b64eb0622b6c5e59eeb2161223e793ba4164ceb9 muditaos/test/pytest/service-desktop/test_messages.py -rw-r--r-- 6.4 KiB
b64eb062 — Pawel Olejniczak [EGD-5783] Exclude empty body from response message 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

import pytest
from harness.interface.defs import status


MESSAGES_PAGE_SIZE = 4


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("usb_unlocked")
def test_get_messages_without_pagination(harness):
    limit = MESSAGES_PAGE_SIZE - 1
    get_body = {"category": "message", "limit": limit, "offset": 0}
    ret = harness.endpoint_request("messages", "get", get_body)
    assert ret["status"] == status["OK"]

    messages = ret["body"]["entries"]
    messages_count = len(messages)
    assert messages_count == limit


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("usb_unlocked")
def test_get_messages_with_pagination(harness):
    limit = MESSAGES_PAGE_SIZE + 1
    body = {"category": "message", "limit": limit, "offset": 0}
    ret = harness.endpoint_request("messages", "get", body)
    assert ret["status"] == status["OK"]

    messages = ret["body"]["entries"]
    messages_count = len(messages)
    assert messages_count == MESSAGES_PAGE_SIZE


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("usb_unlocked")
def test_get_all_messages(harness):
    # get messages count
    body = {"category": "message", "count": True}
    ret = harness.endpoint_request("messages", "get", body)
    assert ret["status"] == status["OK"]

    count = ret["body"]["count"]
    assert count > 0

    # get first messages page
    body = {"category": "message", "limit": count, "offset": 0}
    ret = harness.endpoint_request("messages", "get", body)

    assert ret["status"] == status["OK"]
    assert count == ret["body"]["totalCount"]

    all_messages = ret["body"]["entries"]
    if count > MESSAGES_PAGE_SIZE:

        # get rest of the messages
        while MESSAGES_PAGE_SIZE == len(ret["body"]["entries"]):

            if "nextPage" in ret["body"]:

                offset = ret["body"]["nextPage"]["offset"]
                messages_left_count =  count - len(all_messages)

                body = {"category": "message", "limit": messages_left_count, "offset": offset}
                ret = harness.endpoint_request("messages", "get", body)

                assert ret["status"] == status["OK"]

                all_messages += ret["body"]["entries"]

            else:
                break

    assert count == len(all_messages)


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("usb_unlocked")
def test_get_messages_by_thread_id_without_pagination(harness):
    thread_id = 1
    limit = MESSAGES_PAGE_SIZE - 1
    body = {"category": "message", "threadID": thread_id, "limit": limit, "offset": 0}
    ret = harness.endpoint_request("messages", "get", body)
    assert ret["status"] == status["OK"]

    messages = ret["body"]["entries"]
    messages_count = len(messages)
    assert messages_count == limit
    for message in ret["body"]["entries"]:
        assert message["threadID"] == thread_id


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("usb_unlocked")
def test_get_messages_by_thread_id_with_pagination(harness):
    thread_id = 1
    limit = MESSAGES_PAGE_SIZE + 1
    body = {"category": "message", "threadID": thread_id, "limit": limit, "offset": 0}
    ret = harness.endpoint_request("messages", "get", body)
    assert ret["status"] == status["OK"]

    messages = ret["body"]["entries"]
    messages_count = len(messages)
    assert messages_count == MESSAGES_PAGE_SIZE
    for message in ret["body"]["entries"]:
        assert message["threadID"] == thread_id


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("usb_unlocked")
def test_get_all_messages_by_thread_id(harness):
    thread_id = 1
    # get messages count
    body = {"category": "message", "count": True}
    ret = harness.endpoint_request("messages", "get", body)
    assert ret["status"] == status["OK"]

    messages_count = ret["body"]["count"]
    assert messages_count > 0

    # get first messages page
    body = {"category": "message", "threadID": thread_id, "limit": messages_count, "offset": 0}
    ret = harness.endpoint_request("messages", "get", body)

    assert ret["status"] == status["OK"]
    messages_by_thread_id_count = ret["body"]["totalCount"]

    all_messages = ret["body"]["entries"]
    if messages_by_thread_id_count > MESSAGES_PAGE_SIZE:

        # get rest of the messages
        while MESSAGES_PAGE_SIZE == len(ret["body"]["entries"]):

            if "nextPage" in ret["body"]:

                offset = ret["body"]["nextPage"]["offset"]
                messages_left_count =  messages_by_thread_id_count - len(all_messages)

                body = {"category": "message", "threadID": thread_id, "limit": messages_left_count, "offset": offset}
                ret = harness.endpoint_request("messages", "get", body)

                assert ret["status"] == status["OK"]

                all_messages += ret["body"]["entries"]

            else:
                break

    assert messages_by_thread_id_count == len(all_messages)


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("usb_unlocked")
def test_remove_message(harness):
    # get messages count
    body = {"category": "message", "count": True}
    ret = harness.endpoint_request("messages", "get", body)
    assert ret["status"] == status["OK"]

    count = ret["body"]["count"]
    assert count > 0

    # get message
    number_of_requested_messages = 1
    body = {"category": "message", "limit": number_of_requested_messages, "offset": 0}
    ret = harness.endpoint_request("messages", "get", body)
    assert ret["status"] == status["OK"]

    messages = ret["body"]["entries"]
    messages_count = len(messages)
    assert messages_count == number_of_requested_messages

    # remove message
    sms_to_remove = messages[0]
    body = {"category": "message", "messageID": sms_to_remove["messageID"]}
    ret = harness.endpoint_request("messages", "del", body)
    assert ret["status"] == status["NoContent"]

    # get messages count again
    body = {"category": "message", "count": True}
    ret = harness.endpoint_request("messages", "get", body)
    assert ret["status"] == status["OK"]
    assert ret["body"]["count"] == count - 1


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("usb_unlocked")
def test_get_message_by_id(harness):
    message_id = 1
    get_body = {"category": "message", "messageID": message_id}
    ret = harness.endpoint_request("messages", "get", get_body)
    assert ret["status"] == status["OK"]
    assert ret["body"]["messageID"] == message_id