@flusys/nestjs-storage 6.0.2 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/controllers/file-manager.controller.spec.js +135 -0
- package/cjs/controllers/folder.controller.spec.js +86 -0
- package/cjs/controllers/storage-config.controller.spec.js +85 -0
- package/cjs/controllers/upload.controller.spec.js +132 -0
- package/cjs/docs/storage-swagger.config.spec.js +72 -0
- package/cjs/entities/index.spec.js +18 -0
- package/cjs/middlewares/file-serve.middleware.spec.js +211 -0
- package/cjs/modules/storage.module.spec.js +184 -0
- package/cjs/providers/azure-provider.optional.spec.js +228 -0
- package/cjs/providers/local-provider.spec.js +239 -0
- package/cjs/providers/s3-provider.optional.spec.js +243 -0
- package/cjs/providers/sftp-provider.optional.spec.js +217 -0
- package/cjs/providers/storage-factory.service.spec.js +253 -0
- package/cjs/providers/storage-provider.registry.spec.js +67 -0
- package/cjs/services/file-manager.service.spec.js +549 -0
- package/cjs/services/folder.service.spec.js +163 -0
- package/cjs/services/storage-config.service.spec.js +257 -0
- package/cjs/services/storage-datasource.provider.spec.js +217 -0
- package/cjs/services/storage-provider-config.service.spec.js +243 -0
- package/cjs/services/upload.service.spec.js +372 -0
- package/cjs/utils/file-validator.util.spec.js +259 -0
- package/cjs/utils/image-compressor.util.spec.js +166 -0
- package/fesm/controllers/file-manager.controller.spec.js +131 -0
- package/fesm/controllers/folder.controller.spec.js +82 -0
- package/fesm/controllers/storage-config.controller.spec.js +81 -0
- package/fesm/controllers/upload.controller.spec.js +128 -0
- package/fesm/docs/storage-swagger.config.spec.js +68 -0
- package/fesm/entities/index.spec.js +14 -0
- package/fesm/middlewares/file-serve.middleware.spec.js +207 -0
- package/fesm/modules/storage.module.spec.js +180 -0
- package/fesm/providers/azure-provider.optional.spec.js +224 -0
- package/fesm/providers/local-provider.spec.js +194 -0
- package/fesm/providers/s3-provider.optional.spec.js +239 -0
- package/fesm/providers/sftp-provider.optional.spec.js +213 -0
- package/fesm/providers/storage-factory.service.spec.js +249 -0
- package/fesm/providers/storage-provider.registry.spec.js +63 -0
- package/fesm/services/file-manager.service.spec.js +545 -0
- package/fesm/services/folder.service.spec.js +159 -0
- package/fesm/services/storage-config.service.spec.js +253 -0
- package/fesm/services/storage-datasource.provider.spec.js +172 -0
- package/fesm/services/storage-provider-config.service.spec.js +239 -0
- package/fesm/services/upload.service.spec.js +368 -0
- package/fesm/utils/file-validator.util.spec.js +255 -0
- package/fesm/utils/image-compressor.util.spec.js +162 -0
- package/package.json +3 -3
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { Test } from '@nestjs/testing';
|
|
2
|
+
import { UtilsService } from '@flusys/nestjs-shared/modules';
|
|
3
|
+
import { createMockDataSourceProvider, createMockRepository } from '@test-utils/mocks/repository.mock';
|
|
4
|
+
import { buildMockUser } from '@test-utils/mocks/logged-user.mock';
|
|
5
|
+
import { FolderService } from './folder.service';
|
|
6
|
+
import { StorageConfigService } from './storage-config.service';
|
|
7
|
+
import { StorageDataSourceProvider } from './storage-datasource.provider';
|
|
8
|
+
import { Folder, FolderWithCompany } from '../entities';
|
|
9
|
+
describe('FolderService', ()=>{
|
|
10
|
+
let service;
|
|
11
|
+
let mockRepo;
|
|
12
|
+
let mockStorageConfig;
|
|
13
|
+
beforeEach(async ()=>{
|
|
14
|
+
mockRepo = createMockRepository();
|
|
15
|
+
const mockDataSourceProvider = createMockDataSourceProvider(mockRepo);
|
|
16
|
+
mockStorageConfig = {
|
|
17
|
+
isCompanyFeatureEnabled: jest.fn().mockReturnValue(false)
|
|
18
|
+
};
|
|
19
|
+
const module = await Test.createTestingModule({
|
|
20
|
+
providers: [
|
|
21
|
+
FolderService,
|
|
22
|
+
{
|
|
23
|
+
provide: 'CACHE_INSTANCE',
|
|
24
|
+
useValue: {}
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
provide: UtilsService,
|
|
28
|
+
useValue: {}
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
provide: StorageConfigService,
|
|
32
|
+
useValue: mockStorageConfig
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
provide: StorageDataSourceProvider,
|
|
36
|
+
useValue: mockDataSourceProvider
|
|
37
|
+
}
|
|
38
|
+
]
|
|
39
|
+
}).compile();
|
|
40
|
+
service = await module.resolve(FolderService);
|
|
41
|
+
});
|
|
42
|
+
describe('resolveEntity', ()=>{
|
|
43
|
+
it('resolves to the core Folder entity when company feature is disabled', ()=>{
|
|
44
|
+
expect(service.resolveEntity()).toBe(Folder);
|
|
45
|
+
});
|
|
46
|
+
it('resolves to FolderWithCompany when company feature is enabled', ()=>{
|
|
47
|
+
mockStorageConfig.isCompanyFeatureEnabled.mockReturnValue(true);
|
|
48
|
+
expect(service.resolveEntity()).toBe(FolderWithCompany);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
describe('convertSingleDtoToEntity', ()=>{
|
|
52
|
+
beforeEach(async ()=>{
|
|
53
|
+
mockRepo.create.mockReturnValue({});
|
|
54
|
+
await service.ensureDataSourceRepository();
|
|
55
|
+
});
|
|
56
|
+
it('does not attach companyId when the company feature is disabled', async ()=>{
|
|
57
|
+
const entity = await service.convertSingleDtoToEntity({
|
|
58
|
+
name: 'Invoices'
|
|
59
|
+
}, buildMockUser());
|
|
60
|
+
expect(entity.companyId).toBeUndefined();
|
|
61
|
+
});
|
|
62
|
+
it('attaches the current user companyId when the company feature is enabled', async ()=>{
|
|
63
|
+
mockStorageConfig.isCompanyFeatureEnabled.mockReturnValue(true);
|
|
64
|
+
const entity = await service.convertSingleDtoToEntity({
|
|
65
|
+
name: 'Invoices'
|
|
66
|
+
}, buildMockUser({
|
|
67
|
+
companyId: 'company-7'
|
|
68
|
+
}));
|
|
69
|
+
expect(entity.companyId).toBe('company-7');
|
|
70
|
+
});
|
|
71
|
+
it('defaults companyId to null when company feature is enabled but user has none', async ()=>{
|
|
72
|
+
mockStorageConfig.isCompanyFeatureEnabled.mockReturnValue(true);
|
|
73
|
+
const entity = await service.convertSingleDtoToEntity({
|
|
74
|
+
name: 'Invoices'
|
|
75
|
+
}, null);
|
|
76
|
+
expect(entity.companyId).toBeNull();
|
|
77
|
+
});
|
|
78
|
+
it('loads and merges the existing entity when updating (dto has an id)', async ()=>{
|
|
79
|
+
const existing = {
|
|
80
|
+
id: 'folder-1',
|
|
81
|
+
name: 'Old Name'
|
|
82
|
+
};
|
|
83
|
+
mockRepo.findOne.mockResolvedValue(existing);
|
|
84
|
+
const entity = await service.convertSingleDtoToEntity({
|
|
85
|
+
id: 'folder-1',
|
|
86
|
+
name: 'New Name'
|
|
87
|
+
}, buildMockUser());
|
|
88
|
+
expect(mockRepo.findOne).toHaveBeenCalledWith({
|
|
89
|
+
where: {
|
|
90
|
+
id: 'folder-1'
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
expect(entity.name).toBe('New Name');
|
|
94
|
+
});
|
|
95
|
+
it('throws NotFoundException when updating a folder that does not exist', async ()=>{
|
|
96
|
+
mockRepo.findOne.mockResolvedValue(null);
|
|
97
|
+
await expect(service.convertSingleDtoToEntity({
|
|
98
|
+
id: 'missing-id',
|
|
99
|
+
name: 'x'
|
|
100
|
+
}, buildMockUser())).rejects.toThrow();
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
describe('getSelectQuery', ()=>{
|
|
104
|
+
it('selects the default field set (id, name, slug, createdAt, deletedAt) when none is provided', async ()=>{
|
|
105
|
+
const query = {
|
|
106
|
+
select: jest.fn().mockReturnThis()
|
|
107
|
+
};
|
|
108
|
+
await service.getSelectQuery(query, buildMockUser());
|
|
109
|
+
expect(query.select).toHaveBeenCalledWith([
|
|
110
|
+
'folder.id',
|
|
111
|
+
'folder.name',
|
|
112
|
+
'folder.slug',
|
|
113
|
+
'folder.createdAt',
|
|
114
|
+
'folder.deletedAt'
|
|
115
|
+
]);
|
|
116
|
+
});
|
|
117
|
+
it('adds companyId to the default select list when company feature is enabled', async ()=>{
|
|
118
|
+
mockStorageConfig.isCompanyFeatureEnabled.mockReturnValue(true);
|
|
119
|
+
const query = {
|
|
120
|
+
select: jest.fn().mockReturnThis()
|
|
121
|
+
};
|
|
122
|
+
await service.getSelectQuery(query, buildMockUser());
|
|
123
|
+
const selected = query.select.mock.calls[0][0];
|
|
124
|
+
expect(selected).toContain('folder.companyId');
|
|
125
|
+
});
|
|
126
|
+
it('respects an explicitly provided select list', async ()=>{
|
|
127
|
+
const query = {
|
|
128
|
+
select: jest.fn().mockReturnThis()
|
|
129
|
+
};
|
|
130
|
+
await service.getSelectQuery(query, buildMockUser(), [
|
|
131
|
+
'id'
|
|
132
|
+
]);
|
|
133
|
+
expect(query.select).toHaveBeenCalledWith([
|
|
134
|
+
'folder.id'
|
|
135
|
+
]);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
describe('getExtraManipulateQuery', ()=>{
|
|
139
|
+
it('applies the company filter when company feature is enabled', async ()=>{
|
|
140
|
+
mockStorageConfig.isCompanyFeatureEnabled.mockReturnValue(true);
|
|
141
|
+
const query = {
|
|
142
|
+
andWhere: jest.fn().mockReturnThis()
|
|
143
|
+
};
|
|
144
|
+
await service.getExtraManipulateQuery(query, {}, buildMockUser({
|
|
145
|
+
companyId: 'company-3'
|
|
146
|
+
}));
|
|
147
|
+
expect(query.andWhere).toHaveBeenCalledWith('folder.companyId = :companyId', {
|
|
148
|
+
companyId: 'company-3'
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
it('does not apply a company filter when company feature is disabled', async ()=>{
|
|
152
|
+
const query = {
|
|
153
|
+
andWhere: jest.fn().mockReturnThis()
|
|
154
|
+
};
|
|
155
|
+
await service.getExtraManipulateQuery(query, {}, buildMockUser());
|
|
156
|
+
expect(query.andWhere).not.toHaveBeenCalled();
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
});
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { StorageConfigService } from './storage-config.service';
|
|
2
|
+
import { UPLOAD_MESSAGES } from '../config';
|
|
3
|
+
function buildOptions(overrides = {}) {
|
|
4
|
+
return {
|
|
5
|
+
bootstrapAppConfig: {
|
|
6
|
+
databaseMode: 'single',
|
|
7
|
+
enableCompanyFeature: false
|
|
8
|
+
},
|
|
9
|
+
config: {},
|
|
10
|
+
...overrides
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
describe('StorageConfigService', ()=>{
|
|
14
|
+
describe('bootstrap config getters', ()=>{
|
|
15
|
+
it('reflects enableCompanyFeature from bootstrap config', ()=>{
|
|
16
|
+
const service = new StorageConfigService(buildOptions({
|
|
17
|
+
bootstrapAppConfig: {
|
|
18
|
+
databaseMode: 'single',
|
|
19
|
+
enableCompanyFeature: true
|
|
20
|
+
}
|
|
21
|
+
}));
|
|
22
|
+
expect(service.isCompanyFeatureEnabled()).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
it('defaults enableCompanyFeature to false when bootstrapAppConfig is absent', ()=>{
|
|
25
|
+
const service = new StorageConfigService(buildOptions({
|
|
26
|
+
bootstrapAppConfig: undefined
|
|
27
|
+
}));
|
|
28
|
+
expect(service.isCompanyFeatureEnabled()).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
it('defaults databaseMode to single when bootstrapAppConfig is absent', ()=>{
|
|
31
|
+
const service = new StorageConfigService(buildOptions({
|
|
32
|
+
bootstrapAppConfig: undefined
|
|
33
|
+
}));
|
|
34
|
+
expect(service.getDatabaseMode()).toBe('single');
|
|
35
|
+
});
|
|
36
|
+
it('reports isMultiTenant() true only in multi-tenant mode', ()=>{
|
|
37
|
+
const service = new StorageConfigService(buildOptions({
|
|
38
|
+
bootstrapAppConfig: {
|
|
39
|
+
databaseMode: 'multi-tenant',
|
|
40
|
+
enableCompanyFeature: true
|
|
41
|
+
}
|
|
42
|
+
}));
|
|
43
|
+
expect(service.isMultiTenant()).toBe(true);
|
|
44
|
+
});
|
|
45
|
+
it('reports isMultiTenant() false in single mode', ()=>{
|
|
46
|
+
const service = new StorageConfigService(buildOptions());
|
|
47
|
+
expect(service.isMultiTenant()).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
describe('getMaxFileSize', ()=>{
|
|
51
|
+
it('returns the configured max file size', ()=>{
|
|
52
|
+
const service = new StorageConfigService(buildOptions({
|
|
53
|
+
config: {
|
|
54
|
+
maxFileSize: 5000
|
|
55
|
+
}
|
|
56
|
+
}));
|
|
57
|
+
expect(service.getMaxFileSize()).toBe(5000);
|
|
58
|
+
});
|
|
59
|
+
it('falls back to the default max file size (100MB) when not configured', ()=>{
|
|
60
|
+
const service = new StorageConfigService(buildOptions({
|
|
61
|
+
config: {}
|
|
62
|
+
}));
|
|
63
|
+
expect(service.getMaxFileSize()).toBe(100 * 1024 * 1024);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
describe('getAllowedFileTypes', ()=>{
|
|
67
|
+
it('returns configured allowed types', ()=>{
|
|
68
|
+
const service = new StorageConfigService(buildOptions({
|
|
69
|
+
config: {
|
|
70
|
+
allowedFileTypes: [
|
|
71
|
+
'image/png'
|
|
72
|
+
]
|
|
73
|
+
}
|
|
74
|
+
}));
|
|
75
|
+
expect(service.getAllowedFileTypes()).toEqual([
|
|
76
|
+
'image/png'
|
|
77
|
+
]);
|
|
78
|
+
});
|
|
79
|
+
it('defaults to */* when not configured', ()=>{
|
|
80
|
+
const service = new StorageConfigService(buildOptions({
|
|
81
|
+
config: {}
|
|
82
|
+
}));
|
|
83
|
+
expect(service.getAllowedFileTypes()).toEqual([
|
|
84
|
+
'*/*'
|
|
85
|
+
]);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
describe('getDefaultLocalStoragePath', ()=>{
|
|
89
|
+
it('returns the configured local storage path', ()=>{
|
|
90
|
+
const service = new StorageConfigService(buildOptions({
|
|
91
|
+
config: {
|
|
92
|
+
localStoragePath: './custom-uploads'
|
|
93
|
+
}
|
|
94
|
+
}));
|
|
95
|
+
expect(service.getDefaultLocalStoragePath()).toBe('./custom-uploads');
|
|
96
|
+
});
|
|
97
|
+
it('defaults to ./uploads when not configured', ()=>{
|
|
98
|
+
const service = new StorageConfigService(buildOptions({
|
|
99
|
+
config: {}
|
|
100
|
+
}));
|
|
101
|
+
expect(service.getDefaultLocalStoragePath()).toBe('./uploads');
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
describe('getAppUrl', ()=>{
|
|
105
|
+
const originalAppUrl = process.env.APP_URL;
|
|
106
|
+
const originalPort = process.env.PORT;
|
|
107
|
+
afterEach(()=>{
|
|
108
|
+
process.env.APP_URL = originalAppUrl;
|
|
109
|
+
process.env.PORT = originalPort;
|
|
110
|
+
});
|
|
111
|
+
it('returns the explicitly configured appUrl', ()=>{
|
|
112
|
+
const service = new StorageConfigService(buildOptions({
|
|
113
|
+
config: {
|
|
114
|
+
appUrl: 'https://files.example.com'
|
|
115
|
+
}
|
|
116
|
+
}));
|
|
117
|
+
expect(service.getAppUrl()).toBe('https://files.example.com');
|
|
118
|
+
});
|
|
119
|
+
it('falls back to process.env.APP_URL when config.appUrl is absent', ()=>{
|
|
120
|
+
delete process.env.APP_URL;
|
|
121
|
+
process.env.APP_URL = 'https://env.example.com';
|
|
122
|
+
const service = new StorageConfigService(buildOptions({
|
|
123
|
+
config: {}
|
|
124
|
+
}));
|
|
125
|
+
expect(service.getAppUrl()).toBe('https://env.example.com');
|
|
126
|
+
});
|
|
127
|
+
it('falls back to localhost with PORT when neither config nor env var is set', ()=>{
|
|
128
|
+
delete process.env.APP_URL;
|
|
129
|
+
process.env.PORT = '4000';
|
|
130
|
+
const service = new StorageConfigService(buildOptions({
|
|
131
|
+
config: {}
|
|
132
|
+
}));
|
|
133
|
+
expect(service.getAppUrl()).toBe('http://localhost:4000');
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
describe('isFileTypeAllowed', ()=>{
|
|
137
|
+
it('allows any type when */* is configured', ()=>{
|
|
138
|
+
const service = new StorageConfigService(buildOptions({
|
|
139
|
+
config: {
|
|
140
|
+
allowedFileTypes: [
|
|
141
|
+
'*/*'
|
|
142
|
+
]
|
|
143
|
+
}
|
|
144
|
+
}));
|
|
145
|
+
expect(service.isFileTypeAllowed('application/pdf')).toBe(true);
|
|
146
|
+
});
|
|
147
|
+
it('allows types matching a category wildcard', ()=>{
|
|
148
|
+
const service = new StorageConfigService(buildOptions({
|
|
149
|
+
config: {
|
|
150
|
+
allowedFileTypes: [
|
|
151
|
+
'image/*'
|
|
152
|
+
]
|
|
153
|
+
}
|
|
154
|
+
}));
|
|
155
|
+
expect(service.isFileTypeAllowed('image/png')).toBe(true);
|
|
156
|
+
});
|
|
157
|
+
it('rejects types outside the allow-list', ()=>{
|
|
158
|
+
const service = new StorageConfigService(buildOptions({
|
|
159
|
+
config: {
|
|
160
|
+
allowedFileTypes: [
|
|
161
|
+
'image/*'
|
|
162
|
+
]
|
|
163
|
+
}
|
|
164
|
+
}));
|
|
165
|
+
expect(service.isFileTypeAllowed('application/pdf')).toBe(false);
|
|
166
|
+
});
|
|
167
|
+
it('allows an exact MIME type match', ()=>{
|
|
168
|
+
const service = new StorageConfigService(buildOptions({
|
|
169
|
+
config: {
|
|
170
|
+
allowedFileTypes: [
|
|
171
|
+
'application/pdf'
|
|
172
|
+
]
|
|
173
|
+
}
|
|
174
|
+
}));
|
|
175
|
+
expect(service.isFileTypeAllowed('application/pdf')).toBe(true);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
describe('validateFileSize', ()=>{
|
|
179
|
+
it('passes when file size is within the limit', ()=>{
|
|
180
|
+
const service = new StorageConfigService(buildOptions({
|
|
181
|
+
config: {
|
|
182
|
+
maxFileSize: 1000
|
|
183
|
+
}
|
|
184
|
+
}));
|
|
185
|
+
expect(service.validateFileSize(500)).toEqual({
|
|
186
|
+
valid: true
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
it('passes at the exact boundary size', ()=>{
|
|
190
|
+
const service = new StorageConfigService(buildOptions({
|
|
191
|
+
config: {
|
|
192
|
+
maxFileSize: 1000
|
|
193
|
+
}
|
|
194
|
+
}));
|
|
195
|
+
expect(service.validateFileSize(1000).valid).toBe(true);
|
|
196
|
+
});
|
|
197
|
+
it('fails when file size exceeds the limit by one byte', ()=>{
|
|
198
|
+
const service = new StorageConfigService(buildOptions({
|
|
199
|
+
config: {
|
|
200
|
+
maxFileSize: 1000
|
|
201
|
+
}
|
|
202
|
+
}));
|
|
203
|
+
const result = service.validateFileSize(1001);
|
|
204
|
+
expect(result.valid).toBe(false);
|
|
205
|
+
expect(result.messageKey).toBe(UPLOAD_MESSAGES.FILE_TOO_LARGE);
|
|
206
|
+
expect(result.messageVariables).toEqual({
|
|
207
|
+
fileSize: '0.00',
|
|
208
|
+
maxSize: '0.00'
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
it('reports MB-converted size values in the failure message', ()=>{
|
|
212
|
+
const service = new StorageConfigService(buildOptions({
|
|
213
|
+
config: {
|
|
214
|
+
maxFileSize: 1 * 1024 * 1024
|
|
215
|
+
}
|
|
216
|
+
}));
|
|
217
|
+
const result = service.validateFileSize(2 * 1024 * 1024);
|
|
218
|
+
expect(result.message).toContain('2.00MB');
|
|
219
|
+
expect(result.message).toContain('1.00MB');
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
describe('validateFileType', ()=>{
|
|
223
|
+
it('passes for an allowed type', ()=>{
|
|
224
|
+
const service = new StorageConfigService(buildOptions({
|
|
225
|
+
config: {
|
|
226
|
+
allowedFileTypes: [
|
|
227
|
+
'image/*'
|
|
228
|
+
]
|
|
229
|
+
}
|
|
230
|
+
}));
|
|
231
|
+
expect(service.validateFileType('image/png')).toEqual({
|
|
232
|
+
valid: true
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
it('fails for a disallowed type with a descriptive message and messageKey', ()=>{
|
|
236
|
+
const service = new StorageConfigService(buildOptions({
|
|
237
|
+
config: {
|
|
238
|
+
allowedFileTypes: [
|
|
239
|
+
'image/*'
|
|
240
|
+
]
|
|
241
|
+
}
|
|
242
|
+
}));
|
|
243
|
+
const result = service.validateFileType('application/pdf');
|
|
244
|
+
expect(result.valid).toBe(false);
|
|
245
|
+
expect(result.messageKey).toBe(UPLOAD_MESSAGES.INVALID_TYPE);
|
|
246
|
+
expect(result.message).toContain('application/pdf');
|
|
247
|
+
expect(result.messageVariables).toEqual({
|
|
248
|
+
mimeType: 'application/pdf',
|
|
249
|
+
allowedTypes: 'image/*'
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
});
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { MultiTenantDataSourceService } from '@flusys/nestjs-shared/modules';
|
|
2
|
+
import { StorageDataSourceProvider } from './storage-datasource.provider';
|
|
3
|
+
import * as entities from '../entities';
|
|
4
|
+
function buildMockConfigService(overrides = {}) {
|
|
5
|
+
const options = {
|
|
6
|
+
bootstrapAppConfig: {
|
|
7
|
+
databaseMode: 'single',
|
|
8
|
+
enableCompanyFeature: false
|
|
9
|
+
},
|
|
10
|
+
config: {},
|
|
11
|
+
...overrides
|
|
12
|
+
};
|
|
13
|
+
return {
|
|
14
|
+
isCompanyFeatureEnabled: jest.fn().mockReturnValue(options.bootstrapAppConfig?.enableCompanyFeature ?? false),
|
|
15
|
+
getOptions: jest.fn().mockReturnValue(options)
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
describe('StorageDataSourceProvider', ()=>{
|
|
19
|
+
afterEach(()=>{
|
|
20
|
+
jest.restoreAllMocks();
|
|
21
|
+
});
|
|
22
|
+
describe('construction', ()=>{
|
|
23
|
+
it('propagates bootstrap options from StorageConfigService into the parent data-source service', ()=>{
|
|
24
|
+
const configService = buildMockConfigService({
|
|
25
|
+
bootstrapAppConfig: {
|
|
26
|
+
databaseMode: 'multi-tenant',
|
|
27
|
+
enableCompanyFeature: true
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
const provider = new StorageDataSourceProvider(configService);
|
|
31
|
+
expect(provider.getDatabaseMode()).toBe('multi-tenant');
|
|
32
|
+
expect(provider.isMultiTenant()).toBe(true);
|
|
33
|
+
});
|
|
34
|
+
it('defaults to single database mode when bootstrap config is absent', ()=>{
|
|
35
|
+
const configService = buildMockConfigService({
|
|
36
|
+
bootstrapAppConfig: undefined
|
|
37
|
+
});
|
|
38
|
+
const provider = new StorageDataSourceProvider(configService);
|
|
39
|
+
expect(provider.getDatabaseMode()).toBe('single');
|
|
40
|
+
expect(provider.isMultiTenant()).toBe(false);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
describe('getEnableCompanyFeatureForTenant', ()=>{
|
|
44
|
+
it('uses the tenant-level override when provided', ()=>{
|
|
45
|
+
const configService = buildMockConfigService();
|
|
46
|
+
const provider = new StorageDataSourceProvider(configService);
|
|
47
|
+
const result = provider.getEnableCompanyFeatureForTenant({
|
|
48
|
+
id: 'tenant-1',
|
|
49
|
+
enableCompanyFeature: true
|
|
50
|
+
});
|
|
51
|
+
expect(result).toBe(true);
|
|
52
|
+
});
|
|
53
|
+
it('falls back to global config when tenant has no override', ()=>{
|
|
54
|
+
const configService = buildMockConfigService({
|
|
55
|
+
bootstrapAppConfig: {
|
|
56
|
+
databaseMode: 'single',
|
|
57
|
+
enableCompanyFeature: true
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
const provider = new StorageDataSourceProvider(configService);
|
|
61
|
+
const result = provider.getEnableCompanyFeatureForTenant({
|
|
62
|
+
id: 'tenant-1'
|
|
63
|
+
});
|
|
64
|
+
expect(result).toBe(true);
|
|
65
|
+
});
|
|
66
|
+
it('falls back to global config when no tenant is provided', ()=>{
|
|
67
|
+
const configService = buildMockConfigService({
|
|
68
|
+
bootstrapAppConfig: {
|
|
69
|
+
databaseMode: 'single',
|
|
70
|
+
enableCompanyFeature: false
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
const provider = new StorageDataSourceProvider(configService);
|
|
74
|
+
expect(provider.getEnableCompanyFeatureForTenant(undefined)).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
describe('getStorageEntities', ()=>{
|
|
78
|
+
it('returns company-scoped entities when company feature is enabled', async ()=>{
|
|
79
|
+
const configService = buildMockConfigService();
|
|
80
|
+
const provider = new StorageDataSourceProvider(configService);
|
|
81
|
+
const result = await provider.getStorageEntities(true);
|
|
82
|
+
expect(result).toEqual([
|
|
83
|
+
entities.FileManagerWithCompany,
|
|
84
|
+
entities.FolderWithCompany,
|
|
85
|
+
entities.StorageConfigWithCompany
|
|
86
|
+
]);
|
|
87
|
+
});
|
|
88
|
+
it('returns core entities when company feature is disabled', async ()=>{
|
|
89
|
+
const configService = buildMockConfigService();
|
|
90
|
+
const provider = new StorageDataSourceProvider(configService);
|
|
91
|
+
const result = await provider.getStorageEntities(false);
|
|
92
|
+
expect(result).toEqual([
|
|
93
|
+
entities.FileManager,
|
|
94
|
+
entities.Folder,
|
|
95
|
+
entities.StorageConfig
|
|
96
|
+
]);
|
|
97
|
+
});
|
|
98
|
+
it('falls back to the config service value when no explicit flag is passed', async ()=>{
|
|
99
|
+
const configService = buildMockConfigService({
|
|
100
|
+
bootstrapAppConfig: {
|
|
101
|
+
databaseMode: 'single',
|
|
102
|
+
enableCompanyFeature: true
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
const provider = new StorageDataSourceProvider(configService);
|
|
106
|
+
const result = await provider.getStorageEntities();
|
|
107
|
+
expect(result).toEqual([
|
|
108
|
+
entities.FileManagerWithCompany,
|
|
109
|
+
entities.FolderWithCompany,
|
|
110
|
+
entities.StorageConfigWithCompany
|
|
111
|
+
]);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
describe('createDataSourceFromConfig', ()=>{
|
|
115
|
+
it('resolves storage entities for the current tenant/config and delegates to the parent implementation', async ()=>{
|
|
116
|
+
const configService = buildMockConfigService({
|
|
117
|
+
bootstrapAppConfig: {
|
|
118
|
+
databaseMode: 'single',
|
|
119
|
+
enableCompanyFeature: false
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
const provider = new StorageDataSourceProvider(configService);
|
|
123
|
+
jest.spyOn(provider, 'getCurrentTenant').mockReturnValue(null);
|
|
124
|
+
const superCreateSpy = jest.spyOn(MultiTenantDataSourceService.prototype, 'createDataSourceFromConfig').mockResolvedValue({
|
|
125
|
+
isInitialized: true
|
|
126
|
+
});
|
|
127
|
+
const config = {
|
|
128
|
+
type: 'mysql',
|
|
129
|
+
host: 'localhost',
|
|
130
|
+
port: 3306,
|
|
131
|
+
username: 'root',
|
|
132
|
+
password: '',
|
|
133
|
+
database: 'test'
|
|
134
|
+
};
|
|
135
|
+
const result = await provider.createDataSourceFromConfig(config);
|
|
136
|
+
expect(superCreateSpy).toHaveBeenCalledWith(config, [
|
|
137
|
+
entities.FileManager,
|
|
138
|
+
entities.Folder,
|
|
139
|
+
entities.StorageConfig
|
|
140
|
+
]);
|
|
141
|
+
expect(result).toEqual({
|
|
142
|
+
isInitialized: true
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
it('uses the tenant enableCompanyFeature override to pick company-scoped entities', async ()=>{
|
|
146
|
+
const configService = buildMockConfigService({
|
|
147
|
+
bootstrapAppConfig: {
|
|
148
|
+
databaseMode: 'multi-tenant',
|
|
149
|
+
enableCompanyFeature: false
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
const provider = new StorageDataSourceProvider(configService);
|
|
153
|
+
jest.spyOn(provider, 'getCurrentTenant').mockReturnValue({
|
|
154
|
+
id: 'tenant-1',
|
|
155
|
+
enableCompanyFeature: true
|
|
156
|
+
});
|
|
157
|
+
const superCreateSpy = jest.spyOn(MultiTenantDataSourceService.prototype, 'createDataSourceFromConfig').mockResolvedValue({
|
|
158
|
+
isInitialized: true
|
|
159
|
+
});
|
|
160
|
+
const config = {
|
|
161
|
+
type: 'mysql',
|
|
162
|
+
database: 'tenant_db'
|
|
163
|
+
};
|
|
164
|
+
await provider.createDataSourceFromConfig(config);
|
|
165
|
+
expect(superCreateSpy).toHaveBeenCalledWith(config, [
|
|
166
|
+
entities.FileManagerWithCompany,
|
|
167
|
+
entities.FolderWithCompany,
|
|
168
|
+
entities.StorageConfigWithCompany
|
|
169
|
+
]);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
});
|