Skip to content

Commit fb2219d

Browse files
author
arthosofteq
authored
Merge pull request #1400 from RedisInsight/be/feature/RI-3238-UTests
Be/feature/ri 3238 u tests
2 parents 20e02c2 + 1e70a33 commit fb2219d

File tree

116 files changed

+4872
-3610
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+4872
-3610
lines changed
Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
getTCPEndpoints,
3-
} from 'src/utils/auto-discovery-helper';
4-
5-
const winNetstat = ''
1+
export const mockWinNetstat = ''
62
+ 'Proto Local Address Foreign Address State PID\n'
73
+ 'TCP 0.0.0.0:5000 0.0.0.0:0 LISTENING 13728\n'
84
+ 'TCP 0.0.0.0:6379 0.0.0.0:0 LISTENING 13728\n'
@@ -15,7 +11,7 @@ const winNetstat = ''
1511
+ 'TCP [::]:5000 [::]:0 LISTENING 6056\n'
1612
+ 'TCP *:* LISTENING 6056';
1713

18-
const linuxNetstat = ''
14+
export const mockLinuxNetstat = ''
1915
+ 'Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name \n'
2016
+ 'tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN - \n'
2117
+ 'tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN - \n'
@@ -30,7 +26,7 @@ const linuxNetstat = ''
3026
+ 'tcp6 0 0 ::1:6379 :::* LISTEN - \n';
3127

3228
/* eslint-disable max-len */
33-
const macNetstat = ''
29+
export const mockMacNetstat = ''
3430
+ 'Proto Recv-Q Send-Q Local Address Foreign Address (state) rhiwat shiwat pid epid state options\n'
3531
+ 'tcp4 0 0 10.55.1.235.5000 10.55.1.235.52217 FIN_WAIT_2 407280 146988 30555 0 0x2131 0x00000104\n'
3632
+ 'tcp4 0 0 10.55.1.235.6379 10.55.1.235.5001 CLOSE_WAIT 407682 146988 872 0 0x0122 0x00000008\n'
@@ -40,54 +36,7 @@ const macNetstat = ''
4036
+ 'tcp6 0 0 ::1.52167 ::1.5002 ESTABLISHED 406172 146808 31200 0 0x0102 0x00000008\n';
4137
/* eslint-enable max-len */
4238

43-
const getTCPEndpointsTests = [
44-
{
45-
name: 'win output',
46-
input: winNetstat.split('\n'),
47-
output: [
48-
{ host: 'localhost', port: 5000 },
49-
{ host: 'localhost', port: 6379 },
50-
{ host: 'localhost', port: 6380 },
51-
{ host: 'localhost', port: 135 },
52-
{ host: 'localhost', port: 445 },
53-
{ host: 'localhost', port: 808 },
54-
{ host: 'localhost', port: 2701 },
55-
],
56-
},
57-
{
58-
name: 'linux output',
59-
input: linuxNetstat.split('\n'),
60-
output: [
61-
{ host: 'localhost', port: 5000 },
62-
{ host: 'localhost', port: 6379 },
63-
{ host: 'localhost', port: 6380 },
64-
{ host: 'localhost', port: 28100 },
65-
{ host: 'localhost', port: 8100 },
66-
{ host: 'localhost', port: 8101 },
67-
{ host: 'localhost', port: 8102 },
68-
{ host: 'localhost', port: 8103 },
69-
{ host: 'localhost', port: 8200 },
70-
],
71-
},
72-
{
73-
name: 'mac output',
74-
input: macNetstat.split('\n'),
75-
output: [
76-
{ host: 'localhost', port: 5000 },
77-
{ host: 'localhost', port: 6379 },
78-
{ host: 'localhost', port: 6380 },
79-
{ host: 'localhost', port: 5002 },
80-
{ host: 'localhost', port: 52167 },
81-
],
82-
},
83-
];
84-
85-
describe('getTCP4Endpoints', () => {
86-
getTCPEndpointsTests.forEach((test) => {
87-
it(`Should return endpoints to test ${test.name}`, async () => {
88-
const result = getTCPEndpoints(test.input);
89-
90-
expect(result).toEqual(test.output);
91-
});
92-
});
93-
});
39+
export const mockAutodiscoveryEndpoint = {
40+
host: '127.0.0.1',
41+
port: 6379,
42+
};
Lines changed: 82 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,90 @@
1-
import {
2-
CaCertDto,
3-
ClientCertPairDto,
4-
} from 'src/modules/instances/dto/database-instance.dto';
5-
import { CaCertificateEntity } from 'src/modules/core/models/ca-certificate.entity';
6-
import { ClientCertificateEntity } from 'src/modules/core/models/client-certificate.entity';
7-
8-
export const mockCaCertDto: CaCertDto = {
1+
import { pick, omit } from 'lodash';
2+
import { CaCertificateEntity } from 'src/modules/certificate/entities/ca-certificate.entity';
3+
import { ClientCertificateEntity } from 'src/modules/certificate/entities/client-certificate.entity';
4+
import { CreateCaCertificateDto } from 'src/modules/certificate/dto/create.ca-certificate.dto';
5+
import { CreateClientCertificateDto } from 'src/modules/certificate/dto/create.client-certificate.dto';
6+
import { CaCertificate } from 'src/modules/certificate/models/ca-certificate';
7+
import { EncryptionStrategy } from 'src/modules/encryption/models';
8+
import { ClientCertificate } from 'src/modules/certificate/models/client-certificate';
9+
10+
// ================== CA Certificate ==================
11+
export const mockCaCertificateId = 'a77b23c1-7816-4ea4-b61f-d37795a0f805';
12+
13+
export const mockCaCertificateCertificateEncrypted = 'caCertificate.certificate_ENCRYPTED';
14+
15+
export const mockCaCertificateCertificatePlain = '-----BEGIN CERTIFICATE-----\nMIIDejCCAmKgAwIBAgIUehUr5AHdJM';
16+
17+
export const mockCaCertificate = Object.assign(new CaCertificate(), {
18+
id: mockCaCertificateId,
919
name: 'ca-cert',
10-
cert: '-----BEGIN CERTIFICATE-----\nMIIDejCCAmKgAwIBAgIUehUr5AHdJM',
11-
};
20+
certificate: mockCaCertificateCertificatePlain,
21+
});
1222

