fix: Allow users to login even if they have access to more than 15 accounts (#4475)

This commit is contained in:
Pranav Raj S 2022-04-14 20:54:26 +05:30 committed by GitHub
parent 80e5d6d7a0
commit 0319b78eac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 368 additions and 350 deletions

View file

@ -1,6 +1,8 @@
import 'expect-more-jest';
import { validateAuthenticateRoutePermission } from './index';
import auth from '../api/auth';
import {
validateAuthenticateRoutePermission,
validateRouteAccess,
} from './index';
jest.mock('./dashboard/dashboard.routes', () => ({
routes: [],
@ -11,94 +13,119 @@ jest.mock('./auth/auth.routes', () => ({
jest.mock('./login/login.routes', () => ({
routes: [],
}));
jest.mock('../constants', () => {
return {
GRAVATAR_URL: 'https://www.gravatar.com/avatar',
CHANNELS: {
FACEBOOK: 'facebook',
},
ASSIGNEE_TYPE: {
ME: 'me',
UNASSIGNED: 'unassigned',
ALL: 'all',
},
STATUS_TYPE: {
OPEN: 'open',
RESOLVED: 'resolved',
},
};
});
window.roleWiseRoutes = {};
describe(`behavior`, () => {
describe('#validateAuthenticateRoutePermission', () => {
describe(`when route is not protected`, () => {
it(`should go to the dashboard when user is logged in`, () => {
// Arrange
spyOn(auth, 'isLoggedIn').and.returnValue(true);
spyOn(auth, 'getCurrentUser').and.returnValue({
account_id: 1,
accounts: [{ id: 1, role: 'agent' }],
});
const to = { name: 'login' };
const to = { name: 'login', params: { accountId: 1 } };
const from = { name: '', params: { accountId: 1 } };
const next = jest.fn();
// Act
validateAuthenticateRoutePermission(to, from, next);
// Assert
const getters = {
isLoggedIn: true,
getCurrentUser: {
account_id: 1,
id: 1,
accounts: [{ id: 1, role: 'admin' }],
},
};
validateAuthenticateRoutePermission(to, from, next, { getters });
expect(next).toHaveBeenCalledWith('/app/accounts/1/dashboard');
});
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();
});
});
describe(`when route is protected`, () => {
describe(`when user not logged in`, () => {
it(`should redirect to login`, () => {
// Arrange
spyOn(auth, 'isLoggedIn').and.returnValue(false);
spyOn(auth, 'getCurrentUser').and.returnValue(null);
const to = { name: 'some-protected-route', params: { accountId: 1 } };
const from = { name: '' };
const next = jest.fn();
// Act
validateAuthenticateRoutePermission(to, from, next);
// Assert
const getters = {
isLoggedIn: false,
getCurrentUser: {
account_id: null,
id: null,
accounts: [],
},
};
validateAuthenticateRoutePermission(to, from, next, { getters });
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`, () => {
// Arrange
spyOn(auth, 'isLoggedIn').and.returnValue(true);
spyOn(auth, 'getCurrentUser').and.returnValue({
accounts: [{ id: 1, role: 'agent' }],
});
window.roleWiseRoutes.agent = ['dashboard'];
const to = { name: 'admin', params: { accountId: 1 } };
const from = { name: '' };
const next = jest.fn();
// Act
validateAuthenticateRoutePermission(to, from, next);
// Assert
const getters = {
isLoggedIn: true,
getCurrentUser: {
account_id: 1,
id: 1,
accounts: [{ id: 1, role: 'agent' }],
},
};
validateAuthenticateRoutePermission(to, from, next, { getters });
expect(next).toHaveBeenCalledWith('/app/accounts/1/dashboard');
});
});
describe(`when route is accessible to current user`, () => {
it(`should go there`, () => {
// Arrange
spyOn(auth, 'isLoggedIn').and.returnValue(true);
spyOn(auth, 'getCurrentUser').and.returnValue({
accounts: [{ id: 1, role: 'agent' }],
});
window.roleWiseRoutes.agent = ['dashboard', 'admin'];
const to = { name: 'admin', params: { accountId: 1 } };
const from = { name: '' };
const next = jest.fn();
// Act
validateAuthenticateRoutePermission(to, from, next);
// Assert
const getters = {
isLoggedIn: true,
getCurrentUser: {
account_id: 1,
id: 1,
accounts: [{ id: 1, role: 'agent' }],
},
};
validateAuthenticateRoutePermission(to, from, next, { getters });
expect(next).toHaveBeenCalledWith();
});
});
});
});
});
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();
});
});