f7e0169600018b88cd51194358ad0ec0fd4de3d0
[platform/upstream/libsecret.git] / libsecret / tests / test-clear-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": "1", "string": "one", "even": "false" };
35
36 var password = Secret.password_lookup_sync (STORE_SCHEMA, attributes, null);
37 assertEquals("111", password);
38
39 var deleted = Secret.password_clear_sync (STORE_SCHEMA, attributes, null);
40 assertEquals(true, deleted);
41
42 var password = Secret.password_lookup_sync (STORE_SCHEMA, attributes, null);
43 assertEquals(null, password);
44
45 var deleted = Secret.password_clear_sync (STORE_SCHEMA, attributes, null);
46 assertEquals(false, deleted);
47
48 /* Asynchronous */ 
49
50 var attributes = { "number": "2", "string": "two", "even": "true" };
51
52 var password = Secret.password_lookup_sync (STORE_SCHEMA, attributes, null);
53 assertEquals("222", password);
54
55 var loop = new GLib.MainLoop.new(null, false);
56
57 Secret.password_clear (STORE_SCHEMA, attributes,
58                        null, function(source, result) {
59         loop.quit();
60         var deleted = Secret.password_clear_finish(result);
61         assertEquals(true, deleted);
62 });
63
64 loop.run();
65
66 var password = Secret.password_lookup_sync (STORE_SCHEMA, attributes, null);
67 assertEquals(null, password);
68
69 Secret.password_clear (STORE_SCHEMA, attributes,
70         null, function(source, result) {
71         loop.quit();
72         var deleted = Secret.password_clear_finish(result);
73         assertEquals(false, deleted);
74 });
75
76 loop.run();
77
78 Mock.stop();