2023-01-28 00:12:11 -05:00
|
|
|
import { BadRequestException } from '@nestjs/common';
|
2024-03-20 21:42:58 +01:00
|
|
|
import { IKeyRepository } from 'src/interfaces/api-key.repository';
|
|
|
|
|
import { ICryptoRepository } from 'src/interfaces/crypto.repository';
|
2024-03-21 00:07:30 +01:00
|
|
|
import { APIKeyService } from 'src/services/api-key.service';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { keyStub } from 'test/fixtures/api-key.stub';
|
|
|
|
|
import { authStub } from 'test/fixtures/auth.stub';
|
|
|
|
|
import { newKeyRepositoryMock } from 'test/repositories/api-key.repository.mock';
|
|
|
|
|
import { newCryptoRepositoryMock } from 'test/repositories/crypto.repository.mock';
|
2023-01-18 09:40:15 -05:00
|
|
|
|
|
|
|
|
describe(APIKeyService.name, () => {
|
|
|
|
|
let sut: APIKeyService;
|
|
|
|
|
let keyMock: jest.Mocked<IKeyRepository>;
|
|
|
|
|
let cryptoMock: jest.Mocked<ICryptoRepository>;
|
|
|
|
|
|
2024-03-05 23:23:06 +01:00
|
|
|
beforeEach(() => {
|
2023-01-18 09:40:15 -05:00
|
|
|
cryptoMock = newCryptoRepositoryMock();
|
|
|
|
|
keyMock = newKeyRepositoryMock();
|
|
|
|
|
sut = new APIKeyService(cryptoMock, keyMock);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('create', () => {
|
|
|
|
|
it('should create a new key', async () => {
|
2023-01-31 13:11:49 -05:00
|
|
|
keyMock.create.mockResolvedValue(keyStub.admin);
|
2023-01-18 09:40:15 -05:00
|
|
|
await sut.create(authStub.admin, { name: 'Test Key' });
|
|
|
|
|
expect(keyMock.create).toHaveBeenCalledWith({
|
|
|
|
|
key: 'cmFuZG9tLWJ5dGVz (hashed)',
|
|
|
|
|
name: 'Test Key',
|
2023-12-09 23:34:12 -05:00
|
|
|
userId: authStub.admin.user.id,
|
2023-01-18 09:40:15 -05:00
|
|
|
});
|
|
|
|
|
expect(cryptoMock.randomBytes).toHaveBeenCalled();
|
2023-01-27 20:50:07 +00:00
|
|
|
expect(cryptoMock.hashSha256).toHaveBeenCalled();
|
2023-01-18 09:40:15 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not require a name', async () => {
|
2023-01-31 13:11:49 -05:00
|
|
|
keyMock.create.mockResolvedValue(keyStub.admin);
|
2023-01-18 09:40:15 -05:00
|
|
|
|
|
|
|
|
await sut.create(authStub.admin, {});
|
|
|
|
|
|
|
|
|
|
expect(keyMock.create).toHaveBeenCalledWith({
|
|
|
|
|
key: 'cmFuZG9tLWJ5dGVz (hashed)',
|
|
|
|
|
name: 'API Key',
|
2023-12-09 23:34:12 -05:00
|
|
|
userId: authStub.admin.user.id,
|
2023-01-18 09:40:15 -05:00
|
|
|
});
|
|
|
|
|
expect(cryptoMock.randomBytes).toHaveBeenCalled();
|
2023-01-27 20:50:07 +00:00
|
|
|
expect(cryptoMock.hashSha256).toHaveBeenCalled();
|
2023-01-18 09:40:15 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('update', () => {
|
|
|
|
|
it('should throw an error if the key is not found', async () => {
|
|
|
|
|
keyMock.getById.mockResolvedValue(null);
|
|
|
|
|
|
2023-02-18 20:58:55 +00:00
|
|
|
await expect(sut.update(authStub.admin, 'random-guid', { name: 'New Name' })).rejects.toBeInstanceOf(
|
|
|
|
|
BadRequestException,
|
|
|
|
|
);
|
2023-01-18 09:40:15 -05:00
|
|
|
|
2023-02-18 20:58:55 +00:00
|
|
|
expect(keyMock.update).not.toHaveBeenCalledWith('random-guid');
|
2023-01-18 09:40:15 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should update a key', async () => {
|
2023-01-31 13:11:49 -05:00
|
|
|
keyMock.getById.mockResolvedValue(keyStub.admin);
|
2023-06-30 21:49:30 -04:00
|
|
|
keyMock.update.mockResolvedValue(keyStub.admin);
|
2023-01-18 09:40:15 -05:00
|
|
|
|
2023-02-18 20:58:55 +00:00
|
|
|
await sut.update(authStub.admin, 'random-guid', { name: 'New Name' });
|
2023-01-18 09:40:15 -05:00
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
expect(keyMock.update).toHaveBeenCalledWith(authStub.admin.user.id, 'random-guid', { name: 'New Name' });
|
2023-01-18 09:40:15 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('delete', () => {
|
|
|
|
|
it('should throw an error if the key is not found', async () => {
|
|
|
|
|
keyMock.getById.mockResolvedValue(null);
|
|
|
|
|
|
2023-02-18 20:58:55 +00:00
|
|
|
await expect(sut.delete(authStub.admin, 'random-guid')).rejects.toBeInstanceOf(BadRequestException);
|
2023-01-18 09:40:15 -05:00
|
|
|
|
2023-02-18 20:58:55 +00:00
|
|
|
expect(keyMock.delete).not.toHaveBeenCalledWith('random-guid');
|
2023-01-18 09:40:15 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should delete a key', async () => {
|
2023-01-31 13:11:49 -05:00
|
|
|
keyMock.getById.mockResolvedValue(keyStub.admin);
|
2023-01-18 09:40:15 -05:00
|
|
|
|
2023-02-18 20:58:55 +00:00
|
|
|
await sut.delete(authStub.admin, 'random-guid');
|
2023-01-18 09:40:15 -05:00
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
expect(keyMock.delete).toHaveBeenCalledWith(authStub.admin.user.id, 'random-guid');
|
2023-01-18 09:40:15 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('getById', () => {
|
|
|
|
|
it('should throw an error if the key is not found', async () => {
|
|
|
|
|
keyMock.getById.mockResolvedValue(null);
|
|
|
|
|
|
2023-02-18 20:58:55 +00:00
|
|
|
await expect(sut.getById(authStub.admin, 'random-guid')).rejects.toBeInstanceOf(BadRequestException);
|
2023-01-18 09:40:15 -05:00
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
expect(keyMock.getById).toHaveBeenCalledWith(authStub.admin.user.id, 'random-guid');
|
2023-01-18 09:40:15 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should get a key by id', async () => {
|
2023-01-31 13:11:49 -05:00
|
|
|
keyMock.getById.mockResolvedValue(keyStub.admin);
|
2023-01-18 09:40:15 -05:00
|
|
|
|
2023-02-18 20:58:55 +00:00
|
|
|
await sut.getById(authStub.admin, 'random-guid');
|
2023-01-18 09:40:15 -05:00
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
expect(keyMock.getById).toHaveBeenCalledWith(authStub.admin.user.id, 'random-guid');
|
2023-01-18 09:40:15 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('getAll', () => {
|
|
|
|
|
it('should return all the keys for a user', async () => {
|
2023-01-31 13:11:49 -05:00
|
|
|
keyMock.getByUserId.mockResolvedValue([keyStub.admin]);
|
2023-01-18 09:40:15 -05:00
|
|
|
|
|
|
|
|
await expect(sut.getAll(authStub.admin)).resolves.toHaveLength(1);
|
|
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
expect(keyMock.getByUserId).toHaveBeenCalledWith(authStub.admin.user.id);
|
2023-01-18 09:40:15 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|