File: test_collection.py

package info (click to toggle)
python-secretstorage 3.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 252 kB
  • sloc: python: 762; makefile: 11; sh: 8
file content (100 lines) | stat: -rw-r--r-- 3,483 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
# Tests for SecretStorage
# Author: Dmitry Shachnev, 2013-2025
# License: 3-clause BSD, see LICENSE file

# This file tests the secretstorage.Collection class.

import unittest

from secretstorage import (
    Collection,
    Item,
    create_collection,
    dbus_init,
    get_all_collections,
    get_any_collection,
    get_default_collection,
)
from secretstorage.exceptions import ItemNotFoundException
from secretstorage.util import BUS_NAME


class CollectionTest(unittest.TestCase):
    """A test case that tests that all common methods of Collection
    class work and do not crash."""

    def setUp(self) -> None:
        self.connection = dbus_init()
        self.collection = get_any_collection(self.connection)
        self.original_label = self.collection.get_label()

    def tearDown(self) -> None:
        self.collection.set_label(self.original_label)
        self.connection.close()

    def test_all_collections(self) -> None:
        labels = map(Collection.get_label, get_all_collections(self.connection))
        self.assertIn(self.collection.get_label(), labels)

    def test_all_items(self) -> None:
        for item in self.collection.get_all_items():
            item.get_label()

    def test_create_empty_item(self) -> None:
        item = self.collection.create_item('Test', {}, b'')
        item.delete()

    def test_label(self) -> None:
        old_label = self.collection.get_label()
        self.collection.set_label('Hello!')
        self.assertEqual(self.collection.get_label(), 'Hello!')
        self.collection.set_label(old_label)
        self.assertEqual(self.collection.get_label(), old_label)

    def test_repr(self) -> None:
        self.collection.set_label("My collection")
        self.assertEqual(
            repr(self.collection),
            f"<Collection 'My collection' path='{self.collection.collection_path}'>",
        )


@unittest.skipIf(BUS_NAME == "org.freedesktop.secrets",
                 "This test should only be run with the mocked server.")
class MockCollectionTest(unittest.TestCase):
    def setUp(self) -> None:
        self.connection = dbus_init()

    def tearDown(self) -> None:
        self.connection.close()

    def test_default_collection(self) -> None:
        collection = get_default_collection(self.connection)
        self.assertEqual(collection.get_label(), "Collection One")

    def test_deleting(self) -> None:
        collection_path = "/org/freedesktop/secrets/collection/spanish"
        collection = Collection(self.connection, collection_path)
        collection.unlock()
        collection.delete()

    def test_deleting_prompt(self) -> None:
        collection_path = "/org/freedesktop/secrets/collection/lockprompt"
        try:
            collection = Collection(self.connection, collection_path)
        except ItemNotFoundException:
            self.skipTest("This test should only be run with mock-service-lock.")
        collection.unlock()
        collection.delete()

    def test_deleting_item_prompt(self) -> None:
        item_path = "/org/freedesktop/secrets/collection/lockone/confirm"
        try:
            item = Item(self.connection, item_path)
        except ItemNotFoundException:
            self.skipTest("This test should only be run with mock-service-lock.")
        item.delete()

    def test_create_collection(self) -> None:
        collection = create_collection(self.connection, "My Label")
        self.assertEqual(collection.get_label(), "My Label")