13-
export const mockClientCertDto: ClientCertPairDto = {
14-
name: 'client-cert',
15-
cert: '-----BEGIN CERTIFICATE-----\nMIIDejCCAmKgAwIBAgIUehUr5AHdJM',
16-
key: '-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAAM',
17-
};
18-
19-
export const mockCaCertEntity: CaCertificateEntity = {
20-
id: 'a77b23c1-7816-4ea4-b61f-d37795a0f805',
21-
name: mockCaCertDto.name,
22-
encryption: null,
23-
certificate: mockCaCertDto.cert,
24-
databases: [],
25-
};
26-
27-
export const mockClientCertEntity: ClientCertificateEntity = {
28-
id: 'a77b23c1-7816-4ea4-b61f-d37795a0f809',
29-
name: mockClientCertDto.name,
30-
encryption: null,
31-
certificate: mockClientCertDto.cert,
32-
key: mockClientCertDto.key,
33-
databases: [],
34-
};
35-
36-
export const mockCaCertificatesService = () => ({
23+
export const mockCreateCaCertificateDto = Object.assign(new CreateCaCertificateDto(), {
24+
...omit(mockCaCertificate, 'id'),
25+
});
26+
27+
export const mockCaCertificateEntity = Object.assign(new CaCertificateEntity(), {
28+
...mockCaCertificate,
29+
certificate: mockCaCertificateCertificateEncrypted,
30+
encryption: EncryptionStrategy.KEYTAR,
31+
});
32+
33+
export const mockCaCertificateRepository = jest.fn(() => ({
34+
get: jest.fn().mockResolvedValue(mockCaCertificate),
35+
list: jest.fn().mockResolvedValueOnce([
36+
pick(mockCaCertificate, 'id', 'name'),
37+
pick(mockCaCertificate, 'id', 'name'),
38+
]),
39+
create: jest.fn().mockResolvedValue(mockCaCertificate),
40+
delete: jest.fn().mockResolvedValue(undefined),
41+
}));
42+
43+
export const mockCaCertificateService = jest.fn(() => ({
3744
getAll: jest.fn(),
3845
getOneById: jest.fn(),
46+
}));
47+
48+
// ================== Client Certificate ==================
49+
export const mockClientCertificateId = 'a77b23c1-7816-4ea4-b61f-d37f2915f805';
50+
51+
export const mockClientCertificateCertificateEncrypted = 'clientCertificate.certificate_ENCRYPTED';
52+
53+
export const mockClientCertificateCertificatePlain = '-----BEGIN CERTIFICATE-----\nMICLIENTCERTIDejCCAmKgAwIB';
54+
55+
export const mockClientCertificateKeyEncrypted = 'clientCertificate.key_ENCRYPTED';
56+
57+
export const mockClientCertificateKeyPlain = '-----BEGIN PRIVATE KEY-----\nMICLIENTCERTIDejCCAmKgAwIB';
58+
59+
export const mockClientCertificate = Object.assign(new ClientCertificate(), {
60+
id: mockClientCertificateId,
61+
name: 'client-cert',
62+
certificate: mockClientCertificateCertificatePlain,
63+
key: mockClientCertificateKeyPlain,
3964
});
4065

41-
export const mockClientCertificatesService = () => ({
66+
export const mockCreateClientCertificateDto = Object.assign(new CreateClientCertificateDto(), {
67+
...omit(mockClientCertificate, 'id'),
68+
});
69+
70+
export const mockClientCertificateEntity = Object.assign(new ClientCertificateEntity(), {
71+
...mockClientCertificate,
72+
certificate: mockClientCertificateCertificateEncrypted,
73+
key: mockClientCertificateKeyEncrypted,
74+
encryption: EncryptionStrategy.KEYTAR,
75+
});
76+
77+
export const mockClientCertificateRepository = jest.fn(() => ({
78+
get: jest.fn().mockResolvedValue(mockClientCertificate),
79+
list: jest.fn().mockResolvedValueOnce([
80+
pick(mockClientCertificate, 'id', 'name'),
81+
pick(mockClientCertificate, 'id', 'name'),
82+
]),
83+
create: jest.fn().mockResolvedValue(mockClientCertificate),
84+
delete: jest.fn().mockResolvedValue(undefined),
85+
}));
86+
87+
export const mockClientCertificateService = jest.fn(() => ({
4288
getAll: jest.fn(),
4389
getOneById: jest.fn(),
44-
});
90+
}));

redisinsight/api/src/__mocks__/common.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ export const mockRedisClusterConsumer = () => ({
1919
getRedisClient: jest.fn(),
2020
});
2121

22+
export const mockQueryBuilderWhere = jest.fn().mockReturnThis();
2223
export const mockQueryBuilderGetOne = jest.fn();
2324
export const mockQueryBuilderGetMany = jest.fn();
2425
export const mockQueryBuilderGetManyRaw = jest.fn();
2526
export const mockQueryBuilderGetCount = jest.fn();
2627
export const mockQueryBuilderExecute = jest.fn();
2728
export const mockCreateQueryBuilder = jest.fn(() => ({
28-
where: jest.fn().mockReturnThis(),
29+
// where: jest.fn().mockReturnThis(),
30+
where: mockQueryBuilderWhere,
2931
update: jest.fn().mockReturnThis(),
3032
select: jest.fn().mockReturnThis(),
3133
set: jest.fn().mockReturnThis(),
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { when } from 'jest-when';
2+
import config from 'src/utils/config';
3+
4+
jest.mock('src/utils/config', jest.fn(
5+
() => jest.requireActual('src/utils/config') as object,
6+
));
7+
8+
const configGetSpy = jest.spyOn(config, 'get');
9+
10+
export const mockServerConfig = config.get('server');
11+
when(configGetSpy).calledWith('server').mockReturnValue(mockServerConfig);
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { Database } from 'src/modules/database/models/database';
2+
import {
3+
mockCaCertificate,
4+
mockClientCertificate,
5+
} from 'src/__mocks__/certificates';
6+
import { SentinelMaster } from 'src/modules/redis-sentinel/models/sentinel-master';
7+
import { ConnectionType, DatabaseEntity } from 'src/modules/database/entities/database.entity';
8+
import { EncryptionStrategy } from 'src/modules/encryption/models';
9+
import { mockIORedisClient } from 'src/__mocks__/redis';
10+
11+
export const mockDatabaseId = 'a77b23c1-7816-4ea4-b61f-d37795a0f805-db-id';
12+
13+
export const mockDatabasePasswordEncrypted = 'database.password_ENCRYPTED';
14+
15+
export const mockDatabasePasswordPlain = 'some pass';
16+
17+
export const mockDatabaseSentinelMasterPasswordEncrypted = 'database.sentinelMasterPassword_ENCRYPTED';
18+
19+
export const mockDatabaseSentinelMasterPasswordPlain = 'some sentinel pass';
20+
21+
export const mockDatabase = Object.assign(new Database(), {
22+
id: mockDatabaseId,
23+
name: 'database-name',
24+
host: 'localhost',
25+
port: 3679,
26+
connectionType: ConnectionType.STANDALONE,
27+
});
28+
29+
export const mockDatabaseEntity = Object.assign(new DatabaseEntity(), {
30+
...mockDatabase,
31+
encryption: null,
32+
});
33+
34+
export const mockDatabaseWithAuth = Object.assign(new Database(), {
35+
...mockDatabase,
36+
username: 'some username',
37+
password: mockDatabasePasswordPlain,
38+
});
39+
40+
export const mockDatabaseWithAuthEntity = Object.assign(new DatabaseEntity(), {
41+
...mockDatabaseWithAuth,
42+
password: mockDatabasePasswordEncrypted,
43+
encryption: EncryptionStrategy.KEYTAR,
44+
});
45+
46+
export const mockDatabaseWithTls = Object.assign(new Database(), {
47+
...mockDatabaseWithAuth,
48+
tls: true,
49+
verifyServerCert: true,
50+
tlsServername: 'some.local',
51+
caCert: mockCaCertificate,
52+
});
53+
54+
export const mockDatabaseWithTlsEntity = Object.assign(new DatabaseEntity(), {
55+
...mockDatabaseWithTls,
56+
password: mockDatabasePasswordEncrypted,
57+
encryption: EncryptionStrategy.KEYTAR,
58+
caCert: mockCaCertificate, // !not client ca entity since it managed on own repository
59+
});
60+
61+
export const mockDatabaseWithTlsAuth = Object.assign(new Database(), {
62+
...mockDatabaseWithTls,
63+
clientCert: mockClientCertificate,
64+
});
65+
66+
export const mockDatabaseWithTlsAuthEntity = Object.assign(new DatabaseEntity(), {
67+
...mockDatabaseWithTlsEntity,
68+
clientCert: mockClientCertificate, // !not client cert entity since it managed on own repository
69+
});
70+
71+
export const mockSentinelMaster = Object.assign(new SentinelMaster(), {
72+
name: 'master_group_name',
73+
username: 'master_group_username',
74+
password: mockDatabaseSentinelMasterPasswordPlain,
75+
});
76+
77+
export const mockSentinelDatabaseWithTlsAuth = Object.assign(new Database(), {
78+
...mockDatabaseWithTlsAuth,
79+
sentinelMaster: mockSentinelMaster,
80+
connectionType: ConnectionType.SENTINEL,
81+
});
82+
83+
export const mockSentinelDatabaseWithTlsAuthEntity = Object.assign(new DatabaseEntity(), {
84+
...mockDatabaseWithTlsAuthEntity,
85+
sentinelMasterName: mockSentinelMaster.name,
86+
sentinelMasterUsername: mockSentinelMaster.username,
87+
sentinelMasterPassword: mockDatabaseSentinelMasterPasswordEncrypted,
88+
connectionType: ConnectionType.SENTINEL,
89+
});
90+
91+
export const mockClusterDatabaseWithTlsAuth = Object.assign(new Database(), {
92+
...mockDatabaseWithTlsAuth,
93+
connectionType: ConnectionType.CLUSTER,
94+
});
95+
96+
export const mockClusterDatabaseWithTlsAuthEntity = Object.assign(new DatabaseEntity(), {
97+
...mockDatabaseWithTlsAuthEntity,
98+
connectionType: ConnectionType.CLUSTER,
99+
});
100+
101+
export const mockDatabaseService = jest.fn(() => ({
102+
get: jest.fn().mockResolvedValue(mockDatabase),
103+
create: jest.fn().mockResolvedValue(mockDatabase),
104+
list: jest.fn(),
105+
}));
106+
107+
export const mockDatabaseConnectionService = jest.fn(() => ({
108+
getOrCreateClient: jest.fn().mockResolvedValue(mockIORedisClient),
109+
}));

redisinsight/api/src/__mocks__/encryption.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1+
import { EncryptionStrategy } from 'src/modules/encryption/models';
2+
13
export const mockDataToEncrypt = 'stringtoencrypt';
24
export const mockKeytarPassword = 'somepassword';
5+
6+
export const mockEncryptionStrategy = EncryptionStrategy.KEYTAR;
7+
38
export const mockEncryptResult = {
49
data: '4a558dfef5c1abbdf745232614194ee9',
5-
encryption: 'KEYTAR',
10+
encryption: mockEncryptionStrategy,
611
};
712

8-
export const mockEncryptionService = () => ({
13+
export const mockEncryptionService = jest.fn(() => ({
914
getAvailableEncryptionStrategies: jest.fn(),
1015
encrypt: jest.fn(),
1116
decrypt: jest.fn(),
12-
});
17+
}));
1318

14-
export const mockEncryptionStrategy = jest.fn(() => ({
19+
export const mockEncryptionStrategyInstance = jest.fn(() => ({
1520
isAvailable: jest.fn(),
1621
encrypt: jest.fn(),
1722
decrypt: jest.fn(),

0 commit comments

Comments
 (0)