const {models} = require('vn-loopback/server/server'); describe('VnUser validate-auth()', () => { 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'); expect(token.token).toBeDefined(); }); }); 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; } expect(error).toBeDefined(); expect(error.statusCode).toBe(400); expect(error.message).toEqual('Invalid or expired verification code'); }); 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) }); await models.VnUser.validateCode('developer', '555555'); } catch (e) { authCode && await authCode.destroy(); error = e; } expect(error).toBeDefined(); expect(error.statusCode).toBe(400); expect(error.message).toEqual('Authentication failed'); }); }); });