tests: Fix up the javascript and python tests
[platform/upstream/libsecret.git] / libsecret / tests / test-lookup-password.py
1 #!/usr/bin/env python
2
3 #
4 # Copyright 2012 Red Hat Inc.
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser General Public License as published
8 # by the Free Software Foundation; either version 2.1 of the licence or (at
9 # your option) any later version.
10 #
11 # See the included COPYING file for more information.
12 #
13
14 import unittest
15
16 from gi.repository import MockService as Mock
17 from gi.repository import Secret, SecretUnstable, GLib
18
19 STORE_SCHEMA = Secret.Schema.new("org.mock.Schema",
20         Secret.SchemaFlags.NONE,
21         {
22                 "number": Secret.SchemaAttributeType.INTEGER,
23                 "string": Secret.SchemaAttributeType.STRING,
24                 "even": Secret.SchemaAttributeType.BOOLEAN,
25         }
26 )
27
28 class TestLookup(unittest.TestCase):
29         def setUp(self):
30                 Mock.start("mock-service-normal.py")
31
32         def tearDown(self):
33                 SecretUnstable.Service.disconnect()
34                 Mock.stop()
35
36         def testSynchronous(self):
37                 password = Secret.password_lookup_sync (STORE_SCHEMA, { "number": "1", "even": "false" }, None)
38                 self.assertEqual("111", password)
39
40         def testSyncNotFound(self):
41                 password = Secret.password_lookup_sync (STORE_SCHEMA, { "number": "5", "even": "true" }, None)
42                 self.assertEqual(None, password)
43
44         def testAsynchronous(self):
45                 loop = GLib.MainLoop(None)
46
47                 def on_result_ready(source, result, unused):
48                         loop.quit()
49                         password = Secret.password_lookup_finish(result)
50                         self.assertEquals("222", password)
51
52                 Secret.password_lookup (STORE_SCHEMA, { "number": "2", "string": "two" },
53                                         None, on_result_ready, None)
54
55                 loop.run()
56
57         def testAsyncNotFound(self):
58                 loop = GLib.MainLoop(None)
59
60                 def on_result_ready(source, result, unused):
61                         loop.quit()
62                         password = Secret.password_lookup_finish(result)
63                         self.assertEquals(None, password)
64
65                 Secret.password_lookup (STORE_SCHEMA, { "number": "7", "string": "five" },
66                                         None, on_result_ready, None)
67
68                 loop.run()
69
70 if __name__ == '__main__':
71                 unittest.main()