~aleteoryx/muditaos

ref: 0e8b4c848e3f87f3bbb1f64ef7460cf56017b87f muditaos/test/pytest/service-desktop/test_outbox.py -rw-r--r-- 6.9 KiB
0e8b4c84 — Lefucjusz [BH-2108] Fix misaligned charging symbol 3 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
# Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

import time
import pytest
from harness.api.contacts import AddContact, DeleteContactById
from harness.api.messages import AddMessage, DeleteMessageById
from harness.api.outbox import NotificationEntry, NotificationType, NotificationChange, GetNotifications, \
    DeleteNotifications
from harness.request import TransactionError


class OutboxTester:
    def __init__(self, harness):
        self.harness = harness

    def get_notifications(self):
        try:
            notifications = GetNotifications().run(self.harness).entries
        except TransactionError:
            return False
        else:
            return True, notifications

    def delete_notifications(self, entries: list):
        try:
            DeleteNotifications(entries).run(self.harness)
        except TransactionError:
            return False
        else:
            return True

    def add_message(self, message_number, message_body):
        try:
            message = AddMessage(message_number, message_body).run(self.harness).message
        except TransactionError:
            return False
        else:
            return True, message

    def delete_message_by_id(self, message_record_id):
        try:
            DeleteMessageById(message_record_id).run(self.harness)
        except TransactionError:
            return False
        else:
            return True

    def add_contact(self, contact_record):
        try:
            contact_id = AddContact(contact_record).run(self.harness).id
        except TransactionError:
            return False
        else:
            return True, contact_id

    def delete_contact_by_id(self, contact_record_id):
        try:
            DeleteContactById(contact_record_id).run(self.harness)
        except TransactionError:
            return False
        else:
            return True


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_unlocked")
def test_getting_notifications(harness):
    outbox_tester = OutboxTester(harness)
    result, received_notifications = outbox_tester.get_notifications()
    assert result, "Failed to get notifications!"
    assert not received_notifications, "Notification list is not empty at the beginning of the test!"

    # Add message to generate "message created" and "thread created" notifications
    message_number = "123456789"
    message_body = "Hello, how are you?"

    result, message_record = outbox_tester.add_message(message_number, message_body)
    assert result, "Failed to add message!"

    # Add contact to generate "contact created" notification
    contact_record = {
        "address": "6 Czeczota St.\n02600 Warsaw",
        "altName": "Smith",
        "email": "john.smith@mudita.com",
        "blocked": False,
        "favourite": False,
        "ice": False,
        "numbers": [
            "123456789"
        ],
        "speedDial": "1",
        "priName": "John",
        "note": "Some note"
    }

    result, contact_id = outbox_tester.add_contact(contact_record)
    assert result, "Failed to add contact!"

    result, received_notifications = outbox_tester.get_notifications()
    assert result, "Failed to get notifications!"

    # Check if all notifications are present
    found_message_notification = False
    for notification in received_notifications:
        if notification.type == NotificationType.MESSAGE and notification.change == NotificationChange.CREATED \
                and notification.record_id == message_record["messageID"]:
            found_message_notification = True
            break
    found_thread_notification = False
    for notification in received_notifications:
        if notification.type == NotificationType.THREAD and notification.change == NotificationChange.CREATED:
            found_thread_notification = True
            break
    found_contacts_notification = False
    for notification in received_notifications:
        if notification.type == NotificationType.CONTACT and notification.change == NotificationChange.CREATED \
                and notification.record_id == contact_id:
            found_contacts_notification = True
            break
    assert found_message_notification and found_thread_notification and found_contacts_notification

    # Wait for 20 seconds to be sure that notifications are cleaned by timer
    time.sleep(20)

    result, received_notifications = outbox_tester.get_notifications()
    assert result, "Failed to get notifications!"
    assert not received_notifications, "Notification list is not empty after timeout!"

    assert outbox_tester.delete_message_by_id(message_record["messageID"]), "Failed to delete a message!"
    assert outbox_tester.delete_contact_by_id(contact_id), "Failed to delete a contact!"


@pytest.mark.service_desktop_test
@pytest.mark.usefixtures("phone_unlocked")
def test_deleting_notifications(harness):
    outbox_tester = OutboxTester(harness)
    result, received_notifications = outbox_tester.get_notifications()
    assert result, "Failed to get notifications!"
    assert not received_notifications, "Notification list is not empty at the beginning of the test!"

    # Add message to generate "message created" and "thread created" notifications
    message_number = "123456789"
    message_body = "Hello, how are you?"

    result, message_record = outbox_tester.add_message(message_number, message_body)
    assert result, "Failed to add message!"

    # Add contact to generate "contact created" notification
    contact_record = {
        "address": "6 Czeczota St.\n02600 Warsaw",
        "altName": "Smith",
        "email": "john.smith@mudita.com",
        "blocked": False,
        "favourite": False,
        "ice": False,
        "numbers": [
            "123456789"
        ],
        "speedDial": "1",
        "priName": "John",
        "note": "Some note"
    }

    result, contact_id = outbox_tester.add_contact(contact_record)
    assert result, "Failed to add contact!"

    result, received_notifications = outbox_tester.get_notifications()
    assert result, "Failed to get notifications!"
    uids_of_notifications_to_be_deleted = []
    for notification in received_notifications:
        uids_of_notifications_to_be_deleted.append(notification.uid)

    result = outbox_tester.delete_notifications(uids_of_notifications_to_be_deleted)
    assert result, "Failed to get notifications!"

    # Check if all generated notifications are deleted
    result, received_notifications = outbox_tester.get_notifications()
    assert result, "Failed to get notifications!"

    for notification in received_notifications:
        assert notification.uid not in uids_of_notifications_to_be_deleted, "Notification not deleted!"

    assert outbox_tester.delete_message_by_id(message_record["messageID"]), "Failed to delete a message!"
    assert outbox_tester.delete_contact_by_id(contact_id), "Failed to delete a contact!"