From f0b308e4081a4c804da4f7bfbe4802a8999d4c26 Mon Sep 17 00:00:00 2001 From: Azul Date: Sat, 4 Aug 2012 16:01:05 +0200 Subject: copied jqueryRest and restful specs from django no real change yet --- lib/jqueryRest.js | 124 ++++++++++++++++++++++++++++++++++++++++++++ lib/srp.js | 4 +- spec/DjangoSpecRunner.html | 4 +- spec/RestfulSpecRunner.html | 75 +++++++++++++++++++++++++++ spec/django/login.js | 67 ++++++++++++++++++++++++ spec/django/signup.js | 59 +++++++++++++++++++++ spec/login.js | 67 ------------------------ spec/restful/.login.js.swp | Bin 0 -> 12288 bytes spec/restful/.signup.js.swp | Bin 0 -> 12288 bytes spec/restful/login.js | 67 ++++++++++++++++++++++++ spec/restful/signup.js | 59 +++++++++++++++++++++ spec/signup.js | 59 --------------------- 12 files changed, 455 insertions(+), 130 deletions(-) create mode 100644 lib/jqueryRest.js create mode 100644 spec/RestfulSpecRunner.html create mode 100644 spec/django/login.js create mode 100644 spec/django/signup.js delete mode 100644 spec/login.js create mode 100644 spec/restful/.login.js.swp create mode 100644 spec/restful/.signup.js.swp create mode 100644 spec/restful/login.js create mode 100644 spec/restful/signup.js delete mode 100644 spec/signup.js diff --git a/lib/jqueryRest.js b/lib/jqueryRest.js new file mode 100644 index 0000000..926dc6c --- /dev/null +++ b/lib/jqueryRest.js @@ -0,0 +1,124 @@ +jqueryRest = function() { + + function getUrl() + { + return ""; + } + + function paths(path) + { + return path + } + + // Perform ajax requests at the specified path, with the specified parameters + // Calling back the specified function. + function ajaxRequest(relative_path, params, callback) + { + var full_url = this.geturl() + this.paths(relative_path); + if( window.XMLHttpRequest) + xhr = new XMLHttpRequest(); + else if (window.ActiveXObject){ + try{ + xhr = new ActiveXObject("Microsoft.XMLHTTP"); + }catch (e){} + } + else + { + session.error_message("Ajax not supported."); + return; + } + if(xhr){ + xhr.onreadystatechange = function() { + if(xhr.readyState == 4 && xhr.status == 200) { + callback(parseResponse()); + } + }; + xhr.open("POST", full_url, true); + xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); + xhr.setRequestHeader("Content-length", params.length); + xhr.send(params); + } + else + { + session.error_message("Ajax failed."); + } + }; + + function parseResponse() { + if (responseIsXML()) { + return parseXML(xhr.responseXML); + } else if (responseIsJSON()) { + return JSON.parse(xhr.responseText); + } + }; + + function responseIsXML() { + return (xhr.responseType == 'document') || + (xhr.getResponseHeader("Content-Type").indexOf('application/xml') >= 0) + } + + function responseIsJSON() { + return (xhr.responseType == 'json') || + (xhr.getResponseHeader("Content-Type").indexOf('application/json') >= 0) + } + + function parseXML(xml) { + if (xml.getElementsByTagName("r").length > 0) { + return parseAttributesOfElement(xml.getElementsByTagName("r")[0]); + } else { + return parseNodes(xml.childNodes); + } + }; + + function parseAttributesOfElement(elem) { + var response = {}; + for (var i = 0; i < elem.attributes.length; i++) { + var attrib = elem.attributes[i]; + if (attrib.specified) { + response[attrib.name] = attrib.value; + } + } + return response; + }; + + function parseNodes(nodes) { + var response = {}; + for (var i = 0; i < nodes.length; i++) { + var node = nodes[i]; + response[node.tagName] = node.textContent || true; + } + return response; + }; + + function register(I, callback) + { + this.ajaxRequest("register/salt/", "I="+I, callback); + } + + function sendVerifier(v, callback) { + this.ajaxRequest("register/user/", "v="+v, callback); + } + + function handshake(I, Astr, callback) { + this.ajaxRequest("handshake/", "I="+I+"&A="+Astr, callback); + } + + function authenticate(M, callback) { + this.ajaxRequest("authenticate/", "M="+M, callback); + } + + function upgrade(M, callback) { + this.ajaxRequest("upgrade/authenticate/", "M="+M, callback); + } + + return { + geturl: getUrl, + paths: paths, + ajaxRequest: ajaxRequest, + register: register, + register_send_verifier: sendVerifier, + handshake: handshake, + authenticate: authenticate, + upgrade: upgrade + } +} diff --git a/lib/srp.js b/lib/srp.js index 9ef75f5..a5a2c14 100644 --- a/lib/srp.js +++ b/lib/srp.js @@ -1,4 +1,4 @@ -function SRP() +function SRP(remote) { // Variables session will be used in the SRP protocol var Nstr = "115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3"; @@ -22,7 +22,7 @@ function SRP() var authenticated = false; var I = document.getElementById("srp_username").value; var p = document.getElementById("srp_password").value; - var remote = plainXHR(); + remote = remote || plainXHR(); // *** Accessor methods *** diff --git a/spec/DjangoSpecRunner.html b/spec/DjangoSpecRunner.html index 8315c5b..7a39dde 100644 --- a/spec/DjangoSpecRunner.html +++ b/spec/DjangoSpecRunner.html @@ -25,8 +25,8 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + +
Username:
Password:
+ +
+ + diff --git a/spec/django/login.js b/spec/django/login.js new file mode 100644 index 0000000..eea6062 --- /dev/null +++ b/spec/django/login.js @@ -0,0 +1,67 @@ +describe("Login", function() { + + it("has an identify function", function() { + var srp = new SRP(); + expect(typeof srp.identify).toBe('function'); + }); + + describe("(INTEGRATION)", function (){ + // a valid auth attempt for the user / password given in the spec runner: + var a = 'af141ae6'; + var B = '887005895b1f5528b4e4dfdce914f73e763b96d3c901d2f41d8b8cd26255a75'; + var salt = '5d3055e0acd3ddcfc15'; + var M = 'be6d7db2186d5f6a2c55788479b6eaf75229a7ca0d9e7dc1f886f1970a0e8065' + var M2 = '2547cf26318519090f506ab73a68995a2626b1c948e6f603ef9e1b0b78bf0f7b'; + var A, callback; + + + beforeEach(function() { + this.srp = new SRP(); + A = this.srp.calculateAndSetA(a); + + specHelper.setupFakeXHR.apply(this); + + this.srp.success = sinon.spy(); + }); + + afterEach(function() { + this.xhr.restore(); + }); + + it("works with XML responses", function(){ + this.srp.identify(); + + this.expectRequest('handshake/', 'I=user&A='+A); + this.respondXML(""); + this.expectRequest('authenticate/', 'M='+M); + this.respondXML(""+M2+""); + + expect(this.srp.success).toHaveBeenCalled(); + }); + + it("works with JSON responses", function(){ + this.srp.identify(); + + this.expectRequest('handshake/', 'I=user&A='+A); + this.respondJSON({s: salt, B: B}); + this.expectRequest('authenticate/', 'M='+M); + this.respondJSON({M: M2}); + + expect(this.srp.success).toHaveBeenCalled(); + }); + + it("rejects B = 0", function(){ + this.srp.error_message = sinon.spy(); + this.srp.identify(); + + this.expectRequest('handshake/', 'I=user&A='+A); + this.respondJSON({s: salt, B: 0}); + // aborting if B=0 + expect(this.requests).toEqual([]); + expect(this.srp.error_message).toHaveBeenCalled(); + }); + }); + + +}); + diff --git a/spec/django/signup.js b/spec/django/signup.js new file mode 100644 index 0000000..b38778a --- /dev/null +++ b/spec/django/signup.js @@ -0,0 +1,59 @@ +describe("Signup", function() { + + beforeEach(function() { + this.srp = new SRP(); + specHelper.setupFakeXHR.apply(this); + }); + + afterEach(function() { + this.xhr.restore(); + }); + + it("has a register function", function() { + expect(typeof this.srp.register).toBe('function'); + }); + + it("fetches a salt from /register/salt", function(){ + var callback = sinon.spy(); + this.srp.register_receive_salt = callback; + this.srp.register(); + this.expectRequest('register/salt/', "I=user") + this.respondXML("5d3055e0acd3ddcfc15"); + expect(callback.called).toBeTruthy(); + }); + + it("receives the salt from /register/salt", function(){ + var callback = sinon.spy(); + this.srp.remote.register_send_verifier = callback; + this.srp.register(); + this.expectRequest('register/salt/', "I=user") + this.respondXML("5d3055e0acd3ddcfc15"); + expect(callback).toHaveBeenCalledWith("adcd57b4a4a05c2e205b0b7b30014d9ff635d8d8db2f502f08e9b9c132800c44", this.srp.registered_user); + }); + + it("identifies after successful registration (INTEGRATION)", function(){ + var callback = sinon.spy(); + this.srp.identify = callback; + this.srp.register(); + this.expectRequest('register/salt/', "I=user") + this.respondXML("5d3055e0acd3ddcfc15"); + this.expectRequest('register/user/', "v=adcd57b4a4a05c2e205b0b7b30014d9ff635d8d8db2f502f08e9b9c132800c44"); + this.respondXML(""); + expect(callback).toHaveBeenCalled(); + }); + + it("identifies after successful registration with JSON (INTEGRATION)", function(){ + var callback = sinon.spy(); + this.srp.identify = callback; + this.srp.register(); + this.expectRequest('register/salt/', "I=user") + this.respondJSON({salt: "5d3055e0acd3ddcfc15"}); + this.expectRequest('register/user/', "v=adcd57b4a4a05c2e205b0b7b30014d9ff635d8d8db2f502f08e9b9c132800c44"); + this.respondJSON({ok: true}); + expect(callback).toHaveBeenCalled(); + }); + + +}); + + diff --git a/spec/login.js b/spec/login.js deleted file mode 100644 index eea6062..0000000 --- a/spec/login.js +++ /dev/null @@ -1,67 +0,0 @@ -describe("Login", function() { - - it("has an identify function", function() { - var srp = new SRP(); - expect(typeof srp.identify).toBe('function'); - }); - - describe("(INTEGRATION)", function (){ - // a valid auth attempt for the user / password given in the spec runner: - var a = 'af141ae6'; - var B = '887005895b1f5528b4e4dfdce914f73e763b96d3c901d2f41d8b8cd26255a75'; - var salt = '5d3055e0acd3ddcfc15'; - var M = 'be6d7db2186d5f6a2c55788479b6eaf75229a7ca0d9e7dc1f886f1970a0e8065' - var M2 = '2547cf26318519090f506ab73a68995a2626b1c948e6f603ef9e1b0b78bf0f7b'; - var A, callback; - - - beforeEach(function() { - this.srp = new SRP(); - A = this.srp.calculateAndSetA(a); - - specHelper.setupFakeXHR.apply(this); - - this.srp.success = sinon.spy(); - }); - - afterEach(function() { - this.xhr.restore(); - }); - - it("works with XML responses", function(){ - this.srp.identify(); - - this.expectRequest('handshake/', 'I=user&A='+A); - this.respondXML(""); - this.expectRequest('authenticate/', 'M='+M); - this.respondXML(""+M2+""); - - expect(this.srp.success).toHaveBeenCalled(); - }); - - it("works with JSON responses", function(){ - this.srp.identify(); - - this.expectRequest('handshake/', 'I=user&A='+A); - this.respondJSON({s: salt, B: B}); - this.expectRequest('authenticate/', 'M='+M); - this.respondJSON({M: M2}); - - expect(this.srp.success).toHaveBeenCalled(); - }); - - it("rejects B = 0", function(){ - this.srp.error_message = sinon.spy(); - this.srp.identify(); - - this.expectRequest('handshake/', 'I=user&A='+A); - this.respondJSON({s: salt, B: 0}); - // aborting if B=0 - expect(this.requests).toEqual([]); - expect(this.srp.error_message).toHaveBeenCalled(); - }); - }); - - -}); - diff --git a/spec/restful/.login.js.swp b/spec/restful/.login.js.swp new file mode 100644 index 0000000..7a6e842 Binary files /dev/null and b/spec/restful/.login.js.swp differ diff --git a/spec/restful/.signup.js.swp b/spec/restful/.signup.js.swp new file mode 100644 index 0000000..686174a Binary files /dev/null and b/spec/restful/.signup.js.swp differ diff --git a/spec/restful/login.js b/spec/restful/login.js new file mode 100644 index 0000000..729f902 --- /dev/null +++ b/spec/restful/login.js @@ -0,0 +1,67 @@ +describe("Login", function() { + + it("has an identify function", function() { + var srp = new SRP(jqueryRest()); + expect(typeof srp.identify).toBe('function'); + }); + + describe("(INTEGRATION)", function (){ + // a valid auth attempt for the user / password given in the spec runner: + var a = 'af141ae6'; + var B = '887005895b1f5528b4e4dfdce914f73e763b96d3c901d2f41d8b8cd26255a75'; + var salt = '5d3055e0acd3ddcfc15'; + var M = 'be6d7db2186d5f6a2c55788479b6eaf75229a7ca0d9e7dc1f886f1970a0e8065' + var M2 = '2547cf26318519090f506ab73a68995a2626b1c948e6f603ef9e1b0b78bf0f7b'; + var A, callback; + + + beforeEach(function() { + this.srp = new SRP(jqueryRest()); + A = this.srp.calculateAndSetA(a); + + specHelper.setupFakeXHR.apply(this); + + this.srp.success = sinon.spy(); + }); + + afterEach(function() { + this.xhr.restore(); + }); + + it("works with XML responses", function(){ + this.srp.identify(); + + this.expectRequest('handshake/', 'I=user&A='+A); + this.respondXML(""); + this.expectRequest('authenticate/', 'M='+M); + this.respondXML(""+M2+""); + + expect(this.srp.success).toHaveBeenCalled(); + }); + + it("works with JSON responses", function(){ + this.srp.identify(); + + this.expectRequest('handshake/', 'I=user&A='+A); + this.respondJSON({s: salt, B: B}); + this.expectRequest('authenticate/', 'M='+M); + this.respondJSON({M: M2}); + + expect(this.srp.success).toHaveBeenCalled(); + }); + + it("rejects B = 0", function(){ + this.srp.error_message = sinon.spy(); + this.srp.identify(); + + this.expectRequest('handshake/', 'I=user&A='+A); + this.respondJSON({s: salt, B: 0}); + // aborting if B=0 + expect(this.requests).toEqual([]); + expect(this.srp.error_message).toHaveBeenCalled(); + }); + }); + + +}); + diff --git a/spec/restful/signup.js b/spec/restful/signup.js new file mode 100644 index 0000000..26e97b5 --- /dev/null +++ b/spec/restful/signup.js @@ -0,0 +1,59 @@ +describe("Signup", function() { + + beforeEach(function() { + this.srp = new SRP(jqueryRest()); + specHelper.setupFakeXHR.apply(this); + }); + + afterEach(function() { + this.xhr.restore(); + }); + + it("has a register function", function() { + expect(typeof this.srp.register).toBe('function'); + }); + + it("fetches a salt from /register/salt", function(){ + var callback = sinon.spy(); + this.srp.register_receive_salt = callback; + this.srp.register(); + this.expectRequest('register/salt/', "I=user") + this.respondXML("5d3055e0acd3ddcfc15"); + expect(callback.called).toBeTruthy(); + }); + + it("receives the salt from /register/salt", function(){ + var callback = sinon.spy(); + this.srp.remote.register_send_verifier = callback; + this.srp.register(); + this.expectRequest('register/salt/', "I=user") + this.respondXML("5d3055e0acd3ddcfc15"); + expect(callback).toHaveBeenCalledWith("adcd57b4a4a05c2e205b0b7b30014d9ff635d8d8db2f502f08e9b9c132800c44", this.srp.registered_user); + }); + + it("identifies after successful registration (INTEGRATION)", function(){ + var callback = sinon.spy(); + this.srp.identify = callback; + this.srp.register(); + this.expectRequest('register/salt/', "I=user") + this.respondXML("5d3055e0acd3ddcfc15"); + this.expectRequest('register/user/', "v=adcd57b4a4a05c2e205b0b7b30014d9ff635d8d8db2f502f08e9b9c132800c44"); + this.respondXML(""); + expect(callback).toHaveBeenCalled(); + }); + + it("identifies after successful registration with JSON (INTEGRATION)", function(){ + var callback = sinon.spy(); + this.srp.identify = callback; + this.srp.register(); + this.expectRequest('register/salt/', "I=user") + this.respondJSON({salt: "5d3055e0acd3ddcfc15"}); + this.expectRequest('register/user/', "v=adcd57b4a4a05c2e205b0b7b30014d9ff635d8d8db2f502f08e9b9c132800c44"); + this.respondJSON({ok: true}); + expect(callback).toHaveBeenCalled(); + }); + + +}); + + diff --git a/spec/signup.js b/spec/signup.js deleted file mode 100644 index b38778a..0000000 --- a/spec/signup.js +++ /dev/null @@ -1,59 +0,0 @@ -describe("Signup", function() { - - beforeEach(function() { - this.srp = new SRP(); - specHelper.setupFakeXHR.apply(this); - }); - - afterEach(function() { - this.xhr.restore(); - }); - - it("has a register function", function() { - expect(typeof this.srp.register).toBe('function'); - }); - - it("fetches a salt from /register/salt", function(){ - var callback = sinon.spy(); - this.srp.register_receive_salt = callback; - this.srp.register(); - this.expectRequest('register/salt/', "I=user") - this.respondXML("5d3055e0acd3ddcfc15"); - expect(callback.called).toBeTruthy(); - }); - - it("receives the salt from /register/salt", function(){ - var callback = sinon.spy(); - this.srp.remote.register_send_verifier = callback; - this.srp.register(); - this.expectRequest('register/salt/', "I=user") - this.respondXML("5d3055e0acd3ddcfc15"); - expect(callback).toHaveBeenCalledWith("adcd57b4a4a05c2e205b0b7b30014d9ff635d8d8db2f502f08e9b9c132800c44", this.srp.registered_user); - }); - - it("identifies after successful registration (INTEGRATION)", function(){ - var callback = sinon.spy(); - this.srp.identify = callback; - this.srp.register(); - this.expectRequest('register/salt/', "I=user") - this.respondXML("5d3055e0acd3ddcfc15"); - this.expectRequest('register/user/', "v=adcd57b4a4a05c2e205b0b7b30014d9ff635d8d8db2f502f08e9b9c132800c44"); - this.respondXML(""); - expect(callback).toHaveBeenCalled(); - }); - - it("identifies after successful registration with JSON (INTEGRATION)", function(){ - var callback = sinon.spy(); - this.srp.identify = callback; - this.srp.register(); - this.expectRequest('register/salt/', "I=user") - this.respondJSON({salt: "5d3055e0acd3ddcfc15"}); - this.expectRequest('register/user/', "v=adcd57b4a4a05c2e205b0b7b30014d9ff635d8d8db2f502f08e9b9c132800c44"); - this.respondJSON({ok: true}); - expect(callback).toHaveBeenCalled(); - }); - - -}); - - -- cgit v1.2.3