summaryrefslogtreecommitdiffstatshomepage
path: root/tests/test_mattermost_keyring.py
blob: 93cc96d3f175ac5e9b9c8b63e0868585860dc68d (plain) (blame)
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
import subprocess
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch

from scripts import mattermost_keyring


class MattermostKeyringTests(unittest.TestCase):
    def test_adds_rules_only_once(self):
        original = 'profile "snap.mattermost-desktop.mattermost-desktop" {\n}\n'
        patched = mattermost_keyring.patch_profile(original)
        self.assertIn("interface=org.freedesktop.Secret.", patched)
        self.assertEqual(mattermost_keyring.patch_profile(patched), patched)

    def test_rejects_other_profiles(self):
        with self.assertRaises(ValueError):
            mattermost_keyring.patch_profile('profile "snap.other" {\n}\n')

    def test_rejects_incomplete_profile(self):
        with self.assertRaises(ValueError):
            mattermost_keyring.patch_profile(
                'profile "snap.mattermost-desktop.mattermost-desktop" {\n'
            )

    def test_rejects_multiple_profiles(self):
        with self.assertRaises(ValueError):
            mattermost_keyring.patch_profile(
                'profile "snap.mattermost-desktop.mattermost-desktop" {\n}\n'
                'profile "snap.other" {\n}\n'
            )

    def test_preserves_snap_permissions(self):
        original = (
            'profile "snap.mattermost-desktop.mattermost-desktop" '
            "flags=(attach_disconnected,mediate_deleted) {\n  /example r,\n}\n"
        )
        patched = mattermost_keyring.patch_profile(original)
        self.assertTrue(patched.startswith(original[:-2]))
        self.assertNotIn("kwallet", patched)
        self.assertNotIn("complain", patched)

    def test_parser_failure_leaves_installed_profile_unchanged(self):
        with tempfile.TemporaryDirectory() as directory:
            profile = Path(directory) / mattermost_keyring.PROFILE.name
            profile.write_text("original")
            with (
                patch.object(mattermost_keyring, "PROFILE", profile),
                patch.object(
                    mattermost_keyring.subprocess,
                    "run",
                    side_effect=subprocess.CalledProcessError(1, "apparmor_parser"),
                ),
                self.assertRaises(subprocess.CalledProcessError),
            ):
                mattermost_keyring.install_profile("invalid")
            self.assertEqual(profile.read_text(), "original")
            self.assertEqual(list(Path(directory).iterdir()), [profile])