Skip to content

Commit 841209b

Browse files
feat(files): add support manage video/files
1 parent 7677492 commit 841209b

File tree

8 files changed

+394
-19
lines changed

8 files changed

+394
-19
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import { RemoteConfig } from '@lomray/microservice-helpers';
2+
import { TypeormMock } from '@lomray/microservice-helpers/mocks';
3+
import { expect } from 'chai';
4+
import sinon from 'sinon';
5+
import { getRepository } from 'typeorm';
6+
import { bucketNameMock } from '@__mocks__/common';
7+
import FileType from '@constants/file-type';
8+
import CONST from '@constants/index';
9+
import File from '@entities/file';
10+
import type { IRemoteConfig } from '@interfaces/remote-config';
11+
import AnyFile from '@services/file/any-file';
12+
import StorageFactory from '@services/storage/factory';
13+
14+
describe('services/file/any-file', () => {
15+
const sandbox = sinon.createSandbox();
16+
const fileData = getRepository(File).create({
17+
id: 'file_id',
18+
});
19+
const processingConfig: IRemoteConfig = {
20+
storagePathPrefix: CONST.STORAGE_PATH_PREFIX,
21+
};
22+
23+
beforeEach(() => {
24+
TypeormMock.sandbox.reset();
25+
});
26+
27+
afterEach(() => {
28+
sandbox.restore();
29+
});
30+
31+
it('should successfully save file', async () => {
32+
TypeormMock.entityManager.save.resolves(fileData);
33+
sandbox.stub(RemoteConfig, 'get').resolves({ s3: { bucketName: bucketNameMock } });
34+
35+
const storage = await StorageFactory.create();
36+
const service = new AnyFile(
37+
FileType.file,
38+
TypeormMock.entityManager,
39+
storage,
40+
processingConfig,
41+
);
42+
43+
sandbox.stub(service, <any>'composeData').resolves({
44+
fileData: { url: 'url', type: FileType.file },
45+
fileBuffer: undefined,
46+
});
47+
48+
sandbox.stub(service, <any>'uploadFile');
49+
50+
const entity = await service.save('file', 'user_id');
51+
52+
const [, file] = TypeormMock.entityManager.save.firstCall.args;
53+
54+
expect(entity).to.deep.equal(fileData);
55+
expect(file).to.deep.equal({
56+
userId: 'user_id',
57+
url: '/',
58+
type: FileType.file,
59+
});
60+
});
61+
62+
it('should successfully remove file', async () => {
63+
sandbox.stub(RemoteConfig, 'get').resolves({ s3: { bucketName: bucketNameMock } });
64+
65+
const storage = await StorageFactory.create();
66+
const service = new AnyFile(
67+
FileType.file,
68+
TypeormMock.entityManager,
69+
storage,
70+
processingConfig,
71+
);
72+
73+
sandbox.stub(storage, 'delete');
74+
75+
const isRemoved = await service.remove(fileData);
76+
77+
const [, file] = TypeormMock.entityManager.remove.firstCall.args;
78+
79+
expect(isRemoved).to.ok;
80+
expect(file).to.deep.equal(fileData);
81+
});
82+
83+
it('should successfully update file', async () => {
84+
TypeormMock.entityManager.save.resolves(fileData);
85+
sandbox.stub(RemoteConfig, 'get').resolves({ s3: { bucketName: bucketNameMock } });
86+
87+
const storage = await StorageFactory.create();
88+
const service = new AnyFile(
89+
FileType.file,
90+
TypeormMock.entityManager,
91+
storage,
92+
processingConfig,
93+
);
94+
95+
sandbox.stub(service, <any>'composeData').resolves({
96+
fileData: { url: 'url', type: FileType.file },
97+
fileBuffer: undefined,
98+
});
99+
100+
sandbox.stub(service, <any>'uploadFile');
101+
102+
sandbox.stub(storage, 'delete');
103+
104+
const entity = await service.update(fileData, 'file');
105+
106+
const [, fileEntity] = TypeormMock.entityManager.save.firstCall.args;
107+
108+
expect(entity).to.deep.equal(fileData);
109+
expect(fileEntity).to.deep.equal({
110+
id: 'file_id',
111+
url: 'url',
112+
type: FileType.file,
113+
});
114+
});
115+
});

microservices/files/__tests__/services/file/factory-test.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { RemoteConfig } from '@lomray/microservice-helpers';
22
import { TypeormMock } from '@lomray/microservice-helpers/mocks';
3-
import { waitResult } from '@lomray/microservice-helpers/test-helpers';
43
import { expect } from 'chai';
54
import sinon from 'sinon';
6-
import { bucketNameMock } from '@__mocks__/common';
75
import FileType from '@constants/file-type';
6+
import AnyFile from '@services/file/any-file';
87
import Factory from '@services/file/factory';
98
import Image from '@services/file/image';
109
import StorageFactory from '@services/storage/factory';
@@ -25,11 +24,21 @@ describe('services/file/factory', () => {
2524
expect(service).instanceof(Image);
2625
});
2726

28-
it('should throw error: Not implemented', async () => {
29-
sandbox.stub(RemoteConfig, 'get').resolves({ s3: { bucketName: bucketNameMock } });
30-
// @ts-ignore
31-
const service = Factory.create('unknown', TypeormMock.entityManager);
27+
it('should successful create AnyFile instance', async () => {
28+
sandbox.stub(RemoteConfig, 'get').resolves({});
29+
sandbox.stub(StorageFactory, 'create');
30+
31+
const service = await Factory.create(FileType.file, TypeormMock.entityManager);
32+
33+
expect(service).instanceof(AnyFile);
34+
});
35+
36+
it('should successful create AnyFile instance for video', async () => {
37+
sandbox.stub(RemoteConfig, 'get').resolves({});
38+
sandbox.stub(StorageFactory, 'create');
39+
40+
const service = await Factory.create(FileType.video, TypeormMock.entityManager);
3241

33-
expect(await waitResult(service)).to.throw('Not implemented');
42+
expect(service).instanceof(AnyFile);
3443
});
3544
});

microservices/files/__tests__/services/file/image-test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ describe('services/file/image', () => {
2121
storagePathPrefix: CONST.STORAGE_PATH_PREFIX,
2222
};
2323

24+
beforeEach(() => {
25+
TypeormMock.sandbox.reset();
26+
});
27+
2428
afterEach(() => {
2529
sandbox.restore();
2630
});
@@ -89,8 +93,8 @@ describe('services/file/image', () => {
8993

9094
expect(entity).to.deep.equal(fileData);
9195
expect(fileEntity).to.deep.equal({
92-
userId: 'user_id',
93-
url: '/',
96+
id: 'file_id',
97+
url: 'url',
9498
type: FileType.image,
9599
});
96100
});

microservices/files/package-lock.json

Lines changed: 127 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)