UNPKG

linagora-rse

Version:
549 lines (450 loc) 27.5 kB
'use strict'; /* global chai, sinon: false */ var expect = chai.expect; describe('The Avatar Angular module', function() { var picture130x130 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIIAAACCCAIAAAAFYYeqAAAACXBIWXMAAAGKAAABigEzlzBYAAAAB3RJTUUH3wYeDhoh5uAW2QAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAB/pSURBVHjazZ3beqLcEkVRliBq8v4P8L/gToLikX0x4uhqNLYHMHLRXzqdjkCtOs2aVTX677//xuNxSqmqqtlstlgsFovFfD6fz+fT6XQ8Hu92u91uV9d1XdcfHx+fn59fX1/r9Xq/32dZllIqy3I6nb69vb2/v8/n89lsNpvNiqLI8zzP86zXa7/fb7fbpmlWq9Vyufzf//738fFR13XTNJvNpm3bPM/Lsqyqaj6fv7+/c0tVVU0mk8lkMh6Pr/mUw+Gw2+02m81ms+GD6rr++vri6/V63bbtfr8fj8dlWU4mk6qqeHs8flVVRVGklCaTyZVvII1Go8PhcDgcttvtcrkcjUaj0SjP89FolGVZnueHw6FpmqZplsvlarVqmma/3+/3e565KIqiKHjs6fFKKaWUrnzmm648z9u2nUwm3PB8Pt/tdofjxdebzWY8Hud5zm3kec6d+MU/ZbDf73e7HS+kaZqvry9lwPd5RXwEMnh7e+MNVFU1nU7z43Xlc6XJZLLdbrfbLX9HEt5QSol7QgabzWa73W42G6SFwDl9s9lsOp2WZclBGEIGXOPxeDKZtG1bVdV+v6+qipOLDHgQJJGOF1/vdjte32UZqAo8LzKo6xrl2O12CpVTX1UV9gMB+Pg3WYJUliUqtt/v1+t1lmVFUaxWq/F4vF6vOXfcE8dhu90eDgcerCiKsiyxY3yN7vduizpiSCkdDoeiKGaz2X6/32w20+kUezUej3mP3LzC4K44PT8dkbZtEaTP+/n5qcXDDGRZNhqNeNdRDziL2IbxeHzrKUxlWR4Oh/V6vdvteJK6rvEHvFaec71e+zNt23KsOAtlWXIT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NOlDdrccruaEC3CI7TiR3EnNnGnYn2jmt2WXHNmB0wiqeRkm4ED+RHXdtPqgQWZZBtjRyjVNdkAR7z2KPkICY1RRe+sfHB7EpSYmGOm7dJWwRqMAZ4DhvfZxkt6xjvWxHIDTyM06JRmdrdvaVRh6mCY5yUhi97FaPqTWHSXngqxylE5EYx31ZauRVxOIloGF0yK7ZhQRk65iDku+AbZLtaW63QAmikGOX2enzx0+1jUJXTL3erEpTIH4eNzDfLYk4ZpTPsrFJm2lK4SIVZAAKIpYjRGGHQNz4qpHgjMb2vUdoaikuLcLk8RkOJf9n9iGQGQdFdGYO+CLcpGEK5r/K7HtEErg0O5Bj0RAvRc7vCALWP…'; beforeEach(angular.mock.module('esn.avatar')); describe('imgLoaded directive', function() { var html = '<img-loaded width="512"/>'; beforeEach(inject(['$compile', '$rootScope', 'selectionService', function($c, $r, selectionService) { this.$compile = $c; this.$rootScope = $r; this.selectionService = selectionService; }])); it('should draw the loaded image in a canvas on crop:loaded event', function(done) { var img = {img: 'mock'}; this.selectionService.image = img; var element = this.$compile(html)(this.$rootScope); var document = element[0].ownerDocument; var create = document.createElement; var drawImage = function(image) { document.createElement = create; expect(image).to.equal(img); done(); }; document.createElement = function() { return { getContext: function() { return { drawImage: drawImage }; } }; }; this.$rootScope.$broadcast('crop:loaded'); }); }); describe('loadButton directive', function() { var html = '<input type="file" load-button/>'; var initDirective; beforeEach(inject(['$compile', '$rootScope', 'selectionService', function($c, $r, selectionService) { this.$compile = $c; this.$rootScope = $r; this.selectionService = selectionService; this.$scope = this.$rootScope.$new(); }])); beforeEach(function() { var self = this; initDirective = function() { var element = self.$compile(html)(self.$scope); self.$scope.$digest(); return element; }; }); it('should set an error in the scope if file is not set', function(done) { var element = initDirective(); element.trigger('change'); expect(this.selectionService.getError()).to.equal('Wrong file type, please select a valid image'); done(); }); it('should set an error in the scope if file size is > 5MB', function(done) { var element = initDirective(); var file = { type: 'change', dataTransfer: { files: [{ type: 'image/', size: Math.pow(2, 24) }] } }; element.trigger(file); expect(this.selectionService.getError()).to.equal('File is too large (maximum size is 5 Mb)'); done(); }); it('should not set an error in the scope if file size is < 5MB', function(done) { var element = initDirective(); var file = { type: 'change', dataTransfer: { files: [{ type: 'image/', size: Math.pow(2, 10) }] } }; element.trigger(file); expect(this.selectionService.getError()).to.be.null; done(); }); }); describe('selectionService service', function() { var AVATAR_MIN_SIZE; beforeEach(angular.mock.inject(function(selectionService, $rootScope, _AVATAR_MIN_SIZE_PX_) { this.selectionService = selectionService; AVATAR_MIN_SIZE = _AVATAR_MIN_SIZE_PX_; this.$rootScope = $rootScope; })); it('should fire an event to crop:loaded topic when setting an image', function(done) { var image = 'foo.png'; this.$rootScope.$broadcast = function(topic) { expect(topic).to.equal('crop:loaded'); done(); }; this.selectionService.setImage(image); }); it('should broadcast x to crop:selected topic when calling broadcastSelection(x)', function(done) { var selection = {x: 0, y: 1}; this.$rootScope.$broadcast = function(topic, data) { expect(topic).to.equal('crop:selected'); expect(data).to.equal(selection); done(); }; this.selectionService.broadcastSelection(selection); }); it('should save the input image', function(done) { var input = 'foo.png'; this.selectionService.setImage(input); expect(this.selectionService.image).to.equal(input); done(); }); it('should save the error', function(done) { var error = 'fail'; this.selectionService.setError(error); expect(this.selectionService.error).to.equal(error); done(); }); it('should broadcast the error to crop:error topic when calling setError(err)', function(done) { var error = 'fail'; this.$rootScope.$broadcast = function(topic, data) { expect(topic).to.equal('crop:error'); expect(data).to.equal(error); done(); }; this.selectionService.setError(error); }); it('should return the stored image when calling getImage', function(done) { var input = 'foo.png'; this.selectionService.image = input; var image = this.selectionService.getImage(); expect(image).to.equal(input); done(); }); it('should return 130x130 for tested picture', function(done) { var originalImage = new Image(); originalImage.src = picture130x130; originalImage.onload = function() { expect(originalImage.width).to.equal(130); expect(originalImage.height).to.equal(130); done(); }; }); it('should return AVATAR_MIN_SIZE * AVATAR_MIN_SIZE image when calling getBlob and no selection', function(done) { this.selectionService.broadcastSelection({cords: {w: 0, h: 0}}); var originalImage = new Image(); originalImage.src = picture130x130; this.selectionService.setImage(originalImage); this.selectionService.getBlob('image/png', function(blob) { var reader = new FileReader(); reader.onload = function(e) { var img = new Image(); img.src = e.target.result; img.onload = function() { expect(img.width).to.equal(AVATAR_MIN_SIZE); expect(img.height).to.equal(AVATAR_MIN_SIZE); done(); }; }; reader.readAsDataURL(blob); }); }); it('should return AVATAR_MIN_SIZE * AVATAR_MIN_SIZE image when calling getBlob and selection', function(done) { this.selectionService.broadcastSelection({cords: {w: 1, h: 1}}); var originalImage = new Image(); originalImage.src = picture130x130; this.selectionService.setImage(originalImage); this.selectionService.getBlob('image/png', function(blob) { var reader = new FileReader(); reader.onload = function(e) { var img = new Image(); img.src = e.target.result; img.onload = function() { expect(img.width).to.equal(AVATAR_MIN_SIZE); expect(img.height).to.equal(AVATAR_MIN_SIZE); done(); }; }; reader.readAsDataURL(blob); }); }); }); describe('The avatarEdit controller', function() { var userId = '123'; var domainId = '456'; beforeEach(function() { module('esn.avatar'); inject(function( session, selectionService, avatarAPI, $rootScope, $controller ) { this.session = session; this.selectionService = selectionService; this.$rootScope = $rootScope; this.avatarAPI = avatarAPI; this.scope = $rootScope.$new(); $controller('avatarEdit', { $rootScope: this.$rootScope, $scope: this.scope, session: this.session, selectionService: this.selectionService, avatarAPI: this.avatarAPI }); }); }); describe('The send function', function() { it('should call the avatarAPI.uploadAvatar to upload avatar for current user', function(done) { this.session.user = { _id: userId }; this.scope.user = { _id: userId }; var blob = 'foo'; var mime = 'bar'; this.avatarAPI.uploadAvatar = function(_blob, _mime) { expect(_blob).to.equal(blob); expect(_mime).to.equal(mime); done(); }; this.scope.send(blob, mime); done(new Error('Should not be called')); }); it('should call the avatarAPI.uploadUserAvatar to upload avatar for a specific user', function(done) { var specificUserId = '456'; var blob = 'foo'; var mime = 'bar'; this.session.user = { _id: userId }; this.session.domain = { _id: domainId }; this.scope.user = { _id: specificUserId }; this.avatarAPI.uploadUserAvatar = function(_blob, _mime, _userId, _domainId) { expect(_blob).to.equal(blob); expect(_mime).to.equal(mime); expect(_userId).to.equal(specificUserId); expect(_domainId).to.equal(domainId); done(); }; this.scope.send(blob, mime); done(new Error('Should not be called')); }); }); }); describe('avatarAPI service', function() { beforeEach(angular.mock.inject(function(selectionService, $rootScope, $httpBackend, avatarAPI) { this.selectionService = selectionService; this.$rootScope = $rootScope; this.avatarAPI = avatarAPI; this.$httpBackend = $httpBackend; })); describe('The uploadAvatar function', function() { it('should send POST to /api/user/profile/avatar with valid mime, parameters and blob', function() { var blob = '123'; var mime = 'image/png'; this.$httpBackend.expectPOST('/api/user/profile/avatar?mimetype=image%2Fpng', blob).respond(200); this.avatarAPI.uploadAvatar(blob, mime); this.$httpBackend.flush(); }); it('should return a promise', function() { var promise = this.avatarAPI.uploadAvatar('foo', 'bar'); expect(promise.then).to.be.a.function; }); }); describe('The uploadUserAvatar function', function() { it('should PUT to right endpoint to upload avatar for a specific user', function() { var blob = 'foobar'; var mime = 'image/png'; var userId = '123'; var domainId = '456'; this.$httpBackend.expectPUT('/api/users/' + userId + '/profile/avatar?domain_id=' + domainId + '&mimetype=image%2Fpng', blob).respond(200); this.avatarAPI.uploadUserAvatar(blob, mime, userId, domainId); this.$httpBackend.flush(); }); it('should return a promise', function() { var promise = this.avatarAPI.uploadUserAvatar('foo', 'bar', '123', '456'); expect(promise.then).to.be.a.function; }); }); }); describe('the EsnAvatarController', function() { var $q, $compile, $rootScope, $logMock, element, controller, esnAvatarUrlServiceMock, userAPIMock, user, result; function compileEsnAvatar(html) { element = $compile(html)($rootScope.$new()); $rootScope.$digest(); $('body').append(element); controller = element.controller('esnAvatar'); return element; } afterEach(function() { if (element) { element.remove(); } }); beforeEach(function() { $logMock = { error: sinon.spy(), info: sinon.spy(), debug: sinon.spy() }; user = { _id: '123', firstname: 'Dali', lastname: 'Dali' }; result = { data: [user] }; userAPIMock = { user: angular.noop, getUsersByEmail: sinon.spy(function() { return $q.when(result); }) }; esnAvatarUrlServiceMock = { generateUrl: sinon.spy(), generateUrlByUserId: sinon.spy() }; angular.mock.module('jadeTemplates', function($provide) { $provide.value('userAPI', userAPIMock); $provide.value('$log', $logMock); $provide.value('esnAvatarUrlService', esnAvatarUrlServiceMock); }); angular.mock.inject(function(_$compile_, _$rootScope_, _$q_) { $compile = _$compile_; $rootScope = _$rootScope_; $q = _$q_; }); $rootScope.userId = '58be757006a35238647028d8'; $rootScope.userEmail = 'dali@open-paas.org'; $rootScope.avatarURL = '/api/user/profile/avatar?cb=1490951414696'; $rootScope.resolveAvatar = function() { return $q.when({ id: 'myId', url: 'myUrl', email: 'myEmail' }); }; }); describe('$onInit function', function() { it('should initialize the avatar object from the resolveAvatar function when provided', function() { compileEsnAvatar('<esn-avatar resolve-avatar="resolveAvatar()" />'); expect(controller.avatar).to.deep.equal({ id: 'myId', url: 'myUrl', email: 'myEmail' }); }); it('should initialize the avatar.url with the same URL in avatarUrl if it is defined', function() { compileEsnAvatar('<esn-avatar avatar-url="avatarURL" user-id="userId" user-email="userEmail" />'); expect(controller.avatar.url).to.be.equal($rootScope.avatarURL); }); it('should initialize the avatarURL with the URL generate from the userId if userId defined and avatarUrl is undefined', function() { compileEsnAvatar('<esn-avatar user-id="userId" user-email="userEmail" />'); expect(esnAvatarUrlServiceMock.generateUrlByUserId).to.be.calledWith($rootScope.userId); }); it('should initialize the avatarURL with the URL generate from the userEmail if userEmail defined and the avatarUrl and userId are undefined', function() { compileEsnAvatar('<esn-avatar user-email="userEmail" />'); expect(esnAvatarUrlServiceMock.generateUrl).to.be.calledWith(controller.userEmail); }); it('should call userAPI.getUserByEmail and initialize avatar.id if the userEmail is defined and userId is undefined', function() { compileEsnAvatar('<esn-avatar user-email="userEmail" />'); expect(controller.avatar.id).to.be.equal(user._id); }); it('should not update userId if the userId is defined', function() { compileEsnAvatar('<esn-avatar user-id="userId" user-email="userEmail" />'); expect(controller.userId).to.be.equal($rootScope.userId); }); it('should not initialize userId if the userAPI.getUsersByEmail returned an empty array', function() { userAPIMock.getUsersByEmail = function() { return $q.when({ data: [] }); }; compileEsnAvatar('<esn-avatar user-email="userEmail" />'); expect(controller.userId).to.equal(undefined); }); }); describe('displayUserStatus function', function() { it('should return true if avatar.id is defined and hideUserStatus = false', function() { compileEsnAvatar('<esn-avatar resolve-avatar="resolveAvatar()" />'); expect(controller.displayUserStatus()).to.equal(true); }); it('should return false if avatar.id is defined and hideUserStatus = true', function() { compileEsnAvatar('<esn-avatar resolve-avatar="resolveAvatar()" hide-user-status="true" />'); expect(controller.displayUserStatus()).to.equal(false); }); it('should return false if avatar.id is undefined and hideUserStatus = true', function() { compileEsnAvatar('<esn-avatar hide-user-status="true" />'); expect(controller.displayUserStatus()).to.equal(false); }); it('should return false if avatar.id is undefined and hideUserStatus = false', function() { compileEsnAvatar('<esn-avatar />'); expect(controller.displayUserStatus()).to.equal(false); }); }); }); describe('esnAvatar component', function() { var $rootScope, $compile, $scope, $httpBackend, element; beforeEach(angular.mock.module('esn.user')); beforeEach(module('jadeTemplates')); beforeEach(inject(function(_$rootScope_, _$compile_, _$httpBackend_) { $rootScope = _$rootScope_; $compile = _$compile_; $httpBackend = _$httpBackend_; $scope = $rootScope.$new(); })); afterEach(function() { if (element) { element.remove(); } }); function buildElement() { var html = '<esn-avatar user-id="user.id" user-email="user.email"></esn-avatar>'; element = $compile(html)($scope); $('body').append(element); return element; } it('should update avatar url when switching from a user id to a user email', function() { $httpBackend.expectGET('/api/users?email=k2r@linagora.com').respond(200); $scope.user = { id: 'user1' }; buildElement(); $rootScope.$digest(); expect(element.find('img').attr('src')).to.equal('/api/users/user1/profile/avatar'); $scope.user = { email: 'k2r@linagora.com' }; $rootScope.$digest(); expect(element.find('img').attr('src')).to.equal('/api/avatars?email=k2r@linagora.com&objectType=email'); }); it('should update avatar url when switching from a user id to another user id', function() { $scope.user = { id: 'user1' }; buildElement(); $rootScope.$digest(); expect(element.find('img').attr('src')).to.equal('/api/users/user1/profile/avatar'); $scope.user = { id: 'user2' }; $rootScope.$digest(); expect(element.find('img').attr('src')).to.equal('/api/users/user2/profile/avatar'); }); }); });