~aleteoryx/muditaos

ref: sign_test muditaos/test/pytest/service-desktop/test_templates.py -rw-r--r-- 7.7 KiB
a217eeb3 — Dawid Wojtas [BH-2024] Fix lack of alarm directory after updating software 1 year, 5 months 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Copyright (c) 2017-2022, 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


class TemplatesTester:
    def __init__(self, harness):
        self.harness = harness
        self.template_body = "TEST TEMPLATE BODY"
        self.templates_page_size = 4

    def __get_count(self):
        body = {"category": "template", "count": True}
        ret = self.harness.endpoint_request("messages", "get", body)
        assert ret["status"] == status["OK"]
        return ret["body"]["count"]

    def __add_template(self):
        body = {"category": "template", "templateBody": self.template_body}
        ret = self.harness.endpoint_request("messages", "post", body)
        assert ret["status"] == status["OK"]
        assert type(ret["body"]["templateID"]) == int
        return ret

    def __get_templates(self, limit, offset):
        body = {"category": "template", "limit": limit, "offset": offset}
        return self.harness.endpoint_request("messages", "get", body)

    def __remove_template(self, template_id):
        body = {"category": "template", "templateID": template_id}
        ret = self.harness.endpoint_request("messages", "del", body)
        assert ret["status"] == status["NoContent"]

    def __get_all_templates(self):
        body = {"category": "template", "count": True}
        ret = self.harness.endpoint_request("messages", "get", body)

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

        total_count = ret["body"]["count"]
        offset = 0

        templates = []

        while True:
            ret = self.__get_templates(total_count, offset)
            templates += ret["body"]["entries"]

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

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

                assert "limit" in ret["body"]["nextPage"]

                assert "offset" in ret["body"]["nextPage"]
                offset = ret["body"]["nextPage"]["offset"]

            else:
                break

        return templates

    def __remove_added_templates(self, templates):
        for template in templates:
            if template["templateBody"] == self.template_body:
                self.__remove_template(template["templateID"])

    def test_getting_template_by_id(self):
        template_id = 1
        body = {"category": "template", "templateID": template_id}
        ret = self.harness.endpoint_request("messages", "get", body)

        assert ret["status"] == status["OK"]
        assert ret["body"]["templateID"] == template_id

    def test_getting_templates_without_pagination(self):
        count = self.__get_count()

        number_of_requested_templates = self.templates_page_size - 1
        ret = self.__get_templates(number_of_requested_templates, 0)

        assert ret["status"] == status["OK"]
        assert ret["body"]["totalCount"] == count
        assert len(ret["body"]["entries"]) == number_of_requested_templates
        for template in ret["body"]["entries"]:
            assert type(template["lastUsedAt"]) == int
            assert type(template["templateBody"]) == str
            assert type(template["templateID"]) == int

    def test_changing_template_body(self):
        test_passed = False
        initial_count = self.__get_count()

        self.__add_template()

        templates = self.__get_all_templates()

        for template in templates:

            if template["templateBody"] == self.template_body:
                # Change template
                new_template_body = "NEW TEMPLATE BODY TEST"
                body = {"category": "template", "templateID": template["templateID"], "templateBody": new_template_body}
                ret = self.harness.endpoint_request("messages", "put", body)
                assert ret["status"] == status["NoContent"]

                # and then remove it to clean env
                self.__remove_template(template["templateID"])
                test_passed = True
                break

        # templates count at the end of the test should match initial templates count
        total_count = self.__get_count()
        assert total_count == initial_count
        assert test_passed == True

    def test_changing_template_order(self):
        template_id = 1
        initial_order = 1
        new_order = 9
        body = {"category": "template", "templateID": template_id}
        ret = self.harness.endpoint_request("messages", "get", body)

        assert ret["status"] == status["OK"]
        assert ret["body"]["templateID"] == template_id
        assert ret["body"]["order"] == initial_order

        body = {"category": "template", "templateID": template_id, "order": new_order}
        ret = self.harness.endpoint_request("messages", "put", body)
        assert ret["status"] == status["NoContent"]

        body = {"category": "template", "templateID": template_id}
        ret = self.harness.endpoint_request("messages", "get", body)
        assert ret["status"] == status["OK"]
        assert ret["body"]["templateID"] == template_id
        assert ret["body"]["order"] == new_order

        body = {"category": "template", "templateID": template_id, "order": initial_order}
        ret = self.harness.endpoint_request("messages", "put", body)
        assert ret["status"] == status["NoContent"]

        body = {"category": "template", "templateID": template_id}
        ret = self.harness.endpoint_request("messages", "get", body)
        assert ret["status"] == status["OK"]
        assert ret["body"]["templateID"] == template_id
        assert ret["body"]["order"] == initial_order

    def test_getting_templates_with_pagination(self):
        initial_count = self.__get_count()

        self.__add_template()

        total_count = self.__get_count()
        assert total_count == initial_count + 1

        # adding new templates to make the total count be above templates page size
        expected_templates_count = self.templates_page_size * 3 + 1
        num_of_templates_to_insert = expected_templates_count - total_count

        while 0 < num_of_templates_to_insert:
            self.__add_template()
            num_of_templates_to_insert -= 1

        total_count = self.__get_count()
        assert total_count == expected_templates_count

        templates = self.__get_all_templates()

        self.__remove_added_templates(templates)

        # templates count at the end of the test should match initial templates count
        total_count = self.__get_count()
        assert total_count == initial_count


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_unlocked")
def test_get_template_by_id(harness):
    templates_tester = TemplatesTester(harness)
    templates_tester.test_getting_template_by_id()


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_unlocked")
def test_get_templates_without_pagination(harness):
    templates_tester = TemplatesTester(harness)
    templates_tester.test_getting_templates_without_pagination()


@pytest.mark.rt1051
@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_unlocked")
def test_change_template_text(harness):
    templates_tester = TemplatesTester(harness)
    templates_tester.test_changing_template_body()


@pytest.mark.rt1051
@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_unlocked")
def test_change_template_order(harness):
    templates_tester = TemplatesTester(harness)
    templates_tester.test_changing_template_order()


@pytest.mark.rt1051
@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_unlocked")
def test_get_templates_with_pagination(harness):
    templates_tester = TemplatesTester(harness)
    templates_tester.test_getting_templates_with_pagination()