test(user): write tests for authenticate
This commit is contained in:
@@ -12,6 +12,7 @@ import Step from './step.js';
|
|||||||
import Subscription from './subscription.ee.js';
|
import Subscription from './subscription.ee.js';
|
||||||
import UsageData from './usage-data.ee.js';
|
import UsageData from './usage-data.ee.js';
|
||||||
import User from './user.js';
|
import User from './user.js';
|
||||||
|
import { createUser } from '../../test/factories/user.js';
|
||||||
|
|
||||||
describe('User model', () => {
|
describe('User model', () => {
|
||||||
it('tableName should return correct name', () => {
|
it('tableName should return correct name', () => {
|
||||||
@@ -199,4 +200,49 @@ describe('User model', () => {
|
|||||||
'https://automatisch.io/accept-invitation?token=invitation-token'
|
'https://automatisch.io/accept-invitation?token=invitation-token'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('authenticate', () => {
|
||||||
|
it('should create and return the token for correct email and password', async () => {
|
||||||
|
const user = await createUser({
|
||||||
|
email: 'test-user@automatisch.io',
|
||||||
|
password: 'sample-password',
|
||||||
|
});
|
||||||
|
|
||||||
|
const token = await User.authenticate(
|
||||||
|
'test-user@automatisch.io',
|
||||||
|
'sample-password'
|
||||||
|
);
|
||||||
|
|
||||||
|
const persistedToken = await AccessToken.query().findOne({
|
||||||
|
userId: user.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(token).toBe(persistedToken.token);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return undefined for existing email and incorrect password', async () => {
|
||||||
|
await createUser({
|
||||||
|
email: 'test-user@automatisch.io',
|
||||||
|
password: 'sample-password',
|
||||||
|
});
|
||||||
|
|
||||||
|
const token = await User.authenticate(
|
||||||
|
'test-user@automatisch.io',
|
||||||
|
'wrong-password'
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(token).toBe(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return undefined for non-existing email', async () => {
|
||||||
|
await createUser({
|
||||||
|
email: 'test-user@automatisch.io',
|
||||||
|
password: 'sample-password',
|
||||||
|
});
|
||||||
|
|
||||||
|
const token = await User.authenticate('non-existing-user@automatisch.io');
|
||||||
|
|
||||||
|
expect(token).toBe(undefined);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user