File: test_corrections.py

package info (click to toggle)
gajim 2.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,496 kB
  • sloc: python: 79,966; sh: 591; xml: 67; makefile: 6
file content (253 lines) | stat: -rw-r--r-- 8,294 bytes parent folder | download | duplicates (2)
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
# This file is part of Gajim.
#
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import annotations

import unittest
from datetime import datetime
from datetime import UTC

from nbxmpp.protocol import JID

from gajim.common import app
from gajim.common.helpers import get_uuid
from gajim.common.settings import Settings
from gajim.common.storage.archive.const import ChatDirection
from gajim.common.storage.archive.const import MessageState
from gajim.common.storage.archive.const import MessageType
from gajim.common.storage.archive.models import Message
from gajim.common.storage.archive.models import Occupant
from gajim.common.storage.archive.models import OOB
from gajim.common.storage.archive.storage import MessageArchiveStorage


class CorrectionsTest(unittest.TestCase):
    def setUp(self) -> None:
        self._archive = MessageArchiveStorage(in_memory=True)
        self._archive.init()

        self._account = "testacc1"
        self._account_jid = JID.from_string("user@domain.org")
        self._remote_jid = JID.from_string("remote@jid.org")
        self._occupant_id = "occupantid1"
        self._init_settings()

    def tearDown(self) -> None:
        self._archive.shutdown()

    def _init_settings(self) -> None:
        app.settings = Settings(in_memory=True)
        app.settings.init()
        app.settings.add_account("testacc1")
        app.settings.set_account_setting("testacc1", "address", "user@domain.org")

        app.settings.add_account("testacc2")
        app.settings.set_account_setting("testacc2", "address", "user2@domain.org")

    def _create_base_occupant(self, occupant_id: str) -> Occupant:
        return Occupant(
            account_=self._account,
            remote_jid_=self._remote_jid,
            id=occupant_id,
            nickname="peter",
            updated_at=datetime.fromtimestamp(50, UTC),
        )

    def _create_base_message(self) -> Message:
        return Message(
            account_=self._account,
            remote_jid_=self._remote_jid,
            type=MessageType.GROUPCHAT,
            direction=ChatDirection.INCOMING,
            state=MessageState.ACKNOWLEDGED,
            timestamp=datetime.fromtimestamp(0, UTC),
            resource="res1",
            text="Some Message",
            id="messageid1",
            stanza_id=get_uuid(),
            correction_id=None,
            occupant_=self._create_base_occupant("occupantid1"),
            oob=[OOB(url="https://www.test.com", description="desc")],
        )

    def test_join_correction_groupchat_occupant_id(self) -> None:
        # Order corrections by timestamp
        # Join with occupant id

        m = self._create_base_message()
        message_pk = self._archive.insert_object(m)

        # Insert in not ascending order to test ordering

        m = self._create_base_message()
        m.text = "second correction"
        m.id = "messageid3"
        m.timestamp = datetime.fromtimestamp(2, UTC)
        m.correction_id = "messageid1"
        assert m.oob
        m.oob[0].description = "second corrected desc"

        self._archive.insert_object(m)

        # Change resource, but leave occupant id the same
        # this message should be still joined

        m = self._create_base_message()
        m.text = "first correction"
        m.resource = "otherres"
        m.id = "messageid2"
        m.timestamp = datetime.fromtimestamp(1, UTC)
        m.correction_id = "messageid1"

        self._archive.insert_object(m)

        # Add correction from unrelated occupant, should not be joined

        m = self._create_base_message()
        m.occupant_ = self._create_base_occupant("occupantid2")
        m.text = "third correction"
        m.id = "messageid4"
        m.timestamp = datetime.fromtimestamp(3, UTC)
        m.correction_id = "messageid1"

        self._archive.insert_object(m)

        # Add correction with no occupant id, should not be joined

        m = self._create_base_message()
        m.occupant_ = None
        m.text = "third correction"
        m.id = "messageid5"
        m.timestamp = datetime.fromtimestamp(4, UTC)
        m.correction_id = "messageid1"

        self._archive.insert_object(m)

        message = self._archive.get_message_with_pk(message_pk)

        assert message is not None
        assert message.corrections

        self.assertEqual(len(message.corrections), 2)
        self.assertEqual(message.corrections[0].text, "first correction")
        self.assertEqual(message.corrections[1].text, "second correction")
        assert message.corrections[1].oob is not None
        self.assertEqual(
            message.corrections[1].oob[0].description, "second corrected desc"
        )

    def test_join_correction_groupchat_resource(self) -> None:
        # Join only with resource, when occupant id is not supported

        m = self._create_base_message()
        m.occupant_ = None
        message_pk = self._archive.insert_object(m)

        m = self._create_base_message()
        m.occupant_ = None
        m.text = "first correction"
        m.id = "messageid2"
        m.timestamp = datetime.fromtimestamp(1, UTC)
        m.correction_id = "messageid1"

        self._archive.insert_object(m)

        # Add correction from unrelated resource, should not be joined

        m = self._create_base_message()
        m.occupant_ = None
        m.resource = "unrelatedres"
        m.text = "second correction"
        m.id = "messageid3"
        m.timestamp = datetime.fromtimestamp(2, UTC)
        m.correction_id = "messageid1"

        self._archive.insert_object(m)

        message = self._archive.get_message_with_pk(message_pk)

        assert message is not None
        assert message.corrections

        self.assertEqual(len(message.corrections), 1)
        self.assertEqual(message.corrections[0].text, "first correction")

    def test_join_correction_single_chat(self) -> None:
        # Order corrections by timestamp
        # Join with occupant id

        m = self._create_base_message()
        m.type = MessageType.CHAT
        message_pk = self._archive.insert_object(m)

        m = self._create_base_message()
        m.type = MessageType.CHAT
        m.text = "first correction"
        m.id = "messageid2"
        m.timestamp = datetime.fromtimestamp(1, UTC)
        m.correction_id = "messageid1"

        self._archive.insert_object(m)

        # Change resource, should still be joined because
        # resource does not matter in single chat

        m = self._create_base_message()
        m.type = MessageType.CHAT
        m.text = "second correction"
        m.resource = "otherres"
        m.id = "messageid3"
        m.timestamp = datetime.fromtimestamp(2, UTC)
        m.correction_id = "messageid1"

        self._archive.insert_object(m)

        # Change remote jid, should not be joined

        m = self._create_base_message()
        m.type = MessageType.CHAT
        m.remote_jid_ = JID.from_string("other@remote.jid")
        m.text = "third correction"
        m.id = "messageid4"
        m.timestamp = datetime.fromtimestamp(3, UTC)
        m.correction_id = "messageid1"

        self._archive.insert_object(m)

        # Change account, should not be joined

        m = self._create_base_message()
        m.type = MessageType.CHAT
        m.account_ = "testacc2"
        m.text = "fourth correction"
        m.id = "messageid5"
        m.timestamp = datetime.fromtimestamp(4, UTC)
        m.correction_id = "messageid1"

        self._archive.insert_object(m)

        # Change direction, should not be joined

        m = self._create_base_message()
        m.type = MessageType.CHAT
        m.direction = ChatDirection.OUTGOING
        m.text = "fifth correction"
        m.id = "messageid6"
        m.timestamp = datetime.fromtimestamp(5, UTC)
        m.correction_id = "messageid1"

        self._archive.insert_object(m)

        message = self._archive.get_message_with_pk(message_pk)

        assert message is not None
        assert message.corrections

        self.assertEqual(len(message.corrections), 2)
        self.assertEqual(message.corrections[0].text, "first correction")
        self.assertEqual(message.corrections[1].text, "second correction")


if __name__ == "__main__":
    unittest.main()