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

256 lines
6.4 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import {expect} from 'chai';
import {AuthenticationMWs} from '../../../../../backend/middlewares/user/AuthenticationMWs';
import {ErrorCodes, ErrorDTO} from '../../../../../common/entities/Error';
import {UserDTO, UserRoles} from '../../../../../common/entities/UserDTO';
import {ObjectManagerRepository} from '../../../../../backend/model/ObjectManagerRepository';
import {UserManager} from '../../../../../backend/model/memory/UserManager';
import {Config} from '../../../../../common/config/private/Config';
import {IUserManager} from '../../../../../backend/model/interfaces/IUserManager';
2016-05-26 02:17:42 +08:00
describe('Authentication middleware', () => {
2017-07-04 01:17:49 +08:00
beforeEach(() => {
ObjectManagerRepository.reset();
});
describe('authenticate', () => {
it('should call next on authenticated', (done) => {
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
};
2018-11-29 06:49:33 +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);
});
it('should call next with error on not authenticated', (done) => {
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;
2018-11-29 06:49:33 +08:00
const res: any = {};
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, null, next);
});
});
describe('inverseAuthenticate', () => {
it('should call next with error on authenticated', (done) => {
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 = {};
const next: any = (err:ErrorDTO) => {
2017-07-04 01:17:49 +08:00
expect(err).to.be.undefined;
done();
};
AuthenticationMWs.inverseAuthenticate(req, null, next);
});
it('should call next error on authenticated', (done) => {
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
};
2018-11-29 06:49:33 +08:00
const res: any = {};
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', () => {
it('should call next on authorised', (done) => {
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
};
2018-11-29 06:49:33 +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
});
2017-07-04 01:17:49 +08:00
it('should call next with error on not authorised', (done) => {
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
};
2018-11-29 06:49:33 +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(() => {
ObjectManagerRepository.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...', () => {
2017-07-04 01:17:49 +08:00
it('body', (done) => {
2018-11-29 06:49:33 +08:00
const req: any = {
2017-07-04 02:33:10 +08:00
query: {},
params: {}
};
2018-11-29 06:49:33 +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);
});
it('loginCredential', (done) => {
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
};
2018-11-29 06:49:33 +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);
});
it('loginCredential content', (done) => {
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
};
2018-11-29 06:49:33 +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
});
2017-07-04 01:17:49 +08:00
it('should call next with error on not finding user', (done) => {
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
};
2018-11-29 06:49:33 +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();
};
ObjectManagerRepository.getInstance().UserManager = <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
}
};
AuthenticationMWs.login(req, null, next);
2016-05-26 02:17:42 +08:00
});
2017-07-04 01:17:49 +08:00
it('should call next with user on the session on finding user', (done) => {
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
};
2018-11-29 06:49:33 +08:00
const next: any = (err: ErrorDTO) => {
2017-07-04 01:17:49 +08:00
expect(err).to.be.undefined;
2018-03-31 03:30:30 +08:00
expect(req.session.user).to.be.eql('test user');
2017-07-04 01:17:49 +08:00
done();
};
2017-07-27 01:43:06 +08:00
ObjectManagerRepository.getInstance().UserManager = <IUserManager>{
2017-07-04 02:33:10 +08:00
findOne: (filter) => {
2018-03-31 03:30:30 +08:00
return Promise.resolve(<any>'test user');
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', () => {
it('should call next on logout', (done) => {
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
}
}
};
2018-11-29 06:49:33 +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.undefined;
done();
};
AuthenticationMWs.logout(req, null, next);
});
});
2016-05-26 02:17:42 +08:00
});