summaryrefslogtreecommitdiff
path: root/libsecret/test-js-store.js
blob: 167517263715e5ad31622d521b3b2ae286adc010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * Copyright 2012 Red Hat Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the licence or (at
 * your option) any later version.
 *
 * See the included COPYING file for more information.
 */

const Mock = imports.gi.MockService;
const Secret = imports.gi.Secret;
const GLib = imports.gi.GLib;

const JsUnit = imports.jsUnit;
const assertEquals = JsUnit.assertEquals;
const assertRaises = JsUnit.assertRaises;
const assertTrue = JsUnit.assertTrue;

Mock.start("mock-service-normal.py");

const STORE_SCHEMA = new Secret.Schema("org.mock.Schema",
	Secret.SchemaFlags.NONE,
	{
		"number": Secret.SchemaAttributeType.INTEGER,
		"string": Secret.SchemaAttributeType.STRING,
		"even": Secret.SchemaAttributeType.BOOLEAN,
	}
);

/* Synchronous */

var attributes = { "number": "9", "string": "nine", "even": "false" };

var password = Secret.password_lookup_sync (STORE_SCHEMA, attributes, null);
assertEquals(null, password);

var stored = Secret.password_store_sync (STORE_SCHEMA, attributes, Secret.COLLECTION_DEFAULT,
                                         "The number nine", "999", null);
assertEquals(true, stored);

var password = Secret.password_lookup_sync (STORE_SCHEMA, attributes, null);
assertEquals("999", password);


/* Asynchronous */ 

var attributes = { "number": "888", "string": "eight", "even": "true" };

var password = Secret.password_lookup_sync (STORE_SCHEMA, attributes, null);
assertEquals(null, password);

var loop = new GLib.MainLoop(null, false);

Secret.password_store (STORE_SCHEMA, attributes, null, "The number eight", "888",
                       null, function(source, result) {
	loop.quit();
	var stored = Secret.password_store_finish(result);
	assertEquals(true, stored);
});

loop.run();

var password = Secret.password_lookup_sync (STORE_SCHEMA, attributes, null);
assertEquals("888", password);

Secret.Service.disconnect();
Mock.stop();