From 30749f0f4d8f3118541aa333db13921eac88552b Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Wed, 23 Jan 2019 17:49:28 +0100 Subject: [PATCH] #981 Back & front tests passed, keybinds on it's own module routes.json --- back/methods/account/specs/login.spec.js | 46 +++++------- front/core/index.js | 1 + front/core/lib/get-main-route.js | 8 +++ front/core/lib/index.js | 1 + front/core/lib/specs/acl-service.spec.js | 72 ++++++++++--------- front/{salix/lib => core/services}/auth.js | 0 front/{salix/lib => core/services}/index.js | 0 .../lib => core/services}/interceptor.js | 0 front/{salix/lib => core/services}/modules.js | 7 +- front/{salix/lib => core/services}/token.js | 0 front/salix/index.js | 5 +- front/salix/keybindings.yml | 6 -- front/salix/module.js | 42 ++++++----- front/salix/routes.js | 9 +-- modules/claim/front/routes.json | 3 + modules/client/front/risk/index/index.spec.js | 2 +- modules/client/front/routes.json | 3 + modules/item/front/routes.json | 3 + modules/ticket/front/routes.json | 3 + 19 files changed, 110 insertions(+), 101 deletions(-) create mode 100644 front/core/lib/get-main-route.js rename front/{salix/lib => core/services}/auth.js (100%) rename front/{salix/lib => core/services}/index.js (100%) rename front/{salix/lib => core/services}/interceptor.js (100%) rename front/{salix/lib => core/services}/modules.js (84%) rename front/{salix/lib => core/services}/token.js (100%) delete mode 100644 front/salix/keybindings.yml diff --git a/back/methods/account/specs/login.spec.js b/back/methods/account/specs/login.spec.js index 1768557ff..6a9143790 100644 --- a/back/methods/account/specs/login.spec.js +++ b/back/methods/account/specs/login.spec.js @@ -1,45 +1,33 @@ const app = require(`${serviceRoot}/server/server`); describe('account login()', () => { - it('when the user doesnt exist but the client does and the password is correct', async() => { - let response = await app.models.Account.login('PetterParker', 'nightmare'); - - expect(response.token).toBeDefined(); - }); - - describe('when the user exists and the password is correct', () => { - it('should login and return the token', async() => { + describe('when credentials are correct', () => { + it('should return the token', async() => { let response = await app.models.Account.login('employee', 'nightmare'); expect(response.token).toBeDefined(); }); - it('should define the url to continue upon login', async() => { - let location = 'http://localhost/auth/?apiKey=salix&continue=continueURL'; - let response = await app.models.Account.login('employee', 'nightmare', location); + it('should return the token if the user doesnt exist but the client does', async() => { + let response = await app.models.Account.login('PetterParker', 'nightmare'); - expect(response.continue).toBe('continueURL'); - }); - - it('should define the loginUrl upon login', async() => { - let location = 'http://localhost/auth/?apiKey=salix'; - let response = await app.models.Account.login('employee', 'nightmare', location); - - expect(response.loginUrl).toBeDefined(); + expect(response.token).toBeDefined(); }); }); - it('should throw a 401 error when credentials are incorrect', async() => { - let error; + describe('when credentials are incorrect', () => { + it('should throw a 401 error', async() => { + let error; - try { - await app.models.Account.login('IDontExist', 'TotallyWrongPassword'); - } catch (e) { - error = e; - } + try { + await app.models.Account.login('IDontExist', 'TotallyWrongPassword'); + } catch (e) { + error = e; + } - expect(error).toBeDefined(); - expect(error.statusCode).toBe(401); - expect(error.code).toBe('LOGIN_FAILED'); + expect(error).toBeDefined(); + expect(error.statusCode).toBe(401); + expect(error.code).toBe('LOGIN_FAILED'); + }); }); }); diff --git a/front/core/index.js b/front/core/index.js index d3601a8e1..fdcfaff05 100644 --- a/front/core/index.js +++ b/front/core/index.js @@ -4,4 +4,5 @@ export * from './module'; export * from './directives'; export * from './filters'; export * from './lib'; +export * from './services'; export * from './components'; diff --git a/front/core/lib/get-main-route.js b/front/core/lib/get-main-route.js new file mode 100644 index 000000000..52a86ada1 --- /dev/null +++ b/front/core/lib/get-main-route.js @@ -0,0 +1,8 @@ + +export default function getMainRoute(routes) { + for (let route of routes) { + if (!route.abstract) + return route; + } + return null; +} diff --git a/front/core/lib/index.js b/front/core/lib/index.js index 9ea493c64..a50603d4c 100644 --- a/front/core/lib/index.js +++ b/front/core/lib/index.js @@ -12,3 +12,4 @@ import './modified'; import './key-codes'; import './http-error'; import './user-error'; +import './get-main-route'; diff --git a/front/core/lib/specs/acl-service.spec.js b/front/core/lib/specs/acl-service.spec.js index f29f4bf2d..63fe30f93 100644 --- a/front/core/lib/specs/acl-service.spec.js +++ b/front/core/lib/specs/acl-service.spec.js @@ -3,46 +3,54 @@ describe('Service acl', () => { beforeEach(ngModule('vnCore')); - beforeEach(ngModule($provide => { - $provide.value('aclConstant', {}); - })); - - beforeEach(inject(_aclService_ => { + beforeEach(inject((_aclService_, $httpBackend) => { + $httpBackend.when('GET', `/api/Accounts/acl`).respond({ + roles: [ + {role: {name: 'foo'}}, + {role: {name: 'bar'}}, + {role: {name: 'baz'}} + ] + }); aclService = _aclService_; + aclService.load(); + $httpBackend.flush(); })); - it('should return false as the service doesn\'t have roles', () => { - expect(aclService.routeHasPermission('http://www.verdnatura.es')).toBeFalsy(); + describe('load()', () => { + it('should load roles from backend', () => { + expect(aclService.roles).toEqual({ + foo: true, + bar: true, + baz: true + }); + }); }); - it('should return true as the service has roles but the route has no acl', () => { - aclService.roles = {customer: true}; + describe('hasAny()', () => { + it('should return true when user has any of the passed roles', () => { + let hasAny = aclService.hasAny(['foo', 'nonExistent']); - expect(aclService.routeHasPermission('http://www.verdnatura.es')).toBeTruthy(); + expect(hasAny).toBeTruthy(); + }); + + it('should return true when user has all the passed roles', () => { + let hasAny = aclService.hasAny(['bar', 'baz']); + + expect(hasAny).toBeTruthy(); + }); + + it('should return true when user has not any of the passed roles', () => { + let hasAny = aclService.hasAny(['inventedRole', 'nonExistent']); + + expect(hasAny).toBeFalsy(); + }); }); - it('should return false as the service roles have no length', () => { - aclService.roles = {}; - let route = {url: 'http://www.verdnatura.es', acl: []}; + describe('reset()', () => { + it('should reset the roles', () => { + aclService.reset(); - expect(aclService.routeHasPermission(route)).toBeFalsy(); - }); - - it('should call the service hasAny() function and return false as the service has roles and the rote has acl without length', () => { - aclService.roles = {customer: true, employee: true}; - let route = {url: 'http://www.verdnatura.es', acl: []}; - spyOn(aclService, 'hasAny').and.callThrough(); - - expect(aclService.routeHasPermission(route)).toBeFalsy(); - expect(aclService.hasAny).toHaveBeenCalledWith(route.acl); - }); - - it('should call the service hasAny() function to return true as the service has roles matching with the ones in acl', () => { - aclService.roles = {customer: true, employee: true}; - let route = {url: 'http://www.verdnatura.es', acl: ['customer']}; - spyOn(aclService, 'hasAny').and.callThrough(); - - expect(aclService.routeHasPermission(route)).toBeTruthy(); - expect(aclService.hasAny).toHaveBeenCalledWith(route.acl); + expect(aclService.roles).toBeNull(); + }); }); }); diff --git a/front/salix/lib/auth.js b/front/core/services/auth.js similarity index 100% rename from front/salix/lib/auth.js rename to front/core/services/auth.js diff --git a/front/salix/lib/index.js b/front/core/services/index.js similarity index 100% rename from front/salix/lib/index.js rename to front/core/services/index.js diff --git a/front/salix/lib/interceptor.js b/front/core/services/interceptor.js similarity index 100% rename from front/salix/lib/interceptor.js rename to front/core/services/interceptor.js diff --git a/front/salix/lib/modules.js b/front/core/services/modules.js similarity index 84% rename from front/salix/lib/modules.js rename to front/core/services/modules.js index f44859d5d..b997df08c 100644 --- a/front/salix/lib/modules.js +++ b/front/core/services/modules.js @@ -1,6 +1,5 @@ import ngModule from '../module'; -import {getMainRoute} from '../routes'; -import keybindings from '../keybindings.yml'; +import getMainRoute from '../lib/get-main-route'; export default class Modules { constructor(aclService, $window) { @@ -25,8 +24,8 @@ export default class Modules { continue; let keyBind; - if (keybindings) { - let res = keybindings.find(i => i.sref == route.state); + if (mod.keybindings) { + let res = mod.keybindings.find(i => i.sref == route.state); if (res) keyBind = res.key.toUpperCase(); } diff --git a/front/salix/lib/token.js b/front/core/services/token.js similarity index 100% rename from front/salix/lib/token.js rename to front/core/services/token.js diff --git a/front/salix/index.js b/front/salix/index.js index ca19b465d..6788bae07 100644 --- a/front/salix/index.js +++ b/front/salix/index.js @@ -1,5 +1,4 @@ import './module'; import './routes'; -import './components/index'; -import './lib/index'; -import './styles/index'; +import './components'; +import './styles'; diff --git a/front/salix/keybindings.yml b/front/salix/keybindings.yml deleted file mode 100644 index dd625ea2f..000000000 --- a/front/salix/keybindings.yml +++ /dev/null @@ -1,6 +0,0 @@ -[ - {key: r, sref: claim.index}, - {key: c, sref: client.index}, - {key: a, sref: item.index}, - {key: t, sref: ticket.index}, -] \ No newline at end of file diff --git a/front/salix/module.js b/front/salix/module.js index 50b49849e..75bc18f2d 100644 --- a/front/salix/module.js +++ b/front/salix/module.js @@ -1,6 +1,5 @@ import {ng} from 'core/vendor'; import 'core'; -import keybindings from './keybindings.yml'; export const appName = 'salix'; @@ -21,29 +20,36 @@ export function run($window, $rootScope, vnAuth, vnApp, $state, $document) { window.myAppErrorLog.push(error); }); - for (const binding in keybindings) { - if (!keybindings[binding].key || !keybindings[binding].sref) - throw new Error('Binding not formed correctly'); + if ($window.routes) { + for (const mod of $window.routes) { + if (!mod || !mod.keybindings) + continue; - $document.on('keyup', function(e) { - if (e.defaultPrevented) return; + for (const binding of mod.keybindings) { + if (!binding.key || !binding.sref) + throw new Error('Binding not formed correctly'); - let shortcut = { - altKey: true, - ctrlKey: true, - key: keybindings[binding].key - }; + $document.on('keyup', function(e) { + if (e.defaultPrevented) return; - let correctShortcut = true; + let shortcut = { + altKey: true, + ctrlKey: true, + key: binding.key + }; - for (const key in shortcut) - correctShortcut = correctShortcut && shortcut[key] == e[key]; + let correctShortcut = true; - if (correctShortcut) { - $state.go(keybindings[binding].sref); - e.preventDefault(); + for (const key in shortcut) + correctShortcut = correctShortcut && shortcut[key] == e[key]; + + if (correctShortcut) { + $state.go(binding.sref); + e.preventDefault(); + } + }); } - }); + } } } ngModule.run(run); diff --git a/front/salix/routes.js b/front/salix/routes.js index 41e669e81..661e59a27 100644 --- a/front/salix/routes.js +++ b/front/salix/routes.js @@ -2,14 +2,7 @@ import ngModule from './module'; import deps from 'modules.yml'; import modules from 'spliting'; import splitingRegister from 'core/lib/spliting-register'; - -export function getMainRoute(routes) { - for (let route of routes) { - if (!route.abstract) - return route; - } - return null; -} +import getMainRoute from 'core/lib/get-main-route'; function loader(moduleName, validations) { load.$inject = ['vnModuleLoader']; diff --git a/modules/claim/front/routes.json b/modules/claim/front/routes.json index a418ef8aa..a8ae49f1f 100644 --- a/modules/claim/front/routes.json +++ b/modules/claim/front/routes.json @@ -78,5 +78,8 @@ {"state": "claim.card.detail", "icon": "icon-details"}, {"state": "claim.card.development", "icon": "icon-traceability"}, {"state": "claim.card.action", "icon": "icon-actions"} + ], + "keybindings": [ + {"key": "r", "sref": "claim.index"} ] } \ No newline at end of file diff --git a/modules/client/front/risk/index/index.spec.js b/modules/client/front/risk/index/index.spec.js index 925bcafe4..77f00c5e8 100644 --- a/modules/client/front/risk/index/index.spec.js +++ b/modules/client/front/risk/index/index.spec.js @@ -11,7 +11,7 @@ describe('Client', () => { beforeEach(angular.mock.inject((_$componentController_, $rootScope) => { $componentController = _$componentController_; $scope = $rootScope.$new(); - controller = $componentController('vnClientRiskIndex', {$scope: $scope}); + controller = $componentController('vnClientRiskIndex', {$scope}); })); describe('risks() setter', () => { diff --git a/modules/client/front/routes.json b/modules/client/front/routes.json index 808e5af41..c35c48b78 100644 --- a/modules/client/front/routes.json +++ b/modules/client/front/routes.json @@ -347,5 +347,8 @@ {"state": "client.card.webPayment", "icon": "icon-onlinepayment"} ] } + ], + "keybindings": [ + {"key": "c", "sref": "client.index"} ] } diff --git a/modules/item/front/routes.json b/modules/item/front/routes.json index f7ac8ebb7..bd83276b8 100644 --- a/modules/item/front/routes.json +++ b/modules/item/front/routes.json @@ -125,5 +125,8 @@ {"state": "item.card.itemBarcode", "icon": "icon-barcode"}, {"state": "item.card.diary", "icon": "icon-transaction"}, {"state": "item.card.last-entries", "icon": "icon-regentry"} + ], + "keybindings": [ + {"key": "a", "sref": "item.index"} ] } \ No newline at end of file diff --git a/modules/ticket/front/routes.json b/modules/ticket/front/routes.json index 59fd490bf..cce565342 100644 --- a/modules/ticket/front/routes.json +++ b/modules/ticket/front/routes.json @@ -240,5 +240,8 @@ {"state": "ticket.card.picture", "icon": "image"}, {"state": "ticket.card.log", "icon": "history"}, {"state": "ticket.card.request.index", "icon": "icon-100"} + ], + "keybindings": [ + {"key": "t", "sref": "ticket.index"} ] } \ No newline at end of file