Skip to content

Commit

Permalink
Added image manager upload tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JanSpies82 committed Jul 25, 2023
1 parent f3c949d commit c45b606
Showing 1 changed file with 88 additions and 2 deletions.
90 changes: 88 additions & 2 deletions backend/src/image_manager/image_manager.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,24 @@ import { Asset } from '../assets/entities/asset.entity';
import { Repository } from 'typeorm';
import { AuthService } from '../auth/auth.service';
import { JwtService } from '@nestjs/jwt';
import { AssetDTO } from '../assets/dto/asset.dto';
import * as CryptoJS from 'crypto-js';

jest.mock('crypto-js', () => {
const mockedHash = jest.fn(
() => 'hashed string',
);

return {
SHA256: jest.fn().mockReturnValue({
toString: mockedHash,
}),
};
});
describe('ImageManagerService', () => {
let service: ImageManagerService;
let assetService: AssetsService;
let s3Service: S3Service;

beforeEach(async () => {
const module: TestingModule =
Expand All @@ -33,9 +48,80 @@ describe('ImageManagerService', () => {
service = module.get<ImageManagerService>(
ImageManagerService,
);
assetService = module.get<AssetsService>(
AssetsService,
);
s3Service = module.get<S3Service>(S3Service);
});

it('should be defined', () => {
expect(service).toBeDefined();
describe('upload', () => {
it('should set undefined values to empty strings', async () => {
const uploadImage = new AssetDTO();
uploadImage.Image = 'test';
uploadImage.UserID = 1;

jest
.spyOn(assetService, 'saveAsset')
.mockResolvedValue(uploadImage);

jest
.spyOn(s3Service, 'saveAsset')
.mockImplementation((assetDTO) =>
Promise.resolve(assetDTO),
);

expect(
uploadImage.ConvertedElement,
).toBeUndefined();
expect(uploadImage.Content).toBeUndefined();

const result = await service.upload(
uploadImage,
);

expect(result.ConvertedElement).toEqual('');
expect(result.Content).toEqual('test');
});

it('should store the image in the db and s3', async () => {
const uploadAssetWithImage = new AssetDTO();
uploadAssetWithImage.Image = 'test';
uploadAssetWithImage.UserID = 1;
uploadAssetWithImage.AssetID = 'test';

const uploadAssetWithoutImage =
new AssetDTO();
uploadAssetWithoutImage.UserID = 1;

const expectedAsset = new AssetDTO();
expectedAsset.Image = 'test';
expectedAsset.UserID = 1;
expectedAsset.AssetID = 'hashed string';

jest
.spyOn(assetService, 'saveAsset')
.mockResolvedValue(uploadAssetWithImage);

jest
.spyOn(s3Service, 'saveAsset')
.mockImplementation((assetDTO) =>
Promise.resolve(assetDTO),
);

const result = await service.upload(
uploadAssetWithImage,
);

expect(result).toEqual(
uploadAssetWithImage,
);
expect(
assetService.saveAsset,
).toBeCalledWith(uploadAssetWithoutImage);
expect(s3Service.saveAsset).toBeCalledWith(
uploadAssetWithImage,
);
expect(CryptoJS.SHA256).toBeCalled();
});
});
});

0 comments on commit c45b606

Please sign in to comment.