Newer
Older
Shane Tomlinson
committed
/*jshint browser: true, forin: true, laxbreak: true */
/*global test: true, start: true, stop: 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/. */
Shane Tomlinson
committed
"use strict";
Shane Tomlinson
committed
var controller,
Shane Tomlinson
committed
el = $("body"),
bid = BrowserID,
storage = bid.Storage,
network = bid.Network,
xhr = bid.Mocks.xhr,
Shane Tomlinson
committed
emailRegistered = false,
userCreated = true,
registrations = [],
testHelpers = bid.TestHelpers,
register = testHelpers.register,
provisioning = bid.Mocks.Provisioning;
Shane Tomlinson
committed
function reset() {
emailRegistered = false;
userCreated = true;
}
function createController(options) {
Shane Tomlinson
committed
options = options || {};
controller = bid.Modules.Authenticate.create();
controller.start(options);
module("dialog/js/modules/authenticate", {
Shane Tomlinson
committed
setup: function() {
reset();
Shane Tomlinson
committed
$("input[type=password]").hide();
Shane Tomlinson
committed
},
teardown: function() {
if (controller) {
try {
controller.destroy();
} catch(e) {
// may already be destroyed from close inside of the controller.
}
Shane Tomlinson
committed
}
Shane Tomlinson
committed
reset();
Shane Tomlinson
committed
}
Shane Tomlinson
committed
});
Shane Tomlinson
committed
asyncTest("email declared in options - prefill address field", function() {
controller.destroy();
$("#email").val("");
Shane Tomlinson
committed
createController({ email: "registered@testuser.com",
ready: function() {
equal($("#email").val(), "registered@testuser.com", "email prefilled");
equal($("input[type=password]").is(":visible"), false, "password is not shown");
start();
}
});
});
asyncTest("known secondary email declared in options - show password field", function() {
controller.destroy();
$("#email").val("");
createController({
email: "registered@testuser.com",
type: "secondary",
known: true,
ready: function() {
equal($("#email").val(), "registered@testuser.com", "email prefilled");
equal($("input[type=password]").is(":visible"), true, "password is shown");
start();
}
});
Shane Tomlinson
committed
});
function testUserUnregistered() {
register("new_user", function(msg, info, rehydrate) {
ok(info.email, "new_user triggered with info.email");
// rehydration email used to go back to authentication controller if
// the user cancels one of the next steps.
ok(rehydrate.email, "new_user triggered with rehydrate.email");
start();
});
controller.checkEmail();
}
asyncTest("checkEmail with unknown secondary email - 'new_user' message", function() {
$("#email").val("unregistered@testuser.com");
Shane Tomlinson
committed
xhr.useResult("unknown_secondary");
testUserUnregistered();
});
asyncTest("checkEmail with email with leading/trailing whitespace, user not registered - 'new_user' message", function() {
$("#email").val(" unregistered@testuser.com ");
Shane Tomlinson
committed
xhr.useResult("unknown_secondary");
testUserUnregistered();
});
asyncTest("checkEmail with normal email, user registered - 'enter_password' message", function() {
$("#email").val("registered@testuser.com");
Shane Tomlinson
committed
xhr.useResult("known_secondary");
Shane Tomlinson
committed
register("enter_password", function() {
ok(true, "email was valid, user registered");
start();
});
controller.checkEmail();
});
Shane Tomlinson
committed
asyncTest("clear password if user changes email address", function() {
xhr.useResult("known_secondary");
$("#email").val("registered@testuser.com");
var enterPasswordCount = 0;
mediator.subscribe("enter_password", function() {
// The first time the password is shown, change the email address. The
// second time the password is shown, make sure the password was cleared.
Shane Tomlinson
committed
if(enterPasswordCount === 0) {
Shane Tomlinson
committed
// simulate the user changing the email address. This should clear the
// password.
$("#password").val("password");
$("#email").val("testuser@testuser.com");
$("#email").keyup();
controller.checkEmail();
}
else {
equal($("#password").val(), "", "password field was cleared");
start();
}
enterPasswordCount++;
});
controller.checkEmail();
});
Shane Tomlinson
committed
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
asyncTest("do not clear password if user selects an email address using autofill, then presses a key that does not change the address (CTRL-C for instance)", function() {
xhr.useResult("known_secondary");
// This test is for issue #406
// First, see the staps after this handler.
mediator.subscribe("enter_password", function() {
// The user is now looking at the password field and they decide to copy
// from the email field by hitting CTRL-C.
//
// Simulates the user hitting a key that does not change the
// input. The user should NOT go back to the "enter_email" state at this
// point.
var enterEmailCount = 0;
mediator.subscribe("enter_email", function() {
enterEmailCount++;
});
$("#email").keyup();
equal(enterEmailCount, 0, "enter_email not called after submit if keyup did not change email field");
start();
});
// Simulates the user selecting testuser@testuser.com from the
// autocomplete menu.
$("#email").val("registered@testuser.com");
$("#email").change();
// Simulate the user hitting the "next" button. Once the address is
// verified, the enter_password message will be triggered.
controller.submit();
});
asyncTest("checkEmail with email that has IdP support - 'primary_user' message", function() {
$("#email").val("unregistered@testuser.com");
xhr.useResult("primary");
register("primary_user", function(msg, info) {
equal(info.email, "unregistered@testuser.com", "email correctly passed");
equal(info.auth, "https://auth_url", "IdP authentication URL passed");
equal(info.prov, "https://prov_url", "IdP provisioning URL passed");
start();
});
controller.checkEmail();
});
function testAuthenticated() {
Shane Tomlinson
committed
register("authenticated", function() {
ok(true, "user authenticated as expected");
start();
});
controller.authenticate();
}
asyncTest("normal authentication is kosher", function() {
$("#email").val("registered@testuser.com");
$("#password").val("password");
testAuthenticated();
});
asyncTest("leading/trailing whitespace on the username is stripped for authentication", function() {
$("#email").val(" registered@testuser.com ");
$("#password").val("password");
testAuthenticated();
});
Shane Tomlinson
committed
asyncTest("forgotPassword - trigger forgot_password message", function() {
Shane Tomlinson
committed
$("#email").val("registered@testuser.com");
register("forgot_password", function(msg, info) {
equal(info.email, "registered@testuser.com", "forgot_password with correct email triggered");
Shane Tomlinson
committed
});
controller.forgotPassword();
});
asyncTest("createUser with valid email", function() {
Shane Tomlinson
committed
$("#email").val("unregistered@testuser.com");
xhr.useResult("unknown_secondary");
register("new_user", function(msg, info) {
equal(info.email, "unregistered@testuser.com", "new_user with correct email triggered");
Shane Tomlinson
committed
});
controller.createUser();
});
asyncTest("createUser with invalid email", function() {
Shane Tomlinson
committed
$("#email").val("unregistered");
var handlerCalled = false;
register("new_user", function(msg, info) {
Shane Tomlinson
committed
handlerCalled = true;
});
controller.createUser(function() {
equal(handlerCalled, false, "bad jiji, new_user should not have been called with invalid email");
start();
});
Shane Tomlinson
committed
});