From 5a5a1ed20bebc4bee8be651903980fe5f4e3c778 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 11 Jul 2023 13:22:54 +0200 Subject: [PATCH] refs #5887 feat: addback test --- back/methods/vn-user/specs/addAlias.spec.js | 68 +++++++++++++++++++++ modules/account/front/aliases/index.js | 4 +- modules/account/front/aliases/index.spec.js | 9 ++- 3 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 back/methods/vn-user/specs/addAlias.spec.js diff --git a/back/methods/vn-user/specs/addAlias.spec.js b/back/methods/vn-user/specs/addAlias.spec.js new file mode 100644 index 000000000..239d09c94 --- /dev/null +++ b/back/methods/vn-user/specs/addAlias.spec.js @@ -0,0 +1,68 @@ +const {models} = require('vn-loopback/server/server'); + +describe('VnUser addAlias()', () => { + const employeeId = 1; + const sysadminId = 66; + const developerId = 9; + const customerId = 2; + const mailAlias = 1; + it('should throw an error when user not has privileges', async() => { + const ctx = {req: {accessToken: {userId: employeeId}}}; + const tx = await models.VnUser.beginTransaction({}); + + let error; + try { + const options = {transaction: tx}; + + await models.VnUser.addAlias(ctx, employeeId, mailAlias, options); + + await tx.rollback(); + } catch (e) { + error = e; + await tx.rollback(); + } + + expect(error.message).toContain(`You don't have grant privilege`); + }); + + it('should throw an error when user has privileges but not has the role from user', async() => { + const ctx = {req: {accessToken: {userId: sysadminId}}}; + const tx = await models.VnUser.beginTransaction({}); + + let error; + try { + const options = {transaction: tx}; + + await models.VnUser.addAlias(ctx, employeeId, mailAlias, options); + + await tx.rollback(); + } catch (e) { + error = e; + await tx.rollback(); + } + + expect(error.message).toContain(`You don't have the alias assigned and you can't assign it to another user`); + }); + + it('should add an alias', async() => { + const ctx = {req: {accessToken: {userId: developerId}}}; + const tx = await models.VnUser.beginTransaction({}); + + let result; + try { + const options = {transaction: tx}; + + const user = await models.VnUser.findById(developerId, null, options); + await user.updateAttribute('hasGrant', true, options); + + result = await models.VnUser.addAlias(ctx, customerId, mailAlias, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + } + + expect(result.mailAlias).toBe(mailAlias); + expect(result.account).toBe(customerId); + }); +}); diff --git a/modules/account/front/aliases/index.js b/modules/account/front/aliases/index.js index 91d0c6e7c..e0c738ee4 100644 --- a/modules/account/front/aliases/index.js +++ b/modules/account/front/aliases/index.js @@ -38,9 +38,7 @@ export default class Controller extends Section { }; return this.$http.post(`VnUsers/${this.$params.id}/removeAlias`, params) .then(() => this.refresh()) - .then(() => this.vnApp.showSuccess( - this.$t('Subscribed to alias!')) - ); + .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))); } } diff --git a/modules/account/front/aliases/index.spec.js b/modules/account/front/aliases/index.spec.js index 466f1e1e9..61f71949c 100644 --- a/modules/account/front/aliases/index.spec.js +++ b/modules/account/front/aliases/index.spec.js @@ -25,8 +25,9 @@ describe('component vnUserAliases', () => { describe('onAddSave()', () => { it('should add the new row', () => { controller.addData = {account: 1}; + controller.$params = {id: 1}; - $httpBackend.expectPOST('MailAliasAccounts').respond(); + $httpBackend.expectPOST('VnUsers/1/addAlias').respond(); $httpBackend.expectGET('MailAliasAccounts').respond('foo'); controller.onAddSave(); $httpBackend.flush(); @@ -41,12 +42,14 @@ describe('component vnUserAliases', () => { {id: 1, alias: 'foo'}, {id: 2, alias: 'bar'} ]; + controller.$params = {id: 1}; - $httpBackend.expectDELETE('MailAliasAccounts/1').respond(); + $httpBackend.expectPOST('VnUsers/1/removeAlias').respond(); + $httpBackend.expectGET('MailAliasAccounts').respond(controller.$.data[1]); controller.onRemove(controller.$.data[0]); $httpBackend.flush(); - expect(controller.$.data).toEqual([{id: 2, alias: 'bar'}]); + expect(controller.$.data).toEqual({id: 2, alias: 'bar'}); expect(controller.vnApp.showSuccess).toHaveBeenCalled(); }); });