1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/fxaccounts/tests/mochitest/file_invalidEmailCase.sjs Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,80 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +/** 1.8 + * This server simulates the behavior of /account/login on the Firefox Accounts 1.9 + * auth server in the case where the user is trying to sign in with an email 1.10 + * with the wrong capitalization. 1.11 + * 1.12 + * https://github.com/mozilla/fxa-auth-server/blob/master/docs/api.md#post-v1accountlogin 1.13 + * 1.14 + * The expected behavior is that on the first attempt, with the wrong email, 1.15 + * the server will respond with a 400 and the canonical email capitalization 1.16 + * that the client should use. The client then has one chance to sign in with 1.17 + * this different capitalization. 1.18 + * 1.19 + * In this test, the user with the account id "Greta.Garbo@gmail.COM" initially 1.20 + * tries to sign in as "greta.garbo@gmail.com". 1.21 + * 1.22 + * On success, the client is responsible for updating its sign-in user state 1.23 + * and recording the proper email capitalization. 1.24 + */ 1.25 + 1.26 +const CC = Components.Constructor; 1.27 +const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1", 1.28 + "nsIBinaryInputStream", 1.29 + "setInputStream"); 1.30 + 1.31 +const goodEmail = "Greta.Garbo@gmail.COM"; 1.32 +const badEmail = "greta.garbo@gmail.com"; 1.33 + 1.34 +function handleRequest(request, response) { 1.35 + let body = new BinaryInputStream(request.bodyInputStream); 1.36 + let bytes = []; 1.37 + let available; 1.38 + while ((available = body.available()) > 0) { 1.39 + Array.prototype.push.apply(bytes, body.readByteArray(available)); 1.40 + } 1.41 + 1.42 + let data = JSON.parse(String.fromCharCode.apply(null, bytes)); 1.43 + let message; 1.44 + 1.45 + switch (data.email) { 1.46 + case badEmail: 1.47 + // Almost - try again with fixed email case 1.48 + message = { 1.49 + code: 400, 1.50 + errno: 120, 1.51 + error: "Incorrect email case", 1.52 + email: goodEmail, 1.53 + }; 1.54 + response.setStatusLine(request.httpVersion, 400, "Almost"); 1.55 + break; 1.56 + 1.57 + case goodEmail: 1.58 + // Successful login. 1.59 + message = { 1.60 + uid: "your-uid", 1.61 + sessionToken: "your-sessionToken", 1.62 + keyFetchToken: "your-keyFetchToken", 1.63 + verified: true, 1.64 + authAt: 1392144866, 1.65 + }; 1.66 + response.setStatusLine(request.httpVersion, 200, "Yay"); 1.67 + break; 1.68 + 1.69 + default: 1.70 + // Anything else happening in this test is a failure. 1.71 + message = { 1.72 + code: 400, 1.73 + errno: 999, 1.74 + error: "What happened!?", 1.75 + }; 1.76 + response.setStatusLine(request.httpVersion, 400, "Ouch"); 1.77 + break; 1.78 + } 1.79 + 1.80 + messageStr = JSON.stringify(message); 1.81 + response.bodyOutputStream.write(messageStr, messageStr.length); 1.82 +} 1.83 +