2018-03-30 21:30:30 +02: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-25 20:17:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
describe('Authentication middleware', () => {
|
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
ObjectManagerRepository.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('authenticate', () => {
|
|
|
|
it('should call next on authenticated', (done) => {
|
2018-11-29 00:49:33 +02:00
|
|
|
const req: any = {
|
2017-07-03 19:17:49 +02:00
|
|
|
session: {
|
2018-03-30 21:30:30 +02:00
|
|
|
user: 'A user'
|
2017-07-03 20:33:10 +02:00
|
|
|
},
|
2017-07-19 10:37:00 +02:00
|
|
|
sessionOptions: {},
|
2017-07-03 20:33:10 +02:00
|
|
|
query: {},
|
|
|
|
params: {}
|
2017-07-03 19:17:49 +02:00
|
|
|
};
|
2018-11-29 00:49:33 +02:00
|
|
|
const next: any = (err: ErrorDTO) => {
|
2017-07-03 19:17:49 +02: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 00:49:33 +02:00
|
|
|
const req: any = {
|
2017-07-03 20:33:10 +02:00
|
|
|
session: {},
|
2017-07-19 10:37:00 +02:00
|
|
|
sessionOptions: {},
|
2017-07-03 20:33:10 +02:00
|
|
|
query: {},
|
|
|
|
params: {}
|
2017-07-03 19:17:49 +02:00
|
|
|
};
|
|
|
|
Config.Client.authenticationRequired = true;
|
2018-11-29 00:49:33 +02:00
|
|
|
const res: any = {};
|
|
|
|
const next: any = (err: ErrorDTO) => {
|
2017-07-03 19:17:49 +02: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 00:49:33 +02:00
|
|
|
const req: any = {
|
2017-07-19 10:37:00 +02:00
|
|
|
session: {},
|
|
|
|
sessionOptions: {},
|
2017-07-03 19:17:49 +02:00
|
|
|
};
|
2018-11-29 00:49:33 +02:00
|
|
|
const res: any = {};
|
|
|
|
const next: any = (err:ErrorDTO) => {
|
2017-07-03 19:17:49 +02:00
|
|
|
expect(err).to.be.undefined;
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
AuthenticationMWs.inverseAuthenticate(req, null, next);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should call next error on authenticated', (done) => {
|
2018-11-29 00:49:33 +02:00
|
|
|
const req: any = {
|
2017-07-03 19:17:49 +02:00
|
|
|
session: {
|
2018-03-30 21:30:30 +02:00
|
|
|
user: 'A user'
|
2017-07-19 10:37:00 +02:00
|
|
|
},
|
|
|
|
sessionOptions: {},
|
2017-07-03 19:17:49 +02:00
|
|
|
};
|
2018-11-29 00:49:33 +02:00
|
|
|
const res: any = {};
|
|
|
|
const next: any = (err: ErrorDTO) => {
|
2017-07-03 19:17:49 +02: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 00:49:33 +02:00
|
|
|
const req: any = {
|
2017-07-03 19:17:49 +02:00
|
|
|
session: {
|
|
|
|
user: {
|
2017-07-09 12:03:17 +02:00
|
|
|
role: UserRoles.LimitedGuest
|
2017-07-03 19:17:49 +02:00
|
|
|
}
|
2017-07-19 10:37:00 +02:00
|
|
|
},
|
|
|
|
sessionOptions: {}
|
2017-07-03 19:17:49 +02:00
|
|
|
};
|
2018-11-29 00:49:33 +02:00
|
|
|
const next: any = (err:ErrorDTO) => {
|
2017-07-03 19:17:49 +02:00
|
|
|
expect(err).to.be.undefined;
|
|
|
|
done();
|
|
|
|
};
|
2017-07-09 12:03:17 +02:00
|
|
|
AuthenticationMWs.authorise(UserRoles.LimitedGuest)(req, null, next);
|
2017-07-03 19:17:49 +02:00
|
|
|
|
2016-05-25 20:17:42 +02:00
|
|
|
});
|
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
it('should call next with error on not authorised', (done) => {
|
2018-11-29 00:49:33 +02:00
|
|
|
const req: any = {
|
2017-07-03 19:17:49 +02:00
|
|
|
session: {
|
|
|
|
user: {
|
2017-07-09 12:03:17 +02:00
|
|
|
role: UserRoles.LimitedGuest
|
2017-07-03 19:17:49 +02:00
|
|
|
}
|
2017-07-19 10:37:00 +02:00
|
|
|
},
|
|
|
|
sessionOptions: {}
|
2017-07-03 19:17:49 +02:00
|
|
|
};
|
2018-11-29 00:49:33 +02:00
|
|
|
const next: any = (err: ErrorDTO) => {
|
2017-07-03 19:17:49 +02: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-25 20:17:42 +02:00
|
|
|
});
|
2017-07-03 19:17:49 +02:00
|
|
|
});
|
2016-05-25 20:17:42 +02:00
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
describe('login', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
ObjectManagerRepository.reset();
|
2016-05-25 20:17:42 +02:00
|
|
|
});
|
|
|
|
|
2017-07-15 12:47:11 +02:00
|
|
|
describe('should call input ErrorDTO next on missing...', () => {
|
2017-07-03 19:17:49 +02:00
|
|
|
it('body', (done) => {
|
2018-11-29 00:49:33 +02:00
|
|
|
const req: any = {
|
2017-07-03 20:33:10 +02:00
|
|
|
query: {},
|
|
|
|
params: {}
|
|
|
|
};
|
2018-11-29 00:49:33 +02:00
|
|
|
const next: any = (err: ErrorDTO) => {
|
2017-07-03 20:33:10 +02:00
|
|
|
expect(err).not.to.be.undefined;
|
|
|
|
expect(err.code).to.be.eql(ErrorCodes.INPUT_ERROR);
|
2017-07-03 19:17:49 +02:00
|
|
|
done();
|
|
|
|
};
|
|
|
|
AuthenticationMWs.login(req, null, next);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('loginCredential', (done) => {
|
2018-11-29 00:49:33 +02:00
|
|
|
const req: any = {
|
2017-07-03 20:33:10 +02:00
|
|
|
body: {},
|
|
|
|
query: {},
|
|
|
|
params: {}
|
2017-07-03 19:17:49 +02:00
|
|
|
};
|
2018-11-29 00:49:33 +02:00
|
|
|
const next: any = (err: ErrorDTO) => {
|
2017-07-03 20:33:10 +02:00
|
|
|
expect(err).not.to.be.undefined;
|
|
|
|
expect(err.code).to.be.eql(ErrorCodes.INPUT_ERROR);
|
2017-07-03 19:17:49 +02:00
|
|
|
done();
|
|
|
|
};
|
|
|
|
AuthenticationMWs.login(req, null, next);
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('loginCredential content', (done) => {
|
2018-11-29 00:49:33 +02:00
|
|
|
const req: any = {
|
2017-07-03 20:33:10 +02:00
|
|
|
body: {loginCredential: {}},
|
|
|
|
query: {},
|
|
|
|
params: {}
|
2017-07-03 19:17:49 +02:00
|
|
|
};
|
2018-11-29 00:49:33 +02:00
|
|
|
const next: any = (err: ErrorDTO) => {
|
2017-07-03 20:33:10 +02:00
|
|
|
expect(err).not.to.be.undefined;
|
|
|
|
expect(err.code).to.be.eql(ErrorCodes.INPUT_ERROR);
|
2017-07-03 19:17:49 +02:00
|
|
|
done();
|
|
|
|
};
|
|
|
|
AuthenticationMWs.login(req, null, next);
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-05-25 20:17:42 +02:00
|
|
|
});
|
2017-07-03 19:17:49 +02:00
|
|
|
it('should call next with error on not finding user', (done) => {
|
2018-11-29 00:49:33 +02:00
|
|
|
const req: any = {
|
2017-07-03 19:17:49 +02:00
|
|
|
body: {
|
|
|
|
loginCredential: {
|
2018-03-30 21:30:30 +02:00
|
|
|
username: 'aa',
|
|
|
|
password: 'bb'
|
2017-07-03 19:17:49 +02:00
|
|
|
}
|
2017-07-03 20:33:10 +02:00
|
|
|
},
|
|
|
|
query: {},
|
|
|
|
params: {}
|
2017-07-03 19:17:49 +02:00
|
|
|
};
|
2018-11-29 00:49:33 +02:00
|
|
|
const next: any = (err: ErrorDTO) => {
|
2017-07-03 19:17:49 +02:00
|
|
|
expect(err).not.to.be.undefined;
|
|
|
|
expect(err.code).to.be.eql(ErrorCodes.CREDENTIAL_NOT_FOUND);
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
ObjectManagerRepository.getInstance().UserManager = <UserManager>{
|
2017-07-03 20:33:10 +02:00
|
|
|
findOne: (filter): Promise<UserDTO> => {
|
|
|
|
return Promise.reject(null);
|
2017-07-03 19:17:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
AuthenticationMWs.login(req, null, next);
|
|
|
|
|
2016-05-25 20:17:42 +02:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
it('should call next with user on the session on finding user', (done) => {
|
2018-11-29 00:49:33 +02:00
|
|
|
const req: any = {
|
2017-07-03 19:17:49 +02:00
|
|
|
session: {},
|
|
|
|
body: {
|
|
|
|
loginCredential: {
|
2018-03-30 21:30:30 +02:00
|
|
|
username: 'aa',
|
|
|
|
password: 'bb'
|
2017-07-03 19:17:49 +02:00
|
|
|
}
|
2017-07-03 20:33:10 +02:00
|
|
|
},
|
|
|
|
query: {},
|
|
|
|
params: {}
|
2017-07-03 19:17:49 +02:00
|
|
|
};
|
2018-11-29 00:49:33 +02:00
|
|
|
const next: any = (err: ErrorDTO) => {
|
2017-07-03 19:17:49 +02:00
|
|
|
expect(err).to.be.undefined;
|
2018-03-30 21:30:30 +02:00
|
|
|
expect(req.session.user).to.be.eql('test user');
|
2017-07-03 19:17:49 +02:00
|
|
|
done();
|
|
|
|
};
|
2017-07-26 19:43:06 +02:00
|
|
|
ObjectManagerRepository.getInstance().UserManager = <IUserManager>{
|
2017-07-03 20:33:10 +02:00
|
|
|
findOne: (filter) => {
|
2018-03-30 21:30:30 +02:00
|
|
|
return Promise.resolve(<any>'test user');
|
2017-07-03 19:17:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
AuthenticationMWs.login(req, null, next);
|
2016-05-25 20:17:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
});
|
2017-07-03 19:17:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('logout', () => {
|
|
|
|
it('should call next on logout', (done) => {
|
2018-11-29 00:49:33 +02:00
|
|
|
const req: any = {
|
2017-07-03 19:17:49 +02:00
|
|
|
session: {
|
|
|
|
user: {
|
2017-07-09 12:03:17 +02:00
|
|
|
role: UserRoles.LimitedGuest
|
2017-07-03 19:17:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-11-29 00:49:33 +02:00
|
|
|
const next: any = (err:ErrorDTO) => {
|
2017-07-03 19:17:49 +02:00
|
|
|
expect(err).to.be.undefined;
|
|
|
|
expect(req.session.user).to.be.undefined;
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
AuthenticationMWs.logout(req, null, next);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2016-05-25 20:17:42 +02:00
|
|
|
|
|
|
|
});
|