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
|
# 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 sqlalchemy import select
from gajim.common import app
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.storage import MessageArchiveStorage
class OccupantTest(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_occupant(self) -> None:
occupant_data = Occupant(
account_=self._account,
remote_jid_=self._remote_jid,
id="someid",
nickname="peter",
updated_at=datetime.fromtimestamp(50, UTC),
)
pk = self._archive.upsert_row(occupant_data)
with self._archive.get_session() as s:
occupant = s.scalar(select(Occupant).where(Occupant.pk == pk))
assert occupant is not None
self.assertEqual(occupant.id, "someid")
self.assertEqual(occupant.real_remote, None)
self.assertEqual(occupant.nickname, "peter")
self.assertEqual(occupant.avatar_sha, None)
self.assertEqual(occupant.updated_at, datetime.fromtimestamp(50, UTC))
occupant_data = Occupant(
account_=self._account,
remote_jid_=self._remote_jid,
id="someid",
real_remote_jid_=JID.from_string("real@remote.jid"),
nickname="peter",
avatar_sha="sha1",
updated_at=datetime.fromtimestamp(100, UTC),
)
pk = self._archive.upsert_row(occupant_data)
with self._archive.get_session() as s:
occupant = s.scalar(select(Occupant).where(Occupant.pk == pk))
assert occupant is not None
assert occupant.real_remote is not None
self.assertEqual(occupant.id, "someid")
self.assertEqual(occupant.real_remote.jid, "real@remote.jid")
self.assertIsInstance(occupant.real_remote.jid, JID)
self.assertEqual(occupant.nickname, "peter")
self.assertEqual(occupant.avatar_sha, "sha1")
self.assertEqual(occupant.updated_at, datetime.fromtimestamp(100, UTC))
# Update with old data should be ignored
occupant_data = Occupant(
account_=self._account,
remote_jid_=self._remote_jid,
id="someid",
real_remote_jid_=JID.from_string("real2@remote.jid"),
nickname="peter2",
avatar_sha="sha2",
updated_at=datetime.fromtimestamp(50, UTC),
)
pk = self._archive.upsert_row(occupant_data)
self.assertEqual(pk, 1)
with self._archive.get_session() as s:
occupant = s.scalar(select(Occupant).where(Occupant.pk == pk))
assert occupant is not None
assert occupant.real_remote is not None
self.assertEqual(occupant.id, "someid")
self.assertEqual(occupant.real_remote.jid, "real@remote.jid")
self.assertIsInstance(occupant.real_remote.jid, JID)
self.assertEqual(occupant.nickname, "peter")
self.assertEqual(occupant.avatar_sha, "sha1")
self.assertEqual(occupant.updated_at, datetime.fromtimestamp(100, UTC))
# Update with new data
# Remote JID should not be overwritten
occupant_data = Occupant(
account_=self._account,
remote_jid_=self._remote_jid,
id="someid",
real_remote_jid_=JID.from_string("real2@remote.jid"),
nickname="peter2",
avatar_sha="sha2",
updated_at=datetime.fromtimestamp(200, UTC),
)
pk = self._archive.upsert_row(occupant_data)
self.assertEqual(pk, 1)
with self._archive.get_session() as s:
occupant = s.scalar(select(Occupant).where(Occupant.pk == pk))
assert occupant is not None
assert occupant.real_remote is not None
self.assertEqual(occupant.id, "someid")
self.assertEqual(occupant.real_remote.jid, "real@remote.jid")
self.assertIsInstance(occupant.real_remote.jid, JID)
self.assertEqual(occupant.nickname, "peter2")
self.assertEqual(occupant.avatar_sha, "sha2")
self.assertEqual(occupant.updated_at, datetime.fromtimestamp(200, UTC))
# Set avatar_sha None
# Passing None to for nickname should be ignored
occupant_data = Occupant(
account_=self._account,
remote_jid_=self._remote_jid,
id="someid",
nickname=None,
avatar_sha=None,
updated_at=datetime.fromtimestamp(300, UTC),
)
pk = self._archive.upsert_row(occupant_data)
self.assertEqual(pk, 1)
with self._archive.get_session() as s:
occupant = s.scalar(select(Occupant).where(Occupant.pk == pk))
assert occupant is not None
assert occupant.real_remote is not None
self.assertEqual(occupant.id, "someid")
self.assertEqual(occupant.real_remote.jid, "real@remote.jid")
self.assertIsInstance(occupant.real_remote.jid, JID)
self.assertEqual(occupant.nickname, "peter2")
self.assertEqual(occupant.avatar_sha, None)
self.assertEqual(occupant.updated_at, datetime.fromtimestamp(300, UTC))
def test_occupant_join(self) -> None:
occupant_data = Occupant(
account_=self._account,
remote_jid_=self._remote_jid,
id="someid",
real_remote_jid_=JID.from_string("real@remote.jid"),
nickname="peter3",
avatar_sha="sha1",
updated_at=datetime.fromtimestamp(300, UTC),
)
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",
text="message",
occupant_=occupant_data,
)
pk = self._archive.insert_object(message_data)
message = self._archive.get_message_with_pk(pk)
assert message is not None
assert message.occupant is not None
assert message.occupant.real_remote is not None
self.assertEqual(message.occupant.nickname, "peter3")
self.assertEqual(message.occupant.avatar_sha, "sha1")
self.assertEqual(message.occupant.real_remote.jid, "real@remote.jid")
self.assertIsInstance(message.occupant.real_remote.jid, JID)
if __name__ == "__main__":
unittest.main()
|