2019-10-14 18:48:46 +00:00
|
|
|
import 'expect-more-jest';
|
2022-04-14 15:24:26 +00:00
|
|
|
import {
|
|
|
|
validateAuthenticateRoutePermission,
|
|
|
|
validateRouteAccess,
|
|
|
|
} from './index';
|
2019-10-14 18:48:46 +00:00
|
|
|
|
|
|
|
jest.mock('./dashboard/dashboard.routes', () => ({
|
|
|
|
routes: [],
|
|
|
|
}));
|
|
|
|
jest.mock('./auth/auth.routes', () => ({
|
|
|
|
routes: [],
|
|
|
|
}));
|
|
|
|
jest.mock('./login/login.routes', () => ({
|
|
|
|
routes: [],
|
|
|
|
}));
|
|
|
|
window.roleWiseRoutes = {};
|
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
describe('#validateAuthenticateRoutePermission', () => {
|
2019-10-14 18:48:46 +00:00
|
|
|
describe(`when route is not protected`, () => {
|
|
|
|
it(`should go to the dashboard when user is logged in`, () => {
|
2022-04-14 15:24:26 +00:00
|
|
|
const to = { name: 'login', params: { accountId: 1 } };
|
2021-11-23 12:07:01 +00:00
|
|
|
const from = { name: '', params: { accountId: 1 } };
|
2019-10-14 18:48:46 +00:00
|
|
|
const next = jest.fn();
|
2022-04-14 15:24:26 +00:00
|
|
|
const getters = {
|
|
|
|
isLoggedIn: true,
|
|
|
|
getCurrentUser: {
|
|
|
|
account_id: 1,
|
|
|
|
id: 1,
|
|
|
|
accounts: [{ id: 1, role: 'admin' }],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
validateAuthenticateRoutePermission(to, from, next, { getters });
|
2021-11-23 12:07:01 +00:00
|
|
|
expect(next).toHaveBeenCalledWith('/app/accounts/1/dashboard');
|
2019-10-14 18:48:46 +00:00
|
|
|
});
|
2022-04-14 15:24:26 +00:00
|
|
|
it(`should go there when user is not logged in`, () => {
|
|
|
|
const to = { name: 'login', params: {} };
|
|
|
|
const from = { name: '', params: {} };
|
|
|
|
const next = jest.fn();
|
|
|
|
const getters = { isLoggedIn: false };
|
|
|
|
validateAuthenticateRoutePermission(to, from, next, { getters });
|
|
|
|
expect(next).toHaveBeenCalledWith();
|
|
|
|
});
|
2019-10-14 18:48:46 +00:00
|
|
|
});
|
|
|
|
describe(`when route is protected`, () => {
|
|
|
|
describe(`when user not logged in`, () => {
|
|
|
|
it(`should redirect to login`, () => {
|
|
|
|
// Arrange
|
2021-11-23 12:07:01 +00:00
|
|
|
const to = { name: 'some-protected-route', params: { accountId: 1 } };
|
2019-10-14 18:48:46 +00:00
|
|
|
const from = { name: '' };
|
|
|
|
const next = jest.fn();
|
2022-04-14 15:24:26 +00:00
|
|
|
const getters = {
|
|
|
|
isLoggedIn: false,
|
|
|
|
getCurrentUser: {
|
|
|
|
account_id: null,
|
|
|
|
id: null,
|
|
|
|
accounts: [],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
validateAuthenticateRoutePermission(to, from, next, { getters });
|
2019-10-14 18:48:46 +00:00
|
|
|
expect(next).toHaveBeenCalledWith('/app/login');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe(`when user is logged in`, () => {
|
|
|
|
describe(`when route is not accessible to current user`, () => {
|
|
|
|
it(`should redirect to dashboard`, () => {
|
2021-11-23 12:07:01 +00:00
|
|
|
window.roleWiseRoutes.agent = ['dashboard'];
|
|
|
|
const to = { name: 'admin', params: { accountId: 1 } };
|
2019-10-14 18:48:46 +00:00
|
|
|
const from = { name: '' };
|
|
|
|
const next = jest.fn();
|
2022-04-14 15:24:26 +00:00
|
|
|
const getters = {
|
|
|
|
isLoggedIn: true,
|
|
|
|
getCurrentUser: {
|
|
|
|
account_id: 1,
|
|
|
|
id: 1,
|
|
|
|
accounts: [{ id: 1, role: 'agent' }],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
validateAuthenticateRoutePermission(to, from, next, { getters });
|
2021-11-23 12:07:01 +00:00
|
|
|
expect(next).toHaveBeenCalledWith('/app/accounts/1/dashboard');
|
2019-10-14 18:48:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe(`when route is accessible to current user`, () => {
|
|
|
|
it(`should go there`, () => {
|
2021-11-23 12:07:01 +00:00
|
|
|
window.roleWiseRoutes.agent = ['dashboard', 'admin'];
|
|
|
|
const to = { name: 'admin', params: { accountId: 1 } };
|
2019-10-14 18:48:46 +00:00
|
|
|
const from = { name: '' };
|
|
|
|
const next = jest.fn();
|
2022-04-14 15:24:26 +00:00
|
|
|
const getters = {
|
|
|
|
isLoggedIn: true,
|
|
|
|
getCurrentUser: {
|
|
|
|
account_id: 1,
|
|
|
|
id: 1,
|
|
|
|
accounts: [{ id: 1, role: 'agent' }],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
validateAuthenticateRoutePermission(to, from, next, { getters });
|
2019-10-14 18:48:46 +00:00
|
|
|
expect(next).toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-04-14 15:24:26 +00:00
|
|
|
|
|
|
|
describe('#validateRouteAccess', () => {
|
|
|
|
it('returns to login if signup is disabled', () => {
|
|
|
|
window.chatwootConfig = { signupEnabled: 'false' };
|
|
|
|
const to = { name: 'auth_signup', meta: { requireSignupEnabled: true } };
|
|
|
|
const from = { name: '' };
|
|
|
|
const next = jest.fn();
|
|
|
|
validateRouteAccess(to, from, next, {});
|
|
|
|
expect(next).toHaveBeenCalledWith('/app/login');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns next for an auth ignore route ', () => {
|
|
|
|
const to = { name: 'auth_confirmation' };
|
|
|
|
const from = { name: '' };
|
|
|
|
const next = jest.fn();
|
|
|
|
|
|
|
|
validateRouteAccess(to, from, next, {});
|
|
|
|
expect(next).toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns route validation for everything else ', () => {
|
|
|
|
const to = { name: 'login' };
|
|
|
|
const from = { name: '' };
|
|
|
|
const next = jest.fn();
|
|
|
|
|
|
|
|
validateRouteAccess(to, from, next, { getters: { isLoggedIn: false } });
|
|
|
|
expect(next).toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
});
|