This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 264
/
internal_api.js
193 lines (166 loc) · 6.87 KB
/
internal_api.js
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
/*jshint browsers:true, forin: true, laxbreak: true */
/*global test: true, start: true, module: true, ok: true, equal: true, BrowserID:true */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
(function() {
"use strict";
var bid = BrowserID,
internal = bid.internal,
network = bid.Network,
user = bid.User,
xhr = bid.Mocks.xhr,
origin = "https://login.persona.org",
storage = bid.Storage,
moduleManager = bid.module,
testHelpers = bid.TestHelpers;
function ModuleMock() {}
ModuleMock.prototype = {
init: function() {},
start: function() {}
};
module("resources/internal_api", {
setup: function() {
testHelpers.setup();
moduleManager.reset();
moduleManager.register("dialog", ModuleMock);
moduleManager.start("dialog");
},
teardown: function() {
testHelpers.teardown();
}
});
test("make sure internal api namespace is there", function() {
ok(bid.internal, "BrowserID.internal exists");
});
asyncTest("BrowserID.internal.setPersistent unauthenticated user", function() {
internal.setPersistent(origin, function(status) {
strictEqual(status, null, "user is not authenticated should not succeed in setting persistent");
strictEqual(typeof storage.site.get(origin, "remember"), "undefined", "remember status not set");
strictEqual(typeof storage.site.get(origin, "email"), "undefined", "email not set");
start();
});
});
asyncTest("BrowserID.internal.setPersistent with authenticated user", function() {
user.authenticate("testuser@testuser.com", "password", function() {
internal.setPersistent(origin, function(status) {
equal(status, true, "setPersistent status reported as true");
equal(storage.site.get(origin, "remember"), true, "remember status set to true");
start();
});
});
});
asyncTest("BrowserID.internal.get with silent: true, non-authenticated user - returns null assertion", function() {
internal.get(origin, function(assertion) {
strictEqual(assertion, null, "user not logged in, assertion impossible to get");
start();
}, {
requiredEmail: "testuser@testuser.com",
silent: true
});
});
asyncTest("BrowserID.internal.get with silent: true, authenticated user, no requiredEmail, and no email address associated with site - not enough info to generate an assertion", function() {
user.authenticate("testuser@testuser.com", "password", function() {
internal.get(origin, function(assertion) {
strictEqual(assertion, null, "not enough info to generate an assertion, assertion should not be generated");
start();
}, {
silent: true
});
});
});
asyncTest("BrowserID.internal.get with silent: true, authenticated user, no requiredEmail, email address associated with site, XHR failure - return null assertion.", function() {
user.authenticate("testuser@testuser.com", "password", function() {
user.syncEmails(function() {
storage.site.set(origin, "email", "testuser@testuser.com");
xhr.useResult("invalid");
internal.get(origin, function(assertion) {
strictEqual(assertion, null, "XHR failure while getting assertion");
start();
}, {
silent: true
});
});
});
});
asyncTest("BrowserID.internal.get with silent: true, authenticated user, no requiredEmail, email address associated with site - use info stored for site to get assertion", function() {
user.authenticate("testuser@testuser.com", "password", function() {
user.syncEmails(function() {
storage.site.set(origin, "email", "testuser@testuser.com");
internal.get(origin, function(assertion) {
ok(assertion, "assertion generated using stored email address for site.");
start();
}, {
silent: true
});
});
});
});
asyncTest("BrowserID.internal.get with silent: true, authenticated user, requiredEmail set to uncontrolled email address - return null assertion", function() {
user.authenticate("testuser@testuser.com", "password", function() {
// email addresses will not be synced just because we authenticated.
// Depending on get to do the sync.
internal.get(origin, function(assertion) {
strictEqual(assertion, null, "uncontrolled email address returns null assertion");
start();
}, {
silent: true,
requiredEmail: "invalid@testuser.com"
});
});
});
asyncTest("BrowserID.internal.get with silent: true, authenticated user, requiredEmail and XHR error - return null assertion", function() {
user.authenticate("testuser@testuser.com", "password", function() {
xhr.useResult("invalid");
internal.get(origin, function(assertion) {
strictEqual(assertion, null, "unregistered email address returns null assertion");
start();
}, {
silent: true,
requiredEmail: "invalid@testuser.com"
});
});
});
asyncTest("BrowserID.internal.get with silent: true, authenticated user, requiredEmail, and registered email address - return an assertion", function() {
user.authenticate("testuser@testuser.com", "password", function() {
internal.get(origin, function(assertion) {
ok(assertion, "assertion has been returned");
start();
}, {
silent: true,
requiredEmail: "testuser@testuser.com"
});
});
});
asyncTest("BrowserID.internal.get with dialog - simulate the user return of an assertion", function() {
var controllerOrigin;
ModuleMock.prototype.get = function(getOrigin, options, onsuccess, onerror) {
controllerOrigin = getOrigin;
// simulate the full dialog flow.
onsuccess("simulated_assertion");
};
internal.get(origin, function onComplete(assertion) {
equal(controllerOrigin, origin, "correct origin passed");
equal(assertion, "simulated_assertion", "Kosher assertion");
start();
}, {});
});
asyncTest("BrowserID.internal.get with dialog with failure - simulate the return of a null assertion", function() {
ModuleMock.prototype.get = function(getOrigin, options, onsuccess, onerror) {
onerror();
};
internal.get(origin, function onComplete(assertion) {
equal(assertion, null, "on failure, assertion is null");
start();
}, {});
});
asyncTest("BrowserID.internal.get with dialog with user cancellation - return null assertion", function() {
ModuleMock.prototype.get = function(getOrigin, options, onsuccess, onerror) {
onsuccess(null);
};
internal.get(origin, function onComplete(assertion) {
equal(assertion, null, "on cancel, assertion is null");
start();
}, {});
});
}());