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

249 lines
6.0 KiB
TypeScript
Raw Normal View History

2016-05-26 02:17:42 +08:00
import {expect} from "chai";
import {AuthenticationMWs} from "../../../../../backend/middlewares/user/AuthenticationMWs";
import {Error, ErrorCodes} from "../../../../../common/entities/Error";
2017-07-04 02:33:10 +08:00
import {UserDTO, UserRoles} from "../../../../../common/entities/UserDTO";
2016-05-26 02:17:42 +08:00
import {ObjectManagerRepository} from "../../../../../backend/model/ObjectManagerRepository";
import {UserManager} from "../../../../../backend/model/memory/UserManager";
2017-06-04 21:25:08 +08:00
import {Config} from "../../../../../common/config/private/Config";
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) => {
let req: any = {
session: {
user: "A user"
2017-07-04 02:33:10 +08:00
},
query: {},
params: {}
2017-07-04 01:17:49 +08:00
};
let next: any = (err) => {
expect(err).to.be.undefined;
done();
};
AuthenticationMWs.authenticate(req, null, next);
});
it('should call next with error on not authenticated', (done) => {
let req: any = {
2017-07-04 02:33:10 +08:00
session: {},
query: {},
params: {}
2017-07-04 01:17:49 +08:00
};
Config.Client.authenticationRequired = true;
let res: any = {};
let next: any = (err: Error) => {
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) => {
let req: any = {
session: {}
};
let res: any = {};
let next: any = (err) => {
expect(err).to.be.undefined;
done();
};
AuthenticationMWs.inverseAuthenticate(req, null, next);
});
it('should call next error on authenticated', (done) => {
let req: any = {
session: {
user: "A user"
}
};
let res: any = {};
let next: any = (err: Error) => {
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) => {
let req: any = {
session: {
user: {
role: UserRoles.Guest
}
}
};
let next: any = (err) => {
expect(err).to.be.undefined;
done();
};
AuthenticationMWs.authorise(UserRoles.Guest)(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 authorised', (done) => {
let req: any = {
session: {
user: {
role: UserRoles.Guest
}
}
};
let next: any = (err: Error) => {
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-04 02:33:10 +08:00
describe('should call input Error next on missing...', () => {
2017-07-04 01:17:49 +08:00
it('body', (done) => {
2017-07-04 02:33:10 +08:00
let req: any = {
query: {},
params: {}
};
let next: any = (err: Error) => {
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) => {
let req: any = {
2017-07-04 02:33:10 +08:00
body: {},
query: {},
params: {}
2017-07-04 01:17:49 +08:00
};
2017-07-04 02:33:10 +08:00
let next: any = (err: Error) => {
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) => {
let req: any = {
2017-07-04 02:33:10 +08:00
body: {loginCredential: {}},
query: {},
params: {}
2017-07-04 01:17:49 +08:00
};
2017-07-04 02:33:10 +08:00
let next: any = (err: Error) => {
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) => {
let req: any = {
body: {
loginCredential: {
username: "aa",
password: "bb"
}
2017-07-04 02:33:10 +08:00
},
query: {},
params: {}
2017-07-04 01:17:49 +08:00
};
let next: any = (err: Error) => {
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) => {
let req: any = {
session: {},
body: {
loginCredential: {
username: "aa",
password: "bb"
}
2017-07-04 02:33:10 +08:00
},
query: {},
params: {}
2017-07-04 01:17:49 +08:00
};
let next: any = (err: Error) => {
expect(err).to.be.undefined;
expect(req.session.user).to.be.eql("test user");
done();
};
ObjectManagerRepository.getInstance().UserManager = <UserManager>{
2017-07-04 02:33:10 +08:00
findOne: (filter) => {
return Promise.resolve("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) => {
let req: any = {
session: {
user: {
role: UserRoles.Guest
}
}
};
let next: any = (err) => {
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
});