|
| 1 | +import { pick } from 'lodash'; |
| 2 | +import { DatabaseImportService } from 'src/modules/database-import/database-import.service'; |
| 3 | +import { |
| 4 | + mockDatabase, |
| 5 | + mockDatabaseImportAnalytics, |
| 6 | + mockDatabaseImportFile, |
| 7 | + mockDatabaseImportResponse, |
| 8 | + MockType, |
| 9 | +} from 'src/__mocks__'; |
| 10 | +import { DatabaseRepository } from 'src/modules/database/repositories/database.repository'; |
| 11 | +import { DatabaseImportAnalytics } from 'src/modules/database-import/database-import.analytics'; |
| 12 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 13 | +import { ConnectionType } from 'src/modules/database/entities/database.entity'; |
| 14 | +import { BadRequestException, ForbiddenException } from '@nestjs/common'; |
| 15 | +import { ValidationError } from 'class-validator'; |
| 16 | +import { |
| 17 | + NoDatabaseImportFileProvidedException, SizeLimitExceededDatabaseImportFileException, |
| 18 | + UnableToParseDatabaseImportFileException, |
| 19 | +} from 'src/modules/database-import/exceptions'; |
| 20 | + |
| 21 | +describe('DatabaseImportService', () => { |
| 22 | + let service: DatabaseImportService; |
| 23 | + let databaseRepository: MockType<DatabaseRepository>; |
| 24 | + let analytics: MockType<DatabaseImportAnalytics>; |
| 25 | + let validatoSpy; |
| 26 | + |
| 27 | + beforeEach(async () => { |
| 28 | + jest.clearAllMocks(); |
| 29 | + |
| 30 | + const module: TestingModule = await Test.createTestingModule({ |
| 31 | + providers: [ |
| 32 | + DatabaseImportService, |
| 33 | + { |
| 34 | + provide: DatabaseRepository, |
| 35 | + useFactory: jest.fn(() => ({ |
| 36 | + create: jest.fn().mockResolvedValue(mockDatabase), |
| 37 | + })), |
| 38 | + }, |
| 39 | + { |
| 40 | + provide: DatabaseImportAnalytics, |
| 41 | + useFactory: mockDatabaseImportAnalytics, |
| 42 | + }, |
| 43 | + ], |
| 44 | + }).compile(); |
| 45 | + |
| 46 | + service = await module.get(DatabaseImportService); |
| 47 | + databaseRepository = await module.get(DatabaseRepository); |
| 48 | + analytics = await module.get(DatabaseImportAnalytics); |
| 49 | + validatoSpy = jest.spyOn(service['validator'], 'validateOrReject'); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('importDatabase', () => { |
| 53 | + beforeEach(() => { |
| 54 | + databaseRepository.create.mockRejectedValueOnce(new BadRequestException()); |
| 55 | + databaseRepository.create.mockRejectedValueOnce(new ForbiddenException()); |
| 56 | + validatoSpy.mockRejectedValueOnce([new ValidationError()]); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should import databases from json', async () => { |
| 60 | + const response = await service.import(mockDatabaseImportFile); |
| 61 | + |
| 62 | + expect(response).toEqual({ |
| 63 | + ...mockDatabaseImportResponse, |
| 64 | + errors: undefined, // errors omitted from response |
| 65 | + }); |
| 66 | + expect(analytics.sendImportResults).toHaveBeenCalledWith(mockDatabaseImportResponse); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should import databases from base64', async () => { |
| 70 | + const response = await service.import({ |
| 71 | + ...mockDatabaseImportFile, |
| 72 | + mimetype: 'binary/octet-stream', |
| 73 | + buffer: Buffer.from(mockDatabaseImportFile.buffer.toString('base64')), |
| 74 | + }); |
| 75 | + |
| 76 | + expect(response).toEqual({ |
| 77 | + ...mockDatabaseImportResponse, |
| 78 | + errors: undefined, // errors omitted from response |
| 79 | + }); |
| 80 | + expect(analytics.sendImportResults).toHaveBeenCalledWith(mockDatabaseImportResponse); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should fail due to file was not provided', async () => { |
| 84 | + try { |
| 85 | + await service.import(undefined); |
| 86 | + fail(); |
| 87 | + } catch (e) { |
| 88 | + expect(e).toBeInstanceOf(NoDatabaseImportFileProvidedException); |
| 89 | + expect(e.message).toEqual('No import file provided'); |
| 90 | + expect(analytics.sendImportFailed) |
| 91 | + .toHaveBeenCalledWith(new NoDatabaseImportFileProvidedException('No import file provided')); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + it('should fail due to file exceeded size limitations', async () => { |
| 96 | + try { |
| 97 | + await service.import({ |
| 98 | + ...mockDatabaseImportFile, |
| 99 | + size: 10 * 1024 * 1024 + 1, |
| 100 | + }); |
| 101 | + fail(); |
| 102 | + } catch (e) { |
| 103 | + expect(e).toBeInstanceOf(SizeLimitExceededDatabaseImportFileException); |
| 104 | + expect(e.message).toEqual('Import file is too big. Maximum 10mb allowed'); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + it('should fail due to incorrect json', async () => { |
| 109 | + try { |
| 110 | + await service.import({ |
| 111 | + ...mockDatabaseImportFile, |
| 112 | + buffer: Buffer.from([0, 21]), |
| 113 | + }); |
| 114 | + fail(); |
| 115 | + } catch (e) { |
| 116 | + expect(e).toBeInstanceOf(UnableToParseDatabaseImportFileException); |
| 117 | + expect(e.message).toEqual(`Unable to parse ${mockDatabaseImportFile.originalname}`); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + it('should faile due to incorrect base64 + truncate filename', async () => { |
| 122 | + try { |
| 123 | + await service.import({ |
| 124 | + ...mockDatabaseImportFile, |
| 125 | + originalname: (new Array(1_000).fill(1)).join(''), |
| 126 | + mimetype: 'binary/octet-stream', |
| 127 | + buffer: Buffer.from([0, 21]), |
| 128 | + }); |
| 129 | + fail(); |
| 130 | + } catch (e) { |
| 131 | + expect(e).toBeInstanceOf(UnableToParseDatabaseImportFileException); |
| 132 | + expect(e.message).toEqual(`Unable to parse ${(new Array(50).fill(1)).join('')}...`); |
| 133 | + } |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('createDatabase', () => { |
| 138 | + it('should create standalone database', async () => { |
| 139 | + await service['createDatabase']({ |
| 140 | + ...mockDatabase, |
| 141 | + }); |
| 142 | + |
| 143 | + expect(databaseRepository.create).toHaveBeenCalledWith({ |
| 144 | + ...pick(mockDatabase, ['host', 'port', 'name', 'connectionType']), |
| 145 | + }); |
| 146 | + }); |
| 147 | + it('should create standalone with created name', async () => { |
| 148 | + await service['createDatabase']({ |
| 149 | + ...mockDatabase, |
| 150 | + name: undefined, |
| 151 | + }); |
| 152 | + |
| 153 | + expect(databaseRepository.create).toHaveBeenCalledWith({ |
| 154 | + ...pick(mockDatabase, ['host', 'port', 'name', 'connectionType']), |
| 155 | + name: `${mockDatabase.host}:${mockDatabase.port}`, |
| 156 | + }); |
| 157 | + }); |
| 158 | + it('should create cluster database', async () => { |
| 159 | + await service['createDatabase']({ |
| 160 | + ...mockDatabase, |
| 161 | + cluster: true, |
| 162 | + }); |
| 163 | + |
| 164 | + expect(databaseRepository.create).toHaveBeenCalledWith({ |
| 165 | + ...pick(mockDatabase, ['host', 'port', 'name']), |
| 166 | + connectionType: ConnectionType.CLUSTER, |
| 167 | + }); |
| 168 | + }); |
| 169 | + }); |
| 170 | +}); |
0 commit comments