~aleteoryx/muditaos

ref: sign_test muditaos/test/pytest/test_send_message.py -rw-r--r-- 18.0 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
import time
import pytest
import copy

from harness.interface.defs import key_codes, SMSType, status
from harness.interface.CDCSerial import Keytype

# time for  message to leave the sending queue
extra_time_to_send_message  = 50

def check_for_sent(old_messages,harness, sms_text, phone_number,timeout):
    local_timeout = copy.deepcopy(timeout)
    while local_timeout != 0:

        new_messages = get_message_by_text(harness, sms_text, str(phone_number))
        diff_messages = []

        for message in new_messages:
            if message not in old_messages:
                diff_messages.append(message)

        if len(diff_messages) > 0 and SMSType(diff_messages[0]["messageType"]) == SMSType.OUTBOX:
            return diff_messages
        local_timeout -=1
        time.sleep(1)
    pytest.fail("Message send timeout!")

def erase_all_templates(harness):
    # getting the templates count
    body = {"category": "template", "count": True}
    ret = harness.endpoint_request("messages", "get", body)
    assert ret["status"] == status["OK"]
    count = ret["body"]["count"]

    # getting all templates
    body = {"category": "template", "limit": count, "offset": 0}
    ret = harness.endpoint_request("messages", "get", body)
    assert ret["status"] == status["OK"]

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

    for template in ret["body"]["entries"]:
        body = {"category": "template", "templateID": template["templateID"]}
        del_res = harness.endpoint_request("messages", "del", body)
        assert del_res["status"] == status["NoContent"]


def add_new_template(harness, template_text: str):
    # adding new template
    body = {"category": "template", "templateBody": template_text}
    ret = harness.endpoint_request("messages", "post", body)
    assert ret["status"] == status["NoContent"]


def get_all_contacts(harness):
    body = {"count": True}
    ret = harness.endpoint_request("contacts", "get", body)

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

    contacts = []
    count = ret["body"]["count"]
    if count == 0:
        return contacts

    # try to get more than available
    batch_size = 30
    divider = int((count + 10) / batch_size)
    reminder = (count + 10) % batch_size

    for i in range(divider):
        body = {"count": batch_size, "offset": batch_size * i}
        ret = harness.endpoint_request("contacts", "get", body)
        assert ret["status"] == status["OK"]
        contacts = contacts + ret["body"]["entries"]

    body = {"count": reminder, "offset": (count + 10) - reminder}
    ret = harness.endpoint_request("contacts", "get", body)
    assert ret["status"] == status["OK"]
    contacts = contacts + ret["body"]["entries"]
    return contacts


def erase_contacts_by_name(harness, name):
    contacts = get_all_contacts(harness)

    # collecting contacts to remove by name
    ids = []
    for contact in contacts:
        if name in contact["priName"]:
            ids.append(contact["id"])

    # erase all contacts by id
    for identifier in ids:
        # removing added contact
        body = {"id": identifier}
        ret = harness.endpoint_request("contacts", "del", body)
        assert ret["status"] == status["NoContent"]


def erase_contacts_by_phone_number(harness, phone_number):
    contacts = get_all_contacts(harness)

    # collecting contacts to remove by phone number
    ids = []
    for contact in contacts:
        numbers = contact["numbers"]
        for number in numbers:
            if number == phone_number:
                ids.append(contact["id"])
                break

    # erase all contacts by id
    for identifier in ids:
        # removing added contact
        body = {"id": identifier}
        ret = harness.endpoint_request("contacts", "del", body)
        assert ret["status"] == status["NoContent"]


def get_message_by_text(harness, message: str, phone_number: str):
    body = {"category": "message", "messageBody": message, "phoneNumber": phone_number}
    return harness.endpoint_request("messages", "get", body)["body"]


# default sms type is draft
def prepare_sms(harness, message: str, phone_number: str, sms_type: int = 1):
    body = {"smsCommand": "smsAdd", "messageBody": message, "phoneNumber": phone_number, "messageType": sms_type}
    return harness.endpoint_request("developerMode", "put", body)


def prepare_sms_template(harness, message: str, phone_number: str):
    body = {"template": True, "messageBody": message, "phoneNumber": phone_number}
    return harness.endpoint_request("developerMode", "post", body)


