2019-01-24 11:01:35 +00:00
|
|
|
const app = require('vn-loopback/server/server');
|
2019-01-23 12:11:44 +00:00
|
|
|
|
|
|
|
describe('account logout()', () => {
|
|
|
|
it('should logout and remove token after valid login', async() => {
|
2020-10-16 09:16:24 +00:00
|
|
|
let loginResponse = await app.models.Account.login('buyer', 'nightmare');
|
2019-01-23 12:11:44 +00:00
|
|
|
let accessToken = await app.models.AccessToken.findById(loginResponse.token);
|
|
|
|
let ctx = {req: {accessToken: accessToken}};
|
|
|
|
|
2020-10-16 09:16:24 +00:00
|
|
|
let logoutResponse = await app.models.Account.logout(ctx);
|
|
|
|
let tokenAfterLogout = await app.models.AccessToken.findById(loginResponse.token);
|
2019-01-23 12:11:44 +00:00
|
|
|
|
2020-10-16 09:16:24 +00:00
|
|
|
expect(logoutResponse).toBeTrue();
|
|
|
|
expect(tokenAfterLogout).toBeNull();
|
2019-01-23 12:11:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw a 401 error when token is invalid', async() => {
|
|
|
|
let error;
|
|
|
|
let ctx = {req: {accessToken: {id: 'invalidToken'}}};
|
|
|
|
|
|
|
|
try {
|
|
|
|
response = await app.models.Account.logout(ctx);
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error).toBeDefined();
|
|
|
|
expect(error.statusCode).toBe(401);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error when no token is passed', async() => {
|
|
|
|
let error;
|
|
|
|
let ctx = {req: {accessToken: null}};
|
|
|
|
|
|
|
|
try {
|
|
|
|
response = await app.models.Account.logout(ctx);
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error).toBeDefined();
|
|
|
|
});
|
|
|
|
});
|