1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/test/backend/unit/middlewares/user/AuthenticationMWs.ts

316 lines
8.7 KiB
TypeScript
Raw Normal View History

/* tslint:disable:no-unused-expression */
2018-03-31 03:30:30 +08:00
import {expect} from 'chai';
2019-12-10 17:44:35 +08:00
import {AuthenticationMWs} from '../../../../../src/backend/middlewares/user/AuthenticationMWs';
import {ErrorCodes, ErrorDTO} from '../../../../../src/common/entities/Error';
import {UserDTO, UserRoles} from '../../../../../src/common/entities/UserDTO';
import {ObjectManagers} from '../../../../../src/backend/model/ObjectManagers';
import {UserManager} from '../../../../../src/backend/model/database/memory/UserManager';
2019-12-10 17:44:35 +08:00
import {Config} from '../../../../../src/common/config/private/Config';
import {IUserManager} from '../../../../../src/backend/model/database/interfaces/IUserManager';
2019-02-23 06:39:01 +08:00
import * as path from 'path';
2016-05-26 02:17:42 +08:00
2019-02-23 06:39:01 +08:00
declare const describe: any;
declare const it: any;
declare const beforeEach: any;
2016-05-26 02:17:42 +08:00
describe('Authentication middleware', () => {
2017-07-04 01:17:49 +08:00
beforeEach(() => {
ObjectManagers.reset();
2017-07-04 01:17:49 +08:00
});
describe('authenticate', () => {
2019-02-23 06:39:01 +08:00
it('should call next on authenticated', (done: () => void) => {
2018-11-29 06:49:33 +08:00
const req: any = {
2017-07-04 01:17:49 +08:00
session: {
2018-03-31 03:30:30 +08:00
user: 'A user'
2017-07-04 02:33:10 +08:00
},
2017-07-19 16:37:00 +08:00
sessionOptions: {},
2017-07-04 02:33:10 +08:00
query: {},
params: {}
2017-07-04 01:17:49 +08:00
};
2021-01-04 18:11:55 +08:00
const next: any = (err: ErrorDTO) => {
2017-07-04 01:17:49 +08:00
expect(err).to.be.undefined;
done();
};
AuthenticationMWs.authenticate(req, null, next);
});
2019-02-23 06:39:01 +08:00
it('should call next with error on not authenticated', (done: () => void) => {
2018-11-29 06:49:33 +08:00
const req: any = {
2017-07-04 02:33:10 +08:00
session: {},
2017-07-19 16:37:00 +08:00
sessionOptions: {},
2017-07-04 02:33:10 +08:00
query: {},
params: {}
2017-07-04 01:17:49 +08:00
};
Config.Client.authenticationRequired = true;
2021-01-04 18:11:55 +08:00
const next: any = (err: ErrorDTO) => {
2017-07-04 01:17:49 +08:00
expect(err).not.to.be.undefined;
expect(err.code).to.be.eql(ErrorCodes.NOT_AUTHENTICATED);
done();
};
AuthenticationMWs.authenticate(req, {
status: () => {
}
} as any, next);
2017-07-04 01:17:49 +08:00
});
});
2019-02-23 06:39:01 +08:00
describe('authorisePath', () => {
const req = {
session: {
user: {permissions: null as string[]}
},
2019-02-23 06:39:01 +08:00
sessionOptions: {},
query: {},
params: {
path: '/test'
}
};
const authoriseDirPath = AuthenticationMWs.authorisePath('path', true);
const test = (relativePath: string): Promise<string | number> => {
return new Promise((resolve) => {
req.params.path = path.normalize(relativePath);
authoriseDirPath(req as any, {sendStatus: resolve} as any, () => {
2019-02-23 06:39:01 +08:00
resolve('ok');
});
});
};
it('should catch unauthorized path usage', async () => {
req.session.user.permissions = [path.normalize('/sub/subsub')];
2019-02-23 06:39:01 +08:00
expect(await test('/sub/subsub')).to.be.eql('ok');
expect(await test('/test')).to.be.eql(403);
expect(await test('/')).to.be.eql(403);
expect(await test('/sub/test')).to.be.eql(403);
expect(await test('/sub/subsub/test')).to.be.eql(403);
expect(await test('/sub/subsub/test/test2')).to.be.eql(403);
req.session.user.permissions = [path.normalize('/sub/subsub'), path.normalize('/sub/subsub2')];
2019-02-23 06:39:01 +08:00
expect(await test('/sub/subsub2')).to.be.eql('ok');
expect(await test('/sub/subsub')).to.be.eql('ok');
expect(await test('/test')).to.be.eql(403);
expect(await test('/')).to.be.eql(403);
expect(await test('/sub/test')).to.be.eql(403);
expect(await test('/sub/subsub/test')).to.be.eql(403);
expect(await test('/sub/subsub2/test')).to.be.eql(403);
req.session.user.permissions = [path.normalize('/sub/subsub*')];
2019-02-23 06:39:01 +08:00
expect(await test('/b')).to.be.eql(403);
expect(await test('/sub')).to.be.eql(403);
expect(await test('/sub/subsub2')).to.be.eql(403);
expect(await test('/sub/subsub2/test')).to.be.eql(403);
expect(await test('/sub/subsub')).to.be.eql('ok');
expect(await test('/sub/subsub/test')).to.be.eql('ok');
expect(await test('/sub/subsub/test/two')).to.be.eql('ok');
});
});
2017-07-04 01:17:49 +08:00
describe('inverseAuthenticate', () => {
2019-02-23 06:39:01 +08:00
it('should call next with error on authenticated', (done: () => void) => {
2018-11-29 06:49:33 +08:00
const req: any = {
2017-07-19 16:37:00 +08:00
session: {},
sessionOptions: {},
2017-07-04 01:17:49 +08:00
};
2018-11-29 06:49:33 +08:00
const res: any = {};
2019-02-23 06:39:01 +08:00
const next: any = (err: ErrorDTO) => {
2017-07-04 01:17:49 +08:00
expect(err).to.be.undefined;
done();
};
AuthenticationMWs.inverseAuthenticate(req, null, next);
});
2019-02-23 06:39:01 +08:00
it('should call next error on authenticated', (done: () => void) => {
2018-11-29 06:49:33 +08:00
const req: any = {
2017-07-04 01:17:49 +08:00
session: {
2018-03-31 03:30:30 +08:00
user: 'A user'
2017-07-19 16:37:00 +08:00
},
sessionOptions: {},
2017-07-04 01:17:49 +08:00
};
2021-01-04 18:11:55 +08:00
const next: any = (err: ErrorDTO) => {
2017-07-04 01:17:49 +08:00
expect(err).not.to.be.undefined;
expect(err.code).to.be.eql(ErrorCodes.ALREADY_AUTHENTICATED);
done();
};
AuthenticationMWs.inverseAuthenticate(req, null, next);
});
});
describe('authorise', () => {
2019-02-23 06:39:01 +08:00
it('should call next on authorised', (done: () => void) => {
2018-11-29 06:49:33 +08:00
const req: any = {
2017-07-04 01:17:49 +08:00
session: {
user: {
2017-07-09 18:03:17 +08:00
role: UserRoles.LimitedGuest
2017-07-04 01:17:49 +08:00
}
2017-07-19 16:37:00 +08:00
},
sessionOptions: {}
2017-07-04 01:17:49 +08:00
};
2021-01-04 18:11:55 +08:00
const next: any = (err: ErrorDTO) => {
2017-07-04 01:17:49 +08:00
expect(err).to.be.undefined;
done();
};
2017-07-09 18:03:17 +08:00
AuthenticationMWs.authorise(UserRoles.LimitedGuest)(req, null, next);
2017-07-04 01:17:49 +08:00
2016-05-26 02:17:42 +08:00
});
2019-02-23 06:39:01 +08:00
it('should call next with error on not authorised', (done: () => void) => {
2018-11-29 06:49:33 +08:00
const req: any = {
2017-07-04 01:17:49 +08:00
session: {
user: {
2017-07-09 18:03:17 +08:00
role: UserRoles.LimitedGuest
2017-07-04 01:17:49 +08:00
}
2017-07-19 16:37:00 +08:00
},
sessionOptions: {}
2017-07-04 01:17:49 +08:00
};
2021-01-04 18:11:55 +08:00
const next: any = (err: ErrorDTO) => {
2017-07-04 01:17:49 +08:00
expect(err).not.to.be.undefined;
expect(err.code).to.be.eql(ErrorCodes.NOT_AUTHORISED);
done();
};
AuthenticationMWs.authorise(UserRoles.Developer)(req, null, next);
2016-05-26 02:17:42 +08:00
});
2017-07-04 01:17:49 +08:00
});
2016-05-26 02:17:42 +08:00
2017-07-04 01:17:49 +08:00
describe('login', () => {
beforeEach(() => {
ObjectManagers.reset();
2016-05-26 02:17:42 +08:00
});
2017-07-15 18:47:11 +08:00
describe('should call input ErrorDTO next on missing...', () => {
2019-02-23 06:39:01 +08:00
it('body', (done: () => void) => {
2018-11-29 06:49:33 +08:00
const req: any = {
2017-07-04 02:33:10 +08:00
query: {},
params: {}
};
2021-01-04 18:11:55 +08:00
const next: any = (err: ErrorDTO) => {
2017-07-04 02:33:10 +08:00
expect(err).not.to.be.undefined;
expect(err.code).to.be.eql(ErrorCodes.INPUT_ERROR);
2017-07-04 01:17:49 +08:00
done();
};
AuthenticationMWs.login(req, null, next);
});
2019-02-23 06:39:01 +08:00
it('loginCredential', (done: () => void) => {
2018-11-29 06:49:33 +08:00
const req: any = {
2017-07-04 02:33:10 +08:00
body: {},
query: {},
params: {}
2017-07-04 01:17:49 +08:00
};
2021-01-04 18:11:55 +08:00
const next: any = (err: ErrorDTO) => {
2017-07-04 02:33:10 +08:00
expect(err).not.to.be.undefined;
expect(err.code).to.be.eql(ErrorCodes.INPUT_ERROR);
2017-07-04 01:17:49 +08:00
done();
};
AuthenticationMWs.login(req, null, next);
});
2019-02-23 06:39:01 +08:00
it('loginCredential content', (done: () => void) => {
2018-11-29 06:49:33 +08:00
const req: any = {
2017-07-04 02:33:10 +08:00
body: {loginCredential: {}},
query: {},
params: {}
2017-07-04 01:17:49 +08:00
};
2021-01-04 18:11:55 +08:00
const next: any = (err: ErrorDTO) => {
2017-07-04 02:33:10 +08:00
expect(err).not.to.be.undefined;
expect(err.code).to.be.eql(ErrorCodes.INPUT_ERROR);
2017-07-04 01:17:49 +08:00
done();
};
AuthenticationMWs.login(req, null, next);
});
2016-05-26 02:17:42 +08:00
});
2019-02-23 06:39:01 +08:00
it('should call next with error on not finding user', (done: () => void) => {
2018-11-29 06:49:33 +08:00
const req: any = {
2017-07-04 01:17:49 +08:00
body: {
loginCredential: {
2018-03-31 03:30:30 +08:00
username: 'aa',
password: 'bb'
2017-07-04 01:17:49 +08:00
}
2017-07-04 02:33:10 +08:00
},
query: {},
params: {}
2017-07-04 01:17:49 +08:00
};
2021-01-04 18:11:55 +08:00
const next: any = (err: ErrorDTO) => {
2017-07-04 01:17:49 +08:00
expect(err).not.to.be.undefined;
expect(err.code).to.be.eql(ErrorCodes.CREDENTIAL_NOT_FOUND);
done();
};
ObjectManagers.getInstance().UserManager = {
2017-07-04 02:33:10 +08:00
findOne: (filter): Promise<UserDTO> => {
return Promise.reject(null);
2017-07-04 01:17:49 +08:00
}
} as UserManager;
2017-07-04 01:17:49 +08:00
AuthenticationMWs.login(req, null, next);
2016-05-26 02:17:42 +08:00
});
2019-02-23 06:39:01 +08:00
it('should call next with user on the session on finding user', (done: () => void) => {
2018-11-29 06:49:33 +08:00
const req: any = {
2017-07-04 01:17:49 +08:00
session: {},
body: {
loginCredential: {
2018-03-31 03:30:30 +08:00
username: 'aa',
password: 'bb'
2017-07-04 01:17:49 +08:00
}
2017-07-04 02:33:10 +08:00
},
query: {},
params: {}
2017-07-04 01:17:49 +08:00
};
2021-01-04 18:11:55 +08:00
const next: any = (err: ErrorDTO) => {
2017-07-04 01:17:49 +08:00
expect(err).to.be.undefined;
expect(req.session.user).to.be.eql('test user');
2017-07-04 01:17:49 +08:00
done();
};
ObjectManagers.getInstance().UserManager = {
2017-07-04 02:33:10 +08:00
findOne: (filter) => {
return Promise.resolve('test user' as any);
2017-07-04 01:17:49 +08:00
}
} as IUserManager;
2017-07-04 01:17:49 +08:00
AuthenticationMWs.login(req, null, next);
2016-05-26 02:17:42 +08:00
});
2017-07-04 01:17:49 +08:00
});
describe('logout', () => {
2019-02-23 06:39:01 +08:00
it('should call next on logout', (done: () => void) => {
2018-11-29 06:49:33 +08:00
const req: any = {
2017-07-04 01:17:49 +08:00
session: {
user: {
2017-07-09 18:03:17 +08:00
role: UserRoles.LimitedGuest
2017-07-04 01:17:49 +08:00
}
}
};
2019-02-23 06:39:01 +08:00
const next: any = (err: ErrorDTO) => {
2017-07-04 01:17:49 +08:00
expect(err).to.be.undefined;
2020-01-08 05:17:54 +08:00
expect(req.user).to.be.undefined;
2017-07-04 01:17:49 +08:00
done();
};
AuthenticationMWs.logout(req, null, next);
});
});
2016-05-26 02:17:42 +08:00
});