def compare_messages(old_messages, new_messages, sms_type: SMSType = SMSType.OUTBOX):
    diff_messages = []

    for message in new_messages:
        if message not in old_messages:
            diff_messages.append(message)

    assert len(diff_messages) == 1
    assert SMSType(diff_messages[0]["messageType"]) == sms_type
    return diff_messages


def enter_messages_menu(harness):
    harness.connection.send_key_code(key_codes["enter"])
    harness.open_application("messages")
    if harness.get_application_name() != "ApplicationMessages":
        time.sleep(2)
        assert harness.get_application_name() == "ApplicationMessages"


def enter_contacts_menu(harness):
    harness.connection.send_key_code(key_codes["enter"])
    harness.open_application("contacts")
    if harness.get_application_name() != "ApplicationPhonebook":
        time.sleep(2)
        assert harness.get_application_name() == "ApplicationPhonebook"


def remove_added_messages(harness, diff_messages):
    for  message in diff_messages:
        body = {"category": "message", "messageID": message["messageID"]}
        del_res = harness.endpoint_request("messages", "del", body)
        assert del_res["status"] == status["NoContent"]


@pytest.mark.rt1051
@pytest.mark.usefixtures("phone_unlocked")
@pytest.mark.usefixtures("phone_in_desktop")
def test_send_message(harness, phone_number, sms_text):
    old_messages = get_message_by_text(harness, sms_text, str(phone_number))

    # enter menu
    enter_messages_menu(harness)

    # create new message
    harness.connection.send_key_code(key_codes["left"])
    # enter phone number
    harness.send_number(str(phone_number))
    # move down to message body
    harness.connection.send_key_code(key_codes["down"])
    # write a message
    harness.send_text(sms_text)
    # send
    harness.connection.send_key_code(key_codes["enter"])
    # go back to main screen
    for _ in range(3):
        time.sleep(1.2)  # it take horrendous amount of time to go back to thread view
        harness.connection.send_key_code(key_codes["fnRight"])

    time.sleep(3)

    # check if we back to ApplicationDesktop
    assert harness.get_application_name() == "ApplicationDesktop"

    diff_messages = check_for_sent(old_messages,harness,sms_text,phone_number,extra_time_to_send_message)
    # cleaning
    remove_added_messages(harness,diff_messages)



@pytest.mark.rt1051
@pytest.mark.usefixtures("phone_unlocked")
@pytest.mark.usefixtures("phone_in_desktop")
def test_send_prepared_message(harness, phone_number, sms_text, clean = True):
    old_messages = get_message_by_text(harness, sms_text, str(phone_number))

    # prepare sms and send it (add sms to sending queue)
    prepare_sms(harness, sms_text, str(phone_number), SMSType.QUEUED.value)
    # time to send message
    time.sleep(3)

    diff_messages = check_for_sent(old_messages, harness, sms_text, phone_number, extra_time_to_send_message)

    # cleaning
    if(clean == True):
        remove_added_messages(harness,diff_messages)
    return diff_messages




testdata = [
    "'\"\\àśżżńú😚",
    "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
    "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
    "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA😚"
]


@pytest.mark.rt1051
@pytest.mark.usefixtures("phone_unlocked")
@pytest.mark.parametrize("sms_text", testdata, ids=["special_characters", "long_text"])
def test_send_prepared_draft_message(harness, phone_number, sms_text):
    old_messages = get_message_by_text(harness, sms_text, str(phone_number))

    # prepare sms and send it (add sms to sending queue)
    prepare_sms(harness, sms_text, str(phone_number), SMSType.DRAFT.value)

    # enter menu
    enter_messages_menu(harness)

    # enter thread with draft
    harness.connection.send_key_code(key_codes["enter"])
    # send
    harness.connection.send_key_code(key_codes["enter"])
    # go back to main screen
    harness.connection.send_key_code(key_codes["fnRight"], Keytype.long_press)
    # wait for sms to send
    time.sleep(3)
    assert harness.get_application_name() == "ApplicationDesktop"

    diff_messages = check_for_sent(old_messages, harness, sms_text, phone_number, extra_time_to_send_message)

    # cleaning
    remove_added_messages(harness,diff_messages)



