Skip to content

Commit 9433f14

Browse files
author
Artem
committed
Utests final
1 parent d702ced commit 9433f14

27 files changed

+1817
-1215
lines changed

redisinsight/api/src/__mocks__/certificates.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ export const mockCaCertificateRepository = jest.fn(() => ({
4141
}));
4242

4343
export const mockCaCertificateService = jest.fn(() => ({
44-
getAll: jest.fn(),
45-
getOneById: jest.fn(),
44+
get: jest.fn().mockResolvedValue(mockCaCertificate),
4645
}));
4746

4847
// ================== Client Certificate ==================
@@ -85,6 +84,5 @@ export const mockClientCertificateRepository = jest.fn(() => ({
8584
}));
8685

8786
export const mockClientCertificateService = jest.fn(() => ({
88-
getAll: jest.fn(),
89-
getOneById: jest.fn(),
87+
get: jest.fn().mockResolvedValue(mockClientCertificate),
9088
}));

redisinsight/api/src/__mocks__/databases.ts

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { Database } from 'src/modules/database/models/database';
2-
import {
3-
mockCaCertificate,
4-
mockClientCertificate,
5-
} from 'src/__mocks__/certificates';
2+
import { mockCaCertificate, mockClientCertificate } from 'src/__mocks__/certificates';
63
import { SentinelMaster } from 'src/modules/redis-sentinel/models/sentinel-master';
74
import { ConnectionType, DatabaseEntity } from 'src/modules/database/entities/database.entity';
85
import { EncryptionStrategy } from 'src/modules/encryption/models';
96
import { mockIORedisClient } from 'src/__mocks__/redis';
7+
import { mockSentinelMasterDto } from 'src/__mocks__/redis-sentinel';
8+
import { pick } from 'lodash';
9+
import { mockRedisGeneralInfo } from 'src/modules/database/providers/database-info.provider.spec';
10+
import { ClientMetadata } from 'src/modules/redis/models/client-metadata';
11+
import { AppTool } from 'src/models';
12+
import { DatabaseOverview } from 'src/modules/database/models/database-overview';
1013

1114
export const mockDatabaseId = 'a77b23c1-7816-4ea4-b61f-d37795a0f805-db-id';
1215

@@ -21,8 +24,8 @@ export const mockDatabaseSentinelMasterPasswordPlain = 'some sentinel pass';
2124
export const mockDatabase = Object.assign(new Database(), {
2225
id: mockDatabaseId,
2326
name: 'database-name',
24-
host: 'localhost',
25-
port: 3679,
27+
host: '127.0.100.1',
28+
port: 6379,
2629
connectionType: ConnectionType.STANDALONE,
2730
});
2831

@@ -69,7 +72,7 @@ export const mockDatabaseWithTlsAuthEntity = Object.assign(new DatabaseEntity(),
6972
});
7073

7174
export const mockSentinelMaster = Object.assign(new SentinelMaster(), {
72-
name: 'master_group_name',
75+
name: 'mymaster',
7376
username: 'master_group_username',
7477
password: mockDatabaseSentinelMasterPasswordPlain,
7578
});
@@ -78,6 +81,7 @@ export const mockSentinelDatabaseWithTlsAuth = Object.assign(new Database(), {
7881
...mockDatabaseWithTlsAuth,
7982
sentinelMaster: mockSentinelMaster,
8083
connectionType: ConnectionType.SENTINEL,
84+
nodes: mockSentinelMasterDto.nodes,
8185
});
8286

