File: test_collection.py

package info (click to toggle)
python-secretstorage 3.3.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 256 kB
  • sloc: python: 697; makefile: 11; sh: 8
file content (84 lines) | stat: -rw-r--r-- 3,140 bytes parent folder | download | duplicates (2)
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
# Tests for SecretStorage
# Author: Dmitry Shachnev, 2013-2018
# License: 3-clause BSD, see LICENSE file

# This file tests the secretstorage.Collection class.

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


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)

    def tearDown(self) -> None:
        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('', {}, 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)


@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")