2020-05-12 23:05:26 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const NotFoundError = require('../../../lib/error/notfound-error');
|
|
|
|
const dbInit = require('../helpers/database-init');
|
|
|
|
const getLogger = require('../../fixtures/no-logger');
|
|
|
|
|
|
|
|
let stores;
|
2021-03-19 22:25:21 +01:00
|
|
|
let db;
|
2020-05-12 23:05:26 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
beforeAll(async () => {
|
2021-03-19 22:25:21 +01:00
|
|
|
db = await dbInit('user_store_serial', getLogger);
|
2020-05-12 23:05:26 +02:00
|
|
|
stores = db.stores;
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
afterAll(async () => {
|
2021-03-19 22:25:21 +01:00
|
|
|
await db.destroy();
|
2020-05-12 23:05:26 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should have no users', async () => {
|
2020-05-12 23:05:26 +02:00
|
|
|
const users = await stores.userStore.getAll();
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(users).toEqual([]);
|
2020-05-12 23:05:26 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should insert new user with email', async () => {
|
2021-04-22 23:40:52 +02:00
|
|
|
const user = { email: 'me2@mail.com' };
|
2020-05-12 23:05:26 +02:00
|
|
|
await stores.userStore.upsert(user);
|
|
|
|
const users = await stores.userStore.getAll();
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(users[0].email).toEqual(user.email);
|
|
|
|
expect(users[0].id).toBeTruthy();
|
2020-05-12 23:05:26 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should not allow two users with same email', async () => {
|
|
|
|
await expect(async () => {
|
|
|
|
await stores.userStore.insert({ email: 'me2@mail.com' });
|
|
|
|
await stores.userStore.insert({ email: 'me2@mail.com' });
|
|
|
|
}).rejects.toThrow(/duplicate key value violates unique constraint/);
|
2020-05-12 23:05:26 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should insert new user with email and return it', async () => {
|
2021-04-22 23:40:52 +02:00
|
|
|
const user = { email: 'me2@mail.com' };
|
2020-05-12 23:05:26 +02:00
|
|
|
const newUser = await stores.userStore.upsert(user);
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(newUser.email).toEqual(user.email);
|
|
|
|
expect(newUser.id).toBeTruthy();
|
2020-05-12 23:05:26 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should insert new user with username', async () => {
|
2021-04-22 23:40:52 +02:00
|
|
|
const user = { username: 'admin' };
|
2020-05-12 23:05:26 +02:00
|
|
|
await stores.userStore.upsert(user);
|
|
|
|
const dbUser = await stores.userStore.get(user);
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(dbUser.username).toEqual(user.username);
|
2020-05-12 23:05:26 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('Should require email or username', async () => {
|
|
|
|
await expect(async () => {
|
|
|
|
await stores.userStore.upsert({});
|
|
|
|
}).rejects.toThrow(
|
|
|
|
new Error('Can only find users with id, username or email.'),
|
2020-05-12 23:05:26 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should set password_hash for user', async () => {
|
2020-05-12 23:05:26 +02:00
|
|
|
const store = stores.userStore;
|
2021-04-22 23:40:52 +02:00
|
|
|
const user = await store.insert({ email: 'admin@mail.com' });
|
2020-05-12 23:05:26 +02:00
|
|
|
await store.setPasswordHash(user.id, 'rubbish');
|
|
|
|
const hash = await store.getPasswordHash(user.id);
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(hash).toBe('rubbish');
|
2020-05-12 23:05:26 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should not get password_hash for unknown userId', async () => {
|
2020-05-12 23:05:26 +02:00
|
|
|
const store = stores.userStore;
|
2021-05-28 11:10:24 +02:00
|
|
|
await expect(async () => store.getPasswordHash(-12)).rejects.toThrow(
|
|
|
|
new NotFoundError('User not found'),
|
2020-05-12 23:05:26 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should update loginAttempts for user', async () => {
|
2020-05-12 23:05:26 +02:00
|
|
|
const store = stores.userStore;
|
2021-04-22 23:40:52 +02:00
|
|
|
const user = { email: 'admin@mail.com' };
|
2020-05-12 23:05:26 +02:00
|
|
|
await store.upsert(user);
|
|
|
|
await store.incLoginAttempts(user);
|
|
|
|
await store.incLoginAttempts(user);
|
|
|
|
const storedUser = await store.get(user);
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(storedUser.loginAttempts).toBe(2);
|
2020-05-12 23:05:26 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should not increment for user unknown user', async () => {
|
2020-05-12 23:05:26 +02:00
|
|
|
const store = stores.userStore;
|
2021-04-22 23:40:52 +02:00
|
|
|
const user = { email: 'another@mail.com' };
|
2020-05-12 23:05:26 +02:00
|
|
|
await store.upsert(user);
|
2021-04-22 23:40:52 +02:00
|
|
|
await store.incLoginAttempts({ email: 'unknown@mail.com' });
|
2020-05-12 23:05:26 +02:00
|
|
|
const storedUser = await store.get(user);
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(storedUser.loginAttempts).toBe(0);
|
2020-05-12 23:05:26 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should reset user after successful login', async () => {
|
2020-05-12 23:05:26 +02:00
|
|
|
const store = stores.userStore;
|
2021-04-22 23:40:52 +02:00
|
|
|
const user = await store.insert({ email: 'anotherWithResert@mail.com' });
|
2020-05-12 23:05:26 +02:00
|
|
|
await store.incLoginAttempts(user);
|
|
|
|
await store.incLoginAttempts(user);
|
|
|
|
|
2021-04-09 13:46:53 +02:00
|
|
|
await store.successfullyLogin(user);
|
2020-05-12 23:05:26 +02:00
|
|
|
const storedUser = await store.get(user);
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(storedUser.loginAttempts).toBe(0);
|
|
|
|
expect(storedUser.seenAt >= user.seenAt).toBe(true);
|
2020-05-12 23:05:26 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should only update specified fields on user', async () => {
|
2020-05-12 23:05:26 +02:00
|
|
|
const store = stores.userStore;
|
2021-04-23 15:30:23 +02:00
|
|
|
const email = 'usertobeupdated@mail.com';
|
2021-04-22 23:40:52 +02:00
|
|
|
const user = {
|
2020-05-12 23:05:26 +02:00
|
|
|
email,
|
|
|
|
username: 'test',
|
2021-04-22 23:40:52 +02:00
|
|
|
};
|
2020-05-12 23:05:26 +02:00
|
|
|
|
|
|
|
await store.upsert(user);
|
|
|
|
|
2021-04-22 23:40:52 +02:00
|
|
|
await store.upsert({ username: 'test' });
|
2020-05-12 23:05:26 +02:00
|
|
|
|
|
|
|
const storedUser = await store.get({ email });
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(storedUser.email).toEqual(user.email);
|
|
|
|
expect(storedUser.username).toEqual(user.username);
|
2020-05-12 23:05:26 +02:00
|
|
|
});
|
2021-04-23 15:30:23 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should always lowercase emails on inserts', async () => {
|
2021-04-23 15:30:23 +02:00
|
|
|
const store = stores.userStore;
|
|
|
|
const email = 'someCrazyCasingGoingOn@mail.com';
|
|
|
|
const user = {
|
|
|
|
email,
|
|
|
|
};
|
|
|
|
|
|
|
|
await store.upsert(user);
|
|
|
|
|
|
|
|
const storedUser = await store.get({ email });
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(storedUser.email).toEqual(user.email.toLowerCase());
|
2021-04-23 15:30:23 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should always lowercase emails on updates', async () => {
|
2021-04-23 15:30:23 +02:00
|
|
|
const store = stores.userStore;
|
|
|
|
const email = 'someCrazyCasingGoingOn@mail.com';
|
|
|
|
const user = {
|
|
|
|
email,
|
|
|
|
};
|
|
|
|
|
|
|
|
await store.upsert(user);
|
|
|
|
|
|
|
|
let storedUser = await store.get({ email });
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(storedUser.email).toEqual(user.email.toLowerCase());
|
2021-04-23 15:30:23 +02:00
|
|
|
|
|
|
|
const updatedUser = {
|
|
|
|
id: storedUser.id,
|
|
|
|
email: 'SomeOtherCasing@hotmail.com',
|
|
|
|
};
|
|
|
|
await store.upsert(updatedUser);
|
|
|
|
|
|
|
|
storedUser = await store.get({ id: storedUser.id });
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(storedUser.email).toBe(updatedUser.email.toLowerCase());
|
2021-04-23 15:30:23 +02:00
|
|
|
});
|