From 89ba802b23bf1fd22afbc5e9a4b3b732264e3c18 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Tue, 4 Nov 2014 15:57:23 +0000 Subject: Move webclient to a python module so that it can be installed --- syweb/webclient/test/unit/user-controller.spec.js | 57 +++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 syweb/webclient/test/unit/user-controller.spec.js (limited to 'syweb/webclient/test/unit') diff --git a/syweb/webclient/test/unit/user-controller.spec.js b/syweb/webclient/test/unit/user-controller.spec.js new file mode 100644 index 0000000000..798cc4de48 --- /dev/null +++ b/syweb/webclient/test/unit/user-controller.spec.js @@ -0,0 +1,57 @@ +describe("UserCtrl", function() { + var scope, ctrl, matrixService, routeParams, $q, $timeout; + var userId = "@foo:bar"; + var displayName = "Foo"; + var avatarUrl = "avatar.url"; + + beforeEach(module('matrixWebClient')); + + beforeEach(function() { + + inject(function($rootScope, $injector, $controller, _$q_, _$timeout_) { + $q = _$q_; + $timeout = _$timeout_; + + matrixService = { + config: function() { + return { + user_id: userId + }; + }, + + getDisplayName: function(uid) { + var d = $q.defer(); + d.resolve({ + data: { + displayname: displayName + } + }); + return d.promise; + }, + + getProfilePictureUrl: function(uid) { + var d = $q.defer(); + d.resolve({ + data: { + avatar_url: avatarUrl + } + }); + return d.promise; + } + }; + scope = $rootScope.$new(); + routeParams = { + user_matrix_id: userId + }; + ctrl = $controller('UserController', { + '$scope': scope, + '$routeParams': routeParams, + 'matrixService': matrixService + }); + }); + }); + + it('should display your user id', function() { + expect(scope.user_id).toEqual(userId); + }); +}); -- cgit 1.4.1 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/js/angular-mocks.js | 308 +++++++++++++++-------- syweb/webclient/test/unit/matrix-service.spec.js | 64 +++++ 2 files changed, 261 insertions(+), 111 deletions(-) create mode 100644 syweb/webclient/test/unit/matrix-service.spec.js (limited to 'syweb/webclient/test/unit') diff --git a/syweb/webclient/js/angular-mocks.js b/syweb/webclient/js/angular-mocks.js index 48c0b5decb..24bbcd4137 100755 --- a/syweb/webclient/js/angular-mocks.js +++ b/syweb/webclient/js/angular-mocks.js @@ -1,10 +1,3 @@ -/** - * @license AngularJS v1.2.22 - * (c) 2010-2014 Google, Inc. http://angularjs.org - * License: MIT - */ -(function(window, angular, undefined) { - 'use strict'; /** @@ -63,6 +56,8 @@ angular.mock.$Browser = function() { return listener; }; + self.$$checkUrlChange = angular.noop; + self.cookieHash = {}; self.lastCookieHash = {}; self.deferredFns = []; @@ -125,7 +120,7 @@ angular.mock.$Browser = function() { } }; - self.$$baseHref = ''; + self.$$baseHref = '/'; self.baseHref = function() { return this.$$baseHref; }; @@ -774,13 +769,22 @@ angular.mock.animate = angular.module('ngAnimateMock', ['ng']) }; }); - $provide.decorator('$animate', function($delegate, $$asyncCallback) { + $provide.decorator('$animate', ['$delegate', '$$asyncCallback', '$timeout', '$browser', + function($delegate, $$asyncCallback, $timeout, $browser) { var animate = { queue : [], + cancel : $delegate.cancel, enabled : $delegate.enabled, - triggerCallbacks : function() { + triggerCallbackEvents : function() { $$asyncCallback.flush(); }, + triggerCallbackPromise : function() { + $timeout.flush(0); + }, + triggerCallbacks : function() { + this.triggerCallbackEvents(); + this.triggerCallbackPromise(); + }, triggerReflow : function() { angular.forEach(reflowQueue, function(fn) { fn(); @@ -797,12 +801,12 @@ angular.mock.animate = angular.module('ngAnimateMock', ['ng']) element : arguments[0], args : arguments }); - $delegate[method].apply($delegate, arguments); + return $delegate[method].apply($delegate, arguments); }; }); return animate; - }); + }]); }]); @@ -888,7 +892,7 @@ angular.mock.dump = function(object) { * development please see {@link ngMockE2E.$httpBackend e2e $httpBackend mock}. * * During unit testing, we want our unit tests to run quickly and have no external dependencies so - * we don’t want to send [XHR](https://developer.mozilla.org/en/xmlhttprequest) or + * we don’t want to send [XHR](https://developer.mozilla.org/en/xmlhttprequest) or * [JSONP](http://en.wikipedia.org/wiki/JSONP) requests to a real server. All we really need is * to verify whether a certain request has been sent or not, or alternatively just let the * application make requests, respond with pre-trained responses and assert that the end result is @@ -1007,13 +1011,14 @@ angular.mock.dump = function(object) { ```js // testing controller describe('MyController', function() { - var $httpBackend, $rootScope, createController; + var $httpBackend, $rootScope, createController, authRequestHandler; beforeEach(inject(function($injector) { // Set up the mock http service responses $httpBackend = $injector.get('$httpBackend'); // backend definition common for all tests - $httpBackend.when('GET', '/auth.py').respond({userId: 'userX'}, {'A-Token': 'xxx'}); + authRequestHandler = $httpBackend.when('GET', '/auth.py') + .respond({userId: 'userX'}, {'A-Token': 'xxx'}); // Get hold of a scope (i.e. the root scope) $rootScope = $injector.get('$rootScope'); @@ -1039,11 +1044,23 @@ angular.mock.dump = function(object) { }); + it('should fail authentication', function() { + + // Notice how you can change the response even after it was set + authRequestHandler.respond(401, ''); + + $httpBackend.expectGET('/auth.py'); + var controller = createController(); + $httpBackend.flush(); + expect($rootScope.status).toBe('Failed...'); + }); + + it('should send msg to server', function() { var controller = createController(); $httpBackend.flush(); - // now you don’t care about the authentication, but + // now you don’t care about the authentication, but // the controller will still send the request and // $httpBackend will respond without you having to // specify the expectation and response for this request @@ -1186,32 +1203,39 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * Creates a new backend definition. * * @param {string} method HTTP method. - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives * data string and returns true if the data is as expected. * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header * object and returns true if the headers match the current definition. * @returns {requestHandler} Returns an object with `respond` method that controls how a matched - * request is handled. + * request is handled. You can save this object for later use and invoke `respond` again in + * order to change how a matched request is handled. * - * - respond – + * - respond – * `{function([status,] data[, headers, statusText]) * | function(function(method, url, data, headers)}` - * – The respond method takes a set of static data to be returned or a function that can + * – The respond method takes a set of static data to be returned or a function that can * return an array containing response status (number), response data (string), response - * headers (Object), and the text for the status (string). + * headers (Object), and the text for the status (string). The respond method returns the + * `requestHandler` object for possible overrides. */ $httpBackend.when = function(method, url, data, headers) { var definition = new MockHttpExpectation(method, url, data, headers), chain = { respond: function(status, data, headers, statusText) { + definition.passThrough = undefined; definition.response = createResponse(status, data, headers, statusText); + return chain; } }; if ($browser) { chain.passThrough = function() { + definition.response = undefined; definition.passThrough = true; + return chain; }; } @@ -1225,10 +1249,12 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * @description * Creates a new backend definition for GET requests. For more info see `when()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(Object|function(Object))=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` method that control how a matched - * request is handled. + * request is handled. You can save this object for later use and invoke `respond` again in + * order to change how a matched request is handled. */ /** @@ -1237,10 +1263,12 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * @description * Creates a new backend definition for HEAD requests. For more info see `when()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(Object|function(Object))=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` method that control how a matched - * request is handled. + * request is handled. You can save this object for later use and invoke `respond` again in + * order to change how a matched request is handled. */ /** @@ -1249,10 +1277,12 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * @description * Creates a new backend definition for DELETE requests. For more info see `when()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(Object|function(Object))=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` method that control how a matched - * request is handled. + * request is handled. You can save this object for later use and invoke `respond` again in + * order to change how a matched request is handled. */ /** @@ -1261,12 +1291,14 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * @description * Creates a new backend definition for POST requests. For more info see `when()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives * data string and returns true if the data is as expected. * @param {(Object|function(Object))=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` method that control how a matched - * request is handled. + * request is handled. You can save this object for later use and invoke `respond` again in + * order to change how a matched request is handled. */ /** @@ -1275,12 +1307,14 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * @description * Creates a new backend definition for PUT requests. For more info see `when()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives * data string and returns true if the data is as expected. * @param {(Object|function(Object))=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` method that control how a matched - * request is handled. + * request is handled. You can save this object for later use and invoke `respond` again in + * order to change how a matched request is handled. */ /** @@ -1289,9 +1323,11 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * @description * Creates a new backend definition for JSONP requests. For more info see `when()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @returns {requestHandler} Returns an object with `respond` method that control how a matched - * request is handled. + * request is handled. You can save this object for later use and invoke `respond` again in + * order to change how a matched request is handled. */ createShortMethods('when'); @@ -1303,30 +1339,36 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * Creates a new request expectation. * * @param {string} method HTTP method. - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that * receives data string and returns true if the data is as expected, or Object if request body * is in JSON format. * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header * object and returns true if the headers match the current expectation. * @returns {requestHandler} Returns an object with `respond` method that control how a matched - * request is handled. + * request is handled. You can save this object for later use and invoke `respond` again in + * order to change how a matched request is handled. * - * - respond – + * - respond – * `{function([status,] data[, headers, statusText]) * | function(function(method, url, data, headers)}` - * – The respond method takes a set of static data to be returned or a function that can + * – The respond method takes a set of static data to be returned or a function that can * return an array containing response status (number), response data (string), response - * headers (Object), and the text for the status (string). + * headers (Object), and the text for the status (string). The respond method returns the + * `requestHandler` object for possible overrides. */ $httpBackend.expect = function(method, url, data, headers) { - var expectation = new MockHttpExpectation(method, url, data, headers); + var expectation = new MockHttpExpectation(method, url, data, headers), + chain = { + respond: function (status, data, headers, statusText) { + expectation.response = createResponse(status, data, headers, statusText); + return chain; + } + }; + expectations.push(expectation); - return { - respond: function (status, data, headers, statusText) { - expectation.response = createResponse(status, data, headers, statusText); - } - }; + return chain; }; @@ -1336,10 +1378,12 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * @description * Creates a new request expectation for GET requests. For more info see `expect()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {Object=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` method that control how a matched - * request is handled. See #expect for more info. + * request is handled. You can save this object for later use and invoke `respond` again in + * order to change how a matched request is handled. See #expect for more info. */ /** @@ -1348,10 +1392,12 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * @description * Creates a new request expectation for HEAD requests. For more info see `expect()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {Object=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` method that control how a matched - * request is handled. + * request is handled. You can save this object for later use and invoke `respond` again in + * order to change how a matched request is handled. */ /** @@ -1360,10 +1406,12 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * @description * Creates a new request expectation for DELETE requests. For more info see `expect()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {Object=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` method that control how a matched - * request is handled. + * request is handled. You can save this object for later use and invoke `respond` again in + * order to change how a matched request is handled. */ /** @@ -1372,13 +1420,15 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * @description * Creates a new request expectation for POST requests. For more info see `expect()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that * receives data string and returns true if the data is as expected, or Object if request body * is in JSON format. * @param {Object=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` method that control how a matched - * request is handled. + * request is handled. You can save this object for later use and invoke `respond` again in + * order to change how a matched request is handled. */ /** @@ -1387,13 +1437,15 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * @description * Creates a new request expectation for PUT requests. For more info see `expect()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that * receives data string and returns true if the data is as expected, or Object if request body * is in JSON format. * @param {Object=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` method that control how a matched - * request is handled. + * request is handled. You can save this object for later use and invoke `respond` again in + * order to change how a matched request is handled. */ /** @@ -1402,13 +1454,15 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * @description * Creates a new request expectation for PATCH requests. For more info see `expect()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that * receives data string and returns true if the data is as expected, or Object if request body * is in JSON format. * @param {Object=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` method that control how a matched - * request is handled. + * request is handled. You can save this object for later use and invoke `respond` again in + * order to change how a matched request is handled. */ /** @@ -1417,9 +1471,11 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * @description * Creates a new request expectation for JSONP requests. For more info see `expect()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @returns {requestHandler} Returns an object with `respond` method that control how a matched - * request is handled. + * request is handled. You can save this object for later use and invoke `respond` again in + * order to change how a matched request is handled. */ createShortMethods('expect'); @@ -1434,11 +1490,11 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * all pending requests will be flushed. If there are no pending requests when the flush method * is called an exception is thrown (as this typically a sign of programming error). */ - $httpBackend.flush = function(count) { - $rootScope.$digest(); + $httpBackend.flush = function(count, digest) { + if (digest !== false) $rootScope.$digest(); if (!responses.length) throw new Error('No pending request to flush !'); - if (angular.isDefined(count)) { + if (angular.isDefined(count) && count !== null) { while (count--) { if (!responses.length) throw new Error('No more pending request to flush !'); responses.shift()(); @@ -1448,7 +1504,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { responses.shift()(); } } - $httpBackend.verifyNoOutstandingExpectation(); + $httpBackend.verifyNoOutstandingExpectation(digest); }; @@ -1466,8 +1522,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { * afterEach($httpBackend.verifyNoOutstandingExpectation); * ``` */ - $httpBackend.verifyNoOutstandingExpectation = function() { - $rootScope.$digest(); + $httpBackend.verifyNoOutstandingExpectation = function(digest) { + if (digest !== false) $rootScope.$digest(); if (expectations.length) { throw new Error('Unsatisfied requests: ' + expectations.join(', ')); } @@ -1511,7 +1567,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { function createShortMethods(prefix) { - angular.forEach(['GET', 'DELETE', 'JSONP'], function(method) { + angular.forEach(['GET', 'DELETE', 'JSONP', 'HEAD'], function(method) { $httpBackend[prefix + method] = function(url, headers) { return $httpBackend[prefix](method, url, undefined, headers); }; @@ -1541,6 +1597,7 @@ function MockHttpExpectation(method, url, data, headers) { this.matchUrl = function(u) { if (!url) return true; if (angular.isFunction(url.test)) return url.test(u); + if (angular.isFunction(url)) return url(u); return url == u; }; @@ -1627,7 +1684,7 @@ function MockXhr() { * that adds a "flush" and "verifyNoPendingTasks" methods. */ -angular.mock.$TimeoutDecorator = function($delegate, $browser) { +angular.mock.$TimeoutDecorator = ['$delegate', '$browser', function ($delegate, $browser) { /** * @ngdoc method @@ -1666,9 +1723,9 @@ angular.mock.$TimeoutDecorator = function($delegate, $browser) { } return $delegate; -}; +}]; -angular.mock.$RAFDecorator = function($delegate) { +angular.mock.$RAFDecorator = ['$delegate', function($delegate) { var queue = []; var rafFn = function(fn) { var index = queue.length; @@ -1694,9 +1751,9 @@ angular.mock.$RAFDecorator = function($delegate) { }; return rafFn; -}; +}]; -angular.mock.$AsyncCallbackDecorator = function($delegate) { +angular.mock.$AsyncCallbackDecorator = ['$delegate', function($delegate) { var callbacks = []; var addFn = function(fn) { callbacks.push(fn); @@ -1708,7 +1765,7 @@ angular.mock.$AsyncCallbackDecorator = function($delegate) { callbacks = []; }; return addFn; -}; +}]; /** * @@ -1822,22 +1879,25 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) { * Creates a new backend definition. * * @param {string} method HTTP method. - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(string|RegExp)=} data HTTP request body. * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header * object and returns true if the headers match the current definition. * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that - * control how a matched request is handled. + * control how a matched request is handled. You can save this object for later use and invoke + * `respond` or `passThrough` again in order to change how a matched request is handled. * - * - respond – + * - respond – * `{function([status,] data[, headers, statusText]) * | function(function(method, url, data, headers)}` - * – The respond method takes a set of static data to be returned or a function that can return + * – The respond method takes a set of static data to be returned or a function that can return * an array containing response status (number), response data (string), response headers * (Object), and the text for the status (string). - * - passThrough – `{function()}` – Any request matching a backend definition with + * - passThrough – `{function()}` – Any request matching a backend definition with * `passThrough` handler will be passed through to the real backend (an XHR request will be made * to the server.) + * - Both methods return the `requestHandler` object for possible overrides. */ /** @@ -1847,10 +1907,12 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) { * @description * Creates a new backend definition for GET requests. For more info see `when()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(Object|function(Object))=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that - * control how a matched request is handled. + * control how a matched request is handled. You can save this object for later use and invoke + * `respond` or `passThrough` again in order to change how a matched request is handled. */ /** @@ -1860,10 +1922,12 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) { * @description * Creates a new backend definition for HEAD requests. For more info see `when()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(Object|function(Object))=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that - * control how a matched request is handled. + * control how a matched request is handled. You can save this object for later use and invoke + * `respond` or `passThrough` again in order to change how a matched request is handled. */ /** @@ -1873,10 +1937,12 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) { * @description * Creates a new backend definition for DELETE requests. For more info see `when()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(Object|function(Object))=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that - * control how a matched request is handled. + * control how a matched request is handled. You can save this object for later use and invoke + * `respond` or `passThrough` again in order to change how a matched request is handled. */ /** @@ -1886,11 +1952,13 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) { * @description * Creates a new backend definition for POST requests. For more info see `when()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(string|RegExp)=} data HTTP request body. * @param {(Object|function(Object))=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that - * control how a matched request is handled. + * control how a matched request is handled. You can save this object for later use and invoke + * `respond` or `passThrough` again in order to change how a matched request is handled. */ /** @@ -1900,11 +1968,13 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) { * @description * Creates a new backend definition for PUT requests. For more info see `when()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(string|RegExp)=} data HTTP request body. * @param {(Object|function(Object))=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that - * control how a matched request is handled. + * control how a matched request is handled. You can save this object for later use and invoke + * `respond` or `passThrough` again in order to change how a matched request is handled. */ /** @@ -1914,11 +1984,13 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) { * @description * Creates a new backend definition for PATCH requests. For more info see `when()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @param {(string|RegExp)=} data HTTP request body. * @param {(Object|function(Object))=} headers HTTP headers. * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that - * control how a matched request is handled. + * control how a matched request is handled. You can save this object for later use and invoke + * `respond` or `passThrough` again in order to change how a matched request is handled. */ /** @@ -1928,30 +2000,17 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) { * @description * Creates a new backend definition for JSONP requests. For more info see `when()`. * - * @param {string|RegExp} url HTTP url. + * @param {string|RegExp|function(string)} url HTTP url or function that receives the url + * and returns true if the url match the current definition. * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that - * control how a matched request is handled. + * control how a matched request is handled. You can save this object for later use and invoke + * `respond` or `passThrough` again in order to change how a matched request is handled. */ angular.mock.e2e = {}; angular.mock.e2e.$httpBackendDecorator = ['$rootScope', '$delegate', '$browser', createHttpBackendMock]; -angular.mock.clearDataCache = function() { - var key, - cache = angular.element.cache; - - for(key in cache) { - if (Object.prototype.hasOwnProperty.call(cache,key)) { - var handle = cache[key].handle; - - handle && angular.element(handle.elem).off(); - delete cache[key]; - } - } -}; - - if(window.jasmine || window.mocha) { var currentSpec = null, @@ -1982,8 +2041,6 @@ if(window.jasmine || window.mocha) { injector.get('$browser').pollFns.length = 0; } - angular.mock.clearDataCache(); - // clean up jquery's fragment cache angular.forEach(angular.element.fragments, function(val, key) { delete angular.element.fragments[key]; @@ -2003,6 +2060,7 @@ if(window.jasmine || window.mocha) { * @description * * *NOTE*: This function is also published on window for easy access.
+ * *NOTE*: This function is declared ONLY WHEN running tests with jasmine or mocha * * This function registers a module configuration code. It collects the configuration information * which will be used when the injector is created by {@link angular.mock.inject inject}. @@ -2045,6 +2103,7 @@ if(window.jasmine || window.mocha) { * @description * * *NOTE*: This function is also published on window for easy access.
+ * *NOTE*: This function is declared ONLY WHEN running tests with jasmine or mocha * * The inject function wraps a function into an injectable function. The inject() creates new * instance of {@link auto.$injector $injector} per test, which is then used for @@ -2144,14 +2203,28 @@ if(window.jasmine || window.mocha) { ///////////////////// function workFn() { var modules = currentSpec.$modules || []; - + var strictDi = !!currentSpec.$injectorStrict; modules.unshift('ngMock'); modules.unshift('ng'); var injector = currentSpec.$injector; if (!injector) { - injector = currentSpec.$injector = angular.injector(modules); + if (strictDi) { + // If strictDi is enabled, annotate the providerInjector blocks + angular.forEach(modules, function(moduleFn) { + if (typeof moduleFn === "function") { + angular.injector.$$annotate(moduleFn); + } + }); + } + injector = currentSpec.$injector = angular.injector(modules, strictDi); + currentSpec.$injectorStrict = strictDi; } for(var i = 0, ii = blockFns.length; i < ii; i++) { + if (currentSpec.$injectorStrict) { + // If the injector is strict / strictDi, and the spec wants to inject using automatic + // annotation, then annotate the function here. + injector.annotate(blockFns[i]); + } try { /* jshint -W040 *//* Jasmine explicitly provides a `this` object when calling functions */ injector.invoke(blockFns[i] || angular.noop, this); @@ -2167,7 +2240,20 @@ if(window.jasmine || window.mocha) { } } }; -} -})(window, window.angular); \ No newline at end of file + angular.mock.inject.strictDi = function(value) { + value = arguments.length ? !!value : true; + return isSpecRunning() ? workFn() : workFn; + + function workFn() { + if (value !== currentSpec.$injectorStrict) { + if (currentSpec.$injector) { + throw new Error('Injector already created, can not modify strict annotations'); + } else { + currentSpec.$injectorStrict = value; + } + } + } + }; +} 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.4.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') 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.4.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') 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.4.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') 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.4.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') 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.4.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') 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.4.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') 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.4.1 From 6aba43f6cc2350f0fce3eb15c4bdd38d7a9c17f6 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 5 Nov 2014 15:32:35 +0000 Subject: Add a few eventHandlerService tests. --- .../test/unit/event-handler-service.spec.js | 105 +++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 syweb/webclient/test/unit/event-handler-service.spec.js (limited to 'syweb/webclient/test/unit') diff --git a/syweb/webclient/test/unit/event-handler-service.spec.js b/syweb/webclient/test/unit/event-handler-service.spec.js new file mode 100644 index 0000000000..023abec98b --- /dev/null +++ b/syweb/webclient/test/unit/event-handler-service.spec.js @@ -0,0 +1,105 @@ +describe('EventHandlerService', function() { + var scope; + + var modelService = {}; + + // setup the service and mocked dependencies + beforeEach(function() { + // dependencies + module('matrixService'); + module('notificationService'); + module('mPresence'); + + // cleanup mocked methods + modelService = {}; + + // mocked dependencies + module(function ($provide) { + $provide.value('modelService', modelService); + }); + + // tested service + module('eventHandlerService'); + }); + + beforeEach(inject(function($rootScope) { + scope = $rootScope; + })); + + it('should be able to get the number of joined users in a room', inject( + function(eventHandlerService) { + var roomId = "!foo:matrix.org"; + // set mocked data + modelService.getRoom = function(roomId) { + return { + room_id: roomId, + current_room_state: { + members: { + "@adam:matrix.org": { + content: { membership: "join" }, + user_id: "@adam:matrix.org" + }, + "@beth:matrix.org": { + content: { membership: "invite" }, + user_id: "@beth:matrix.org" + }, + "@charlie:matrix.org": { + content: { membership: "join" }, + user_id: "@charlie:matrix.org" + }, + "@danice:matrix.org": { + content: { membership: "leave" }, + user_id: "@danice:matrix.org" + } + } + } + }; + } + + var num = eventHandlerService.getUsersCountInRoom(roomId); + expect(num).toEqual(2); + })); + + it('should be able to get a users power level', inject( + function(eventHandlerService) { + var roomId = "!foo:matrix.org"; + // set mocked data + modelService.getRoom = function(roomId) { + return { + room_id: roomId, + current_room_state: { + members: { + "@adam:matrix.org": { + content: { membership: "join" }, + user_id: "@adam:matrix.org" + }, + "@beth:matrix.org": { + content: { membership: "join" }, + user_id: "@beth:matrix.org" + } + }, + s: { + "m.room.power_levels": { + content: { + "@adam:matrix.org": 90, + "default": 50 + } + } + }, + state: function(type, key) { + return key ? this.s[type+key] : this.s[type] + } + } + }; + }; + + var num = eventHandlerService.getUserPowerLevel(roomId, "@beth:matrix.org"); + expect(num).toEqual(50); + + num = eventHandlerService.getUserPowerLevel(roomId, "@adam:matrix.org"); + expect(num).toEqual(90); + + num = eventHandlerService.getUserPowerLevel(roomId, "@unknown:matrix.org"); + expect(num).toEqual(50); + })); +}); \ No newline at end of file -- cgit 1.4.1 From 69c396825b28074346e3cfe3f239b5b1f2c143df Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 5 Nov 2014 17:49:03 +0000 Subject: Add duration filter unit tests. --- syweb/webclient/app-filter.js | 15 ++-------- syweb/webclient/test/unit/filters.spec.js | 50 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 syweb/webclient/test/unit/filters.spec.js (limited to 'syweb/webclient/test/unit') diff --git a/syweb/webclient/app-filter.js b/syweb/webclient/app-filter.js index f19db4141d..65da0d312d 100644 --- a/syweb/webclient/app-filter.js +++ b/syweb/webclient/app-filter.js @@ -29,10 +29,10 @@ angular.module('matrixWebClient') return s + "s"; } if (t < 60 * 60) { - return m + "m "; // + s + "s"; + return m + "m"; // + s + "s"; } if (t < 24 * 60 * 60) { - return h + "h "; // + m + "m"; + return h + "h"; // + m + "m"; } return d + "d "; // + h + "h"; }; @@ -76,17 +76,6 @@ angular.module('matrixWebClient') return filtered; }; }) -.filter('stateEventsFilter', function($sce) { - return function(events) { - var filtered = {}; - angular.forEach(events, function(value, key) { - if (value && typeof(value.state_key) === "string") { - filtered[key] = value; - } - }); - return filtered; - }; -}) .filter('unsafe', ['$sce', function($sce) { return function(text) { return $sce.trustAsHtml(text); diff --git a/syweb/webclient/test/unit/filters.spec.js b/syweb/webclient/test/unit/filters.spec.js new file mode 100644 index 0000000000..3dc735b2c8 --- /dev/null +++ b/syweb/webclient/test/unit/filters.spec.js @@ -0,0 +1,50 @@ +describe('durationFilter', function() { + var filter, durationFilter; + + beforeEach(module('matrixWebClient')); + beforeEach(module('matrixFilter')); + beforeEach(inject(function($filter) { + filter = $filter; + durationFilter = filter("duration"); + })); + + it("should represent 15000 ms as '15s'", function() { + var output = durationFilter(15000); + expect(output).toEqual("15s"); + }); + + it("should represent 60000 ms as '1m'", function() { + var output = durationFilter(60000); + expect(output).toEqual("1m"); + }); + + it("should represent 65000 ms as '1m'", function() { + var output = durationFilter(65000); + expect(output).toEqual("1m"); + }); + + it("should represent 10 ms as '0s'", function() { + var output = durationFilter(10); + expect(output).toEqual("0s"); + }); + + it("should represent 4m as '4m'", function() { + var output = durationFilter(1000*60*4); + expect(output).toEqual("4m"); + }); + + it("should represent 4m30s as '4m'", function() { + var output = durationFilter(1000*60*4 + 1000*30); + expect(output).toEqual("4m"); + }); + + it("should represent 2h as '2h'", function() { + var output = durationFilter(1000*60*60*2); + expect(output).toEqual("2h"); + }); + + it("should represent 2h35m as '2h'", function() { + var output = durationFilter(1000*60*60*2 + 1000*60*35); + expect(output).toEqual("2h"); + }); +}); \ No newline at end of file -- cgit 1.4.1 From c5eec32c583f2d577579dac9bc9ecb278b512d98 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 6 Nov 2014 11:04:43 +0000 Subject: Add mRoomName and orderMembersList filter tests. Mark FIXME on broken tests for now. --- syweb/webclient/test/unit/filters.spec.js | 447 +++++++++++++++++++++++++++++- 1 file changed, 444 insertions(+), 3 deletions(-) (limited to 'syweb/webclient/test/unit') diff --git a/syweb/webclient/test/unit/filters.spec.js b/syweb/webclient/test/unit/filters.spec.js index 3dc735b2c8..f037425208 100644 --- a/syweb/webclient/test/unit/filters.spec.js +++ b/syweb/webclient/test/unit/filters.spec.js @@ -1,8 +1,243 @@ -describe('durationFilter', function() { +describe('mRoomName filter', function() { + var filter, mRoomName; + + var roomId = "!weufhewifu:matrix.org"; + + // test state values (f.e. test) + var testUserId, testAlias, testDisplayName, testOtherDisplayName, testRoomState; + + // mocked services which return the test values above. + var matrixService = { + getRoomIdToAliasMapping: function(room_id) { + return testAlias; + }, + + config: function() { + return { + user_id: testUserId + }; + } + }; + + var eventHandlerService = { + getUserDisplayName: function(room_id, user_id) { + if (user_id === testUserId) { + return testDisplayName; + } + return testOtherDisplayName; + } + }; + + var modelService = { + getRoom: function(room_id) { + return { + current_room_state: testRoomState + }; + } + }; + + beforeEach(function() { + // inject mocked dependencies + module(function ($provide) { + $provide.value('matrixService', matrixService); + $provide.value('eventHandlerService', eventHandlerService); + $provide.value('modelService', modelService); + }); + + module('matrixFilter'); + }); + + beforeEach(inject(function($filter) { + filter = $filter; + mRoomName = filter("mRoomName"); + + // purge the previous test values + testUserId = undefined; + testAlias = undefined; + testDisplayName = undefined; + testOtherDisplayName = undefined; + + // mock up a stub room state + testRoomState = { + s:{}, // internal; stores the state events + state: function(type, key) { + // accessor used by filter + return key ? this.s[type+key] : this.s[type]; + }, + members: {}, // struct used by filter + + // test helper methods + setJoinRule: function(rule) { + this.s["m.room.join_rules"] = { + content: { + join_rule: rule + } + }; + }, + setRoomName: function(name) { + this.s["m.room.name"] = { + content: { + name: name + } + }; + }, + setMember: function(user_id, membership, inviter_user_id) { + if (!inviter_user_id) { + inviter_user_id = user_id; + } + this.s["m.room.member" + user_id] = { + content: { + membership: membership + }, + state_key: user_id, + user_id: inviter_user_id + }; + this.members[user_id] = this.s["m.room.member" + user_id]; + } + }; + })); + + /**** ROOM NAME ****/ + + it("should show the room name if one exists for private (invite join_rules) rooms.", function() { + var roomName = "The Room Name"; + testUserId = "@me:matrix.org"; + testRoomState.setJoinRule("invite"); + testRoomState.setRoomName(roomName); + testRoomState.setMember(testUserId, "join"); + var output = mRoomName(roomId); + expect(output).toEqual(roomName); + }); + + it("should show the room name if one exists for public (public join_rules) rooms.", function() { + var roomName = "The Room Name"; + testUserId = "@me:matrix.org"; + testRoomState.setJoinRule("public"); + testRoomState.setRoomName(roomName); + testRoomState.setMember(testUserId, "join"); + var output = mRoomName(roomId); + expect(output).toEqual(roomName); + }); + + /**** ROOM ALIAS ****/ + + // FIXME + xit("should show the room alias if one exists for private (invite join_rules) rooms if a room name doesn't exist.", function() { + var testAlias = "#thealias:matrix.org"; + testUserId = "@me:matrix.org"; + testRoomState.setJoinRule("invite"); + testRoomState.setMember(testUserId, "join"); + var output = mRoomName(roomId); + expect(output).toEqual(testAlias); + }); + + // FIXME + xit("should show the room alias if one exists for public (public join_rules) rooms if a room name doesn't exist.", function() { + var testAlias = "#thealias:matrix.org"; + testUserId = "@me:matrix.org"; + testRoomState.setJoinRule("public"); + testRoomState.setMember(testUserId, "join"); + var output = mRoomName(roomId); + expect(output).toEqual(testAlias); + }); + + /**** ROOM ID ****/ + + it("should show the room ID for public (public join_rules) rooms if a room name and alias don't exist.", function() { + testUserId = "@me:matrix.org"; + testRoomState.setJoinRule("public"); + testRoomState.setMember(testUserId, "join"); + var output = mRoomName(roomId); + expect(output).toEqual(roomId); + }); + + it("should show the room ID for private (invite join_rules) rooms if a room name and alias don't exist and there are >2 members.", function() { + testUserId = "@me:matrix.org"; + testRoomState.setJoinRule("public"); + testRoomState.setMember(testUserId, "join"); + testRoomState.setMember("@alice:matrix.org", "join"); + testRoomState.setMember("@bob:matrix.org", "join"); + var output = mRoomName(roomId); + expect(output).toEqual(roomId); + }); + + /**** SELF-CHAT ****/ + + it("should show your display name for private (invite join_rules) rooms if a room name and alias don't exist and it is a self-chat.", function() { + testUserId = "@me:matrix.org"; + testDisplayName = "Me"; + testRoomState.setJoinRule("private"); + testRoomState.setMember(testUserId, "join"); + var output = mRoomName(roomId); + expect(output).toEqual(testDisplayName); + }); + + // FIXME + xit("should show your user ID for private (invite join_rules) rooms if a room name and alias don't exist and it is a self-chat and they don't have a display name set.", function() { + testUserId = "@me:matrix.org"; + testRoomState.setJoinRule("private"); + testRoomState.setMember(testUserId, "join"); + var output = mRoomName(roomId); + expect(output).toEqual(testUserId); + }); + + /**** ONE-TO-ONE CHAT ****/ + + it("should show the other user's display name for private (invite join_rules) rooms if a room name and alias don't exist and it is a 1:1-chat.", function() { + testUserId = "@me:matrix.org"; + otherUserId = "@alice:matrix.org"; + testOtherDisplayName = "Alice"; + testRoomState.setJoinRule("private"); + testRoomState.setMember(testUserId, "join"); + testRoomState.setMember("@alice:matrix.org", "join"); + var output = mRoomName(roomId); + expect(output).toEqual(testOtherDisplayName); + }); + + // FIXME + xit("should show the other user's ID for private (invite join_rules) rooms if a room name and alias don't exist and it is a 1:1-chat and they don't have a display name set.", function() { + testUserId = "@me:matrix.org"; + otherUserId = "@alice:matrix.org"; + testRoomState.setJoinRule("private"); + testRoomState.setMember(testUserId, "join"); + testRoomState.setMember("@alice:matrix.org", "join"); + var output = mRoomName(roomId); + expect(output).toEqual(otherUserId); + }); + + /**** INVITED TO ROOM ****/ + + it("should show the other user's display name for private (invite join_rules) rooms if you are invited to it.", function() { + testUserId = "@me:matrix.org"; + testDisplayName = "Me"; + otherUserId = "@alice:matrix.org"; + testOtherDisplayName = "Alice"; + testRoomState.setJoinRule("private"); + testRoomState.setMember(testUserId, "join"); + testRoomState.setMember(otherUserId, "join"); + testRoomState.setMember(testUserId, "invite"); + var output = mRoomName(roomId); + expect(output).toEqual(testOtherDisplayName); + }); + + // FIXME + xit("should show the other user's ID for private (invite join_rules) rooms if you are invited to it and the inviter doesn't have a display name.", function() { + testUserId = "@me:matrix.org"; + testDisplayName = "Me"; + otherUserId = "@alice:matrix.org"; + testRoomState.setJoinRule("private"); + testRoomState.setMember(testUserId, "join"); + testRoomState.setMember(otherUserId, "join"); + testRoomState.setMember(testUserId, "invite"); + var output = mRoomName(roomId); + expect(output).toEqual(otherUserId); + }); +}); + +describe('duration filter', function() { var filter, durationFilter; beforeEach(module('matrixWebClient')); - beforeEach(module('matrixFilter')); beforeEach(inject(function($filter) { filter = $filter; durationFilter = filter("duration"); @@ -47,4 +282,210 @@ describe('durationFilter', function() { var output = durationFilter(1000*60*60*2 + 1000*60*35); expect(output).toEqual("2h"); }); -}); \ No newline at end of file +}); + +describe('orderMembersList filter', function() { + var filter, orderMembersList; + + beforeEach(module('matrixWebClient')); + beforeEach(inject(function($filter) { + filter = $filter; + orderMembersList = filter("orderMembersList"); + })); + + it("should sort a single entry", function() { + var output = orderMembersList({ + "@a:example.com": { + last_active_ago: 50, + last_updated: 1415266943964 + } + }); + expect(output).toEqual([{ + id: "@a:example.com", + last_active_ago: 50, + last_updated: 1415266943964 + }]); + }); + + it("should sort by taking last_active_ago into account", function() { + var output = orderMembersList({ + "@a:example.com": { + last_active_ago: 1000, + last_updated: 1415266943964 + }, + "@b:example.com": { + last_active_ago: 50, + last_updated: 1415266943964 + }, + "@c:example.com": { + last_active_ago: 99999, + last_updated: 1415266943964 + } + }); + expect(output).toEqual([ + { + id: "@b:example.com", + last_active_ago: 50, + last_updated: 1415266943964 + }, + { + id: "@a:example.com", + last_active_ago: 1000, + last_updated: 1415266943964 + }, + { + id: "@c:example.com", + last_active_ago: 99999, + last_updated: 1415266943964 + }, + ]); + }); + + it("should sort by taking last_updated into account", function() { + var output = orderMembersList({ + "@a:example.com": { + last_active_ago: 1000, + last_updated: 1415266943964 + }, + "@b:example.com": { + last_active_ago: 1000, + last_updated: 1415266900000 + }, + "@c:example.com": { + last_active_ago: 1000, + last_updated: 1415266943000 + } + }); + expect(output).toEqual([ + { + id: "@a:example.com", + last_active_ago: 1000, + last_updated: 1415266943964 + }, + { + id: "@c:example.com", + last_active_ago: 1000, + last_updated: 1415266943000 + }, + { + id: "@b:example.com", + last_active_ago: 1000, + last_updated: 1415266900000 + }, + ]); + }); + + it("should sort by taking last_updated and last_active_ago into account", + function() { + var output = orderMembersList({ + "@a:example.com": { + last_active_ago: 1000, + last_updated: 1415266943000 + }, + "@b:example.com": { + last_active_ago: 100000, + last_updated: 1415266943900 + }, + "@c:example.com": { + last_active_ago: 1000, + last_updated: 1415266943964 + } + }); + expect(output).toEqual([ + { + id: "@c:example.com", + last_active_ago: 1000, + last_updated: 1415266943964 + }, + { + id: "@a:example.com", + last_active_ago: 1000, + last_updated: 1415266943000 + }, + { + id: "@b:example.com", + last_active_ago: 100000, + last_updated: 1415266943900 + }, + ]); + }); + + // SYWEB-26 comment + it("should sort members who do not have last_active_ago value at the end of the list", + function() { + // single undefined entry + var output = orderMembersList({ + "@a:example.com": { + last_active_ago: 1000, + last_updated: 1415266943964 + }, + "@b:example.com": { + last_active_ago: 100000, + last_updated: 1415266943964 + }, + "@c:example.com": { + last_active_ago: undefined, + last_updated: 1415266943964 + } + }); + expect(output).toEqual([ + { + id: "@a:example.com", + last_active_ago: 1000, + last_updated: 1415266943964 + }, + { + id: "@b:example.com", + last_active_ago: 100000, + last_updated: 1415266943964 + }, + { + id: "@c:example.com", + last_active_ago: undefined, + last_updated: 1415266943964 + }, + ]); + }); + + it("should sort multiple members who do not have last_active_ago according to presence", + function() { + // single undefined entry + var output = orderMembersList({ + "@a:example.com": { + last_active_ago: undefined, + last_updated: 1415266943964, + presence: "unavailable" + }, + "@b:example.com": { + last_active_ago: undefined, + last_updated: 1415266943964, + presence: "online" + }, + "@c:example.com": { + last_active_ago: undefined, + last_updated: 1415266943964, + presence: "offline" + } + }); + expect(output).toEqual([ + { + id: "@b:example.com", + last_active_ago: undefined, + last_updated: 1415266943964, + presence: "online" + }, + { + id: "@a:example.com", + last_active_ago: undefined, + last_updated: 1415266943964, + presence: "unavailable" + }, + { + id: "@c:example.com", + last_active_ago: undefined, + last_updated: 1415266943964, + presence: "offline" + }, + ]); + }); +}); -- cgit 1.4.1 From a92092340b5de022d1e48ecd4176cfb9b200b4d6 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 6 Nov 2014 11:14:31 +0000 Subject: Fix broken tests which were previously skipped. --- syweb/webclient/components/matrix/matrix-filter.js | 14 +++++++++++++- syweb/webclient/test/unit/filters.spec.js | 19 +++++++------------ 2 files changed, 20 insertions(+), 13 deletions(-) (limited to 'syweb/webclient/test/unit') diff --git a/syweb/webclient/components/matrix/matrix-filter.js b/syweb/webclient/components/matrix/matrix-filter.js index 4d264e93f3..e84c197c76 100644 --- a/syweb/webclient/components/matrix/matrix-filter.js +++ b/syweb/webclient/components/matrix/matrix-filter.js @@ -38,7 +38,7 @@ function($rootScope, matrixService, eventHandlerService, modelService) { if (room.state("m.room.join_rules") && room.state("m.room.join_rules").content) { isPublicRoom = ("public" === room.state("m.room.join_rules").content.join_rule); } - + if (room_name_event) { roomName = room_name_event.content.name; } @@ -56,6 +56,9 @@ function($rootScope, matrixService, eventHandlerService, modelService) { var member = room.members[i]; if (member.state_key !== user_id) { roomName = eventHandlerService.getUserDisplayName(room_id, member.state_key); + if (!roomName) { + roomName = member.state_key; + } break; } } @@ -70,13 +73,22 @@ function($rootScope, matrixService, eventHandlerService, modelService) { if (room.members[otherUserId].content.membership === "invite") { // someone invited us, use the right ID. roomName = eventHandlerService.getUserDisplayName(room_id, room.members[otherUserId].user_id); + if (!roomName) { + roomName = room.members[otherUserId].user_id; + } } else { roomName = eventHandlerService.getUserDisplayName(room_id, otherUserId); + if (!roomName) { + roomName = user_id; + } } } else { // it isn't us, so use their name if we know it. roomName = eventHandlerService.getUserDisplayName(room_id, otherUserId); + if (!roomName) { + roomName = otherUserId; + } } } else if (Object.keys(room.members).length === 0) { diff --git a/syweb/webclient/test/unit/filters.spec.js b/syweb/webclient/test/unit/filters.spec.js index f037425208..2e8d0c4036 100644 --- a/syweb/webclient/test/unit/filters.spec.js +++ b/syweb/webclient/test/unit/filters.spec.js @@ -121,9 +121,8 @@ describe('mRoomName filter', function() { /**** ROOM ALIAS ****/ - // FIXME - xit("should show the room alias if one exists for private (invite join_rules) rooms if a room name doesn't exist.", function() { - var testAlias = "#thealias:matrix.org"; + it("should show the room alias if one exists for private (invite join_rules) rooms if a room name doesn't exist.", function() { + testAlias = "#thealias:matrix.org"; testUserId = "@me:matrix.org"; testRoomState.setJoinRule("invite"); testRoomState.setMember(testUserId, "join"); @@ -131,9 +130,8 @@ describe('mRoomName filter', function() { expect(output).toEqual(testAlias); }); - // FIXME - xit("should show the room alias if one exists for public (public join_rules) rooms if a room name doesn't exist.", function() { - var testAlias = "#thealias:matrix.org"; + it("should show the room alias if one exists for public (public join_rules) rooms if a room name doesn't exist.", function() { + testAlias = "#thealias:matrix.org"; testUserId = "@me:matrix.org"; testRoomState.setJoinRule("public"); testRoomState.setMember(testUserId, "join"); @@ -172,8 +170,7 @@ describe('mRoomName filter', function() { expect(output).toEqual(testDisplayName); }); - // FIXME - xit("should show your user ID for private (invite join_rules) rooms if a room name and alias don't exist and it is a self-chat and they don't have a display name set.", function() { + it("should show your user ID for private (invite join_rules) rooms if a room name and alias don't exist and it is a self-chat and they don't have a display name set.", function() { testUserId = "@me:matrix.org"; testRoomState.setJoinRule("private"); testRoomState.setMember(testUserId, "join"); @@ -194,8 +191,7 @@ describe('mRoomName filter', function() { expect(output).toEqual(testOtherDisplayName); }); - // FIXME - xit("should show the other user's ID for private (invite join_rules) rooms if a room name and alias don't exist and it is a 1:1-chat and they don't have a display name set.", function() { + it("should show the other user's ID for private (invite join_rules) rooms if a room name and alias don't exist and it is a 1:1-chat and they don't have a display name set.", function() { testUserId = "@me:matrix.org"; otherUserId = "@alice:matrix.org"; testRoomState.setJoinRule("private"); @@ -220,8 +216,7 @@ describe('mRoomName filter', function() { expect(output).toEqual(testOtherDisplayName); }); - // FIXME - xit("should show the other user's ID for private (invite join_rules) rooms if you are invited to it and the inviter doesn't have a display name.", function() { + it("should show the other user's ID for private (invite join_rules) rooms if you are invited to it and the inviter doesn't have a display name.", function() { testUserId = "@me:matrix.org"; testDisplayName = "Me"; otherUserId = "@alice:matrix.org"; -- cgit 1.4.1 From dd8af5565b5822dd2700ef8c2082134679fdc3ba Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 6 Nov 2014 11:55:07 +0000 Subject: Start adding regression tests. First up, register-controller for SYWEB-109. --- syweb/webclient/login/register-controller.js | 2 +- .../test/unit/register-controller.spec.js | 84 ++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 syweb/webclient/test/unit/register-controller.spec.js (limited to 'syweb/webclient/test/unit') diff --git a/syweb/webclient/login/register-controller.js b/syweb/webclient/login/register-controller.js index be970ce1c3..b23a72b185 100644 --- a/syweb/webclient/login/register-controller.js +++ b/syweb/webclient/login/register-controller.js @@ -124,7 +124,7 @@ angular.module('RegisterController', ['matrixService']) $location.url("home"); }, function(error) { - console.trace("Registration error: "+error); + console.error("Registration error: "+JSON.stringify(error)); if (useCaptcha) { Recaptcha.reload(); } diff --git a/syweb/webclient/test/unit/register-controller.spec.js b/syweb/webclient/test/unit/register-controller.spec.js new file mode 100644 index 0000000000..369704c0da --- /dev/null +++ b/syweb/webclient/test/unit/register-controller.spec.js @@ -0,0 +1,84 @@ +describe("RegisterController ", function() { + var rootScope, scope, ctrl, $q, $timeout; + var userId = "@foo:bar"; + var displayName = "Foo"; + var avatarUrl = "avatar.url"; + + window.webClientConfig = { + useCapatcha: false + }; + + // test vars + var testRegisterData, testFailRegisterData; + + + // mock services + var matrixService = { + config: function() { + return { + user_id: userId + } + }, + setConfig: function(){}, + register: function(mxid, password, threepidCreds, useCaptcha) { + var d = $q.defer(); + if (testFailRegisterData) { + d.reject({ + data: testFailRegisterData + }); + } + else { + d.resolve({ + data: testRegisterData + }); + } + return d.promise; + } + }; + + var eventStreamService = {}; + + beforeEach(function() { + module('matrixWebClient'); + + // reset test vars + testRegisterData = undefined; + testFailRegisterData = undefined; + }); + + beforeEach(inject(function($rootScope, $injector, $location, $controller, _$q_, _$timeout_) { + $q = _$q_; + $timeout = _$timeout_; + scope = $rootScope.$new(); + rootScope = $rootScope; + routeParams = { + user_matrix_id: userId + }; + ctrl = $controller('RegisterController', { + '$scope': scope, + '$rootScope': $rootScope, + '$location': $location, + 'matrixService': matrixService, + 'eventStreamService': eventStreamService + }); + }) + ); + + // SYWEB-109 + it('should display an error if the HS rejects the username on registration', function() { + var prevFeedback = angular.copy(scope.feedback); + + testFailRegisterData = { + errcode: "M_UNKNOWN", + error: "I am rejecting you." + }; + + scope.account.pwd1 = "password"; + scope.account.pwd2 = "password"; + scope.account.desired_user_id = "bob"; + scope.register(); + rootScope.$digest(); + + expect(scope.feedback).toNotEqual(prevFeedback); + }); +}); -- cgit 1.4.1 From c9c2e395316a8f8ef905d1ca9d7e0bbab2c39f7e Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 6 Nov 2014 12:00:03 +0000 Subject: Use .not.toEqual instead of .toNotEqual which is in a newer version of Jasmine. --- syweb/webclient/test/unit/register-controller.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'syweb/webclient/test/unit') diff --git a/syweb/webclient/test/unit/register-controller.spec.js b/syweb/webclient/test/unit/register-controller.spec.js index 369704c0da..ce6ef1f4e2 100644 --- a/syweb/webclient/test/unit/register-controller.spec.js +++ b/syweb/webclient/test/unit/register-controller.spec.js @@ -79,6 +79,6 @@ describe("RegisterController ", function() { scope.register(); rootScope.$digest(); - expect(scope.feedback).toNotEqual(prevFeedback); + expect(scope.feedback).not.toEqual(prevFeedback); }); }); -- cgit 1.4.1 From 8bcd36377a04bede2e2d74dcd7f18742d0982ad5 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 6 Nov 2014 13:37:05 +0000 Subject: Factor out room name logic: mRoomName is the canonical source. --- .../webclient/components/matrix/event-handler-service.js | 16 ++++------------ syweb/webclient/test/unit/register-controller.spec.js | 4 ++-- 2 files changed, 6 insertions(+), 14 deletions(-) (limited to 'syweb/webclient/test/unit') diff --git a/syweb/webclient/components/matrix/event-handler-service.js b/syweb/webclient/components/matrix/event-handler-service.js index 027c80a1b6..38a6efced7 100644 --- a/syweb/webclient/components/matrix/event-handler-service.js +++ b/syweb/webclient/components/matrix/event-handler-service.js @@ -26,8 +26,8 @@ Typically, this service will store events and broadcast them to any listeners (e.g. controllers) via $broadcast. */ angular.module('eventHandlerService', []) -.factory('eventHandlerService', ['matrixService', '$rootScope', '$q', '$timeout', 'mPresence', 'notificationService', 'modelService', -function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService, modelService) { +.factory('eventHandlerService', ['matrixService', '$rootScope', '$q', '$timeout', '$filter', 'mPresence', 'notificationService', 'modelService', +function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificationService, modelService) { var ROOM_CREATE_EVENT = "ROOM_CREATE_EVENT"; var MSG_EVENT = "MSG_EVENT"; var MEMBER_EVENT = "MEMBER_EVENT"; @@ -135,16 +135,8 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService else if (event.content.msgtype === "m.image") { message = displayname + " sent an image."; } - - var roomTitle = matrixService.getRoomIdToAliasMapping(event.room_id); - var theRoom = modelService.getRoom(event.room_id); - if (!roomTitle && theRoom.current_room_state.state("m.room.name") && theRoom.current_room_state.state("m.room.name").content) { - roomTitle = theRoom.current_room_state.state("m.room.name").content.name; - } - - if (!roomTitle) { - roomTitle = event.room_id; - } + + var roomTitle = $filter("mRoomName")(event.room_id); notificationService.showNotification( displayname + " (" + roomTitle + ")", diff --git a/syweb/webclient/test/unit/register-controller.spec.js b/syweb/webclient/test/unit/register-controller.spec.js index ce6ef1f4e2..b5c7842358 100644 --- a/syweb/webclient/test/unit/register-controller.spec.js +++ b/syweb/webclient/test/unit/register-controller.spec.js @@ -76,8 +76,8 @@ describe("RegisterController ", function() { scope.account.pwd1 = "password"; scope.account.pwd2 = "password"; scope.account.desired_user_id = "bob"; - scope.register(); - rootScope.$digest(); + scope.register(); // this depends on the result of a deferred + rootScope.$digest(); // which is delivered after the digest expect(scope.feedback).not.toEqual(prevFeedback); }); -- cgit 1.4.1 From b77cce4ec56c3b4fd74ea65416e030fc321d4fa4 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 6 Nov 2014 14:18:23 +0000 Subject: Add modelService test. Thin for now but will be expanded upon. --- syweb/webclient/test/unit/model-service.spec.js | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 syweb/webclient/test/unit/model-service.spec.js (limited to 'syweb/webclient/test/unit') diff --git a/syweb/webclient/test/unit/model-service.spec.js b/syweb/webclient/test/unit/model-service.spec.js new file mode 100644 index 0000000000..2e012efe90 --- /dev/null +++ b/syweb/webclient/test/unit/model-service.spec.js @@ -0,0 +1,30 @@ +describe('ModelService', function() { + + // setup the dependencies + beforeEach(function() { + // dependencies + module('matrixService'); + + // tested service + module('modelService'); + }); + + it('should be able to get a member in a room', inject( + function(modelService) { + var roomId = "!wefiohwefuiow:matrix.org"; + var userId = "@bob:matrix.org"; + + modelService.getRoom(roomId).current_room_state.storeStateEvent({ + type: "m.room.member", + id: "fwefw:matrix.org", + user_id: userId, + state_key: userId, + content: { + membership: "join" + } + }); + + var user = modelService.getMember(roomId, userId); + expect(user.state_key).toEqual(userId); + })); +}); -- cgit 1.4.1 From e3c3f5a6d04bfbc0256010e9fb4dad7616ebbcc5 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 6 Nov 2014 14:52:22 +0000 Subject: Swap from using raw m.room.member events for room members to using actual RoomMember objects, so User objects can be tacked on. Update tests. --- .../components/matrix/event-handler-service.js | 9 +++-- syweb/webclient/components/matrix/matrix-call.js | 2 +- syweb/webclient/components/matrix/matrix-filter.js | 8 ++--- syweb/webclient/components/matrix/model-service.js | 4 ++- syweb/webclient/recents/recents-filter.js | 3 ++ syweb/webclient/room/room-controller.js | 4 +-- .../test/unit/event-handler-service.spec.js | 38 ++++++++++++++-------- syweb/webclient/test/unit/filters.spec.js | 12 ++++--- syweb/webclient/test/unit/model-service.spec.js | 2 +- 9 files changed, 52 insertions(+), 30 deletions(-) (limited to 'syweb/webclient/test/unit') diff --git a/syweb/webclient/components/matrix/event-handler-service.js b/syweb/webclient/components/matrix/event-handler-service.js index 38a6efced7..a9c6eb34c7 100644 --- a/syweb/webclient/components/matrix/event-handler-service.js +++ b/syweb/webclient/components/matrix/event-handler-service.js @@ -141,7 +141,7 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati notificationService.showNotification( displayname + " (" + roomTitle + ")", message, - member ? member.avatar_url : undefined, + member ? member.event.content.avatar_url : undefined, function() { console.log("notification.onclick() room=" + event.room_id); $rootScope.goToPage('room/' + event.room_id); @@ -306,6 +306,9 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati // Get the user display name from the member list of the room var member = modelService.getMember(room_id, user_id); + if (member) { + member = member.event; + } if (member && member.content.displayname) { // Do not consider null displayname displayName = member.content.displayname; @@ -315,7 +318,7 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati for (var member_id in room.current_room_state.members) { if (room.current_room_state.members.hasOwnProperty(member_id) && member_id !== user_id) { - var member2 = room.current_room_state.members[member_id]; + var member2 = room.current_room_state.members[member_id].event; if (member2.content.displayname && member2.content.displayname === displayName) { displayName = displayName + " (" + user_id + ")"; break; @@ -551,7 +554,7 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati for (var i in room.current_room_state.members) { if (!room.current_room_state.members.hasOwnProperty(i)) continue; - var member = room.current_room_state.members[i]; + var member = room.current_room_state.members[i].event; if ("join" === member.content.membership) { memberCount = memberCount + 1; diff --git a/syweb/webclient/components/matrix/matrix-call.js b/syweb/webclient/components/matrix/matrix-call.js index 5a2807c755..465b2b7807 100644 --- a/syweb/webclient/components/matrix/matrix-call.js +++ b/syweb/webclient/components/matrix/matrix-call.js @@ -214,7 +214,7 @@ angular.module('MatrixCall', []) var self = this; var roomMembers = modelService.getRoom(this.room_id).current_room_state.members; - if (roomMembers[matrixService.config().user_id].membership != 'join') { + if (roomMembers[matrixService.config().user_id].event.content.membership != 'join') { console.log("We need to join the room before we can accept this call"); matrixService.join(this.room_id).then(function() { self.answer(); diff --git a/syweb/webclient/components/matrix/matrix-filter.js b/syweb/webclient/components/matrix/matrix-filter.js index e84c197c76..aeebedc784 100644 --- a/syweb/webclient/components/matrix/matrix-filter.js +++ b/syweb/webclient/components/matrix/matrix-filter.js @@ -53,7 +53,7 @@ function($rootScope, matrixService, eventHandlerService, modelService) { for (var i in room.members) { if (!room.members.hasOwnProperty(i)) continue; - var member = room.members[i]; + var member = room.members[i].event; if (member.state_key !== user_id) { roomName = eventHandlerService.getUserDisplayName(room_id, member.state_key); if (!roomName) { @@ -70,11 +70,11 @@ function($rootScope, matrixService, eventHandlerService, modelService) { if (otherUserId === user_id) { // it's us, we may have been invited to this room or it could // be a self chat. - if (room.members[otherUserId].content.membership === "invite") { + if (room.members[otherUserId].event.content.membership === "invite") { // someone invited us, use the right ID. - roomName = eventHandlerService.getUserDisplayName(room_id, room.members[otherUserId].user_id); + roomName = eventHandlerService.getUserDisplayName(room_id, room.members[otherUserId].event.user_id); if (!roomName) { - roomName = room.members[otherUserId].user_id; + roomName = room.members[otherUserId].event.user_id; } } else { diff --git a/syweb/webclient/components/matrix/model-service.js b/syweb/webclient/components/matrix/model-service.js index 8b2ee877b1..8e0ce8d1a9 100644 --- a/syweb/webclient/components/matrix/model-service.js +++ b/syweb/webclient/components/matrix/model-service.js @@ -106,7 +106,9 @@ angular.module('modelService', []) storeStateEvent: function storeState(event) { this.state_events[event.type + event.state_key] = event; if (event.type === "m.room.member") { - this.members[event.state_key] = event; + var rm = new RoomMember(); + rm.event = event; + this.members[event.state_key] = rm; } }, diff --git a/syweb/webclient/recents/recents-filter.js b/syweb/webclient/recents/recents-filter.js index 39c2359967..cfbc6f4bd8 100644 --- a/syweb/webclient/recents/recents-filter.js +++ b/syweb/webclient/recents/recents-filter.js @@ -30,6 +30,9 @@ angular.module('RecentsController') // Show the room only if the user has joined it or has been invited // (ie, do not show it if he has been banned) var member = modelService.getMember(room_id, user_id); + if (member) { + member = member.event; + } room.recent.me = member; if (member && ("invite" === member.content.membership || "join" === member.content.membership)) { if ("invite" === member.content.membership) { diff --git a/syweb/webclient/room/room-controller.js b/syweb/webclient/room/room-controller.js index a2bc23195d..d3fb85b9dc 100644 --- a/syweb/webclient/room/room-controller.js +++ b/syweb/webclient/room/room-controller.js @@ -754,13 +754,13 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput']) for (var i in members) { if (!members.hasOwnProperty(i)) continue; - var member = members[i]; + var member = members[i].event; updateMemberList(member); } // Check if the user has already join the room if ($scope.state.user_id in members) { - if ("join" === members[$scope.state.user_id].membership) { + if ("join" === members[$scope.state.user_id].event.content.membership) { needsToJoin = false; } } diff --git a/syweb/webclient/test/unit/event-handler-service.spec.js b/syweb/webclient/test/unit/event-handler-service.spec.js index 023abec98b..2a4dc3b5a5 100644 --- a/syweb/webclient/test/unit/event-handler-service.spec.js +++ b/syweb/webclient/test/unit/event-handler-service.spec.js @@ -36,20 +36,28 @@ describe('EventHandlerService', function() { current_room_state: { members: { "@adam:matrix.org": { - content: { membership: "join" }, - user_id: "@adam:matrix.org" + event: { + content: { membership: "join" }, + user_id: "@adam:matrix.org" + } }, "@beth:matrix.org": { - content: { membership: "invite" }, - user_id: "@beth:matrix.org" + event: { + content: { membership: "invite" }, + user_id: "@beth:matrix.org" + } }, "@charlie:matrix.org": { - content: { membership: "join" }, - user_id: "@charlie:matrix.org" + event: { + content: { membership: "join" }, + user_id: "@charlie:matrix.org" + } }, "@danice:matrix.org": { - content: { membership: "leave" }, - user_id: "@danice:matrix.org" + event: { + content: { membership: "leave" }, + user_id: "@danice:matrix.org" + } } } } @@ -70,12 +78,16 @@ describe('EventHandlerService', function() { current_room_state: { members: { "@adam:matrix.org": { - content: { membership: "join" }, - user_id: "@adam:matrix.org" + event: { + content: { membership: "join" }, + user_id: "@adam:matrix.org" + } }, "@beth:matrix.org": { - content: { membership: "join" }, - user_id: "@beth:matrix.org" + event: { + content: { membership: "join" }, + user_id: "@beth:matrix.org" + } } }, s: { @@ -102,4 +114,4 @@ describe('EventHandlerService', function() { num = eventHandlerService.getUserPowerLevel(roomId, "@unknown:matrix.org"); expect(num).toEqual(50); })); -}); \ No newline at end of file +}); diff --git a/syweb/webclient/test/unit/filters.spec.js b/syweb/webclient/test/unit/filters.spec.js index 2e8d0c4036..7324a8e028 100644 --- a/syweb/webclient/test/unit/filters.spec.js +++ b/syweb/webclient/test/unit/filters.spec.js @@ -86,11 +86,13 @@ describe('mRoomName filter', function() { inviter_user_id = user_id; } this.s["m.room.member" + user_id] = { - content: { - membership: membership - }, - state_key: user_id, - user_id: inviter_user_id + event: { + content: { + membership: membership + }, + state_key: user_id, + user_id: inviter_user_id + } }; this.members[user_id] = this.s["m.room.member" + user_id]; } diff --git a/syweb/webclient/test/unit/model-service.spec.js b/syweb/webclient/test/unit/model-service.spec.js index 2e012efe90..e2fa8ceba3 100644 --- a/syweb/webclient/test/unit/model-service.spec.js +++ b/syweb/webclient/test/unit/model-service.spec.js @@ -25,6 +25,6 @@ describe('ModelService', function() { }); var user = modelService.getMember(roomId, userId); - expect(user.state_key).toEqual(userId); + expect(user.event.state_key).toEqual(userId); })); }); -- cgit 1.4.1