salix/back/methods/vn-user/specs/validate-auth.spec.js

53 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2023-04-13 09:54:56 +00:00
const {models} = require('vn-loopback/server/server');
2023-06-27 13:30:54 +00:00
describe('VnUser validate-auth()', () => {
2023-06-21 12:17:25 +00:00
describe('validateAuth', () => {
it('should signin if data is correct', async() => {
await models.AuthCode.create({
userFk: 9,
code: '555555',
expires: Date.vnNow() + (60 * 1000)
});
const token = await models.VnUser.validateAuth('developer', 'nightmare', '555555');
2023-04-13 09:54:56 +00:00
2023-06-21 12:17:25 +00:00
expect(token.token).toBeDefined();
});
});
2023-04-13 09:54:56 +00:00
2023-06-21 12:17:25 +00:00
describe('validateCode', () => {
it('should throw an error for a non existent code', async() => {
let error;
try {
await models.VnUser.validateCode('developer', '123456');
} catch (e) {
error = e;
}
2023-04-13 09:54:56 +00:00
2023-06-21 12:17:25 +00:00
expect(error).toBeDefined();
expect(error.statusCode).toBe(400);
expect(error.message).toEqual('Invalid or expired verification code');
});
2023-04-13 09:54:56 +00:00
2023-06-21 12:17:25 +00:00
it('should throw an error when a code doesn`t match the login username', async() => {
let error;
let authCode;
try {
authCode = await models.AuthCode.create({
userFk: 1,
code: '555555',
expires: Date.vnNow() + (60 * 1000)
});
2023-04-13 09:54:56 +00:00
2023-06-21 12:17:25 +00:00
await models.VnUser.validateCode('developer', '555555');
} catch (e) {
authCode && await authCode.destroy();
error = e;
}
2023-04-13 09:54:56 +00:00
2023-06-21 12:17:25 +00:00
expect(error).toBeDefined();
expect(error.statusCode).toBe(400);
expect(error.message).toEqual('Authentication failed');
});
2023-04-13 09:54:56 +00:00
});
});