0d9cb1bd62df4ab20386ab60b6e36c5bdc6144b8
[platform/upstream/libsecret.git] / libsecret / tests / test-store-password.js
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2.1 of the licence or (at
7  * your option) any later version.
8  *
9  * See the included COPYING file for more information.
10  */
11
12 const Mock = imports.gi.MockService;
13 const Secret = imports.gi.Secret;
14 const GLib = imports.gi.GLib;
15
16 const JsUnit = imports.jsUnit;
17 const assertEquals = JsUnit.assertEquals;
18 const assertRaises = JsUnit.assertRaises;
19 const assertTrue = JsUnit.assertTrue;
20
21 Mock.start("mock-service-normal.py");
22
23 const STORE_SCHEMA = new Secret.Schema.new("org.mock.Schema",
24         Secret.SchemaFlags.NONE,
25         {
26                 "number": Secret.SchemaAttributeType.INTEGER,
27                 "string": Secret.SchemaAttributeType.STRING,
28                 "even": Secret.SchemaAttributeType.BOOLEAN,
29         }
30 );
31
32 /* Synchronous */
33
34 var attributes = { "number": "9", "string": "nine", "even": "false" };
35
36 var password = Secret.password_lookup_sync (STORE_SCHEMA, attributes, null);
37 assertEquals(null, password);
38
39 var stored = Secret.password_store_sync (STORE_SCHEMA, attributes, Secret.COLLECTION_DEFAULT,
40                                          "The number nine", "999", null);
41 assertEquals(true, stored);
42
43 var password = Secret.password_lookup_sync (STORE_SCHEMA, attributes, null);
44 assertEquals("999", password);
45
46
47 /* Asynchronous */ 
48
49 var attributes = { "number": "888", "string": "eight", "even": "true" };
50
51 var password = Secret.password_lookup_sync (STORE_SCHEMA, attributes, null);
52 assertEquals(null, password);
53
54 var loop = new GLib.MainLoop.new(null, false);
55
56 Secret.password_store (STORE_SCHEMA, attributes, null, "The number eight", "888",
57                        null, function(source, result) {
58         loop.quit();
59         var stored = Secret.password_store_finish(result);
60         assertEquals(true, stored);
61 });
62
63 loop.run();
64
65 var password = Secret.password_lookup_sync (STORE_SCHEMA, attributes, null);
66 assertEquals("888", password);
67
68 Mock.stop();