salix/back/methods/vn-user/specs/logout.spec.js

43 lines
1.3 KiB
JavaScript
Raw Normal View History

2023-01-24 08:04:43 +00:00
const {models} = require('vn-loopback/server/server');
describe('account logout()', () => {
it('should logout and remove token after valid login', async() => {
2023-01-24 08:04:43 +00:00
let loginResponse = await models.VnUser.login('buyer', 'nightmare');
let accessToken = await models.AccessToken.findById(loginResponse.token);
let ctx = {req: {accessToken: accessToken}};
2023-01-24 08:04:43 +00:00
let logoutResponse = await models.VnUser.logout(ctx);
let tokenAfterLogout = await models.AccessToken.findById(loginResponse.token);
expect(logoutResponse).toBeTrue();
expect(tokenAfterLogout).toBeNull();
});
it('should throw a 401 error when token is invalid', async() => {
let error;
let ctx = {req: {accessToken: {id: 'invalidToken'}}};
try {
2023-01-24 08:04:43 +00:00
response = await models.VnUser.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 {
2023-01-24 08:04:43 +00:00
response = await models.VnUser.logout(ctx);
} catch (e) {
error = e;
}
expect(error).toBeDefined();
});
});