From d046f64d0893eace050663c3a4d38a330943b398 Mon Sep 17 00:00:00 2001 From: Shane Tomlinson <stomlinson@mozilla.com> Date: Thu, 29 Mar 2012 13:09:04 +0100 Subject: [PATCH] Hook up the "prolong_session" wsapi call. * add network.prolongSession * add user.setComputerOwnershipStatus and user.isUsersComputer. setComputerOwnershipSTatus calls network.prolongSession if called with true. * in controllers/is_this_your_computer, call user.setComputerOwnershipStatus issue #1349 --- .../controllers/is_this_your_computer.js | 12 ++---- resources/static/shared/error-messages.js | 4 ++ resources/static/shared/network.js | 25 ++++++++++- resources/static/shared/user.js | 36 +++++++++++++++- .../controllers/is_this_your_computer.js | 23 +++++++++-- resources/static/test/cases/shared/network.js | 19 +++++++++ resources/static/test/cases/shared/user.js | 41 +++++++++++++++++++ resources/static/test/mocks/xhr.js | 5 ++- 8 files changed, 150 insertions(+), 15 deletions(-) diff --git a/resources/static/dialog/controllers/is_this_your_computer.js b/resources/static/dialog/controllers/is_this_your_computer.js index 1c2bc2c9e..182e60c01 100644 --- a/resources/static/dialog/controllers/is_this_your_computer.js +++ b/resources/static/dialog/controllers/is_this_your_computer.js @@ -8,8 +8,6 @@ BrowserID.Modules.IsThisYourComputer = (function() { var bid = BrowserID, user = bid.User, - network = bid.Network, - storage = bid.Storage, errors = bid.Errors, email; @@ -30,21 +28,19 @@ BrowserID.Modules.IsThisYourComputer = (function() { }, yes: function() { - // TODO - Move this to user.js where it could be used by other clients in - // other areas. - storage.usersComputer.setConfirmed(network.userid()); this.confirmed(true); }, no: function() { - storage.usersComputer.setDenied(network.userid()); this.confirmed(false); }, confirmed: function(status) { - this.publish("user_computer_status_set", { users_computer: status }); + var self=this; + user.setComputerOwnershipStatus(status, function() { + self.close("user_computer_status_set", { users_computer: status }); + }, self.getErrorDialog(errors.setComputerOwnershipStatus)); } - }); diff --git a/resources/static/shared/error-messages.js b/resources/static/shared/error-messages.js index 48f122424..2ce74cfde 100644 --- a/resources/static/shared/error-messages.js +++ b/resources/static/shared/error-messages.js @@ -113,6 +113,10 @@ BrowserID.Errors = (function(){ title: "Remove Email Address from Account" }, + setComputerOwnershipStatus: { + title: "Setting whether the user owns the computer" + }, + setPassword: { title: "Setting Password" }, diff --git a/resources/static/shared/network.js b/resources/static/shared/network.js index 7e8834285..676e60dca 100644 --- a/resources/static/shared/network.js +++ b/resources/static/shared/network.js @@ -60,7 +60,7 @@ BrowserID.Network = (function() { function clearContext() { xhr.clearContext(); var undef; - context = server_time = auth_status = undef; + context = server_time = auth_status = userid = undef; } function handleAuthenticationResponse(type, onComplete, onFailure, status) { @@ -548,6 +548,7 @@ BrowserID.Network = (function() { }, /** + * TODO - move this into user. * Return the user's userid, which will an integer if the user * is authenticated, undefined otherwise. * @@ -634,6 +635,28 @@ BrowserID.Network = (function() { complete(onComplete, enabled); }, onFailure); + }, + + /** + * Prolong a user's session so that they are not re-prompted to enter their + * password + * @method prolongSession + * @param {function} [onComplete] - Called whenever complete. + * @param {function} [onFailure] - Called on XHR failure. + */ + prolongSession: function(onComplete, onFailure) { + Network.checkAuth(function(authenticated) { + if(authenticated) { + post({ + url: "/wsapi/prolong_session", + success: onComplete, + error: onFailure + }); + } + else { + complete(onFailure, "user not authenticated"); + } + }, onFailure); } }; diff --git a/resources/static/shared/user.js b/resources/static/shared/user.js index e1fe4502f..6719b94fc 100644 --- a/resources/static/shared/user.js +++ b/resources/static/shared/user.js @@ -14,7 +14,8 @@ BrowserID.User = (function() { User, pollTimeout, provisioning = bid.Provisioning, addressCache = {}, - primaryAuthCache = {}; + primaryAuthCache = {}, + complete = bid.Helpers.complete; function prepareDeps() { if (!jwk) { @@ -1122,6 +1123,39 @@ BrowserID.User = (function() { } onComplete(hasSecondary); + }, + + /** + * Set whether the user owns the computer or not. + * @method setComputerOwnershipStatus + * @param {boolean} userOwnsComputer - true if user owns computer, false otw. + * @param {function} onComplete - called on successful completion. + * @param {function} onFailure - called on XHR failure. + */ + setComputerOwnershipStatus: function(userOwnsComputer, onComplete, onFailure) { + var userID = network.userid(); + if(typeof userID !== "undefined") { + if (userOwnsComputer) { + storage.usersComputer.setConfirmed(userID); + network.prolongSession(onComplete, onFailure); + } + else { + storage.usersComputer.setDenied(userID); + complete(onComplete); + } + } else { + complete(onFailure, "user is not authenticated"); + } + }, + + isUsersComputer: function(onComplete, onFailure) { + var userID = network.userid(); + if(typeof userID !== "undefined") { + complete(onComplete, storage.usersComputer.confirmed(userID)); + } else { + complete(onFailure, "user is not authenticated"); + } + } }; diff --git a/resources/static/test/cases/controllers/is_this_your_computer.js b/resources/static/test/cases/controllers/is_this_your_computer.js index 8e6702c9b..9e4945fd0 100644 --- a/resources/static/test/cases/controllers/is_this_your_computer.js +++ b/resources/static/test/cases/controllers/is_this_your_computer.js @@ -10,6 +10,7 @@ el = $("body"), bid = BrowserID, user = bid.User, + network = bid.Network, xhr = bid.Mocks.xhr, modules = bid.Modules, testHelpers = bid.TestHelpers, @@ -39,12 +40,26 @@ controller.start(options || {}); } - test("yes - sets ownership flag to true for the user", function() { - console.log("add a test"); + asyncTest("yes - sets ownership flag to true for the user", function() { + createController(); + network.authenticate("testuser@testuser.com", "password", function() { + register("user_computer_status_set", function(msg, data) { + equal(data.users_computer, true, "user_computer_status_set called with correct status"); + start(); + }); + controller.yes(); + }, testHelpers.unexpectedXHRFailure); }); - test("no - set the ownership flag to false for the user", function() { - console.log("add a test"); + asyncTest("no - set the ownership flag to false for the user", function() { + createController(); + network.authenticate("testuser@testuser.com", "password", function() { + register("user_computer_status_set", function(msg, data) { + equal(data.users_computer, false, "user_computer_status_set called with correct status"); + start(); + }); + controller.no(); + }, testHelpers.unexpectedXHRFailure); }); }()); diff --git a/resources/static/test/cases/shared/network.js b/resources/static/test/cases/shared/network.js index c30bcf46d..d2fc75083 100644 --- a/resources/static/test/cases/shared/network.js +++ b/resources/static/test/cases/shared/network.js @@ -565,4 +565,23 @@ } }); + asyncTest("prolongSession with authenticated user, success - call complete", function() { + network.authenticate("testuser@testuser.com", "password", function() { + network.prolongSession(function() { + ok(true, "prolongSession completed"); + start(); + }, testHelpers.unexpectedXHRFailure); + }, testHelpers.unexpectedXHRFailure); + }); + + asyncTest("prolongSession with unauthenticated user - call failure", function() { + transport.useResult("unauthenticated"); + network.prolongSession(testHelpers.unexpectedSuccess, testHelpers.expectedXHRFailure); + }); + + asyncTest("prolongSession with XHR Failure - call failure", function() { + transport.useResult("ajaxError"); + network.prolongSession(testHelpers.unexpectedSuccess, testHelpers.expectedXHRFailure); + }); + }()); diff --git a/resources/static/test/cases/shared/user.js b/resources/static/test/cases/shared/user.js index 976b55de0..20ece75b7 100644 --- a/resources/static/test/cases/shared/user.js +++ b/resources/static/test/cases/shared/user.js @@ -1113,4 +1113,45 @@ var vep = require("./vep"); }, testHelpers.unexpectedXHRFailure); }); + asyncTest("setComputerOwnershipStatus with true, isUsersComputer - mark the computer as the users, prolongs the user's session", function() { + lib.authenticate("testuser@testuser.com", "password", function() { + storage.usersComputer.clear(network.userid()); + lib.setComputerOwnershipStatus(true, function() { + lib.isUsersComputer(function(usersComputer) { + equal(usersComputer, true, "user is marked as owner of computer"); + start(); + }, testHelpers.unexpectedXHRFailure); + }, testHelpers.unexpectedXHRFailure); + }, testHelpers.unexpectedXHRFailure); + }); + + asyncTest("setComputerOwnershipStatus with false, isUsersComputer - mark the computer as not the users", function() { + lib.authenticate("testuser@testuser.com", "password", function() { + storage.usersComputer.clear(network.userid()); + lib.setComputerOwnershipStatus(false, function() { + lib.isUsersComputer(function(usersComputer) { + equal(usersComputer, false, "user is marked as not an owner"); + start(); + }, testHelpers.unexpectedXHRFailure); + }, testHelpers.unexpectedXHRFailure); + }, testHelpers.unexpectedXHRFailure); + }); + + asyncTest("setComputerOwnershipStatus with unauthenticated user - call onFailure", function() { + lib.setComputerOwnershipStatus(false, + testHelpers.unexpectedSuccess, + testHelpers.expectedXHRFailure + ); + }); + + asyncTest("setComputerOwnershipStatus with true & XHR Failure - call onFailure", function() { + lib.authenticate("testuser@testuser.com", "password", function() { + xhr.useResult("ajaxError"); + lib.setComputerOwnershipStatus(true, + testHelpers.unexpectedSuccess, + testHelpers.expectedXHRFailure + ); + }, testHelpers.unexpectedXHRFailure); + }); + }()); diff --git a/resources/static/test/mocks/xhr.js b/resources/static/test/mocks/xhr.js index e33be1b0d..8876767f3 100644 --- a/resources/static/test/mocks/xhr.js +++ b/resources/static/test/mocks/xhr.js @@ -111,7 +111,10 @@ BrowserID.Mocks.xhr = (function() { "get /wsapi/address_info?email=testuser%40testuser.com primary": { type: "primary", auth: "https://auth_url", prov: "https://prov_url" }, "get /wsapi/address_info?email=testuser%40testuser.com ajaxError": undefined, "post /wsapi/add_email_with_assertion invalid": { success: false }, - "post /wsapi/add_email_with_assertion valid": { success: true } + "post /wsapi/add_email_with_assertion valid": { success: true }, + "post /wsapi/prolong_session valid": { success: true }, + "post /wsapi/prolong_session unauthenticated": 400, + "post /wsapi/prolong_session ajaxError": undefined, }, setContextInfo: function(field, value) { -- GitLab