@pytest.mark.rt1051
@pytest.mark.usefixtures("phone_unlocked")
@pytest.mark.usefixtures("phone_in_desktop")
def test_send_message_from_template(harness, phone_number, sms_text):
    old_messages = get_message_by_text(harness, sms_text, str(phone_number))
    # erasing all templates
    erase_all_templates(harness)
    # add own (well-known) template
    add_new_template(harness, sms_text)
    # try to send message based on template

    # enter menu
    enter_messages_menu(harness)
    # create new message
    harness.connection.send_key_code(key_codes["left"])
    # enter phone number
    harness.send_number(str(phone_number))
    # move down to message body
    harness.connection.send_key_code(key_codes["down"])
    # move to options
    harness.connection.send_key_code(key_codes["fnLeft"])
    # move to templates
    harness.connection.send_key_code(key_codes["enter"])
    # select template
    harness.connection.send_key_code(key_codes["enter"])
    # send message
    harness.connection.send_key_code(key_codes["enter"])
    # go back to main screen
    harness.connection.send_key_code(key_codes["fnRight"], Keytype.long_press)
    # give some time to back to ApplicationDesktop and send message
    time.sleep(3)
    assert harness.get_application_name() == "ApplicationDesktop"

    diff_messages = check_for_sent(old_messages, harness, sms_text, phone_number, extra_time_to_send_message)

    # cleaning
    remove_added_messages(harness,diff_messages)


@pytest.mark.rt1051
@pytest.mark.usefixtures("phone_unlocked")
@pytest.mark.usefixtures("phone_in_desktop")
def test_forward_message(harness, phone_number, sms_text):
    # send first message in order to forward it
    diff_messages_org = test_send_prepared_message(harness, phone_number, sms_text, False)

    old_messages = get_message_by_text(harness, sms_text, str(phone_number))

    # enter menu
    enter_messages_menu(harness)
    # enter thread with sent message
    harness.connection.send_key_code(key_codes["enter"])
    # select message
    harness.connection.send_key_code(key_codes["up"])
    # move to options
    harness.connection.send_key_code(key_codes["fnLeft"])
    # move down to forward message option (2 times down)
    for _ in range(2):
        harness.connection.send_key_code(key_codes["down"])

    # enter thread with sent message
    harness.connection.send_key_code(key_codes["enter"])
    # go to number field
    harness.connection.send_key_code(key_codes["up"])
    # enter phone number
    harness.send_number(str(phone_number))
    # go to sms body
    harness.connection.send_key_code(key_codes["down"])
    # send message
    harness.connection.send_key_code(key_codes["enter"])
    # go back to main screen
    harness.connection.send_key_code(key_codes["fnRight"], Keytype.long_press)
    # give some time to back to ApplicationDesktop and send message
    time.sleep(3)
    assert harness.get_application_name() == "ApplicationDesktop"

    diff_messages = check_for_sent(old_messages, harness, sms_text, phone_number, extra_time_to_send_message)

    # cleaning
    remove_added_messages(harness,diff_messages)
    remove_added_messages(harness,diff_messages_org)


@pytest.mark.rt1051
@pytest.mark.usefixtures("phone_unlocked")
@pytest.mark.usefixtures("phone_in_desktop")
def test_resend_message(harness, phone_number, sms_text):
    # send first message in order to resend it
    prepare_sms(harness, sms_text, str(phone_number), SMSType.FAILED.value)

    old_messages = get_message_by_text(harness, sms_text, str(phone_number))

    # enter menu
    enter_messages_menu(harness)
    # enter thread with sent message
    harness.connection.send_key_code(key_codes["enter"])
    # select message
    harness.connection.send_key_code(key_codes["up"])
    # move to options
    harness.connection.send_key_code(key_codes["fnLeft"])
    # resending message
    harness.connection.send_key_code(key_codes["enter"])
    # go back to main screen
    harness.connection.send_key_code(key_codes["fnRight"], Keytype.long_press)
    # give some time to back to ApplicationDesktop and send message
    time.sleep(3)

    # check if we back to ApplicationDesktop
    assert harness.get_application_name() == "ApplicationDesktop"

    diff_messages = check_for_sent(old_messages, harness, sms_text, phone_number, extra_time_to_send_message)

    # cleaning
    remove_added_messages(harness,diff_messages)



