| |
1 /* Any copyright is dedicated to the Public Domain. |
| |
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
| |
3 |
| |
4 "use strict"; |
| |
5 |
| |
6 const {utils: Cu} = Components; |
| |
7 |
| |
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"); |
| |
12 |
| |
13 XPCOMUtils.defineLazyModuleGetter(this, "FxAccountsMgmtService", |
| |
14 "resource://gre/modules/FxAccountsMgmtService.jsm", |
| |
15 "FxAccountsMgmtService"); |
| |
16 |
| |
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 }); |
| |
25 |
| |
26 // Make profile available so that fxaccounts can store user data |
| |
27 do_get_profile(); |
| |
28 |
| |
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 }; |
| |
33 |
| |
34 function run_test() { |
| |
35 run_next_test(); |
| |
36 } |
| |
37 |
| |
38 add_task(function test_overall() { |
| |
39 do_check_neq(FxAccountsMgmtService, null); |
| |
40 }); |
| |
41 |
| |
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; |
| |
49 |
| |
50 function writeResp(response, msg) { |
| |
51 if (typeof msg === "object") { |
| |
52 msg = JSON.stringify(msg); |
| |
53 } |
| |
54 response.bodyOutputStream.write(msg, msg.length); |
| |
55 } |
| |
56 |
| |
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; |
| |
63 |
| |
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 } |
| |
70 |
| |
71 let body = CommonUtils.readBytesFromInputStream(request.bodyInputStream); |
| |
72 let jsonBody = JSON.parse(body); |
| |
73 let email = jsonBody.email; |
| |
74 |
| |
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 } |
| |
88 |
| |
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 }); |
| |
101 |
| |
102 // Point the FxAccountsClient's hawk rest request client to the mock server |
| |
103 Services.prefs.setCharPref("identity.fxaccounts.auth.uri", server.baseURI); |
| |
104 |
| |
105 // Receive a mozFxAccountsChromeEvent message |
| |
106 function onMessage(subject, topic, data) { |
| |
107 let message = subject.wrappedJSObject; |
| |
108 |
| |
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; |
| |
124 |
| |
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"); |
| |
130 |
| |
131 do_check_eq(message.data.accountId, canonicalEmail); |
| |
132 |
| |
133 do_test_finished(); |
| |
134 server.stop(run_next_test); |
| |
135 break; |
| |
136 |
| |
137 // We should not receive any other mozFxAccountsChromeEvent messages |
| |
138 default: |
| |
139 do_throw("wat!"); |
| |
140 break; |
| |
141 } |
| |
142 } |
| |
143 |
| |
144 Services.obs.addObserver(onMessage, "mozFxAccountsChromeEvent", false); |
| |
145 |
| |
146 SystemAppProxy._sendCustomEvent = mockSendCustomEvent; |
| |
147 |
| |
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 }); |
| |
160 |
| |
161 // End of tests |
| |
162 // Utility functions follow |
| |
163 |
| |
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 } |
| |
175 |
| |
176 // Set the base URI for convenience. |
| |
177 let i = server.identity; |
| |
178 server.baseURI = i.primaryScheme + "://" + i.primaryHost + ":" + i.primaryPort; |
| |
179 |
| |
180 return server; |
| |
181 } |
| |
182 |
| |
183 |