@@ -5,19 +5,37 @@ import { getRepositoryToken } from '@nestjs/typeorm';
5
5
import { Repository } from 'typeorm' ;
6
6
import {
7
7
mockCaCertificateRepository ,
8
- mockClientCertificateRepository , mockClusterDatabaseWithTlsAuth , mockClusterDatabaseWithTlsAuthEntity ,
8
+ mockClientCertificateRepository ,
9
+ mockClusterDatabaseWithTlsAuth ,
10
+ mockClusterDatabaseWithTlsAuthEntity ,
9
11
mockDatabase ,
10
12
mockDatabaseEntity ,
11
13
mockDatabaseId ,
12
14
mockDatabasePasswordEncrypted ,
13
15
mockDatabasePasswordPlain ,
14
16
mockDatabaseSentinelMasterPasswordEncrypted ,
15
17
mockDatabaseSentinelMasterPasswordPlain ,
16
- mockDatabaseWithTls , mockDatabaseWithTlsAuth ,
18
+ mockDatabaseWithSshBasic ,
19
+ mockDatabaseWithSshBasicEntity ,
20
+ mockDatabaseWithSshPrivateKey ,
21
+ mockDatabaseWithSshPrivateKeyEntity ,
22
+ mockDatabaseWithTls ,
23
+ mockDatabaseWithTlsAuth ,
17
24
mockDatabaseWithTlsAuthEntity ,
18
25
mockDatabaseWithTlsEntity ,
19
26
mockEncryptionService ,
20
- mockRepository , mockSentinelDatabaseWithTlsAuth , mockSentinelDatabaseWithTlsAuthEntity ,
27
+ mockRepository ,
28
+ mockSentinelDatabaseWithTlsAuth ,
29
+ mockSentinelDatabaseWithTlsAuthEntity ,
30
+ mockSshOptionsBasicEntity ,
31
+ mockSshOptionsPassphraseEncrypted ,
32
+ mockSshOptionsPassphrasePlain ,
33
+ mockSshOptionsPasswordEncrypted ,
34
+ mockSshOptionsPasswordPlain ,
35
+ mockSshOptionsPrivateKeyEncrypted , mockSshOptionsPrivateKeyEntity ,
36
+ mockSshOptionsPrivateKeyPlain ,
37
+ mockSshOptionsUsernameEncrypted ,
38
+ mockSshOptionsUsernamePlain ,
21
39
MockType ,
22
40
} from 'src/__mocks__' ;
23
41
import { EncryptionService } from 'src/modules/encryption/encryption.service' ;
@@ -26,6 +44,7 @@ import { DatabaseEntity } from 'src/modules/database/entities/database.entity';
26
44
import { CaCertificateRepository } from 'src/modules/certificate/repositories/ca-certificate.repository' ;
27
45
import { ClientCertificateRepository } from 'src/modules/certificate/repositories/client-certificate.repository' ;
28
46
import { cloneClassInstance } from 'src/utils' ;
47
+ import { SshOptionsEntity } from 'src/modules/ssh/entities/ssh-options.entity' ;
29
48
30
49
const listFields = [
31
50
'id' , 'name' , 'host' , 'port' , 'db' ,
@@ -36,6 +55,7 @@ describe('LocalDatabaseRepository', () => {
36
55
let service : LocalDatabaseRepository ;
37
56
let encryptionService : MockType < EncryptionService > ;
38
57
let repository : MockType < Repository < DatabaseEntity > > ;
58
+ let sshOptionsRepository : MockType < Repository < SshOptionsEntity > > ;
39
59
let caCertRepository : MockType < CaCertificateRepository > ;
40
60
let clientCertRepository : MockType < ClientCertificateRepository > ;
41
61
@@ -49,6 +69,10 @@ describe('LocalDatabaseRepository', () => {
49
69
provide : getRepositoryToken ( DatabaseEntity ) ,
50
70
useFactory : mockRepository ,
51
71
} ,
72
+ {
73
+ provide : getRepositoryToken ( SshOptionsEntity ) ,
74
+ useFactory : mockRepository ,
75
+ } ,
52
76
{
53
77
provide : EncryptionService ,
54
78
useFactory : mockEncryptionService ,
@@ -65,6 +89,7 @@ describe('LocalDatabaseRepository', () => {
65
89
} ) . compile ( ) ;
66
90
67
91
repository = await module . get ( getRepositoryToken ( DatabaseEntity ) ) ;
92
+ sshOptionsRepository = await module . get ( getRepositoryToken ( SshOptionsEntity ) ) ;
68
93
caCertRepository = await module . get ( CaCertificateRepository ) ;
69
94
clientCertRepository = await module . get ( ClientCertificateRepository ) ;
70
95
encryptionService = await module . get ( EncryptionService ) ;
@@ -83,7 +108,15 @@ describe('LocalDatabaseRepository', () => {
83
108
. calledWith ( mockDatabasePasswordEncrypted , jasmine . anything ( ) )
84
109
. mockResolvedValue ( mockDatabasePasswordPlain )
85
110
. calledWith ( mockDatabaseSentinelMasterPasswordEncrypted , jasmine . anything ( ) )
86
- . mockResolvedValue ( mockDatabaseSentinelMasterPasswordPlain ) ;
111
+ . mockResolvedValue ( mockDatabaseSentinelMasterPasswordPlain )
112
+ . calledWith ( mockSshOptionsUsernameEncrypted , jasmine . anything ( ) )
113
+ . mockResolvedValue ( mockSshOptionsUsernamePlain )
114
+ . calledWith ( mockSshOptionsPasswordEncrypted , jasmine . anything ( ) )
115
+ . mockResolvedValue ( mockSshOptionsPasswordPlain )
116
+ . calledWith ( mockSshOptionsPrivateKeyEncrypted , jasmine . anything ( ) )
117
+ . mockResolvedValue ( mockSshOptionsPrivateKeyPlain )
118
+ . calledWith ( mockSshOptionsPassphraseEncrypted , jasmine . anything ( ) )
119
+ . mockResolvedValue ( mockSshOptionsPassphrasePlain ) ;
87
120
when ( encryptionService . encrypt )
88
121
. calledWith ( mockDatabasePasswordPlain )
89
122
. mockResolvedValue ( {
@@ -94,6 +127,26 @@ describe('LocalDatabaseRepository', () => {
94
127
. mockResolvedValue ( {
95
128
data : mockDatabaseSentinelMasterPasswordEncrypted ,
96
129
encryption : mockDatabaseWithTlsAuthEntity . encryption ,
130
+ } )
131
+ . calledWith ( mockSshOptionsUsernamePlain )
132
+ . mockResolvedValue ( {
133
+ data : mockSshOptionsUsernameEncrypted ,
134
+ encryption : mockSshOptionsBasicEntity . encryption ,
135
+ } )
136
+ . calledWith ( mockSshOptionsPasswordPlain )
137
+ . mockResolvedValue ( {
138
+ data : mockSshOptionsPasswordEncrypted ,
139
+ encryption : mockSshOptionsBasicEntity . encryption ,
140
+ } )
141
+ . calledWith ( mockSshOptionsPrivateKeyPlain )
142
+ . mockResolvedValue ( {
143
+ data : mockSshOptionsPrivateKeyEncrypted ,
144
+ encryption : mockSshOptionsPrivateKeyEntity . encryption ,
145
+ } )
146
+ . calledWith ( mockSshOptionsPassphrasePlain )
147
+ . mockResolvedValue ( {
148
+ data : mockSshOptionsPassphraseEncrypted ,
149
+ encryption : mockSshOptionsPrivateKeyEntity . encryption ,
97
150
} ) ;
98
151
} ) ;
99
152
@@ -117,6 +170,24 @@ describe('LocalDatabaseRepository', () => {
117
170
expect ( clientCertRepository . get ) . not . toHaveBeenCalled ( ) ;
118
171
} ) ;
119
172
173
+ it ( 'should return standalone database model with ssh enabled (basic)' , async ( ) => {
174
+ repository . findOneBy . mockResolvedValue ( mockDatabaseWithSshBasicEntity ) ;
175
+ const result = await service . get ( mockDatabaseWithSshBasic . id ) ;
176
+
177
+ expect ( result ) . toEqual ( mockDatabaseWithSshBasic ) ;
178
+ expect ( caCertRepository . get ) . not . toHaveBeenCalled ( ) ;
179
+ expect ( clientCertRepository . get ) . not . toHaveBeenCalled ( ) ;
180
+ } ) ;
181
+
182
+ it ( 'should return standalone database model with ssh enabled (privateKey + passphrase)' , async ( ) => {
183
+ repository . findOneBy . mockResolvedValue ( mockDatabaseWithSshPrivateKeyEntity ) ;
184
+ const result = await service . get ( mockDatabaseWithSshPrivateKey . id ) ;
185
+
186
+ expect ( result ) . toEqual ( mockDatabaseWithSshPrivateKey ) ;
187
+ expect ( caCertRepository . get ) . not . toHaveBeenCalled ( ) ;
188
+ expect ( clientCertRepository . get ) . not . toHaveBeenCalled ( ) ;
189
+ } ) ;
190
+
120
191
it ( 'should return standalone model with ca tls' , async ( ) => {
121
192
repository . findOneBy . mockResolvedValue ( mockDatabaseWithTlsEntity ) ;
122
193
@@ -201,14 +272,51 @@ describe('LocalDatabaseRepository', () => {
201
272
202
273
describe ( 'update' , ( ) => {
203
274
it ( 'should update standalone database' , async ( ) => {
204
- const result = await service . update ( mockDatabaseId , mockDatabase ) ;
275
+ repository . merge . mockReturnValue ( mockDatabaseEntity ) ;
205
276
206
- expect ( result ) . toEqual ( mockDatabase ) ;
277
+ const result = await service . update ( mockDatabaseId , {
278
+ ...mockDatabase ,
279
+ caCert : null ,
280
+ clientCert : null ,
281
+ sshOptions : null ,
282
+ } ) ;
283
+
284
+ expect ( result ) . toEqual ( {
285
+ ...mockDatabase ,
286
+ caCert : null ,
287
+ clientCert : null ,
288
+ sshOptions : null ,
289
+ } ) ;
290
+ expect ( caCertRepository . create ) . not . toHaveBeenCalled ( ) ;
291
+ expect ( clientCertRepository . create ) . not . toHaveBeenCalled ( ) ;
292
+ expect ( sshOptionsRepository . createQueryBuilder ) . toHaveBeenCalled ( ) ;
293
+ } ) ;
294
+
295
+ it ( 'should update standalone database with ssh enabled (basic)' , async ( ) => {
296
+ repository . findOneBy . mockResolvedValue ( mockDatabaseWithSshBasicEntity ) ;
297
+ repository . merge . mockReturnValue ( mockDatabaseWithSshBasic ) ;
298
+
299
+ const result = await service . update ( mockDatabaseId , mockDatabaseWithSshBasic ) ;
300
+
301
+ expect ( result ) . toEqual ( mockDatabaseWithSshBasic ) ;
302
+ expect ( caCertRepository . create ) . not . toHaveBeenCalled ( ) ;
303
+ expect ( clientCertRepository . create ) . not . toHaveBeenCalled ( ) ;
304
+ } ) ;
305
+
306
+ it ( 'should update standalone database with ssh enabled (privateKey)' , async ( ) => {
307
+ repository . findOneBy . mockResolvedValue ( mockDatabaseWithSshPrivateKeyEntity ) ;
308
+ repository . merge . mockReturnValue ( mockDatabaseWithSshPrivateKey ) ;
309
+
310
+ const result = await service . update ( mockDatabaseId , mockDatabaseWithSshPrivateKey ) ;
311
+
312
+ expect ( result ) . toEqual ( mockDatabaseWithSshPrivateKey ) ;
207
313
expect ( caCertRepository . create ) . not . toHaveBeenCalled ( ) ;
208
314
expect ( clientCertRepository . create ) . not . toHaveBeenCalled ( ) ;
209
315
} ) ;
210
316
211
317
it ( 'should update standalone database (with existing certificates)' , async ( ) => {
318
+ repository . merge . mockReturnValue ( mockDatabaseWithTlsAuth ) ;
319
+ repository . findOneBy . mockResolvedValueOnce ( mockDatabaseWithTlsAuthEntity ) ;
212
320
repository . findOneBy . mockResolvedValueOnce ( mockDatabaseWithTlsAuthEntity ) ;
213
321
214
322
const result = await service . update ( mockDatabaseId , mockDatabaseWithTlsAuth ) ;
@@ -219,6 +327,8 @@ describe('LocalDatabaseRepository', () => {
219
327
} ) ;
220
328
221
329
it ( 'should update standalone database (and certificates)' , async ( ) => {
330
+ repository . merge . mockReturnValue ( mockDatabaseWithTlsAuth ) ;
331
+ repository . findOneBy . mockResolvedValueOnce ( mockDatabaseWithTlsAuthEntity ) ;
222
332
repository . findOneBy . mockResolvedValueOnce ( mockDatabaseWithTlsAuthEntity ) ;
223
333
224
334
const result = await service . update (
0 commit comments