8387
export const mockSentinelDatabaseWithTlsAuthEntity = Object.assign(new DatabaseEntity(), {
@@ -86,18 +90,62 @@ export const mockSentinelDatabaseWithTlsAuthEntity = Object.assign(new DatabaseE
8690
sentinelMasterUsername: mockSentinelMaster.username,
8791
sentinelMasterPassword: mockDatabaseSentinelMasterPasswordEncrypted,
8892
connectionType: ConnectionType.SENTINEL,
93+
nodes: JSON.stringify(mockSentinelDatabaseWithTlsAuth.nodes),
8994
});
95+
export const mockClusterNodes = [
96+
{
97+
host: '127.0.100.1',
98+
port: 6379,
99+
},
100+
{
101+
host: '127.0.100.2',
102+
port: 6379,
103+
},
104+
];
90105

91106
export const mockClusterDatabaseWithTlsAuth = Object.assign(new Database(), {
92107
...mockDatabaseWithTlsAuth,
93108
connectionType: ConnectionType.CLUSTER,
109+
nodes: mockClusterNodes,
94110
});
95111

96112
export const mockClusterDatabaseWithTlsAuthEntity = Object.assign(new DatabaseEntity(), {
97113
...mockDatabaseWithTlsAuthEntity,
98114
connectionType: ConnectionType.CLUSTER,
115+
nodes: JSON.stringify(mockClusterNodes),
99116
});
100117

118+
export const mockClientMetadata: ClientMetadata = {
119+
databaseId: mockDatabase.id,
120+
namespace: AppTool.Common,
121+
};
122+
123+
export const mockDatabaseOverview: DatabaseOverview = {
124+
version: '6.2.4',
125+
usedMemory: 1,
126+
totalKeys: 2,
127+
totalKeysPerDb: {
128+
db0: 1,
129+
},
130+
connectedClients: 1,
131+
opsPerSecond: 1,
132+
networkInKbps: 1,
133+
networkOutKbps: 1,
134+
cpuUsagePercentage: null,
135+
};
136+
137+
export const mockDatabaseRepository = jest.fn(() => ({
138+
exists: jest.fn().mockResolvedValue(true),
139+
get: jest.fn().mockResolvedValue(mockDatabase),
140+
create: jest.fn().mockResolvedValue(mockDatabase),
141+
update: jest.fn().mockResolvedValue(mockDatabase),
142+
delete: jest.fn(),
143+
list: jest.fn().mockResolvedValue([
144+
pick(mockDatabase, 'id', 'name'),
145+
pick(mockDatabase, 'id', 'name'),
146+
]),
147+
}));
148+
101149
export const mockDatabaseService = jest.fn(() => ({
102150
get: jest.fn().mockResolvedValue(mockDatabase),
103151
create: jest.fn().mockResolvedValue(mockDatabase),
@@ -107,3 +155,32 @@ export const mockDatabaseService = jest.fn(() => ({
107155
export const mockDatabaseConnectionService = jest.fn(() => ({
108156
getOrCreateClient: jest.fn().mockResolvedValue(mockIORedisClient),
109157
}));
158+
159+
export const mockDatabaseInfoProvider = jest.fn(() => ({
160+
isCluster: jest.fn(),
161+
isSentinel: jest.fn(),
162+
determineDatabaseModules: jest.fn(),
163+
determineSentinelMasterGroups: jest.fn().mockReturnValue([mockSentinelMasterDto]),
164+
determineClusterNodes: jest.fn().mockResolvedValue(mockClusterNodes),
165+
getRedisGeneralInfo: jest.fn().mockResolvedValueOnce(mockRedisGeneralInfo),
166+
}));
167+
168+
export const mockDatabaseOverviewProvider = jest.fn(() => ({
169+
getOverview: jest.fn().mockResolvedValue(mockDatabaseOverview),
170+
}));
171+
172+
export const mockDatabaseFactory = jest.fn(() => ({
173+
createDatabaseModel: jest.fn().mockResolvedValue(mockDatabase),
174+
createStandaloneDatabaseModel: jest.fn().mockResolvedValue(mockDatabase),
175+
createClusterDatabaseModel: jest.fn().mockResolvedValue(mockClusterDatabaseWithTlsAuth),
176+
createSentinelDatabaseModel: jest.fn().mockResolvedValue(mockSentinelDatabaseWithTlsAuth),
177+
}));
178+
179+
export const mockDatabaseAnalytics = jest.fn(() => ({
180+
sendInstanceListReceivedEvent: jest.fn(),
181+
sendConnectionFailedEvent: jest.fn(),
182+
sendInstanceAddedEvent: jest.fn(),
183+
sendInstanceAddFailedEvent: jest.fn(),
184+
sendInstanceEditedEvent: jest.fn(),
185+
sendInstanceDeletedEvent: jest.fn(),
186+
}));

redisinsight/api/src/__mocks__/errors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { ReplyError } from 'src/models';
22

3+
export const mockRedisNoAuthError: ReplyError = {
4+
name: 'ReplyError',
5+
command: 'AUTH',
6+
message: 'NOAUTH authentication is required',
7+
};
8+
39
export const mockRedisNoPermError: ReplyError = {
410
name: 'ReplyError',
511
command: 'GET',

redisinsight/api/src/__mocks__/redis-sentinel.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import { SentinelMaster, SentinelMasterStatus } from 'src/modules/redis-sentinel/models/sentinel-master';
2+
import { Endpoint } from 'src/common/models';
23

3-
export const mockSentinelMasterDto: SentinelMaster = {
4-
name: 'mymaster',
4+
export const mockSentinelMasterEndpoint: Endpoint = {
55
host: '127.0.0.1',
66
port: 6379,
7+
};
8+
9+
export const mockSentinelMasterDto: SentinelMaster = {
10+
name: 'mymaster',
11+
host: mockSentinelMasterEndpoint.host,
12+
port: mockSentinelMasterEndpoint.port,
713
numberOfSlaves: 1,
814
status: SentinelMasterStatus.Active,
915
nodes: [{
Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import IORedis from 'ioredis';
22

3-
export const mockIORedisClient = {
4-
...Object.create(IORedis.prototype),
3+
const getRedisCommanderMockFunctions = () => ({
54
sendCommand: jest.fn(),
65
info: jest.fn(),
76
monitor: jest.fn(),
@@ -13,6 +12,44 @@ export const mockIORedisClient = {
1312
unsubscribe: jest.fn(),
1413
punsubscribe: jest.fn(),
1514
publish: jest.fn(),
15+
cluster: jest.fn(),
16+
quit: jest.fn(),
17+
});
18+
19+
export const mockIORedisClient = {
20+
...Object.create(IORedis.prototype),
21+
...getRedisCommanderMockFunctions(),
22+
status: 'ready',
23+
options: {
24+
db: 0,
25+
},
26+
};
27+
28+
export const mockIOClusterNode1 = {
29+
...Object.create(IORedis.prototype),
30+
...getRedisCommanderMockFunctions(),
31+
status: 'ready',
32+
options: {
33+
host: '127.0.100.1',
34+
port: 6379,
35+
db: 0,
36+
},
37+
};
38+
39+
export const mockIOClusterNode2 = {
40+
...Object.create(IORedis.prototype),
41+
...getRedisCommanderMockFunctions(),
42+
status: 'ready',
43+
options: {
44+
host: '127.0.100.2',
45+
port: 6379,
46+
db: 0,
47+
},
48+
};
49+
50+
export const mockIORedisSentinel = {
51+
...Object.create(IORedis.prototype),
52+
...getRedisCommanderMockFunctions(),
1653
status: 'ready',
1754
options: {
1855
db: 0,
@@ -21,22 +58,21 @@ export const mockIORedisClient = {
2158

2259
export const mockIORedisCluster = {
2360
...Object.create(IORedis.Cluster.prototype),
61+
...getRedisCommanderMockFunctions(),
2462
isCluster: true,
25-
sendCommand: jest.fn(),
26-
info: jest.fn(),
27-
monitor: jest.fn(),
28-
disconnect: jest.fn(),
29-
duplicate: jest.fn(),
30-
call: jest.fn(),
31-
subscribe: jest.fn(),
32-
psubscribe: jest.fn(),
33-
unsubscribe: jest.fn(),
34-
punsubscribe: jest.fn(),
35-
publish: jest.fn(),
3663
status: 'ready',
37-
nodes: jest.fn().mockReturnValue([mockIORedisClient, mockIORedisClient]),
64+
nodes: jest.fn().mockReturnValue([mockIOClusterNode1, mockIOClusterNode2]),
3865
};
3966

4067
export const mockRedisService = jest.fn(() => ({
41-
createStandaloneClient: jest.fn(),
68+
getClientInstance: jest.fn().mockResolvedValue({
69+
client: mockIORedisClient,
70+
}),
71+
setClientInstance: jest.fn(),
72+
isClientConnected: jest.fn().mockReturnValue(true),
73+
connectToDatabaseInstance: jest.fn().mockResolvedValue(mockIORedisClient),
74+
createStandaloneClient: jest.fn().mockResolvedValue(mockIORedisClient),
75+
createSentinelClient: jest.fn().mockResolvedValue(mockIORedisSentinel),
76+
createClusterClient: jest.fn().mockResolvedValue(mockIORedisCluster),
77+
removeClientInstance: jest.fn(),
4278
}));

redisinsight/api/src/modules/cli/services/cli-analytics/cli-analytics.service.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export class CliAnalyticsService extends CommandTelemetryBaseService {
8888
databaseId,
8989
...(await this.getCommandAdditionalInfo(additionalData['command'])),
9090
...additionalData,
91+
command: additionalData['command']?.toUpperCase() || undefined,
9192
},
9293
);
9394
} catch (e) {
@@ -101,14 +102,15 @@ export class CliAnalyticsService extends CommandTelemetryBaseService {
101102
additionalData: object = {},
102103
): Promise<void> {
103104
try {
105+
const commandFromError = error?.command?.name?.toUpperCase() || undefined;
104106
this.sendEvent(
105107
TelemetryEvents.CliCommandErrorReceived,
106108
{
107109
databaseId,
108110
error: error?.name,
109-
command: error?.command?.name?.toUpperCase(),
110111
...(await this.getCommandAdditionalInfo(additionalData['command'])),
111112
...additionalData,
113+
command: additionalData['command']?.toUpperCase() || commandFromError,
112114
},
113115
);
114116
} catch (e) {
@@ -130,18 +132,20 @@ export class CliAnalyticsService extends CommandTelemetryBaseService {
130132
databaseId,
131133
...(await this.getCommandAdditionalInfo(additionalData['command'])),
132134
...additionalData,
135+
command: additionalData['command']?.toUpperCase() || undefined,
133136
},
134137
);
135138
}
136139
if (status === CommandExecutionStatus.Fail) {
140+
const commandFromError = error?.command?.name?.toUpperCase() || undefined;
137141
this.sendEvent(
138142
TelemetryEvents.CliCommandErrorReceived,
139143
{
140144
databaseId,
141145
error: error.name,
142-
command: error?.command?.name?.toUpperCase(),
143146
...(await this.getCommandAdditionalInfo(additionalData['command'])),
144147
...additionalData,
148+
command: additionalData['command']?.toUpperCase() || commandFromError,
145149
},
146150
);
147151
}

0 commit comments

Comments
 (0)