2024-05-22 16:23:47 -04:00
|
|
|
import { IUserRepository } from 'src/interfaces/user.interface';
|
|
|
|
|
import { CliService } from 'src/services/cli.service';
|
|
|
|
|
import { userStub } from 'test/fixtures/user.stub';
|
2024-10-02 10:54:35 -04:00
|
|
|
import { newTestService } from 'test/utils';
|
2024-05-22 16:23:47 -04:00
|
|
|
import { Mocked, describe, it } from 'vitest';
|
|
|
|
|
|
|
|
|
|
describe(CliService.name, () => {
|
|
|
|
|
let sut: CliService;
|
|
|
|
|
|
2024-10-01 16:03:55 -04:00
|
|
|
let userMock: Mocked<IUserRepository>;
|
2024-05-22 16:23:47 -04:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2024-10-02 10:54:35 -04:00
|
|
|
({ sut, userMock } = newTestService(CliService));
|
2024-05-22 16:23:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('resetAdminPassword', () => {
|
|
|
|
|
it('should only work when there is an admin account', async () => {
|
|
|
|
|
userMock.getAdmin.mockResolvedValue(null);
|
|
|
|
|
const ask = vitest.fn().mockResolvedValue('new-password');
|
|
|
|
|
|
|
|
|
|
await expect(sut.resetAdminPassword(ask)).rejects.toThrowError('Admin account does not exist');
|
|
|
|
|
|
|
|
|
|
expect(ask).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should default to a random password', async () => {
|
|
|
|
|
userMock.getAdmin.mockResolvedValue(userStub.admin);
|
|
|
|
|
const ask = vitest.fn().mockImplementation(() => {});
|
|
|
|
|
|
|
|
|
|
const response = await sut.resetAdminPassword(ask);
|
|
|
|
|
|
|
|
|
|
const [id, update] = userMock.update.mock.calls[0];
|
|
|
|
|
|
|
|
|
|
expect(response.provided).toBe(false);
|
|
|
|
|
expect(ask).toHaveBeenCalled();
|
|
|
|
|
expect(id).toEqual(userStub.admin.id);
|
|
|
|
|
expect(update.password).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should use the supplied password', async () => {
|
|
|
|
|
userMock.getAdmin.mockResolvedValue(userStub.admin);
|
|
|
|
|
const ask = vitest.fn().mockResolvedValue('new-password');
|
|
|
|
|
|
|
|
|
|
const response = await sut.resetAdminPassword(ask);
|
|
|
|
|
|
|
|
|
|
const [id, update] = userMock.update.mock.calls[0];
|
|
|
|
|
|
|
|
|
|
expect(response.provided).toBe(true);
|
|
|
|
|
expect(ask).toHaveBeenCalled();
|
|
|
|
|
expect(id).toEqual(userStub.admin.id);
|
|
|
|
|
expect(update.password).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|