@pytest.mark.rt1051
@pytest.mark.usefixtures("phone_unlocked")
@pytest.mark.usefixtures("phone_in_desktop")
def test_send_message_from_phonebook(harness, phone_number, sms_text):
    # erase Test contacts
    erase_contacts_by_name(harness, "Test")
    # erase Test contacts
    erase_contacts_by_phone_number(harness, "Test")

    old_messages = get_message_by_text(harness, sms_text, str(phone_number))
    # adding new test contact
    body = {"address": "6 Czeczota St.\n02600 Warsaw",
            "altName": "Testowy",
            "blocked": False,
            "favourite": True,
            "numbers": [str(phone_number)],
            "priName": "Test"}
    ret = harness.endpoint_request("contacts", "put", body)
    assert ret["status"] == status["NoContent"]
    added_contact_id = ret["body"]["id"]

    # enter contacts (phonebook) menu
    enter_contacts_menu(harness)

    # search test contact
    # select message
    harness.connection.send_key_code(key_codes["right"])
    # write search text
    harness.send_text("Test")
    # search for added contact
    harness.connection.send_key_code(key_codes["enter"])
    # enter contact
    harness.connection.send_key_code(key_codes["enter"])
    # go to messages in contact
    harness.connection.send_key_code(key_codes["right"])
    # enter new message window
    harness.connection.send_key_code(key_codes["enter"])

    assert harness.get_application_name() == "ApplicationMessages"

    # go to text in new message windows
    harness.connection.send_key_code(key_codes["down"])

    # write a message
    harness.send_text(sms_text)
    # send
    harness.connection.send_key_code(key_codes["enter"])
    # go back to main screen
    harness.connection.send_key_code(key_codes["fnRight"], Keytype.long_press)
    # give some time to back to ApplicationDesktop and send message
    time.sleep(3)
    # check if we back to ApplicationDesktop
    assert harness.get_application_name() == "ApplicationDesktop"

    diff_messages = check_for_sent(old_messages, harness, sms_text, phone_number, extra_time_to_send_message)

    # cleaning
    remove_added_messages(harness,diff_messages)

    # removing added contact
    body = {"id": added_contact_id}
    ret = harness.endpoint_request("contacts", "del", body)
    assert ret["status"] == status["NoContent"]

@pytest.mark.rt1051
@pytest.mark.usefixtures("phone_unlocked")
@pytest.mark.usefixtures("phone_in_desktop")
def test_send_message_using_phonebook(harness, phone_number, sms_text):
    # erase Test contacts
    erase_contacts_by_name(harness, "Test")
    # erase Test contacts
    erase_contacts_by_phone_number(harness, str(phone_number))

    old_messages = get_message_by_text(harness, sms_text, str(phone_number))
    # adding new test contact
    body = {"address": "6 Czeczota St.\n02600 Warsaw",
            "altName": "Testowy",
            "blocked": False,
            "favourite": True,
            "numbers": [str(phone_number)],
            "priName": "Test"}
    ret = harness.endpoint_request("contacts", "put", body)
    assert ret["status"] == status["NoContent"]
    added_contact_id = ret["body"]["id"]

    # enter messages menu
    enter_messages_menu(harness)
    # create new message
    harness.connection.send_key_code(key_codes["left"])
    # select contact
    harness.connection.send_key_code(key_codes["enter"])
    # search test contact
    harness.connection.send_key_code(key_codes["right"])
    # write search text
    harness.send_text("Test")
    # search for added contact
    harness.connection.send_key_code(key_codes["enter"])
    # choose contact
    harness.connection.send_key_code(key_codes["enter"])
    # reset contact
    harness.connection.send_key_code(key_codes["#"])
    # select contact
    harness.connection.send_key_code(key_codes["enter"])
    # search test contact
    harness.connection.send_key_code(key_codes["right"])
    # write search text
    harness.send_text("Test")

    # search for added contact
    harness.connection.send_key_code(key_codes["enter"])
    # select contact
    harness.connection.send_key_code(key_codes["enter"])

    # go to text in new message windows
    harness.connection.send_key_code(key_codes["down"])

    # write a message
    harness.send_text(sms_text)
    # send
    harness.connection.send_key_code(key_codes["enter"])
    # go back to main screen
    harness.connection.send_key_code(key_codes["fnRight"], Keytype.long_press)

    time.sleep(3)
    # check if we back to ApplicationDesktop
    assert harness.get_application_name() == "ApplicationDesktop"

    diff_messages = check_for_sent(old_messages, harness, sms_text, phone_number, extra_time_to_send_message)

    # cleaning
    remove_added_messages(harness,diff_messages)

    # removing added contact
    body = {"id": added_contact_id}
    ret = harness.endpoint_request("contacts", "del", body)
    assert ret["status"] == status["NoContent"]