File: test_security_labels.py

package info (click to toggle)
gajim 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,320 kB
  • sloc: python: 79,114; sh: 387; xml: 67; makefile: 6
file content (119 lines) | stat: -rw-r--r-- 3,952 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
# 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.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 SecurityLabel
from gajim.common.storage.archive.storage import MessageArchiveStorage


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

        self._account_jid = JID.from_string("user@domain.org")
        self._account = "testacc1"
        self._remote_jid = JID.from_string("remote@jid.org")
        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_security_labels_join(self):
        sec_data = SecurityLabel(
            account_=self._account,
            remote_jid_=self._remote_jid,
            updated_at=datetime.fromtimestamp(0, UTC),
            label_hash="label1hash",
            displaymarking="SECRET",
            fgcolor="black",
            bgcolor="red",
        )

        message_data = Message(
            account_=self._account,
            remote_jid_=self._remote_jid,
            type=MessageType.CHAT,
            direction=ChatDirection.INCOMING,
            timestamp=datetime.fromtimestamp(0, UTC),
            state=MessageState.ACKNOWLEDGED,
            resource="res",
            text="Some Message",
            id="messageid1",
            stanza_id=get_uuid(),
            security_label_=sec_data,
        )

        message_pk = self._archive.insert_object(message_data)

        message = self._archive.get_message_with_pk(message_pk)

        assert message is not None
        assert message.security_label is not None

        self.assertEqual(message.security_label.displaymarking, "SECRET")
        self.assertEqual(message.security_label.fgcolor, "black")
        self.assertEqual(message.security_label.bgcolor, "red")

    def test_security_labels_update(self):
        sec_data1 = SecurityLabel(
            account_=self._account,
            remote_jid_=self._remote_jid,
            updated_at=datetime.fromtimestamp(0, UTC),
            label_hash="label1hash",
            displaymarking="SECRET",
            fgcolor="black",
            bgcolor="red",
        )

        sec_data2 = SecurityLabel(
            account_=self._account,
            remote_jid_=self._remote_jid,
            updated_at=datetime.fromtimestamp(1, UTC),
            label_hash="label1hash",
            displaymarking="NOT SECRET",
            fgcolor="white",
            bgcolor="blue",
        )

        pk1 = self._archive.upsert_row(sec_data1)
        pk2 = self._archive.upsert_row(sec_data2)
        pk3 = self._archive.upsert_row(sec_data1)

        self.assertEqual(pk1, pk2)
        self.assertEqual(pk2, pk3)

        with self._archive.get_session() as s:
            res = s.scalar(select(SecurityLabel).where(SecurityLabel.pk == pk3))
        assert res is not None

        self.assertEqual(res.displaymarking, "NOT SECRET")
        self.assertEqual(res.fgcolor, "white")
        self.assertEqual(res.bgcolor, "blue")
        self.assertEqual(res.updated_at, datetime.fromtimestamp(1, UTC))


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