~dobey/ubuntu/oneiric/ubuntu-sso-client/release-1-3-1

« back to all changes in this revision

Viewing changes to ubuntu_sso/utils/tests/test_txsecrets.py

  • Committer: Sebastien Bacher
  • Date: 2011-04-13 09:34:55 UTC
  • mfrom: (29.1.1 ubuntu-sso-client-1.2.1)
  • Revision ID: seb128@ubuntu.com-20110413093455-p12vrtwlq7cpzqza
Tags: 1.2.1-0ubuntu1
releasing version 1.2.1-0ubuntu1

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
PROMPT_BASE_PATH = "/org/freedesktop/secrets/prompt"
41
41
SESSION_BASE_PATH = "/org/freedesktop/secrets/session"
42
42
COLLECTION_BASE_PATH = "/org/freedesktop/secrets/collection/"
 
43
SAMPLE_CONTENT_TYPE = "text/plain; charset=utf8"
43
44
 
44
45
 
45
46
class SampleMiscException(Exception):
95
96
        return True
96
97
 
97
98
 
 
99
class AltItemMock(ItemMock):
 
100
    """The secret in this item has a content_type."""
 
101
 
 
102
    @dbus.service.method(dbus_interface=txsecrets.ITEM_IFACE,
 
103
                         in_signature="o", out_signature="(oayays)")
 
104
    def GetSecret(self, session):
 
105
        """Retrieve the secret for this item."""
 
106
        if self.get_secret_fail:
 
107
            raise SampleMiscException()
 
108
        return (session, "", self.value, SAMPLE_CONTENT_TYPE)
 
109
 
 
110
 
98
111
class PromptMock(dbus.service.Object):
99
112
    """A prompt necessary to complete an operation."""
100
113
 
126
139
    create_item_fail = False
127
140
    locked = False
128
141
    unlock_prompts = False
 
142
    item_mock_class = ItemMock
129
143
 
130
144
    def __init__(self, label, *args, **kwargs):
131
145
        """Initialize this instance."""
150
164
        item_label = properties[txsecrets.LABEL_PROPERTY]
151
165
        session, parameters, value = secret
152
166
        item_path = create_object_path(make_coll_path(self.label))
153
 
        item = self.dbus_publish(item_path, ItemMock, self, item_label,
154
 
                                 attributes, value)
 
167
        item = self.dbus_publish(item_path, self.item_mock_class, self,
 
168
                                 item_label, attributes, value)
155
169
        self.items.append(item)
156
170
        if self.create_item_prompt:
157
171
            prompt_path = create_object_path(PROMPT_BASE_PATH)
746
760
        self.assertEqual(value, sample_secret)
747
761
 
748
762
    @inlineCallbacks
 
763
    def test_get_value_four_items_per_secret(self):
 
764
        """The code works fine when the secret dbus struct has 4 items."""
 
765
        yield self.secretservice.open_session()
 
766
        collection_label = "sample_keyring"
 
767
        coll = yield self.create_sample_collection(collection_label)
 
768
        mock_coll = self.mock_service.collections[collection_label]
 
769
        mock_coll.item_mock_class = AltItemMock
 
770
        attr = {"key-type": "Ubuntu SSO credentials"}
 
771
        sample_secret = "secret83!"
 
772
        yield coll.create_item("Cucaracha", attr, sample_secret)
 
773
        items = yield self.secretservice.search_items(attr)
 
774
        self.assertEqual(len(items), 1)
 
775
        value = yield items[0].get_value()
 
776
        self.assertEqual(value, sample_secret)
 
777
 
 
778
    @inlineCallbacks
749
779
    def test_get_value_throws_dbus_error(self):
750
780
        """The secret value is not retrieved if DBus fails."""
751
781
        yield self.secretservice.open_session()