michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: /** michael@0: * This server simulates the behavior of /account/login on the Firefox Accounts michael@0: * auth server in the case where the user is trying to sign in with an email michael@0: * with the wrong capitalization. michael@0: * michael@0: * https://github.com/mozilla/fxa-auth-server/blob/master/docs/api.md#post-v1accountlogin michael@0: * michael@0: * The expected behavior is that on the first attempt, with the wrong email, michael@0: * the server will respond with a 400 and the canonical email capitalization michael@0: * that the client should use. The client then has one chance to sign in with michael@0: * this different capitalization. michael@0: * michael@0: * In this test, the user with the account id "Greta.Garbo@gmail.COM" initially michael@0: * tries to sign in as "greta.garbo@gmail.com". michael@0: * michael@0: * On success, the client is responsible for updating its sign-in user state michael@0: * and recording the proper email capitalization. michael@0: */ michael@0: michael@0: const CC = Components.Constructor; michael@0: const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1", michael@0: "nsIBinaryInputStream", michael@0: "setInputStream"); michael@0: michael@0: const goodEmail = "Greta.Garbo@gmail.COM"; michael@0: const badEmail = "greta.garbo@gmail.com"; michael@0: michael@0: function handleRequest(request, response) { michael@0: let body = new BinaryInputStream(request.bodyInputStream); michael@0: let bytes = []; michael@0: let available; michael@0: while ((available = body.available()) > 0) { michael@0: Array.prototype.push.apply(bytes, body.readByteArray(available)); michael@0: } michael@0: michael@0: let data = JSON.parse(String.fromCharCode.apply(null, bytes)); michael@0: let message; michael@0: michael@0: switch (data.email) { michael@0: case badEmail: michael@0: // Almost - try again with fixed email case michael@0: message = { michael@0: code: 400, michael@0: errno: 120, michael@0: error: "Incorrect email case", michael@0: email: goodEmail, michael@0: }; michael@0: response.setStatusLine(request.httpVersion, 400, "Almost"); michael@0: break; michael@0: michael@0: case goodEmail: michael@0: // Successful login. michael@0: message = { michael@0: uid: "your-uid", michael@0: sessionToken: "your-sessionToken", michael@0: keyFetchToken: "your-keyFetchToken", michael@0: verified: true, michael@0: authAt: 1392144866, michael@0: }; michael@0: response.setStatusLine(request.httpVersion, 200, "Yay"); michael@0: break; michael@0: michael@0: default: michael@0: // Anything else happening in this test is a failure. michael@0: message = { michael@0: code: 400, michael@0: errno: 999, michael@0: error: "What happened!?", michael@0: }; michael@0: response.setStatusLine(request.httpVersion, 400, "Ouch"); michael@0: break; michael@0: } michael@0: michael@0: messageStr = JSON.stringify(message); michael@0: response.bodyOutputStream.write(messageStr, messageStr.length); michael@0: } michael@0: