aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.4/packages/addon-kit/tests/test-passwords.js
blob: e799b481e735426585e665a87fcf8079ef60079e (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"use strict";

const { store, search, remove } = require("passwords");

exports["test store requires `password` field"] = function(assert, done) {
  store({
    username: "foo",
    realm: "bar",
    onComplete: function onComplete() {
      assert.fail("onComplete should not be called");
    },
    onError: function onError() {
      assert.pass("'`password` is required");
      done();
    }
  });
};

exports["test store requires `username` field"] = function(assert, done) {
  store({
    password: "foo",
    realm: "bar",
    onComplete: function onComplete() {
      assert.fail("onComplete should not be called");
    },
    onError: function onError() {
      assert.pass("'`username` is required");
      done();
    }
  });
};

exports["test onComplete is optional"] = function(assert, done) {
  store({
    realm: "bla",
    username: "bla",
    password: "bla",
    onError: function onError() {
      assert.fail("onError was called");
    }
  });
  assert.pass("exception is not thrown if `onComplete is missing")
  done();
};

exports["test exceptions in onComplete are reported"] = function(assert, done) {
  store({
    realm: "throws",
    username: "error",
    password: "boom!",
    onComplete: function onComplete(error) {
      throw new Error("Boom!")
    },
    onError: function onError(error) {
      assert.equal(error.message, "Boom!", "Error thrown is reported");
      done();
    }
  });
};

exports["test store requires `realm` field"] = function(assert, done) {
  store({
    username: "foo",
    password: "bar",
    onComplete: function onComplete() {
      assert.fail("onComplete should not be called");
    },
    onError: function onError() {
      assert.pass("'`realm` is required");
      done();
    }
  });
};

exports["test can't store same login twice"] = function(assert, done) {
  store({
    username: "user",
    password: "pass",
    realm: "realm",
    onComplete: function onComplete() {
      assert.pass("credential saved");

      store({
        username: "user",
        password: "pass",
        realm: "realm",
        onComplete: function onComplete() {
          assert.fail("onComplete should not be called");
        },
        onError: function onError() {
          assert.pass("re-saving credential failed");

          remove({
            username: "user",
            password: "pass",
            realm: "realm",
            onComplete: function onComplete() {
              assert.pass("credential was removed");
              done();
            },
            onError: function onError() {
              assert.fail("remove should not fail");
            }
          });
        }
      });
    },
    onError: function onError() {
      assert.fail("onError should not be called");
    }
  });
};

exports["test remove fails if no login found"] = function(assert, done) {
  remove({
    username: "foo",
    password: "bar",
    realm: "baz",
    onComplete: function onComplete() {
      assert.fail("should not be able to remove unstored credentials");
    },
    onError: function onError() {
      assert.pass("can't remove unstored credentials");
      done();
    }
  });
};

exports["test addon associated credentials"] = function(assert, done) {
  store({
    username: "foo",
    password: "bar",
    realm: "baz",
    onComplete: function onComplete() {
      search({
        username: "foo",
        password: "bar",
        realm: "baz",
        onComplete: function onComplete([credential]) {
          assert.equal(credential.url.indexOf("addon:"), 0,
                       "`addon:` uri is used for add-on credentials");
          assert.equal(credential.username, "foo",
                       "username matches");
          assert.equal(credential.password, "bar",
                       "password matches");
          assert.equal(credential.realm, "baz", "realm matches");
          assert.equal(credential.formSubmitURL, null,
                       "`formSubmitURL` is `null` for add-on credentials");
          assert.equal(credential.usernameField, "", "usernameField is empty");
          assert.equal(credential.passwordField, "", "passwordField is empty");

          remove({
            username: credential.username,
            password: credential.password,
            realm: credential.realm,
            onComplete: function onComplete() {
              assert.pass("credential is removed");
              done();
            },
            onError: function onError() {
              assert.fail("onError should not be called");
            }
          });
        },
        onError: function onError() {
          assert.fail("onError should not be called");
        }
      });
    },
    onError: function onError() {
      assert.fail("onError should not be called");
    }
  });
};

exports["test web page associated credentials"] = function(assert, done) {
  store({
    url: "http://bar.foo.com/authentication/?login",
    formSubmitURL: "http://login.foo.com/authenticate.cgi",
    username: "user",
    password: "pass",
    usernameField: "user-f",
    passwordField: "pass-f",
    onComplete: function onComplete() {
      search({
        username: "user",
        password: "pass",
        url: "http://bar.foo.com",
        formSubmitURL: "http://login.foo.com",
        onComplete: function onComplete([credential]) {
          assert.equal(credential.url, "http://bar.foo.com", "url matches");
          assert.equal(credential.username, "user", "username matches");
          assert.equal(credential.password, "pass", "password matches");
          assert.equal(credential.realm, null, "realm is null");
          assert.equal(credential.formSubmitURL, "http://login.foo.com",
                       "formSubmitURL matches");
          assert.equal(credential.usernameField, "user-f",
                       "usernameField is matches");
          assert.equal(credential.passwordField, "pass-f",
                       "passwordField matches");

          remove({
            url: credential.url,
            formSubmitURL: credential.formSubmitURL,
            username: credential.username,
            password: credential.password,
            usernameField: credential.usernameField,
            passwordField: credential.passwordField,

            onComplete: function onComplete() {
              assert.pass("credential is removed");
              done();
            },
            onError: function onError(e) {
              assert.fail("onError should not be called");
            }
          });
        },
        onError: function onError() {
          assert.fail("onError should not be called");
        }
      });
    },
    onError: function onError() {
      assert.fail("onError should not be called");
    }
  });
};

exports["test site authentication credentials"] = function(assert, done) {
  store({
    url: "http://authentication.com",
    username: "U",
    password: "P",
    realm: "R",
    onComplete: function onComplete() {
      search({
        url: "http://authentication.com",
        username: "U",
        password: "P",
        realm: "R",
        onComplete: function onComplete([credential]) {
          assert.equal(credential.url,"http://authentication.com",
                       "url matches");
          assert.equal(credential.username, "U", "username matches");
          assert.equal(credential.password, "P", "password matches");
          assert.equal(credential.realm, "R", "realm matches");
          assert.equal(credential.formSubmitURL, null, "formSubmitURL is null");
          assert.equal(credential.usernameField, "", "usernameField is empty");
          assert.equal(credential.passwordField, "", "passwordField is empty");

          remove({
            url: credential.url,
            username: credential.username,
            password: credential.password,
            realm: credential.realm,
            onComplete: function onComplete() {
              assert.pass("credential is removed");
              done();
            },
            onError: function onError() {
              assert.fail("onError should not be called");
            }
          });
        },
        onError: function onError() {
          assert.fail("onError should not be called");
        }
      });
    },
    onError: function onError() {
      assert.fail("onError should not be called");
    }
  });
};

require("test").run(exports);