Skip to content

Commit ddd9803

Browse files
#RI-4132 - fix database service test
1 parent 6df66e0 commit ddd9803

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

redisinsight/api/src/modules/database/database.service.spec.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { InternalServerErrorException, NotFoundException, ServiceUnavailableException } from '@nestjs/common';
22
import { Test, TestingModule } from '@nestjs/testing';
33
import { EventEmitter2 } from '@nestjs/event-emitter';
4-
import { omit, forIn, get, update } from 'lodash';
5-
import { plainToClass } from 'class-transformer';
4+
import { omit, get, update } from 'lodash';
65

76
import { classToClass } from 'src/utils';
87
import {
@@ -33,7 +32,7 @@ describe('DatabaseService', () => {
3332
'sshOptions.passphrase',
3433
'sshOptions.privateKey',
3534
'sentinelMaster.password',
36-
]
35+
];
3736

3837
beforeEach(async () => {
3938
jest.clearAllMocks();
@@ -129,18 +128,21 @@ describe('DatabaseService', () => {
129128

130129
describe('update', () => {
131130
it('should update existing database and send analytics event', async () => {
132-
expect(await service.update(
131+
databaseRepository.update.mockReturnValue({
132+
...mockDatabase,
133+
port: 6380,
134+
password: 'password',
135+
provider: 'LOCALHOST',
136+
});
137+
138+
await expect(await service.update(
133139
mockDatabase.id,
134140
{ password: 'password', port: 6380 } as UpdateDatabaseDto,
135141
true,
136-
)).toEqual(mockDatabase);
142+
)).toEqual({ ...mockDatabase, port: 6380, password: 'password' });
137143
expect(analytics.sendInstanceEditedEvent).toHaveBeenCalledWith(
138144
mockDatabase,
139-
{
140-
...mockDatabase,
141-
password: 'password',
142-
port: 6380,
143-
},
145+
{ ...mockDatabase, port: 6380, password: 'password' },
144146
true,
145147
);
146148
});
@@ -199,48 +201,48 @@ describe('DatabaseService', () => {
199201
it('should return multiple databases without Standalone secrets', async () => {
200202
databaseRepository.get.mockResolvedValueOnce(mockDatabaseWithTlsAuth);
201203

202-
expect(await service.export([mockDatabaseWithTlsAuth.id], false)).toEqual([classToClass(ExportDatabase,omit(mockDatabaseWithTlsAuth, 'password'))]);
203-
})
204+
expect(await service.export([mockDatabaseWithTlsAuth.id], false)).toEqual([classToClass(ExportDatabase, omit(mockDatabaseWithTlsAuth, 'password'))]);
205+
});
204206

205207
it('should return multiple databases without SSH secrets', async () => {
206208
// remove SSH secrets
207-
const mockDatabaseWithSshPrivateKeyTemp = {...mockDatabaseWithSshPrivateKey}
209+
const mockDatabaseWithSshPrivateKeyTemp = { ...mockDatabaseWithSshPrivateKey };
208210
exportSecurityFields.forEach((field) => {
209-
if(get(mockDatabaseWithSshPrivateKeyTemp, field)) {
210-
update(mockDatabaseWithSshPrivateKeyTemp, field, () => null)
211+
if (get(mockDatabaseWithSshPrivateKeyTemp, field)) {
212+
update(mockDatabaseWithSshPrivateKeyTemp, field, () => null);
211213
}
212-
})
214+
});
213215

214216
databaseRepository.get.mockResolvedValueOnce(mockDatabaseWithSshPrivateKey);
215-
expect(await service.export([mockDatabaseWithSshPrivateKey.id], false)).toEqual([classToClass(ExportDatabase, mockDatabaseWithSshPrivateKeyTemp)])
216-
})
217+
expect(await service.export([mockDatabaseWithSshPrivateKey.id], false)).toEqual([classToClass(ExportDatabase, mockDatabaseWithSshPrivateKeyTemp)]);
218+
});
217219

218220
it('should return multiple databases without Sentinel secrets', async () => {
219221
// remove secrets
220-
const mockSentinelDatabaseWithTlsAuthTemp = {...mockSentinelDatabaseWithTlsAuth}
222+
const mockSentinelDatabaseWithTlsAuthTemp = { ...mockSentinelDatabaseWithTlsAuth };
221223
exportSecurityFields.forEach((field) => {
222-
if(get(mockSentinelDatabaseWithTlsAuthTemp, field)) {
223-
update(mockSentinelDatabaseWithTlsAuthTemp, field, () => null)
224+
if (get(mockSentinelDatabaseWithTlsAuthTemp, field)) {
225+
update(mockSentinelDatabaseWithTlsAuthTemp, field, () => null);
224226
}
225-
})
227+
});
226228

227229
databaseRepository.get.mockResolvedValue(mockSentinelDatabaseWithTlsAuth);
228230

229-
expect(await service.export([mockSentinelDatabaseWithTlsAuth.id], false)).toEqual([classToClass(ExportDatabase, omit(mockSentinelDatabaseWithTlsAuthTemp, 'password'))])
231+
expect(await service.export([mockSentinelDatabaseWithTlsAuth.id], false)).toEqual([classToClass(ExportDatabase, omit(mockSentinelDatabaseWithTlsAuthTemp, 'password'))]);
230232
});
231233

232234
it('should return multiple databases with secrets', async () => {
233235
// Standalone
234236
databaseRepository.get.mockResolvedValueOnce(mockDatabaseWithTls);
235-
expect(await service.export([mockDatabaseWithTls.id], true)).toEqual([classToClass(ExportDatabase,mockDatabaseWithTls)]);
237+
expect(await service.export([mockDatabaseWithTls.id], true)).toEqual([classToClass(ExportDatabase, mockDatabaseWithTls)]);
236238

237239
// SSH
238240
databaseRepository.get.mockResolvedValueOnce(mockDatabaseWithSshPrivateKey);
239-
expect(await service.export([mockDatabaseWithSshPrivateKey.id], true)).toEqual([classToClass(ExportDatabase, mockDatabaseWithSshPrivateKey)])
241+
expect(await service.export([mockDatabaseWithSshPrivateKey.id], true)).toEqual([classToClass(ExportDatabase, mockDatabaseWithSshPrivateKey)]);
240242

241243
// Sentinel
242244
databaseRepository.get.mockResolvedValueOnce(mockSentinelDatabaseWithTlsAuth);
243-
expect(await service.export([mockSentinelDatabaseWithTlsAuth.id], true)).toEqual([classToClass(ExportDatabase, mockSentinelDatabaseWithTlsAuth)])
245+
expect(await service.export([mockSentinelDatabaseWithTlsAuth.id], true)).toEqual([classToClass(ExportDatabase, mockSentinelDatabaseWithTlsAuth)]);
244246
});
245247

246248
it('should ignore errors', async () => {

0 commit comments

Comments
 (0)