File: test_retractions.py

package info (click to toggle)
gajim 2.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,512 kB
  • sloc: python: 79,978; sh: 591; xml: 67; makefile: 6
file content (286 lines) | stat: -rw-r--r-- 9,684 bytes parent folder | download
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
# 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 test.database.util import mk_utc_dt

from nbxmpp.protocol import JID
from sqlalchemy import select

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 Retraction
from gajim.common.storage.archive.storage import MessageArchiveStorage


class RetractionTest(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")

    def test_insert_retraction(self) -> None:
        # Test unique index without occupant id

        uuid = get_uuid()
        retraction_data = Retraction(
            account_=self._account,
            remote_jid_=self._remote_jid,
            occupant_=None,
            id=uuid,
            direction=ChatDirection.INCOMING,
            timestamp=datetime.fromtimestamp(0, UTC),
        )

        pk = self._archive.insert_row(retraction_data, ignore_on_conflict=True)
        self.assertNotEqual(pk, -1)

        with self._archive.get_session() as s:
            retraction = s.scalar(select(Retraction).where(Retraction.pk == pk))
        assert retraction is not None

        self.assertEqual(retraction.timestamp, datetime.fromtimestamp(0, UTC))
        self.assertEqual(retraction.id, uuid)
        self.assertEqual(retraction.occupant, None)

        retraction_data = Retraction(
            account_=self._account,
            remote_jid_=self._remote_jid,
            occupant_=None,
            id=uuid,
            direction=ChatDirection.INCOMING,
            timestamp=datetime.fromtimestamp(1, UTC),
        )

        pk = self._archive.insert_row(retraction_data, ignore_on_conflict=True)
        self.assertEqual(pk, -1)

        # Test unique index with occupant id

        occupant_data = Occupant(
            account_=self._account,
            remote_jid_=self._remote_jid,
            id="occupantid1",
            nickname="nickname1",
            updated_at=mk_utc_dt(0),
        )

        uuid = get_uuid()
        retraction_data = Retraction(
            account_=self._account,
            remote_jid_=self._remote_jid,
            occupant_=occupant_data,
            id=uuid,
            direction=ChatDirection.INCOMING,
            timestamp=datetime.fromtimestamp(0, UTC),
        )

        pk = self._archive.insert_row(retraction_data, ignore_on_conflict=True)
        self.assertNotEqual(pk, -1)

        with self._archive.get_session() as s:
            retraction = s.scalar(select(Retraction).where(Retraction.pk == pk))
        assert retraction is not None
        assert retraction.occupant is not None

        self.assertEqual(retraction.timestamp, datetime.fromtimestamp(0, UTC))
        self.assertEqual(retraction.id, uuid)
        self.assertEqual(retraction.occupant.id, "occupantid1")

        retraction_data = Retraction(
            account_=self._account,
            remote_jid_=self._remote_jid,
            occupant_=occupant_data,
            id=uuid,
            direction=ChatDirection.INCOMING,
            timestamp=datetime.fromtimestamp(1, UTC),
        )

        pk = self._archive.insert_row(retraction_data, ignore_on_conflict=True)
        self.assertEqual(pk, -1)

    def test_retraction_join(self) -> None:
        uuid = get_uuid()
        message_data = Message(
            account_=self._account,
            remote_jid_=self._remote_jid,
            resource="someres1",
            type=MessageType.CHAT,
            direction=ChatDirection.INCOMING,
            timestamp=datetime.now(UTC),
            state=MessageState.ACKNOWLEDGED,
            id="1",
            stanza_id=uuid,
            text="message",
        )

        pk = self._archive.insert_object(message_data)

        retraction_data = Retraction(
            account_=self._account,
            remote_jid_=self._remote_jid,
            occupant_=None,
            id="1",
            direction=ChatDirection.INCOMING,
            timestamp=datetime.fromtimestamp(0, UTC),
        )

        self._archive.insert_row(retraction_data, ignore_on_conflict=True)

        message = self._archive.get_message_with_pk(pk)

        assert message is not None
        assert message.retraction is not None
        self.assertEqual(message.retraction.timestamp, datetime.fromtimestamp(0, UTC))
        self.assertEqual(message.retraction.id, "1")
        self.assertEqual(message.retraction.occupant, None)

        # Test incorrect direction does not get joined

        uuid = get_uuid()
        message_data = Message(
            account_=self._account,
            remote_jid_=self._remote_jid,
            resource="someres1",
            type=MessageType.CHAT,
            direction=ChatDirection.OUTGOING,
            timestamp=datetime.now(UTC),
            state=MessageState.ACKNOWLEDGED,
            id="2",
            stanza_id=uuid,
            text="message",
        )

        pk = self._archive.insert_object(message_data)

        retraction_data = Retraction(
            account_=self._account,
            remote_jid_=self._remote_jid,
            occupant_=None,
            id="2",
            direction=ChatDirection.INCOMING,
            timestamp=datetime.fromtimestamp(0, UTC),
        )

        self._archive.insert_row(retraction_data, ignore_on_conflict=True)

        message = self._archive.get_message_with_pk(pk)
        assert message is not None
        self.assertIsNone(message.retraction)

    def test_retractions_join_groupchat(self) -> None:
        occupant_data1 = Occupant(
            account_=self._account,
            remote_jid_=self._remote_jid,
            id="occupantid1",
            nickname="nickname1",
            updated_at=mk_utc_dt(0),
        )

        occupant_data2 = Occupant(
            account_=self._account,
            remote_jid_=self._remote_jid,
            id="occupantid2",
            nickname="nickname2",
            updated_at=mk_utc_dt(0),
        )

        uuid1 = get_uuid()
        message_data1 = Message(
            account_=self._account,
            remote_jid_=self._remote_jid,
            resource="someres1",
            type=MessageType.GROUPCHAT,
            direction=ChatDirection.INCOMING,
            timestamp=datetime.now(UTC),
            state=MessageState.ACKNOWLEDGED,
            id="1",
            stanza_id=uuid1,
            text="message",
            occupant_=occupant_data1,
        )

        uuid2 = get_uuid()
        message_data2 = Message(
            account_=self._account,
            remote_jid_=self._remote_jid,
            resource="someres1",
            type=MessageType.GROUPCHAT,
            direction=ChatDirection.INCOMING,
            timestamp=datetime.now(UTC),
            state=MessageState.ACKNOWLEDGED,
            id="1",
            stanza_id=uuid2,
            text="message",
            occupant_=occupant_data2,
        )

        pk1 = self._archive.insert_object(message_data1)
        pk2 = self._archive.insert_object(message_data2)

        retraction_data1 = Retraction(
            account_=self._account,
            remote_jid_=self._remote_jid,
            occupant_=occupant_data1,
            id=uuid1,
            direction=ChatDirection.INCOMING,
            timestamp=datetime.fromtimestamp(0, UTC),
        )

        retraction_data2 = Retraction(
            account_=self._account,
            remote_jid_=self._remote_jid,
            occupant_=occupant_data2,
            id=uuid2,
            direction=ChatDirection.INCOMING,
            timestamp=datetime.fromtimestamp(0, UTC),
        )

        self._archive.insert_row(retraction_data1, ignore_on_conflict=True)
        self._archive.insert_row(retraction_data2, ignore_on_conflict=True)

        message1 = self._archive.get_message_with_pk(pk1)
        message2 = self._archive.get_message_with_pk(pk2)

        assert message1 is not None
        assert message1.retraction is not None
        assert message1.retraction.occupant is not None
        self.assertEqual(message1.retraction.timestamp, datetime.fromtimestamp(0, UTC))
        self.assertEqual(message1.retraction.id, uuid1)
        self.assertEqual(message1.retraction.occupant.id, "occupantid1")

        assert message2 is not None
        assert message2.retraction is not None
        assert message2.retraction.occupant is not None
        self.assertEqual(message2.retraction.timestamp, datetime.fromtimestamp(0, UTC))
        self.assertEqual(message2.retraction.id, uuid2)
        self.assertEqual(message2.retraction.occupant.id, "occupantid2")


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