~ubuntu-branches/ubuntu/precise/python-keyring/precise-updates

« back to all changes in this revision

Viewing changes to keyring/tests/test_cli.py

  • Committer: Bazaar Package Importer
  • Author(s): Carl Chenet
  • Date: 2011-08-06 14:52:47 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110806145247-bua5w37ntzpmisto
Tags: 0.6.2-1
* New upstream version
* debian/control
  - Bump the Standards-Version field to 3.9.2
  - Removed Breaks: ${python:Breaks} from the binary package description

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Test case to access the keyring from the command line
 
3
"""
 
4
 
 
5
import os.path
 
6
import unittest
 
7
 
 
8
from keyring import cli
 
9
import keyring.backend
 
10
 
 
11
 
 
12
class FakeKeyring(keyring.backend.KeyringBackend):
 
13
    PASSWORD = "GABUZOMEUH"
 
14
    def supported(self):
 
15
        return 1
 
16
 
 
17
    def set_password(self, service, username, password):
 
18
        pass
 
19
 
 
20
    def get_password(self, service, username):
 
21
        return self.PASSWORD
 
22
 
 
23
 
 
24
class SimpleKeyring(keyring.backend.KeyringBackend):
 
25
    """A very simple keyring"""
 
26
 
 
27
    def __init__(self):
 
28
        self.pwd = {}
 
29
 
 
30
    def supported(self):
 
31
        return 1
 
32
 
 
33
    def set_password(self, service, username, password):
 
34
        self.pwd[(service, username)] = password
 
35
 
 
36
    def get_password(self, service, username):
 
37
        try:
 
38
            return self.pwd[(service, username)]
 
39
        except KeyError:
 
40
            return None
 
41
 
 
42
 
 
43
class CommandLineTestCase(unittest.TestCase):
 
44
    def setUp(self):
 
45
        self.old_keyring = keyring.get_keyring()
 
46
        self.old_input_password = cli.input_password
 
47
        self.old_output_password = cli.output_password
 
48
 
 
49
        keyring.set_keyring(SimpleKeyring())
 
50
        self.password = ""
 
51
        self.password_returned = None
 
52
        cli.input_password = self.return_password
 
53
        cli.output_password = self.save_password
 
54
 
 
55
    def tearDown(self):
 
56
        keyring.set_keyring(self.old_keyring)
 
57
        cli.input_password = self.old_input_password
 
58
        cli.output_password = self.old_output_password
 
59
 
 
60
    def return_password(self, *args, **kwargs):
 
61
        return self.password
 
62
 
 
63
    def save_password(self, password):
 
64
        self.password_returned = password
 
65
 
 
66
 
 
67
    def test_wrong_arguments(self):
 
68
        self.assertEqual(1, cli.main([]))
 
69
 
 
70
        self.assertRaises(SystemExit, cli.main, ["get"])
 
71
        self.assertRaises(SystemExit, cli.main, ["get", "foo"])
 
72
        self.assertRaises(SystemExit, cli.main, ["get", "foo", "bar", "baz"])
 
73
 
 
74
        self.assertRaises(SystemExit, cli.main, ["set"])
 
75
        self.assertRaises(SystemExit, cli.main, ["set", "foo"])
 
76
        self.assertRaises(SystemExit, cli.main, ["set", "foo", "bar", "baz"])
 
77
 
 
78
        self.assertRaises(SystemExit, cli.main, ["foo", "bar", "baz"])
 
79
 
 
80
    def test_get_unexistent_password(self):
 
81
        self.assertEqual(1, cli.main(["get", "foo", "bar"]))
 
82
        self.assertEqual(None, self.password_returned)
 
83
 
 
84
    def test_set_and_get_password(self):
 
85
        self.password = "plop"
 
86
        self.assertEqual(0, cli.main(["set", "foo", "bar"]))
 
87
        self.assertEqual(0, cli.main(["get", "foo", "bar"]))
 
88
        self.assertEqual("plop", self.password_returned)
 
89
 
 
90
    def test_load_builtin_backend(self):
 
91
        self.assertEqual(1, cli.main(["get",
 
92
                                      "-b", "keyring.backend.UncryptedFileKeyring",
 
93
                                      "foo", "bar"]))
 
94
        backend = keyring.get_keyring()
 
95
        self.assertTrue(isinstance(backend,
 
96
                                   keyring.backend.UncryptedFileKeyring))
 
97
 
 
98
    def test_load_specific_backend_with_path(self):
 
99
        keyring_path = os.path.join(os.path.dirname(keyring.__file__), 'tests')
 
100
        self.assertEqual(0, cli.main(["get",
 
101
                                      "-b", "test_cli.FakeKeyring",
 
102
                                      "-p", keyring_path,
 
103
                                      "foo", "bar"]))
 
104
 
 
105
        backend = keyring.get_keyring()
 
106
        # Somehow, this doesn't work, because the full dotted name of the class
 
107
        # is not the same as the one expected :(
 
108
        #self.assertTrue(isinstance(backend, FakeKeyring))
 
109
        self.assertEqual(FakeKeyring.PASSWORD, self.password_returned)
 
110
 
 
111
    def test_load_wrong_keyrings(self):
 
112
        self.assertRaises(SystemExit, cli.main,
 
113
                         ["get", "foo", "bar",
 
114
                          "-b", "blablabla" # ImportError
 
115
                         ])
 
116
        self.assertRaises(SystemExit, cli.main,
 
117
                         ["get", "foo", "bar",
 
118
                          "-b", "os.path.blabla" # AttributeError
 
119
                         ])
 
120
        self.assertRaises(SystemExit, cli.main,
 
121
                         ["get", "foo", "bar",
 
122
                          "-b", "__builtin__.str" # TypeError
 
123
                         ])
 
124
 
 
125
 
 
126
 
 
127
def test_suite():
 
128
    suite = unittest.TestSuite()
 
129
    suite.addTest(unittest.makeSuite(CommandLineTestCase))
 
130
    return suite
 
131
 
 
132
 
 
133
if __name__ == '__main__':
 
134
    unittest.main(defaultTest="test_suite")