From d743601c17a650e189f9c4bf1cab926b94363e45 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 2 Jul 2012 12:09:30 +0200 Subject: added integration test for login * added a small hook in srp to set a, A and Astr for testing * moved generic functions for tests to SpecHelper --- javascript/spec/DjangoSpecRunner.html | 3 ++- javascript/spec/login.js | 46 +++++++++++++++++++++++++++++++++++ javascript/spec/signup.js | 20 ++++----------- javascript/spec/specHelper.js | 17 +++++++++++++ javascript/srp.js | 10 ++++++++ 5 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 javascript/spec/specHelper.js (limited to 'javascript') diff --git a/javascript/spec/DjangoSpecRunner.html b/javascript/spec/DjangoSpecRunner.html index 7e045e4..dfa0b8c 100644 --- a/javascript/spec/DjangoSpecRunner.html +++ b/javascript/spec/DjangoSpecRunner.html @@ -23,6 +23,7 @@ + @@ -64,7 +65,7 @@ Username: Password: - + diff --git a/javascript/spec/login.js b/javascript/spec/login.js index e69de29..d73f9d7 100644 --- a/javascript/spec/login.js +++ b/javascript/spec/login.js @@ -0,0 +1,46 @@ +describe("Login", function() { + + beforeEach(function() { + this.srp = new SRP(); + this.xhr = sinon.useFakeXMLHttpRequest(); + var requests = this.requests = []; + this.xhr.onCreate = function (xhr) { + requests.push(xhr); + }; + }); + + afterEach(function() { + this.xhr.restore(); + }); + + it("has an identify function", function() { + expect(typeof this.srp.identify).toBe('function'); + }); + + it("logs in successfully (INTEGRATION)", function(){ + var callback = sinon.spy(); + var a = 'af141ae6'; + var B = '887005895b1f5528b4e4dfdce914f73e763b96d3c901d2f41d8b8cd26255a75'; + var salt = '5d3055e0acd3ddcfc15'; + var M = 'be6d7db2186d5f6a2c55788479b6eaf75229a7ca0d9e7dc1f886f1970a0e8065' + var M2 = '2547cf26318519090f506ab73a68995a2626b1c948e6f603ef9e1b0b78bf0f7b'; + srp = new SRP() + srp.success = callback; + var A = srp.calculateAndSetA(a); + srp.identify(); + + expect(this.requests.length).toBe(1); + expect(this.requests[0].url).toBe("handshake/"); + expect(this.requests[0].requestBody).toBe("I=user&A=" + A); + specHelper.respondXML(this.requests[0], ""); + expect(this.requests.length).toBe(2); + expect(this.requests[1].url).toBe("authenticate/"); + expect(this.requests[1].requestBody).toBe("M=" + M); + specHelper.respondXML(this.requests[1], ""+M2+""); + + expect(callback).toHaveBeenCalled(); + expect(window.location.hash).toBe("#logged_in") + }); + + +}); diff --git a/javascript/spec/signup.js b/javascript/spec/signup.js index fb5c014..8ead93b 100644 --- a/javascript/spec/signup.js +++ b/javascript/spec/signup.js @@ -1,5 +1,3 @@ - - describe("Signup", function() { beforeEach(function() { @@ -15,7 +13,7 @@ describe("Signup", function() { this.xhr.restore(); }); - it("instantiates SRP with a register function", function() { + it("has a register function", function() { expect(typeof this.srp.register).toBe('function'); }); @@ -25,7 +23,7 @@ describe("Signup", function() { this.srp.register(); expect(this.requests.length).toBe(1); - respondXML(this.requests[0], "5d3055e0acd3ddcfc15"); + specHelper.respondXML(this.requests[0], "5d3055e0acd3ddcfc15"); expect(callback.called).toBeTruthy(); }); @@ -35,7 +33,7 @@ describe("Signup", function() { this.srp.register(); expect(this.requests.length).toBe(1); - respondXML(this.requests[0], "5d3055e0acd3ddcfc15"); + specHelper.respondXML(this.requests[0], "5d3055e0acd3ddcfc15"); expect(callback).toHaveBeenCalledWith("adcd57b4a4a05c2e205b0b7b30014d9ff635d8d8db2f502f08e9b9c132800c44"); }); @@ -46,23 +44,15 @@ describe("Signup", function() { expect(this.requests.length).toBe(1); expect(this.requests[0].url).toBe("register/salt/"); expect(this.requests[0].requestBody).toBe("I=user"); - respondXML(this.requests[0], "5d3055e0acd3ddcfc15"); + specHelper.respondXML(this.requests[0], "5d3055e0acd3ddcfc15"); expect(this.requests.length).toBe(2); expect(this.requests[1].url).toBe("register/user/"); expect(this.requests[1].requestBody).toBe("v=adcd57b4a4a05c2e205b0b7b30014d9ff635d8d8db2f502f08e9b9c132800c44"); - respondXML(this.requests[1], ""); + specHelper.respondXML(this.requests[1], ""); expect(callback).toHaveBeenCalled(); }); - // HELPERS - - function respondXML(request, content) { - header = { "Content-Type": "application/xml;charset=utf-8" }; - body = '\n'; - body += content; - request.respond(200, header, body); - } }); diff --git a/javascript/spec/specHelper.js b/javascript/spec/specHelper.js new file mode 100644 index 0000000..e923310 --- /dev/null +++ b/javascript/spec/specHelper.js @@ -0,0 +1,17 @@ +var specHelper = (function() { + // HELPERS + + function respondXML(request, content) { + header = { "Content-Type": "application/xml;charset=utf-8" }; + body = '\n'; + body += content; + request.respond(200, header, body); + } + + var originalBigInteger = BigInteger; + + return { + respondXML: respondXML + } + +})(); diff --git a/javascript/srp.js b/javascript/srp.js index 7021bfa..9aa47b9 100644 --- a/javascript/srp.js +++ b/javascript/srp.js @@ -28,6 +28,16 @@ function SRP() // *** Accessor methods *** + // allows setting the random number A for testing + + this.calculateAndSetA = function(_a) + { + a = new BigInteger(_a, 16); + A = g.modPow(a, N); + Astr = A.toString(16); + return Astr; + }; + // Returns the user's identity this.getI = function() { -- cgit v1.2.3