From a70765ed90196a418023d080dd6ba313c6305633 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 4 Nov 2014 17:19:49 +0000 Subject: Add matrix-service unit tests. Update angular-mocks. --- syweb/webclient/test/unit/matrix-service.spec.js | 64 ++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 syweb/webclient/test/unit/matrix-service.spec.js (limited to 'syweb/webclient/test/unit/matrix-service.spec.js') diff --git a/syweb/webclient/test/unit/matrix-service.spec.js b/syweb/webclient/test/unit/matrix-service.spec.js new file mode 100644 index 0000000000..3c5163e478 --- /dev/null +++ b/syweb/webclient/test/unit/matrix-service.spec.js @@ -0,0 +1,64 @@ +describe('MatrixService', function() { + var scope, httpBackend, createController; + var BASE = "http://example.com"; + var PREFIX = "/_matrix/client/api/v1"; + var URL = BASE + PREFIX; + var roomId = "!wejigf387t34:matrix.org"; + + beforeEach(module('matrixService')); + + beforeEach(inject(function($rootScope, $httpBackend, $controller) { + httpBackend = $httpBackend; + scope = $rootScope; + })); + + afterEach(function() { + httpBackend.verifyNoOutstandingExpectation(); + httpBackend.verifyNoOutstandingRequest(); + }); + + it('should be able to GET /rooms/$roomid/state', inject(function(matrixService) { + matrixService.setConfig({ + access_token: "foobar", + homeserver: "http://example.com" + }); + matrixService.roomState(roomId).then(function(response) { + expect(response.data).toEqual([]); + }); + + httpBackend.expect('GET', + URL + "/rooms/" + roomId + "/state?access_token=foobar") + .respond([]); + httpBackend.flush(); + })); + + it('should be able to POST /join', inject(function(matrixService) { + matrixService.setConfig({ + access_token: "foobar", + homeserver: "http://example.com" + }); + matrixService.joinAlias(roomId).then(function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expect('POST', + URL + "/join/" + encodeURIComponent(roomId) + "?access_token=foobar") + .respond({}); + httpBackend.flush(); + })); + + it('should be able to POST /rooms/$roomid/join', inject(function(matrixService) { + matrixService.setConfig({ + access_token: "foobar", + homeserver: "http://example.com" + }); + matrixService.join(roomId).then(function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expect('POST', + URL + "/rooms/" + encodeURIComponent(roomId) + "/join?access_token=foobar") + .respond({}); + httpBackend.flush(); + })); +}); -- cgit 1.5.1 From 4facbe02fbe53aafebfd68d88ae09c9ae77f3cd3 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 4 Nov 2014 17:48:47 +0000 Subject: URL encoding bugfix and add more tests. --- .../webclient/components/matrix/matrix-service.js | 2 +- syweb/webclient/test/unit/matrix-service.spec.js | 46 +++++++++++++++++++--- 2 files changed, 41 insertions(+), 7 deletions(-) (limited to 'syweb/webclient/test/unit/matrix-service.spec.js') diff --git a/syweb/webclient/components/matrix/matrix-service.js b/syweb/webclient/components/matrix/matrix-service.js index fedfb8910d..5b63fb4a3b 100644 --- a/syweb/webclient/components/matrix/matrix-service.js +++ b/syweb/webclient/components/matrix/matrix-service.js @@ -267,7 +267,7 @@ angular.module('matrixService', []) // get room state for a specific room roomState: function(room_id) { - var path = "/rooms/" + room_id + "/state"; + var path = "/rooms/" + encodeURIComponent(room_id) + "/state"; return doRequest("GET", path); }, diff --git a/syweb/webclient/test/unit/matrix-service.spec.js b/syweb/webclient/test/unit/matrix-service.spec.js index 3c5163e478..29d2ca7be7 100644 --- a/syweb/webclient/test/unit/matrix-service.spec.js +++ b/syweb/webclient/test/unit/matrix-service.spec.js @@ -1,5 +1,5 @@ describe('MatrixService', function() { - var scope, httpBackend, createController; + var scope, httpBackend; var BASE = "http://example.com"; var PREFIX = "/_matrix/client/api/v1"; var URL = BASE + PREFIX; @@ -7,7 +7,7 @@ describe('MatrixService', function() { beforeEach(module('matrixService')); - beforeEach(inject(function($rootScope, $httpBackend, $controller) { + beforeEach(inject(function($rootScope, $httpBackend) { httpBackend = $httpBackend; scope = $rootScope; })); @@ -17,6 +17,40 @@ describe('MatrixService', function() { httpBackend.verifyNoOutstandingRequest(); }); + it('should be able to POST /createRoom with an alias', inject(function(matrixService) { + matrixService.setConfig({ + access_token: "foobar", + homeserver: "http://example.com" + }); + var alias = "flibble"; + matrixService.create(alias).then(function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectPOST(URL + "/createRoom?access_token=foobar", + { + room_alias_name: alias + }) + .respond({}); + httpBackend.flush(); + })); + + it('should be able to GET /initialSync', inject(function(matrixService) { + matrixService.setConfig({ + access_token: "foobar", + homeserver: "http://example.com" + }); + var limit = 15; + matrixService.initialSync(limit).then(function(response) { + expect(response.data).toEqual([]); + }); + + httpBackend.expectGET( + URL + "/initialSync?access_token=foobar&limit=15") + .respond([]); + httpBackend.flush(); + })); + it('should be able to GET /rooms/$roomid/state', inject(function(matrixService) { matrixService.setConfig({ access_token: "foobar", @@ -26,8 +60,8 @@ describe('MatrixService', function() { expect(response.data).toEqual([]); }); - httpBackend.expect('GET', - URL + "/rooms/" + roomId + "/state?access_token=foobar") + httpBackend.expectGET( + URL + "/rooms/" + encodeURIComponent(roomId) + "/state?access_token=foobar") .respond([]); httpBackend.flush(); })); @@ -41,7 +75,7 @@ describe('MatrixService', function() { expect(response.data).toEqual({}); }); - httpBackend.expect('POST', + httpBackend.expectPOST( URL + "/join/" + encodeURIComponent(roomId) + "?access_token=foobar") .respond({}); httpBackend.flush(); @@ -56,7 +90,7 @@ describe('MatrixService', function() { expect(response.data).toEqual({}); }); - httpBackend.expect('POST', + httpBackend.expectPOST( URL + "/rooms/" + encodeURIComponent(roomId) + "/join?access_token=foobar") .respond({}); httpBackend.flush(); -- cgit 1.5.1 From a2aafeb959075acd76374d2a7d01830f59930996 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 5 Nov 2014 11:11:36 +0000 Subject: Add a bunch more unit tests for matrixService. --- syweb/webclient/test/unit/matrix-service.spec.js | 197 ++++++++++++++++++++++- 1 file changed, 191 insertions(+), 6 deletions(-) (limited to 'syweb/webclient/test/unit/matrix-service.spec.js') diff --git a/syweb/webclient/test/unit/matrix-service.spec.js b/syweb/webclient/test/unit/matrix-service.spec.js index 29d2ca7be7..95a43057c4 100644 --- a/syweb/webclient/test/unit/matrix-service.spec.js +++ b/syweb/webclient/test/unit/matrix-service.spec.js @@ -17,7 +17,8 @@ describe('MatrixService', function() { httpBackend.verifyNoOutstandingRequest(); }); - it('should be able to POST /createRoom with an alias', inject(function(matrixService) { + it('should be able to POST /createRoom with an alias', inject( + function(matrixService) { matrixService.setConfig({ access_token: "foobar", homeserver: "http://example.com" @@ -51,7 +52,8 @@ describe('MatrixService', function() { httpBackend.flush(); })); - it('should be able to GET /rooms/$roomid/state', inject(function(matrixService) { + it('should be able to GET /rooms/$roomid/state', inject( + function(matrixService) { matrixService.setConfig({ access_token: "foobar", homeserver: "http://example.com" @@ -61,7 +63,8 @@ describe('MatrixService', function() { }); httpBackend.expectGET( - URL + "/rooms/" + encodeURIComponent(roomId) + "/state?access_token=foobar") + URL + "/rooms/" + encodeURIComponent(roomId) + + "/state?access_token=foobar") .respond([]); httpBackend.flush(); })); @@ -76,12 +79,15 @@ describe('MatrixService', function() { }); httpBackend.expectPOST( - URL + "/join/" + encodeURIComponent(roomId) + "?access_token=foobar") + URL + "/join/" + encodeURIComponent(roomId) + + "?access_token=foobar", + {}) .respond({}); httpBackend.flush(); })); - it('should be able to POST /rooms/$roomid/join', inject(function(matrixService) { + it('should be able to POST /rooms/$roomid/join', inject( + function(matrixService) { matrixService.setConfig({ access_token: "foobar", homeserver: "http://example.com" @@ -91,7 +97,186 @@ describe('MatrixService', function() { }); httpBackend.expectPOST( - URL + "/rooms/" + encodeURIComponent(roomId) + "/join?access_token=foobar") + URL + "/rooms/" + encodeURIComponent(roomId) + + "/join?access_token=foobar", + {}) + .respond({}); + httpBackend.flush(); + })); + + it('should be able to POST /rooms/$roomid/invite', inject( + function(matrixService) { + matrixService.setConfig({ + access_token: "foobar", + homeserver: "http://example.com" + }); + var inviteUserId = "@user:example.com"; + matrixService.invite(roomId, inviteUserId).then(function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectPOST( + URL + "/rooms/" + encodeURIComponent(roomId) + + "/invite?access_token=foobar", + { + user_id: inviteUserId + }) + .respond({}); + httpBackend.flush(); + })); + + it('should be able to POST /rooms/$roomid/leave', inject( + function(matrixService) { + matrixService.setConfig({ + access_token: "foobar", + homeserver: "http://example.com" + }); + matrixService.leave(roomId).then(function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectPOST( + URL + "/rooms/" + encodeURIComponent(roomId) + + "/leave?access_token=foobar", + {}) + .respond({}); + httpBackend.flush(); + })); + + it('should be able to POST /rooms/$roomid/ban', inject( + function(matrixService) { + matrixService.setConfig({ + access_token: "foobar", + homeserver: "http://example.com" + }); + var userId = "@example:example.com"; + var reason = "Because."; + matrixService.ban(roomId, userId, reason).then(function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectPOST( + URL + "/rooms/" + encodeURIComponent(roomId) + + "/ban?access_token=foobar", + { + user_id: userId, + reason: reason + }) + .respond({}); + httpBackend.flush(); + })); + + it('should be able to GET /directory/room/$alias', inject( + function(matrixService) { + matrixService.setConfig({ + access_token: "foobar", + homeserver: "http://example.com" + }); + var alias = "#test:example.com"; + var roomId = "!wefuhewfuiw:example.com"; + matrixService.resolveRoomAlias(alias).then(function(response) { + expect(response.data).toEqual({ + room_id: roomId + }); + }); + + httpBackend.expectGET( + URL + "/directory/room/" + encodeURIComponent(alias) + + "?access_token=foobar") + .respond({ + room_id: roomId + }); + httpBackend.flush(); + })); + + it('should be able to send m.room.name', inject(function(matrixService) { + matrixService.setConfig({ + access_token: "foobar", + homeserver: "http://example.com" + }); + var roomId = "!fh38hfwfwef:example.com"; + var name = "Room Name"; + matrixService.setName(roomId, name).then(function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectPUT( + URL + "/rooms/" + encodeURIComponent(roomId) + + "/state/m.room.name?access_token=foobar", + { + name: name + }) + .respond({}); + httpBackend.flush(); + })); + + it('should be able to send m.room.topic', inject(function(matrixService) { + matrixService.setConfig({ + access_token: "foobar", + homeserver: "http://example.com" + }); + var roomId = "!fh38hfwfwef:example.com"; + var topic = "A room topic can go here."; + matrixService.setTopic(roomId, topic).then(function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectPUT( + URL + "/rooms/" + encodeURIComponent(roomId) + + "/state/m.room.topic?access_token=foobar", + { + topic: topic + }) + .respond({}); + httpBackend.flush(); + })); + + it('should be able to send generic state events without a state key', inject( + function(matrixService) { + matrixService.setConfig({ + access_token: "foobar", + homeserver: "http://example.com" + }); + var roomId = "!fh38hfwfwef:example.com"; + var eventType = "com.example.events.test"; + var content = { + testing: "1 2 3" + }; + matrixService.sendStateEvent(roomId, eventType, content).then( + function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectPUT( + URL + "/rooms/" + encodeURIComponent(roomId) + "/state/" + + encodeURIComponent(eventType) + "?access_token=foobar", + content) + .respond({}); + httpBackend.flush(); + })); + + it('should be able to send generic state events with a state key', inject( + function(matrixService) { + matrixService.setConfig({ + access_token: "foobar", + homeserver: "http://example.com" + }); + var roomId = "!fh38hfwfwef:example.com"; + var eventType = "com.example.events.test"; + var content = { + testing: "1 2 3" + }; + var stateKey = "version1"; + matrixService.sendStateEvent(roomId, eventType, content, stateKey).then( + function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectPUT( + URL + "/rooms/" + encodeURIComponent(roomId) + "/state/" + + encodeURIComponent(eventType) + "/" + encodeURIComponent(stateKey)+ + "?access_token=foobar", + content) .respond({}); httpBackend.flush(); })); -- cgit 1.5.1 From 9f6d1b10ad5a9098a8f72157875ce97fc44bc423 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 5 Nov 2014 11:21:55 +0000 Subject: Be sure to urlencode/decode event types correctly in both the web client and HS. --- synapse/rest/room.py | 2 +- syweb/webclient/components/matrix/matrix-service.js | 4 ++-- syweb/webclient/test/unit/matrix-service.spec.js | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) (limited to 'syweb/webclient/test/unit/matrix-service.spec.js') diff --git a/synapse/rest/room.py b/synapse/rest/room.py index ec0ce78fda..d97babea08 100644 --- a/synapse/rest/room.py +++ b/synapse/rest/room.py @@ -148,7 +148,7 @@ class RoomStateEventRestServlet(RestServlet): content = _parse_json(request) event = self.event_factory.create_event( - etype=event_type, + etype=urllib.unquote(event_type), content=content, room_id=urllib.unquote(room_id), user_id=user.to_string(), diff --git a/syweb/webclient/components/matrix/matrix-service.js b/syweb/webclient/components/matrix/matrix-service.js index 5b63fb4a3b..e1e5b88b3e 100644 --- a/syweb/webclient/components/matrix/matrix-service.js +++ b/syweb/webclient/components/matrix/matrix-service.js @@ -375,9 +375,9 @@ angular.module('matrixService', []) sendStateEvent: function(room_id, eventType, content, state_key) { - var path = "/rooms/$room_id/state/"+eventType; + var path = "/rooms/$room_id/state/"+ encodeURIComponent(eventType); if (state_key !== undefined) { - path += "/" + state_key; + path += "/" + encodeURIComponent(state_key); } room_id = encodeURIComponent(room_id); path = path.replace("$room_id", room_id); diff --git a/syweb/webclient/test/unit/matrix-service.spec.js b/syweb/webclient/test/unit/matrix-service.spec.js index 95a43057c4..b54163a641 100644 --- a/syweb/webclient/test/unit/matrix-service.spec.js +++ b/syweb/webclient/test/unit/matrix-service.spec.js @@ -238,7 +238,7 @@ describe('MatrixService', function() { homeserver: "http://example.com" }); var roomId = "!fh38hfwfwef:example.com"; - var eventType = "com.example.events.test"; + var eventType = "com.example.events.test:special@characters"; var content = { testing: "1 2 3" }; @@ -262,11 +262,11 @@ describe('MatrixService', function() { homeserver: "http://example.com" }); var roomId = "!fh38hfwfwef:example.com"; - var eventType = "com.example.events.test"; + var eventType = "com.example.events.test:special@characters"; var content = { testing: "1 2 3" }; - var stateKey = "version1"; + var stateKey = "version:1"; matrixService.sendStateEvent(roomId, eventType, content, stateKey).then( function(response) { expect(response.data).toEqual({}); -- cgit 1.5.1 From 42081b1937127979f3fb0a673eefb866cb4de64e Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 5 Nov 2014 11:28:22 +0000 Subject: Don't urlencode event types just yet so older HSes don't 500. Skip the tests which test for urlencoding, and add a TODO in matrixService. --- syweb/webclient/components/matrix/matrix-service.js | 4 +++- syweb/webclient/test/unit/matrix-service.spec.js | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'syweb/webclient/test/unit/matrix-service.spec.js') diff --git a/syweb/webclient/components/matrix/matrix-service.js b/syweb/webclient/components/matrix/matrix-service.js index e1e5b88b3e..8ff2999e2d 100644 --- a/syweb/webclient/components/matrix/matrix-service.js +++ b/syweb/webclient/components/matrix/matrix-service.js @@ -375,7 +375,9 @@ angular.module('matrixService', []) sendStateEvent: function(room_id, eventType, content, state_key) { - var path = "/rooms/$room_id/state/"+ encodeURIComponent(eventType); + var path = "/rooms/$room_id/state/"+ eventType; + // TODO: uncomment this when matrix.org is updated, else all state events 500. + // var path = "/rooms/$room_id/state/"+ encodeURIComponent(eventType); if (state_key !== undefined) { path += "/" + encodeURIComponent(state_key); } diff --git a/syweb/webclient/test/unit/matrix-service.spec.js b/syweb/webclient/test/unit/matrix-service.spec.js index b54163a641..ed290f2ff3 100644 --- a/syweb/webclient/test/unit/matrix-service.spec.js +++ b/syweb/webclient/test/unit/matrix-service.spec.js @@ -231,7 +231,7 @@ describe('MatrixService', function() { httpBackend.flush(); })); - it('should be able to send generic state events without a state key', inject( + xit('should be able to send generic state events without a state key', inject( function(matrixService) { matrixService.setConfig({ access_token: "foobar", @@ -255,7 +255,7 @@ describe('MatrixService', function() { httpBackend.flush(); })); - it('should be able to send generic state events with a state key', inject( + xit('should be able to send generic state events with a state key', inject( function(matrixService) { matrixService.setConfig({ access_token: "foobar", -- cgit 1.5.1 From 0881a8ae6f6468af8345fc9e6e46bdb32dfb405b Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 5 Nov 2014 12:32:28 +0000 Subject: Add more tests and a TODO. --- syweb/webclient/test/unit/matrix-service.spec.js | 57 +++++++++++++++++++++++- 1 file changed, 55 insertions(+), 2 deletions(-) (limited to 'syweb/webclient/test/unit/matrix-service.spec.js') diff --git a/syweb/webclient/test/unit/matrix-service.spec.js b/syweb/webclient/test/unit/matrix-service.spec.js index ed290f2ff3..2ca9a24323 100644 --- a/syweb/webclient/test/unit/matrix-service.spec.js +++ b/syweb/webclient/test/unit/matrix-service.spec.js @@ -231,14 +231,14 @@ describe('MatrixService', function() { httpBackend.flush(); })); - xit('should be able to send generic state events without a state key', inject( + it('should be able to send generic state events without a state key', inject( function(matrixService) { matrixService.setConfig({ access_token: "foobar", homeserver: "http://example.com" }); var roomId = "!fh38hfwfwef:example.com"; - var eventType = "com.example.events.test:special@characters"; + var eventType = "com.example.events.test"; var content = { testing: "1 2 3" }; @@ -255,6 +255,8 @@ describe('MatrixService', function() { httpBackend.flush(); })); + // TODO: Skipped since the webclient is purposefully broken so as not to + // 500 matrix.org xit('should be able to send generic state events with a state key', inject( function(matrixService) { matrixService.setConfig({ @@ -280,4 +282,55 @@ describe('MatrixService', function() { .respond({}); httpBackend.flush(); })); + + it('should be able to PUT generic events ', inject( + function(matrixService) { + matrixService.setConfig({ + access_token: "foobar", + homeserver: "http://example.com" + }); + var roomId = "!fh38hfwfwef:example.com"; + var eventType = "com.example.events.test"; + var txnId = "42"; + var content = { + testing: "1 2 3" + }; + matrixService.sendEvent(roomId, eventType, txnId, content).then( + function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectPUT( + URL + "/rooms/" + encodeURIComponent(roomId) + "/send/" + + encodeURIComponent(eventType) + "/" + encodeURIComponent(txnId)+ + "?access_token=foobar", + content) + .respond({}); + httpBackend.flush(); + })); + + it('should be able to PUT text messages ', inject( + function(matrixService) { + matrixService.setConfig({ + access_token: "foobar", + homeserver: "http://example.com" + }); + var roomId = "!fh38hfwfwef:example.com"; + var body = "ABC 123"; + matrixService.sendTextMessage(roomId, body).then( + function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectPUT( + new RegExp(URL + "/rooms/" + encodeURIComponent(roomId) + + "/send/m.room.message/(.*)" + + "?access_token=foobar"), + { + body: body, + msgtype: "m.text" + }) + .respond({}); + httpBackend.flush(); + })); }); -- cgit 1.5.1 From 988a8526b5a75a988fffd9ab5c3b4abbd2a41840 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 5 Nov 2014 14:35:41 +0000 Subject: Finish matrixService unit tests. Add missing encodeURIComponent to path args. --- .../webclient/components/matrix/matrix-service.js | 11 +- syweb/webclient/test/unit/matrix-service.spec.js | 288 ++++++++++++++++----- 2 files changed, 234 insertions(+), 65 deletions(-) (limited to 'syweb/webclient/test/unit/matrix-service.spec.js') diff --git a/syweb/webclient/components/matrix/matrix-service.js b/syweb/webclient/components/matrix/matrix-service.js index 8ff2999e2d..63051c4f47 100644 --- a/syweb/webclient/components/matrix/matrix-service.js +++ b/syweb/webclient/components/matrix/matrix-service.js @@ -443,7 +443,8 @@ angular.module('matrixService', []) redactEvent: function(room_id, event_id) { var path = "/rooms/$room_id/redact/$event_id"; - path = path.replace("$room_id", room_id); + path = path.replace("$room_id", encodeURIComponent(room_id)); + // TODO: encodeURIComponent when HS updated. path = path.replace("$event_id", event_id); var content = {}; return doRequest("POST", path, undefined, content); @@ -461,7 +462,7 @@ angular.module('matrixService', []) paginateBackMessages: function(room_id, from_token, limit) { var path = "/rooms/$room_id/messages"; - path = path.replace("$room_id", room_id); + path = path.replace("$room_id", encodeURIComponent(room_id)); var params = { from: from_token, limit: limit, @@ -509,12 +510,12 @@ angular.module('matrixService', []) setProfileInfo: function(data, info_segment) { var path = "/profile/$user/" + info_segment; - path = path.replace("$user", config.user_id); + path = path.replace("$user", encodeURIComponent(config.user_id)); return doRequest("PUT", path, undefined, data); }, getProfileInfo: function(userId, info_segment) { - var path = "/profile/"+userId + var path = "/profile/"+encodeURIComponent(userId); if (info_segment) path += '/' + info_segment; return doRequest("GET", path); }, @@ -633,7 +634,7 @@ angular.module('matrixService', []) // Set the logged in user presence state setUserPresence: function(presence) { var path = "/presence/$user_id/status"; - path = path.replace("$user_id", config.user_id); + path = path.replace("$user_id", encodeURIComponent(config.user_id)); return doRequest("PUT", path, undefined, { presence: presence }); diff --git a/syweb/webclient/test/unit/matrix-service.spec.js b/syweb/webclient/test/unit/matrix-service.spec.js index 2ca9a24323..4959f2395d 100644 --- a/syweb/webclient/test/unit/matrix-service.spec.js +++ b/syweb/webclient/test/unit/matrix-service.spec.js @@ -5,6 +5,11 @@ describe('MatrixService', function() { var URL = BASE + PREFIX; var roomId = "!wejigf387t34:matrix.org"; + var CONFIG = { + access_token: "foobar", + homeserver: BASE + }; + beforeEach(module('matrixService')); beforeEach(inject(function($rootScope, $httpBackend) { @@ -19,10 +24,7 @@ describe('MatrixService', function() { it('should be able to POST /createRoom with an alias', inject( function(matrixService) { - matrixService.setConfig({ - access_token: "foobar", - homeserver: "http://example.com" - }); + matrixService.setConfig(CONFIG); var alias = "flibble"; matrixService.create(alias).then(function(response) { expect(response.data).toEqual({}); @@ -37,10 +39,7 @@ describe('MatrixService', function() { })); it('should be able to GET /initialSync', inject(function(matrixService) { - matrixService.setConfig({ - access_token: "foobar", - homeserver: "http://example.com" - }); + matrixService.setConfig(CONFIG); var limit = 15; matrixService.initialSync(limit).then(function(response) { expect(response.data).toEqual([]); @@ -54,10 +53,7 @@ describe('MatrixService', function() { it('should be able to GET /rooms/$roomid/state', inject( function(matrixService) { - matrixService.setConfig({ - access_token: "foobar", - homeserver: "http://example.com" - }); + matrixService.setConfig(CONFIG); matrixService.roomState(roomId).then(function(response) { expect(response.data).toEqual([]); }); @@ -70,10 +66,7 @@ describe('MatrixService', function() { })); it('should be able to POST /join', inject(function(matrixService) { - matrixService.setConfig({ - access_token: "foobar", - homeserver: "http://example.com" - }); + matrixService.setConfig(CONFIG); matrixService.joinAlias(roomId).then(function(response) { expect(response.data).toEqual({}); }); @@ -88,10 +81,7 @@ describe('MatrixService', function() { it('should be able to POST /rooms/$roomid/join', inject( function(matrixService) { - matrixService.setConfig({ - access_token: "foobar", - homeserver: "http://example.com" - }); + matrixService.setConfig(CONFIG); matrixService.join(roomId).then(function(response) { expect(response.data).toEqual({}); }); @@ -106,10 +96,7 @@ describe('MatrixService', function() { it('should be able to POST /rooms/$roomid/invite', inject( function(matrixService) { - matrixService.setConfig({ - access_token: "foobar", - homeserver: "http://example.com" - }); + matrixService.setConfig(CONFIG); var inviteUserId = "@user:example.com"; matrixService.invite(roomId, inviteUserId).then(function(response) { expect(response.data).toEqual({}); @@ -127,10 +114,7 @@ describe('MatrixService', function() { it('should be able to POST /rooms/$roomid/leave', inject( function(matrixService) { - matrixService.setConfig({ - access_token: "foobar", - homeserver: "http://example.com" - }); + matrixService.setConfig(CONFIG); matrixService.leave(roomId).then(function(response) { expect(response.data).toEqual({}); }); @@ -145,10 +129,7 @@ describe('MatrixService', function() { it('should be able to POST /rooms/$roomid/ban', inject( function(matrixService) { - matrixService.setConfig({ - access_token: "foobar", - homeserver: "http://example.com" - }); + matrixService.setConfig(CONFIG); var userId = "@example:example.com"; var reason = "Because."; matrixService.ban(roomId, userId, reason).then(function(response) { @@ -168,10 +149,7 @@ describe('MatrixService', function() { it('should be able to GET /directory/room/$alias', inject( function(matrixService) { - matrixService.setConfig({ - access_token: "foobar", - homeserver: "http://example.com" - }); + matrixService.setConfig(CONFIG); var alias = "#test:example.com"; var roomId = "!wefuhewfuiw:example.com"; matrixService.resolveRoomAlias(alias).then(function(response) { @@ -190,10 +168,7 @@ describe('MatrixService', function() { })); it('should be able to send m.room.name', inject(function(matrixService) { - matrixService.setConfig({ - access_token: "foobar", - homeserver: "http://example.com" - }); + matrixService.setConfig(CONFIG); var roomId = "!fh38hfwfwef:example.com"; var name = "Room Name"; matrixService.setName(roomId, name).then(function(response) { @@ -211,10 +186,7 @@ describe('MatrixService', function() { })); it('should be able to send m.room.topic', inject(function(matrixService) { - matrixService.setConfig({ - access_token: "foobar", - homeserver: "http://example.com" - }); + matrixService.setConfig(CONFIG); var roomId = "!fh38hfwfwef:example.com"; var topic = "A room topic can go here."; matrixService.setTopic(roomId, topic).then(function(response) { @@ -233,10 +205,7 @@ describe('MatrixService', function() { it('should be able to send generic state events without a state key', inject( function(matrixService) { - matrixService.setConfig({ - access_token: "foobar", - homeserver: "http://example.com" - }); + matrixService.setConfig(CONFIG); var roomId = "!fh38hfwfwef:example.com"; var eventType = "com.example.events.test"; var content = { @@ -259,10 +228,7 @@ describe('MatrixService', function() { // 500 matrix.org xit('should be able to send generic state events with a state key', inject( function(matrixService) { - matrixService.setConfig({ - access_token: "foobar", - homeserver: "http://example.com" - }); + matrixService.setConfig(CONFIG); var roomId = "!fh38hfwfwef:example.com"; var eventType = "com.example.events.test:special@characters"; var content = { @@ -285,10 +251,7 @@ describe('MatrixService', function() { it('should be able to PUT generic events ', inject( function(matrixService) { - matrixService.setConfig({ - access_token: "foobar", - homeserver: "http://example.com" - }); + matrixService.setConfig(CONFIG); var roomId = "!fh38hfwfwef:example.com"; var eventType = "com.example.events.test"; var txnId = "42"; @@ -311,10 +274,7 @@ describe('MatrixService', function() { it('should be able to PUT text messages ', inject( function(matrixService) { - matrixService.setConfig({ - access_token: "foobar", - homeserver: "http://example.com" - }); + matrixService.setConfig(CONFIG); var roomId = "!fh38hfwfwef:example.com"; var body = "ABC 123"; matrixService.sendTextMessage(roomId, body).then( @@ -333,4 +293,212 @@ describe('MatrixService', function() { .respond({}); httpBackend.flush(); })); + + it('should be able to PUT emote messages ', inject( + function(matrixService) { + matrixService.setConfig(CONFIG); + var roomId = "!fh38hfwfwef:example.com"; + var body = "ABC 123"; + matrixService.sendEmoteMessage(roomId, body).then( + function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectPUT( + new RegExp(URL + "/rooms/" + encodeURIComponent(roomId) + + "/send/m.room.message/(.*)" + + "?access_token=foobar"), + { + body: body, + msgtype: "m.emote" + }) + .respond({}); + httpBackend.flush(); + })); + + it('should be able to POST redactions', inject( + function(matrixService) { + matrixService.setConfig(CONFIG); + var roomId = "!fh38hfwfwef:example.com"; + var eventId = "fwefwexample.com"; + matrixService.redactEvent(roomId, eventId).then( + function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectPOST(URL + "/rooms/" + encodeURIComponent(roomId) + + "/redact/" + encodeURIComponent(eventId) + + "?access_token=foobar") + .respond({}); + httpBackend.flush(); + })); + + it('should be able to GET /directory/room/$alias', inject( + function(matrixService) { + matrixService.setConfig(CONFIG); + var alias = "#test:example.com"; + var roomId = "!wefuhewfuiw:example.com"; + matrixService.resolveRoomAlias(alias).then(function(response) { + expect(response.data).toEqual({ + room_id: roomId + }); + }); + + httpBackend.expectGET( + URL + "/directory/room/" + encodeURIComponent(alias) + + "?access_token=foobar") + .respond({ + room_id: roomId + }); + httpBackend.flush(); + })); + + it('should be able to GET /rooms/$roomid/members', inject( + function(matrixService) { + matrixService.setConfig(CONFIG); + var roomId = "!wefuhewfuiw:example.com"; + matrixService.getMemberList(roomId).then(function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectGET( + URL + "/rooms/" + encodeURIComponent(roomId) + + "/members?access_token=foobar") + .respond({}); + httpBackend.flush(); + })); + + it('should be able to paginate a room', inject( + function(matrixService) { + matrixService.setConfig(CONFIG); + var roomId = "!wefuhewfuiw:example.com"; + var from = "3t_44e_54z"; + var limit = 20; + matrixService.paginateBackMessages(roomId, from, limit).then(function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectGET( + URL + "/rooms/" + encodeURIComponent(roomId) + + "/messages?access_token=foobar&dir=b&from="+ + encodeURIComponent(from)+"&limit="+limit) + .respond({}); + httpBackend.flush(); + })); + + it('should be able to GET /publicRooms', inject( + function(matrixService) { + matrixService.setConfig(CONFIG); + matrixService.publicRooms().then(function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectGET( + new RegExp(URL + "/publicRooms(.*)")) + .respond({}); + httpBackend.flush(); + })); + + it('should be able to GET /profile/$userid/displayname', inject( + function(matrixService) { + matrixService.setConfig(CONFIG); + var userId = "@foo:example.com"; + matrixService.getDisplayName(userId).then(function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectGET(URL + "/profile/" + encodeURIComponent(userId) + + "/displayname?access_token=foobar") + .respond({}); + httpBackend.flush(); + })); + + it('should be able to GET /profile/$userid/avatar_url', inject( + function(matrixService) { + matrixService.setConfig(CONFIG); + var userId = "@foo:example.com"; + matrixService.getProfilePictureUrl(userId).then(function(response) { + expect(response.data).toEqual({}); + }); + + httpBackend.expectGET(URL + "/profile/" + encodeURIComponent(userId) + + "/avatar_url?access_token=foobar") + .respond({}); + httpBackend.flush(); + })); + + it('should be able to PUT /profile/$me/avatar_url', inject( + function(matrixService) { + var testConfig = angular.copy(CONFIG); + testConfig.user_id = "@bob:example.com"; + matrixService.setConfig(testConfig); + var url = "http://example.com/mypic.jpg"; + matrixService.setProfilePictureUrl(url).then(function(response) { + expect(response.data).toEqual({}); + }); + httpBackend.expectPUT(URL + "/profile/" + + encodeURIComponent(testConfig.user_id) + + "/avatar_url?access_token=foobar", + { + avatar_url: url + }) + .respond({}); + httpBackend.flush(); + })); + + it('should be able to PUT /profile/$me/displayname', inject( + function(matrixService) { + var testConfig = angular.copy(CONFIG); + testConfig.user_id = "@bob:example.com"; + matrixService.setConfig(testConfig); + var displayname = "Bob Smith"; + matrixService.setDisplayName(displayname).then(function(response) { + expect(response.data).toEqual({}); + }); + httpBackend.expectPUT(URL + "/profile/" + + encodeURIComponent(testConfig.user_id) + + "/displayname?access_token=foobar", + { + displayname: displayname + }) + .respond({}); + httpBackend.flush(); + })); + + it('should be able to login with password', inject( + function(matrixService) { + matrixService.setConfig(CONFIG); + var userId = "@bob:example.com"; + var password = "monkey"; + matrixService.login(userId, password).then(function(response) { + expect(response.data).toEqual({}); + }); + httpBackend.expectPOST(new RegExp(URL+"/login(.*)"), + { + user: userId, + password: password, + type: "m.login.password" + }) + .respond({}); + httpBackend.flush(); + })); + + it('should be able to PUT presence status', inject( + function(matrixService) { + var testConfig = angular.copy(CONFIG); + testConfig.user_id = "@bob:example.com"; + matrixService.setConfig(testConfig); + var status = "unavailable"; + matrixService.setUserPresence(status).then(function(response) { + expect(response.data).toEqual({}); + }); + httpBackend.expectPUT(URL+"/presence/"+ + encodeURIComponent(testConfig.user_id)+ + "/status?access_token=foobar", + { + presence: status + }) + .respond({}); + httpBackend.flush(); + })); }); -- cgit 1.5.1