2022-09-28 10:24:43 +02:00
|
|
|
import { URL } from 'url';
|
2021-04-09 13:46:53 +02:00
|
|
|
import UserService from './user-service';
|
|
|
|
import UserStoreMock from '../../test/fixtures/fake-user-store';
|
2021-04-27 20:47:11 +02:00
|
|
|
import EventStoreMock from '../../test/fixtures/fake-event-store';
|
2021-04-09 13:46:53 +02:00
|
|
|
import AccessServiceMock from '../../test/fixtures/access-service-mock';
|
2021-04-16 15:29:23 +02:00
|
|
|
import ResetTokenService from './reset-token-service';
|
|
|
|
import { EmailService } from './email-service';
|
|
|
|
import OwaspValidationError from '../error/owasp-validation-error';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { IUnleashConfig } from '../types/option';
|
|
|
|
import { createTestConfig } from '../../test/config/test-config';
|
2021-04-27 09:16:44 +02:00
|
|
|
import SessionService from './session-service';
|
|
|
|
import FakeSessionStore from '../../test/fixtures/fake-session-store';
|
2021-04-27 20:47:11 +02:00
|
|
|
import User from '../types/user';
|
2021-08-12 15:04:37 +02:00
|
|
|
import FakeResetTokenStore from '../../test/fixtures/fake-reset-token-store';
|
2021-10-29 10:25:42 +02:00
|
|
|
import SettingService from './setting-service';
|
|
|
|
import FakeSettingStore from '../../test/fixtures/fake-setting-store';
|
2022-07-21 15:40:31 +02:00
|
|
|
import FakeEventStore from '../../test/fixtures/fake-event-store';
|
2021-04-09 13:46:53 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const config: IUnleashConfig = createTestConfig();
|
2021-04-09 13:46:53 +02:00
|
|
|
|
2021-04-27 20:47:11 +02:00
|
|
|
const systemUser = new User({ id: -1, username: 'system' });
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('Should create new user', async () => {
|
2021-04-09 13:46:53 +02:00
|
|
|
const userStore = new UserStoreMock();
|
2021-04-27 20:47:11 +02:00
|
|
|
const eventStore = new EventStoreMock();
|
2021-04-09 13:46:53 +02:00
|
|
|
const accessService = new AccessServiceMock();
|
2021-08-12 15:04:37 +02:00
|
|
|
const resetTokenStore = new FakeResetTokenStore();
|
2021-04-16 15:29:23 +02:00
|
|
|
const resetTokenService = new ResetTokenService(
|
2021-08-12 15:04:37 +02:00
|
|
|
{ resetTokenStore },
|
2021-04-16 15:29:23 +02:00
|
|
|
config,
|
|
|
|
);
|
2021-04-27 09:16:44 +02:00
|
|
|
const sessionStore = new FakeSessionStore();
|
|
|
|
const sessionService = new SessionService({ sessionStore }, config);
|
2021-04-16 15:29:23 +02:00
|
|
|
const emailService = new EmailService(config.email, config.getLogger);
|
2021-10-29 10:25:42 +02:00
|
|
|
const settingService = new SettingService(
|
2022-07-21 15:40:31 +02:00
|
|
|
{
|
|
|
|
settingStore: new FakeSettingStore(),
|
|
|
|
eventStore: new FakeEventStore(),
|
|
|
|
},
|
2021-10-29 10:25:42 +02:00
|
|
|
config,
|
|
|
|
);
|
2021-04-16 15:29:23 +02:00
|
|
|
|
2021-04-27 20:47:11 +02:00
|
|
|
const service = new UserService({ userStore, eventStore }, config, {
|
2021-04-16 15:29:23 +02:00
|
|
|
accessService,
|
|
|
|
resetTokenService,
|
|
|
|
emailService,
|
2021-04-27 09:16:44 +02:00
|
|
|
sessionService,
|
2021-10-29 10:25:42 +02:00
|
|
|
settingService,
|
2021-04-16 15:29:23 +02:00
|
|
|
});
|
2021-04-27 20:47:11 +02:00
|
|
|
const user = await service.createUser(
|
|
|
|
{
|
|
|
|
username: 'test',
|
|
|
|
rootRole: 1,
|
|
|
|
},
|
|
|
|
systemUser,
|
|
|
|
);
|
2021-08-12 15:04:37 +02:00
|
|
|
const storedUser = await userStore.get(user.id);
|
2021-04-09 13:46:53 +02:00
|
|
|
const allUsers = await userStore.getAll();
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(user.id).toBeTruthy();
|
|
|
|
expect(user.username).toBe('test');
|
|
|
|
expect(allUsers.length).toBe(1);
|
|
|
|
expect(storedUser.username).toBe('test');
|
2021-04-09 13:46:53 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('Should create default user', async () => {
|
2021-04-09 13:46:53 +02:00
|
|
|
const userStore = new UserStoreMock();
|
2021-04-27 20:47:11 +02:00
|
|
|
const eventStore = new EventStoreMock();
|
2021-04-09 13:46:53 +02:00
|
|
|
const accessService = new AccessServiceMock();
|
2021-08-12 15:04:37 +02:00
|
|
|
const resetTokenStore = new FakeResetTokenStore();
|
2021-04-16 15:29:23 +02:00
|
|
|
const resetTokenService = new ResetTokenService(
|
2021-08-12 15:04:37 +02:00
|
|
|
{ resetTokenStore },
|
2021-04-16 15:29:23 +02:00
|
|
|
config,
|
|
|
|
);
|
|
|
|
const emailService = new EmailService(config.email, config.getLogger);
|
2021-04-27 09:16:44 +02:00
|
|
|
const sessionStore = new FakeSessionStore();
|
|
|
|
const sessionService = new SessionService({ sessionStore }, config);
|
2021-10-29 10:25:42 +02:00
|
|
|
const settingService = new SettingService(
|
2022-07-21 15:40:31 +02:00
|
|
|
{
|
|
|
|
settingStore: new FakeSettingStore(),
|
|
|
|
eventStore: new FakeEventStore(),
|
|
|
|
},
|
2021-10-29 10:25:42 +02:00
|
|
|
config,
|
|
|
|
);
|
2021-04-16 15:29:23 +02:00
|
|
|
|
2021-04-27 20:47:11 +02:00
|
|
|
const service = new UserService({ userStore, eventStore }, config, {
|
2021-04-16 15:29:23 +02:00
|
|
|
accessService,
|
|
|
|
resetTokenService,
|
|
|
|
emailService,
|
2021-04-27 09:16:44 +02:00
|
|
|
sessionService,
|
2021-10-29 10:25:42 +02:00
|
|
|
settingService,
|
2021-04-16 15:29:23 +02:00
|
|
|
});
|
2021-04-09 13:46:53 +02:00
|
|
|
|
|
|
|
await service.initAdminUser();
|
|
|
|
|
2021-04-26 12:21:18 +02:00
|
|
|
const user = await service.loginUser('admin', 'unleash4all');
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(user.username).toBe('admin');
|
2021-04-09 13:46:53 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('Should be a valid password', async () => {
|
2021-04-09 13:46:53 +02:00
|
|
|
const userStore = new UserStoreMock();
|
2021-04-27 20:47:11 +02:00
|
|
|
const eventStore = new EventStoreMock();
|
2021-04-09 13:46:53 +02:00
|
|
|
const accessService = new AccessServiceMock();
|
2021-08-12 15:04:37 +02:00
|
|
|
const resetTokenStore = new FakeResetTokenStore();
|
2021-04-16 15:29:23 +02:00
|
|
|
const resetTokenService = new ResetTokenService(
|
2021-08-12 15:04:37 +02:00
|
|
|
{ resetTokenStore },
|
2021-04-16 15:29:23 +02:00
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
const emailService = new EmailService(config.email, config.getLogger);
|
2021-04-27 09:16:44 +02:00
|
|
|
const sessionStore = new FakeSessionStore();
|
|
|
|
const sessionService = new SessionService({ sessionStore }, config);
|
2021-10-29 10:25:42 +02:00
|
|
|
const settingService = new SettingService(
|
2022-07-21 15:40:31 +02:00
|
|
|
{
|
|
|
|
settingStore: new FakeSettingStore(),
|
|
|
|
eventStore: new FakeEventStore(),
|
|
|
|
},
|
2021-10-29 10:25:42 +02:00
|
|
|
config,
|
|
|
|
);
|
2021-04-16 15:29:23 +02:00
|
|
|
|
2021-04-27 20:47:11 +02:00
|
|
|
const service = new UserService({ userStore, eventStore }, config, {
|
2021-04-16 15:29:23 +02:00
|
|
|
accessService,
|
|
|
|
resetTokenService,
|
|
|
|
emailService,
|
2021-04-27 09:16:44 +02:00
|
|
|
sessionService,
|
2021-10-29 10:25:42 +02:00
|
|
|
settingService,
|
2021-04-16 15:29:23 +02:00
|
|
|
});
|
2021-04-09 13:46:53 +02:00
|
|
|
|
|
|
|
const valid = service.validatePassword('this is a strong password!');
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(valid).toBe(true);
|
2021-04-09 13:46:53 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('Password must be at least 10 chars', async () => {
|
2021-04-09 13:46:53 +02:00
|
|
|
const userStore = new UserStoreMock();
|
2021-04-27 20:47:11 +02:00
|
|
|
const eventStore = new EventStoreMock();
|
2021-04-09 13:46:53 +02:00
|
|
|
const accessService = new AccessServiceMock();
|
2021-08-12 15:04:37 +02:00
|
|
|
const resetTokenStore = new FakeResetTokenStore();
|
2021-04-16 15:29:23 +02:00
|
|
|
const resetTokenService = new ResetTokenService(
|
2021-08-12 15:04:37 +02:00
|
|
|
{ resetTokenStore },
|
2021-04-16 15:29:23 +02:00
|
|
|
config,
|
|
|
|
);
|
|
|
|
const emailService = new EmailService(config.email, config.getLogger);
|
2021-04-27 09:16:44 +02:00
|
|
|
const sessionStore = new FakeSessionStore();
|
|
|
|
const sessionService = new SessionService({ sessionStore }, config);
|
2021-10-29 10:25:42 +02:00
|
|
|
const settingService = new SettingService(
|
2022-07-21 15:40:31 +02:00
|
|
|
{
|
|
|
|
settingStore: new FakeSettingStore(),
|
|
|
|
eventStore: new FakeEventStore(),
|
|
|
|
},
|
2021-10-29 10:25:42 +02:00
|
|
|
config,
|
|
|
|
);
|
2021-04-16 15:29:23 +02:00
|
|
|
|
2021-04-27 20:47:11 +02:00
|
|
|
const service = new UserService({ userStore, eventStore }, config, {
|
2021-04-16 15:29:23 +02:00
|
|
|
accessService,
|
|
|
|
resetTokenService,
|
|
|
|
emailService,
|
2021-04-27 09:16:44 +02:00
|
|
|
sessionService,
|
2021-10-29 10:25:42 +02:00
|
|
|
settingService,
|
2021-04-16 15:29:23 +02:00
|
|
|
});
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(() => service.validatePassword('admin')).toThrow(
|
|
|
|
'The password must be at least 10 characters long.',
|
|
|
|
);
|
|
|
|
expect(() => service.validatePassword('qwertyabcde')).toThrowError(
|
|
|
|
OwaspValidationError,
|
|
|
|
);
|
2021-04-09 13:46:53 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('The password must contain at least one uppercase letter.', async () => {
|
2021-04-09 13:46:53 +02:00
|
|
|
const userStore = new UserStoreMock();
|
2021-04-27 20:47:11 +02:00
|
|
|
const eventStore = new EventStoreMock();
|
2021-04-09 13:46:53 +02:00
|
|
|
const accessService = new AccessServiceMock();
|
2021-08-12 15:04:37 +02:00
|
|
|
const resetTokenStore = new FakeResetTokenStore();
|
2021-04-16 15:29:23 +02:00
|
|
|
const resetTokenService = new ResetTokenService(
|
2021-08-12 15:04:37 +02:00
|
|
|
{ resetTokenStore },
|
2021-04-16 15:29:23 +02:00
|
|
|
config,
|
|
|
|
);
|
|
|
|
const emailService = new EmailService(config.email, config.getLogger);
|
2021-04-27 09:16:44 +02:00
|
|
|
const sessionStore = new FakeSessionStore();
|
|
|
|
const sessionService = new SessionService({ sessionStore }, config);
|
2021-10-29 10:25:42 +02:00
|
|
|
const settingService = new SettingService(
|
2022-07-21 15:40:31 +02:00
|
|
|
{
|
|
|
|
settingStore: new FakeSettingStore(),
|
|
|
|
eventStore: new FakeEventStore(),
|
|
|
|
},
|
2021-10-29 10:25:42 +02:00
|
|
|
config,
|
|
|
|
);
|
2021-04-16 15:29:23 +02:00
|
|
|
|
2021-04-27 20:47:11 +02:00
|
|
|
const service = new UserService({ userStore, eventStore }, config, {
|
2021-04-16 15:29:23 +02:00
|
|
|
accessService,
|
|
|
|
resetTokenService,
|
|
|
|
emailService,
|
2021-04-27 09:16:44 +02:00
|
|
|
sessionService,
|
2021-10-29 10:25:42 +02:00
|
|
|
settingService,
|
2021-04-16 15:29:23 +02:00
|
|
|
});
|
2021-04-09 13:46:53 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(() => service.validatePassword('qwertyabcde')).toThrowError(
|
|
|
|
'The password must contain at least one uppercase letter.',
|
|
|
|
);
|
|
|
|
expect(() => service.validatePassword('qwertyabcde')).toThrowError(
|
|
|
|
OwaspValidationError,
|
|
|
|
);
|
2021-04-09 13:46:53 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('The password must contain at least one number', async () => {
|
2021-04-09 13:46:53 +02:00
|
|
|
const userStore = new UserStoreMock();
|
2021-04-27 20:47:11 +02:00
|
|
|
const eventStore = new EventStoreMock();
|
2021-04-09 13:46:53 +02:00
|
|
|
const accessService = new AccessServiceMock();
|
2021-08-12 15:04:37 +02:00
|
|
|
const resetTokenStore = new FakeResetTokenStore();
|
2021-04-16 15:29:23 +02:00
|
|
|
const resetTokenService = new ResetTokenService(
|
2021-08-12 15:04:37 +02:00
|
|
|
{ resetTokenStore },
|
2021-04-16 15:29:23 +02:00
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
const emailService = new EmailService(config.email, config.getLogger);
|
2021-04-27 09:16:44 +02:00
|
|
|
const sessionStore = new FakeSessionStore();
|
|
|
|
const sessionService = new SessionService({ sessionStore }, config);
|
2021-10-29 10:25:42 +02:00
|
|
|
const settingService = new SettingService(
|
2022-07-21 15:40:31 +02:00
|
|
|
{
|
|
|
|
settingStore: new FakeSettingStore(),
|
|
|
|
eventStore: new FakeEventStore(),
|
|
|
|
},
|
2021-10-29 10:25:42 +02:00
|
|
|
config,
|
|
|
|
);
|
2021-04-27 09:16:44 +02:00
|
|
|
|
2021-04-27 20:47:11 +02:00
|
|
|
const service = new UserService({ userStore, eventStore }, config, {
|
2021-04-16 15:29:23 +02:00
|
|
|
accessService,
|
|
|
|
resetTokenService,
|
|
|
|
emailService,
|
2021-04-27 09:16:44 +02:00
|
|
|
sessionService,
|
2021-10-29 10:25:42 +02:00
|
|
|
settingService,
|
2021-04-16 15:29:23 +02:00
|
|
|
});
|
2021-04-09 13:46:53 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(() => service.validatePassword('qwertyabcdE')).toThrowError(
|
|
|
|
'The password must contain at least one number.',
|
|
|
|
);
|
|
|
|
expect(() => service.validatePassword('qwertyabcdE')).toThrowError(
|
|
|
|
OwaspValidationError,
|
|
|
|
);
|
2021-04-09 13:46:53 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('The password must contain at least one special character', async () => {
|
2021-04-09 13:46:53 +02:00
|
|
|
const userStore = new UserStoreMock();
|
2021-04-27 20:47:11 +02:00
|
|
|
const eventStore = new EventStoreMock();
|
2021-04-09 13:46:53 +02:00
|
|
|
const accessService = new AccessServiceMock();
|
2021-08-12 15:04:37 +02:00
|
|
|
const resetTokenStore = new FakeResetTokenStore();
|
2021-04-16 15:29:23 +02:00
|
|
|
const resetTokenService = new ResetTokenService(
|
2021-08-12 15:04:37 +02:00
|
|
|
{ resetTokenStore },
|
2021-04-16 15:29:23 +02:00
|
|
|
config,
|
|
|
|
);
|
|
|
|
const emailService = new EmailService(config.email, config.getLogger);
|
2021-04-27 09:16:44 +02:00
|
|
|
const sessionStore = new FakeSessionStore();
|
|
|
|
const sessionService = new SessionService({ sessionStore }, config);
|
2021-10-29 10:25:42 +02:00
|
|
|
const settingService = new SettingService(
|
2022-07-21 15:40:31 +02:00
|
|
|
{
|
|
|
|
settingStore: new FakeSettingStore(),
|
|
|
|
eventStore: new FakeEventStore(),
|
|
|
|
},
|
2021-10-29 10:25:42 +02:00
|
|
|
config,
|
|
|
|
);
|
2021-04-16 15:29:23 +02:00
|
|
|
|
2021-04-27 20:47:11 +02:00
|
|
|
const service = new UserService({ userStore, eventStore }, config, {
|
2021-04-16 15:29:23 +02:00
|
|
|
accessService,
|
|
|
|
resetTokenService,
|
|
|
|
emailService,
|
2021-04-27 09:16:44 +02:00
|
|
|
sessionService,
|
2021-10-29 10:25:42 +02:00
|
|
|
settingService,
|
2021-04-16 15:29:23 +02:00
|
|
|
});
|
2021-04-09 13:46:53 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(() => service.validatePassword('qwertyabcdE2')).toThrowError(
|
|
|
|
'The password must contain at least one special character.',
|
|
|
|
);
|
|
|
|
expect(() => service.validatePassword('qwertyabcdE2')).toThrowError(
|
|
|
|
OwaspValidationError,
|
|
|
|
);
|
2021-04-09 13:46:53 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('Should be a valid password with special chars', async () => {
|
2021-04-09 13:46:53 +02:00
|
|
|
const userStore = new UserStoreMock();
|
2021-04-27 20:47:11 +02:00
|
|
|
const eventStore = new EventStoreMock();
|
2021-04-09 13:46:53 +02:00
|
|
|
const accessService = new AccessServiceMock();
|
2021-08-12 15:04:37 +02:00
|
|
|
const resetTokenStore = new FakeResetTokenStore();
|
2021-04-16 15:29:23 +02:00
|
|
|
const resetTokenService = new ResetTokenService(
|
2021-08-12 15:04:37 +02:00
|
|
|
{ resetTokenStore },
|
2021-04-16 15:29:23 +02:00
|
|
|
config,
|
|
|
|
);
|
|
|
|
const emailService = new EmailService(config.email, config.getLogger);
|
2021-04-27 09:16:44 +02:00
|
|
|
const sessionStore = new FakeSessionStore();
|
|
|
|
const sessionService = new SessionService({ sessionStore }, config);
|
2021-10-29 10:25:42 +02:00
|
|
|
const settingService = new SettingService(
|
2022-07-21 15:40:31 +02:00
|
|
|
{
|
|
|
|
settingStore: new FakeSettingStore(),
|
|
|
|
eventStore: new FakeEventStore(),
|
|
|
|
},
|
2021-10-29 10:25:42 +02:00
|
|
|
config,
|
|
|
|
);
|
2021-04-16 15:29:23 +02:00
|
|
|
|
2021-04-27 20:47:11 +02:00
|
|
|
const service = new UserService({ userStore, eventStore }, config, {
|
2021-04-16 15:29:23 +02:00
|
|
|
accessService,
|
|
|
|
resetTokenService,
|
|
|
|
emailService,
|
2021-04-27 09:16:44 +02:00
|
|
|
sessionService,
|
2021-10-29 10:25:42 +02:00
|
|
|
settingService,
|
2021-04-16 15:29:23 +02:00
|
|
|
});
|
2021-04-09 13:46:53 +02:00
|
|
|
|
|
|
|
const valid = service.validatePassword('this is a strong password!');
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(valid).toBe(true);
|
2021-04-09 13:46:53 +02:00
|
|
|
});
|
2022-09-28 10:24:43 +02:00
|
|
|
|
|
|
|
test('Should send password reset email if user exists', async () => {
|
|
|
|
const userStore = new UserStoreMock();
|
|
|
|
const eventStore = new EventStoreMock();
|
|
|
|
const accessService = new AccessServiceMock();
|
|
|
|
const resetTokenStore = new FakeResetTokenStore();
|
|
|
|
const resetTokenService = new ResetTokenService(
|
|
|
|
{ resetTokenStore },
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
const emailService = new EmailService(config.email, config.getLogger);
|
|
|
|
const sessionStore = new FakeSessionStore();
|
|
|
|
const sessionService = new SessionService({ sessionStore }, config);
|
|
|
|
const settingService = new SettingService(
|
|
|
|
{
|
|
|
|
settingStore: new FakeSettingStore(),
|
|
|
|
eventStore: new FakeEventStore(),
|
|
|
|
},
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
const service = new UserService({ userStore, eventStore }, config, {
|
|
|
|
accessService,
|
|
|
|
resetTokenService,
|
|
|
|
emailService,
|
|
|
|
sessionService,
|
|
|
|
settingService,
|
|
|
|
});
|
|
|
|
|
|
|
|
const unknownUser = service.createResetPasswordEmail('unknown@example.com');
|
|
|
|
expect(unknownUser).rejects.toThrowError('Could not find user');
|
|
|
|
|
|
|
|
await userStore.insert({
|
|
|
|
id: 123,
|
|
|
|
name: 'User',
|
|
|
|
username: 'Username',
|
|
|
|
email: 'known@example.com',
|
|
|
|
permissions: [],
|
|
|
|
imageUrl: '',
|
|
|
|
seenAt: new Date(),
|
|
|
|
loginAttempts: 0,
|
|
|
|
createdAt: new Date(),
|
|
|
|
isAPI: false,
|
|
|
|
generateImageUrl: () => '',
|
|
|
|
});
|
|
|
|
|
|
|
|
const knownUser = service.createResetPasswordEmail('known@example.com');
|
|
|
|
expect(knownUser).resolves.toBeInstanceOf(URL);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Should throttle password reset email', async () => {
|
|
|
|
const userStore = new UserStoreMock();
|
|
|
|
const eventStore = new EventStoreMock();
|
|
|
|
const accessService = new AccessServiceMock();
|
|
|
|
const resetTokenStore = new FakeResetTokenStore();
|
|
|
|
const resetTokenService = new ResetTokenService(
|
|
|
|
{ resetTokenStore },
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
const emailService = new EmailService(config.email, config.getLogger);
|
|
|
|
const sessionStore = new FakeSessionStore();
|
|
|
|
const sessionService = new SessionService({ sessionStore }, config);
|
|
|
|
const settingService = new SettingService(
|
|
|
|
{
|
|
|
|
settingStore: new FakeSettingStore(),
|
|
|
|
eventStore: new FakeEventStore(),
|
|
|
|
},
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
const service = new UserService({ userStore, eventStore }, config, {
|
|
|
|
accessService,
|
|
|
|
resetTokenService,
|
|
|
|
emailService,
|
|
|
|
sessionService,
|
|
|
|
settingService,
|
|
|
|
});
|
|
|
|
|
|
|
|
await userStore.insert({
|
|
|
|
id: 123,
|
|
|
|
name: 'User',
|
|
|
|
username: 'Username',
|
|
|
|
email: 'known@example.com',
|
|
|
|
permissions: [],
|
|
|
|
imageUrl: '',
|
|
|
|
seenAt: new Date(),
|
|
|
|
loginAttempts: 0,
|
|
|
|
createdAt: new Date(),
|
|
|
|
isAPI: false,
|
|
|
|
generateImageUrl: () => '',
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
|
|
|
const attempt1 = service.createResetPasswordEmail('known@example.com');
|
|
|
|
await expect(attempt1).resolves.toBeInstanceOf(URL);
|
|
|
|
|
|
|
|
const attempt2 = service.createResetPasswordEmail('known@example.com');
|
|
|
|
await expect(attempt2).resolves.toBe(undefined);
|
|
|
|
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
const attempt3 = service.createResetPasswordEmail('known@example.com');
|
|
|
|
await expect(attempt3).resolves.toBeInstanceOf(URL);
|
|
|
|
});
|