|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 const { store, search, remove } = require("sdk/passwords/utils"); |
|
8 |
|
9 exports["test store requires `password` field"] = function(assert) { |
|
10 assert.throws(function() { |
|
11 store({ username: "foo", realm: "bar" }); |
|
12 }, "`password` is required"); |
|
13 }; |
|
14 |
|
15 exports["test store requires `username` field"] = function(assert) { |
|
16 assert.throws(function() { |
|
17 store({ password: "foo", realm: "bar" }); |
|
18 }, "`username` is required"); |
|
19 }; |
|
20 |
|
21 exports["test store requires `realm` field"] = function(assert) { |
|
22 assert.throws(function() { |
|
23 store({ username: "foo", password: "bar" }); |
|
24 }, "`password` is required"); |
|
25 }; |
|
26 |
|
27 exports["test can't store same login twice"] = function(assert) { |
|
28 let options = { username: "user", password: "pass", realm: "realm" }; |
|
29 store(options); |
|
30 assert.throws(function() { |
|
31 store(options); |
|
32 }, "can't store same pass twice"); |
|
33 remove(options); |
|
34 }; |
|
35 |
|
36 exports["test remove throws if no login found"] = function(assert) { |
|
37 assert.throws(function() { |
|
38 remove({ username: "foo", password: "bar", realm: "baz" }); |
|
39 }, "can't remove unstored credentials"); |
|
40 }; |
|
41 |
|
42 exports["test addon associated credentials"] = function(assert) { |
|
43 let options = { username: "foo", password: "bar", realm: "baz" }; |
|
44 store(options); |
|
45 |
|
46 assert.ok(search().length, "credential was stored"); |
|
47 assert.ok(search(options).length, "stored credential found"); |
|
48 assert.ok(search({ username: options.username }).length, "found by username"); |
|
49 assert.ok(search({ password: options.password }).length, "found by password"); |
|
50 assert.ok(search({ realm: options.realm }).length, "found by realm"); |
|
51 |
|
52 let credential = search(options)[0]; |
|
53 assert.equal(credential.url.indexOf("addon:"), 0, |
|
54 "`addon:` uri is used for add-on associated credentials"); |
|
55 assert.equal(credential.username, options.username, "username matches"); |
|
56 assert.equal(credential.password, options.password, "password matches"); |
|
57 assert.equal(credential.realm, options.realm, "realm matches"); |
|
58 assert.equal(credential.formSubmitURL, null, |
|
59 "`formSubmitURL` is `null` for add-on associated credentials"); |
|
60 assert.equal(credential.usernameField, "", "usernameField is empty"); |
|
61 assert.equal(credential.passwordField, "", "passwordField is empty"); |
|
62 |
|
63 remove(search(options)[0]); |
|
64 assert.ok(!search(options).length, "remove worked"); |
|
65 }; |
|
66 |
|
67 exports["test web page associated credentials"] = function(assert) { |
|
68 let options = { |
|
69 url: "http://www.example.com", |
|
70 formSubmitURL: "http://login.example.com", |
|
71 username: "user", |
|
72 password: "pass", |
|
73 usernameField: "user-f", |
|
74 passwordField: "pass-f" |
|
75 }; |
|
76 store({ |
|
77 url: "http://www.example.com/login", |
|
78 formSubmitURL: "http://login.example.com/foo/authenticate.cgi", |
|
79 username: options.username, |
|
80 password: options.password, |
|
81 usernameField: options.usernameField, |
|
82 passwordField: options.passwordField |
|
83 }); |
|
84 |
|
85 assert.ok(search().length, "credential was stored"); |
|
86 assert.ok(search(options).length, "stored credential found"); |
|
87 assert.ok(search({ username: options.username }).length, "found by username"); |
|
88 assert.ok(search({ password: options.password }).length, "found by password"); |
|
89 assert.ok(search({ formSubmitURL: options.formSubmitURL }).length, |
|
90 "found by formSubmitURL"); |
|
91 assert.ok(search({ usernameField: options.usernameField }).length, |
|
92 "found by usernameField"); |
|
93 assert.ok(search({ passwordField: options.passwordField }).length, |
|
94 "found by passwordField"); |
|
95 |
|
96 let credential = search(options)[0]; |
|
97 assert.equal(credential.url, options.url, "url matches"); |
|
98 assert.equal(credential.username, options.username, "username matches"); |
|
99 assert.equal(credential.password, options.password, "password matches"); |
|
100 assert.equal(credential.realm, null, "realm is "); |
|
101 assert.equal(credential.formSubmitURL, options.formSubmitURL, |
|
102 "`formSubmitURL` matches"); |
|
103 assert.equal(credential.usernameField, options.usernameField, |
|
104 "usernameField matches"); |
|
105 assert.equal(credential.passwordField, options.passwordField, |
|
106 "passwordField matches"); |
|
107 |
|
108 remove(search(options)[0]); |
|
109 assert.ok(!search(options).length, "remove worked"); |
|
110 }; |
|
111 |
|
112 exports["test site authentication credentials"] = function(assert) { |
|
113 let options = { |
|
114 url: "http://test.authentication.com", |
|
115 username: "u", |
|
116 password: "p", |
|
117 realm: "r" |
|
118 }; |
|
119 |
|
120 store(options); |
|
121 assert.ok(search().length, "credential was stored"); |
|
122 assert.ok(search(options).length, "stored credential found"); |
|
123 assert.ok(search({ username: options.username }).length, "found by username"); |
|
124 assert.ok(search({ password: options.password }).length, "found by password"); |
|
125 assert.ok(search({ realm: options.realm }).length, "found by realm"); |
|
126 assert.ok(search({ url: options.url }).length, "found by url"); |
|
127 |
|
128 let credential = search(options)[0]; |
|
129 assert.equal(credential.url, options.url, "url matches"); |
|
130 assert.equal(credential.username, options.username, "username matches"); |
|
131 assert.equal(credential.password, options.password, "password matches"); |
|
132 assert.equal(credential.realm, options.realm, "realm matches"); |
|
133 assert.equal(credential.formSubmitURL, null, |
|
134 "`formSubmitURL` is `null` for site authentication credentials"); |
|
135 assert.equal(credential.usernameField, "", "usernameField is empty"); |
|
136 assert.equal(credential.passwordField, "", "passwordField is empty"); |
|
137 |
|
138 remove(search(options)[0]); |
|
139 assert.ok(!search(options).length, "remove worked"); |
|
140 }; |
|
141 |
|
142 require("test").run(exports); |