Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/ */
4 "use strict";
6 const {utils: Cu} = Components;
8 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
9 Cu.import("resource://gre/modules/Services.jsm");
10 Cu.import("resource://services-common/utils.js");
11 Cu.import("resource://testing-common/httpd.js");
13 XPCOMUtils.defineLazyModuleGetter(this, "FxAccountsMgmtService",
14 "resource://gre/modules/FxAccountsMgmtService.jsm",
15 "FxAccountsMgmtService");
17 // At end of test, restore original state
18 const ORIGINAL_AUTH_URI = Services.prefs.getCharPref("identity.fxaccounts.auth.uri");
19 let { SystemAppProxy } = Cu.import("resource://gre/modules/FxAccountsMgmtService.jsm");
20 const ORIGINAL_SENDCUSTOM = SystemAppProxy._sendCustomEvent;
21 do_register_cleanup(function() {
22 Services.prefs.setCharPref("identity.fxaccounts.auth.uri", ORIGINAL_AUTH_URI);
23 SystemAppProxy._sendCustomEvent = ORIGINAL_SENDCUSTOM;
24 });
26 // Make profile available so that fxaccounts can store user data
27 do_get_profile();
29 // Mock the system app proxy; make message passing possible
30 let mockSendCustomEvent = function(aEventName, aMsg) {
31 Services.obs.notifyObservers({wrappedJSObject: aMsg}, aEventName, null);
32 };
34 function run_test() {
35 run_next_test();
36 }
38 add_task(function test_overall() {
39 do_check_neq(FxAccountsMgmtService, null);
40 });
42 // Check that invalid email capitalization is corrected on signIn.
43 // https://github.com/mozilla/fxa-auth-server/blob/master/docs/api.md#post-v1accountlogin
44 add_test(function test_invalidEmailCase_signIn() {
45 do_test_pending();
46 let clientEmail = "greta.garbo@gmail.com";
47 let canonicalEmail = "Greta.Garbo@gmail.COM";
48 let attempts = 0;
50 function writeResp(response, msg) {
51 if (typeof msg === "object") {
52 msg = JSON.stringify(msg);
53 }
54 response.bodyOutputStream.write(msg, msg.length);
55 }
57 // Mock of the fxa accounts auth server, reproducing the behavior of
58 // /account/login when email capitalization is incorrect on signIn.
59 let server = httpd_setup({
60 "/account/login": function(request, response) {
61 response.setHeader("Content-Type", "application/json");
62 attempts += 1;
64 // Ensure we don't get in an endless loop
65 if (attempts > 2) {
66 response.setStatusLine(request.httpVersion, 429, "Sorry, you had your chance");
67 writeResp(response, {});
68 return;
69 }
71 let body = CommonUtils.readBytesFromInputStream(request.bodyInputStream);
72 let jsonBody = JSON.parse(body);
73 let email = jsonBody.email;
75 // The second time through, the accounts client will call the api with
76 // the correct email capitalization.
77 if (email == canonicalEmail) {
78 response.setStatusLine(request.httpVersion, 200, "Yay");
79 writeResp(response, {
80 uid: "your-uid",
81 sessionToken: "your-sessionToken",
82 keyFetchToken: "your-keyFetchToken",
83 verified: true,
84 authAt: 1392144866,
85 });
86 return;
87 }
89 // If the client has the wrong case on the email, we return a 400, with
90 // the capitalization of the email as saved in the accounts database.
91 response.setStatusLine(request.httpVersion, 400, "Incorrect email case");
92 writeResp(response, {
93 code: 400,
94 errno: 120,
95 error: "Incorrect email case",
96 email: canonicalEmail,
97 });
98 return;
99 },
100 });
102 // Point the FxAccountsClient's hawk rest request client to the mock server
103 Services.prefs.setCharPref("identity.fxaccounts.auth.uri", server.baseURI);
105 // Receive a mozFxAccountsChromeEvent message
106 function onMessage(subject, topic, data) {
107 let message = subject.wrappedJSObject;
109 switch (message.id) {
110 // When we signed in as "Greta.Garbo", the server should have told us
111 // that the proper capitalization is really "greta.garbo". Call
112 // getAccounts to get the signed-in user and ensure that the
113 // capitalization is correct.
114 case "signIn":
115 FxAccountsMgmtService.handleEvent({
116 detail: {
117 id: "getAccounts",
118 data: {
119 method: "getAccounts",
120 }
121 }
122 });
123 break;
125 // Having initially signed in as "Greta.Garbo", getAccounts should show
126 // us that the signed-in user has the properly-capitalized email,
127 // "greta.garbo".
128 case "getAccounts":
129 Services.obs.removeObserver(onMessage, "mozFxAccountsChromeEvent");
131 do_check_eq(message.data.accountId, canonicalEmail);
133 do_test_finished();
134 server.stop(run_next_test);
135 break;
137 // We should not receive any other mozFxAccountsChromeEvent messages
138 default:
139 do_throw("wat!");
140 break;
141 }
142 }
144 Services.obs.addObserver(onMessage, "mozFxAccountsChromeEvent", false);
146 SystemAppProxy._sendCustomEvent = mockSendCustomEvent;
148 // Trigger signIn using an email with incorrect capitalization
149 FxAccountsMgmtService.handleEvent({
150 detail: {
151 id: "signIn",
152 data: {
153 method: "signIn",
154 accountId: clientEmail,
155 password: "123456",
156 },
157 },
158 });
159 });
161 // End of tests
162 // Utility functions follow
164 function httpd_setup (handlers, port=-1) {
165 let server = new HttpServer();
166 for (let path in handlers) {
167 server.registerPathHandler(path, handlers[path]);
168 }
169 try {
170 server.start(port);
171 } catch (ex) {
172 dump("ERROR starting server on port " + port + ". Already a process listening?");
173 do_throw(ex);
174 }
176 // Set the base URI for convenience.
177 let i = server.identity;
178 server.baseURI = i.primaryScheme + "://" + i.primaryHost + ":" + i.primaryPort;
180 return server;
181 }