|
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 'use strict'; |
|
5 |
|
6 const { store, search, remove } = require("sdk/passwords"); |
|
7 |
|
8 exports["test store requires `password` field"] = function(assert, done) { |
|
9 store({ |
|
10 username: "foo", |
|
11 realm: "bar", |
|
12 onComplete: function onComplete() { |
|
13 assert.fail("onComplete should not be called"); |
|
14 }, |
|
15 onError: function onError() { |
|
16 assert.pass("'`password` is required"); |
|
17 done(); |
|
18 } |
|
19 }); |
|
20 }; |
|
21 |
|
22 exports["test store requires `username` field"] = function(assert, done) { |
|
23 store({ |
|
24 password: "foo", |
|
25 realm: "bar", |
|
26 onComplete: function onComplete() { |
|
27 assert.fail("onComplete should not be called"); |
|
28 }, |
|
29 onError: function onError() { |
|
30 assert.pass("'`username` is required"); |
|
31 done(); |
|
32 } |
|
33 }); |
|
34 }; |
|
35 |
|
36 exports["test onComplete is optional"] = function(assert, done) { |
|
37 store({ |
|
38 realm: "bla", |
|
39 username: "bla", |
|
40 password: "bla", |
|
41 onError: function onError() { |
|
42 assert.fail("onError was called"); |
|
43 } |
|
44 }); |
|
45 assert.pass("exception is not thrown if `onComplete is missing") |
|
46 done(); |
|
47 }; |
|
48 |
|
49 exports["test exceptions in onComplete are reported"] = function(assert, done) { |
|
50 store({ |
|
51 realm: "throws", |
|
52 username: "error", |
|
53 password: "boom!", |
|
54 onComplete: function onComplete(error) { |
|
55 throw new Error("Boom!") |
|
56 }, |
|
57 onError: function onError(error) { |
|
58 assert.equal(error.message, "Boom!", "Error thrown is reported"); |
|
59 done(); |
|
60 } |
|
61 }); |
|
62 }; |
|
63 |
|
64 exports["test store requires `realm` field"] = function(assert, done) { |
|
65 store({ |
|
66 username: "foo", |
|
67 password: "bar", |
|
68 onComplete: function onComplete() { |
|
69 assert.fail("onComplete should not be called"); |
|
70 }, |
|
71 onError: function onError() { |
|
72 assert.pass("'`realm` is required"); |
|
73 done(); |
|
74 } |
|
75 }); |
|
76 }; |
|
77 |
|
78 exports["test can't store same login twice"] = function(assert, done) { |
|
79 store({ |
|
80 username: "user", |
|
81 password: "pass", |
|
82 realm: "realm", |
|
83 onComplete: function onComplete() { |
|
84 assert.pass("credential saved"); |
|
85 |
|
86 store({ |
|
87 username: "user", |
|
88 password: "pass", |
|
89 realm: "realm", |
|
90 onComplete: function onComplete() { |
|
91 assert.fail("onComplete should not be called"); |
|
92 }, |
|
93 onError: function onError() { |
|
94 assert.pass("re-saving credential failed"); |
|
95 |
|
96 remove({ |
|
97 username: "user", |
|
98 password: "pass", |
|
99 realm: "realm", |
|
100 onComplete: function onComplete() { |
|
101 assert.pass("credential was removed"); |
|
102 done(); |
|
103 }, |
|
104 onError: function onError() { |
|
105 assert.fail("remove should not fail"); |
|
106 } |
|
107 }); |
|
108 } |
|
109 }); |
|
110 }, |
|
111 onError: function onError() { |
|
112 assert.fail("onError should not be called"); |
|
113 } |
|
114 }); |
|
115 }; |
|
116 |
|
117 exports["test remove fails if no login found"] = function(assert, done) { |
|
118 remove({ |
|
119 username: "foo", |
|
120 password: "bar", |
|
121 realm: "baz", |
|
122 onComplete: function onComplete() { |
|
123 assert.fail("should not be able to remove unstored credentials"); |
|
124 }, |
|
125 onError: function onError() { |
|
126 assert.pass("can't remove unstored credentials"); |
|
127 done(); |
|
128 } |
|
129 }); |
|
130 }; |
|
131 |
|
132 exports["test addon associated credentials"] = function(assert, done) { |
|
133 store({ |
|
134 username: "foo", |
|
135 password: "bar", |
|
136 realm: "baz", |
|
137 onComplete: function onComplete() { |
|
138 search({ |
|
139 username: "foo", |
|
140 password: "bar", |
|
141 realm: "baz", |
|
142 onComplete: function onComplete([credential]) { |
|
143 assert.equal(credential.url.indexOf("addon:"), 0, |
|
144 "`addon:` uri is used for add-on credentials"); |
|
145 assert.equal(credential.username, "foo", |
|
146 "username matches"); |
|
147 assert.equal(credential.password, "bar", |
|
148 "password matches"); |
|
149 assert.equal(credential.realm, "baz", "realm matches"); |
|
150 assert.equal(credential.formSubmitURL, null, |
|
151 "`formSubmitURL` is `null` for add-on credentials"); |
|
152 assert.equal(credential.usernameField, "", "usernameField is empty"); |
|
153 assert.equal(credential.passwordField, "", "passwordField is empty"); |
|
154 |
|
155 remove({ |
|
156 username: credential.username, |
|
157 password: credential.password, |
|
158 realm: credential.realm, |
|
159 onComplete: function onComplete() { |
|
160 assert.pass("credential is removed"); |
|
161 done(); |
|
162 }, |
|
163 onError: function onError() { |
|
164 assert.fail("onError should not be called"); |
|
165 } |
|
166 }); |
|
167 }, |
|
168 onError: function onError() { |
|
169 assert.fail("onError should not be called"); |
|
170 } |
|
171 }); |
|
172 }, |
|
173 onError: function onError() { |
|
174 assert.fail("onError should not be called"); |
|
175 } |
|
176 }); |
|
177 }; |
|
178 |
|
179 exports["test web page associated credentials"] = function(assert, done) { |
|
180 store({ |
|
181 url: "http://bar.foo.com/authentication/?login", |
|
182 formSubmitURL: "http://login.foo.com/authenticate.cgi", |
|
183 username: "user", |
|
184 password: "pass", |
|
185 usernameField: "user-f", |
|
186 passwordField: "pass-f", |
|
187 onComplete: function onComplete() { |
|
188 search({ |
|
189 username: "user", |
|
190 password: "pass", |
|
191 url: "http://bar.foo.com", |
|
192 formSubmitURL: "http://login.foo.com", |
|
193 onComplete: function onComplete([credential]) { |
|
194 assert.equal(credential.url, "http://bar.foo.com", "url matches"); |
|
195 assert.equal(credential.username, "user", "username matches"); |
|
196 assert.equal(credential.password, "pass", "password matches"); |
|
197 assert.equal(credential.realm, null, "realm is null"); |
|
198 assert.equal(credential.formSubmitURL, "http://login.foo.com", |
|
199 "formSubmitURL matches"); |
|
200 assert.equal(credential.usernameField, "user-f", |
|
201 "usernameField is matches"); |
|
202 assert.equal(credential.passwordField, "pass-f", |
|
203 "passwordField matches"); |
|
204 |
|
205 remove({ |
|
206 url: credential.url, |
|
207 formSubmitURL: credential.formSubmitURL, |
|
208 username: credential.username, |
|
209 password: credential.password, |
|
210 usernameField: credential.usernameField, |
|
211 passwordField: credential.passwordField, |
|
212 |
|
213 onComplete: function onComplete() { |
|
214 assert.pass("credential is removed"); |
|
215 done(); |
|
216 }, |
|
217 onError: function onError(e) { |
|
218 assert.fail("onError should not be called"); |
|
219 } |
|
220 }); |
|
221 }, |
|
222 onError: function onError() { |
|
223 assert.fail("onError should not be called"); |
|
224 } |
|
225 }); |
|
226 }, |
|
227 onError: function onError() { |
|
228 assert.fail("onError should not be called"); |
|
229 } |
|
230 }); |
|
231 }; |
|
232 |
|
233 exports["test site authentication credentials"] = function(assert, done) { |
|
234 store({ |
|
235 url: "http://authentication.com", |
|
236 username: "U", |
|
237 password: "P", |
|
238 realm: "R", |
|
239 onComplete: function onComplete() { |
|
240 search({ |
|
241 url: "http://authentication.com", |
|
242 username: "U", |
|
243 password: "P", |
|
244 realm: "R", |
|
245 onComplete: function onComplete([credential]) { |
|
246 assert.equal(credential.url,"http://authentication.com", |
|
247 "url matches"); |
|
248 assert.equal(credential.username, "U", "username matches"); |
|
249 assert.equal(credential.password, "P", "password matches"); |
|
250 assert.equal(credential.realm, "R", "realm matches"); |
|
251 assert.equal(credential.formSubmitURL, null, "formSubmitURL is null"); |
|
252 assert.equal(credential.usernameField, "", "usernameField is empty"); |
|
253 assert.equal(credential.passwordField, "", "passwordField is empty"); |
|
254 |
|
255 remove({ |
|
256 url: credential.url, |
|
257 username: credential.username, |
|
258 password: credential.password, |
|
259 realm: credential.realm, |
|
260 onComplete: function onComplete() { |
|
261 assert.pass("credential is removed"); |
|
262 done(); |
|
263 }, |
|
264 onError: function onError() { |
|
265 assert.fail("onError should not be called"); |
|
266 } |
|
267 }); |
|
268 }, |
|
269 onError: function onError() { |
|
270 assert.fail("onError should not be called"); |
|
271 } |
|
272 }); |
|
273 }, |
|
274 onError: function onError() { |
|
275 assert.fail("onError should not be called"); |
|
276 } |
|
277 }); |
|
278 }; |
|
279 |
|
280 require("test").run(exports); |