65 lines
2.9 KiB
JavaScript
65 lines
2.9 KiB
JavaScript
const { resolveContext } = require('./index');
|
|
let passed = 0, failed = 0;
|
|
|
|
function t(name, session, expected) {
|
|
const res = { _redirect: undefined, redirect(u) { this._redirect = u; } };
|
|
const req = { session: session || {} };
|
|
resolveContext(req, res, () => {});
|
|
|
|
if (res._redirect !== (expected._redirect || undefined)) {
|
|
console.log('❌', name, 'redirect:', res._redirect, '!=', expected._redirect);
|
|
failed++; return;
|
|
}
|
|
|
|
for (const k of Object.keys(expected).filter(k => k !== '_redirect')) {
|
|
if (JSON.stringify(req[k]) !== JSON.stringify(expected[k])) {
|
|
console.log('❌', name, k, ':', JSON.stringify(req[k]), '!=', JSON.stringify(expected[k]));
|
|
failed++; return;
|
|
}
|
|
}
|
|
console.log('✅', name); passed++;
|
|
}
|
|
|
|
t('нет сессии', {}, { _redirect: '/v2/login' });
|
|
t('нет user', { user: null }, { _redirect: '/v2/login' });
|
|
|
|
t('обычный юзер 1 компания', { user: {
|
|
email: 'u@t.ru', clientId: 'WZ03709', activeClientId: 'WZ03709',
|
|
allClientIds: ['WZ03709'], companyName: 'test', isAdmin: false, profiles: []
|
|
}}, { 'email': 'u@t.ru', 'clientId': 'WZ03709', 'isAdmin': false, 'isImpersonated': false, 'impersonatedBy': null });
|
|
|
|
t('юзер 2 компании', { user: {
|
|
email: 'm@t.ru', clientId: 'WZ88888', activeClientId: 'WZ77777',
|
|
allClientIds: ['WZ88888','WZ77777'], companyName: 'AB', isAdmin: false, profiles: []
|
|
}}, { 'email': 'm@t.ru', 'clientId': 'WZ77777', 'allClientIds': ['WZ88888','WZ77777'] });
|
|
|
|
t('админ adminMode', { user: {
|
|
email: 'a@t.ru', clientId: 'WZ01112', activeClientId: 'WZ01112',
|
|
allClientIds: ['WZ01112'], isAdmin: true, profiles: []
|
|
}, adminMode: true }, { 'email': 'a@t.ru', 'clientId': 'WZ01112', 'isAdmin': true });
|
|
|
|
t('админ без adminMode', { user: {
|
|
email: 'a@t.ru', clientId: 'WZ01112', activeClientId: 'WZ01112',
|
|
allClientIds: ['WZ01112'], isAdmin: true, profiles: []
|
|
} }, { 'email': 'a@t.ru', 'clientId': 'WZ01112', 'isAdmin': false });
|
|
|
|
t('имперсонация', { user: {
|
|
email: 'admin@t.ru', clientId: 'WZ01112', activeClientId: 'WZ01112',
|
|
allClientIds: ['WZ01112','WZ03709'], isAdmin: true, isImpersonated: true,
|
|
originalUserEmail: 'real@t.ru', impersonatedCompanyId: 'WZ03709', profiles: []
|
|
}, adminMode: true }, { 'email': 'real@t.ru', 'clientId': 'WZ03709', 'isImpersonated': true, 'impersonatedBy': 'admin@t.ru' });
|
|
|
|
t('имперс. без origEmail', { user: {
|
|
email: 'x@t.ru', clientId: 'WZ01112', activeClientId: 'WZ01112',
|
|
allClientIds: ['WZ01112'], isAdmin: true, isImpersonated: true,
|
|
originalUserEmail: '', impersonatedCompanyId: '', profiles: []
|
|
} }, { 'email': 'x@t.ru', 'clientId': 'WZ01112', 'impersonatedBy': 'x@t.ru' });
|
|
|
|
t('без activeClientId', { user: {
|
|
email: 'y@t.ru', clientId: 'WZ03709', allClientIds: ['WZ03709'],
|
|
isAdmin: false, profiles: []
|
|
} }, { 'email': 'y@t.ru', 'clientId': 'WZ03709' });
|
|
|
|
console.log(`\n${passed} passed, ${failed} failed`);
|
|
if (failed) process.exit(1);
|