@@ -11,6 +11,12 @@ import { createMember, createOwner } from '../shared/db/users';
11
11
12
12
const oclifConfig = new Config ( { root : __dirname } ) ;
13
13
14
+ async function importCredential ( argv : string [ ] ) {
15
+ const importer = new ImportCredentialsCommand ( argv , oclifConfig ) ;
16
+ await importer . init ( ) ;
17
+ await importer . run ( ) ;
18
+ }
19
+
14
20
beforeAll ( async ( ) => {
15
21
mockInstance ( InternalHooks ) ;
16
22
mockInstance ( LoadNodesAndCredentials ) ;
@@ -26,28 +32,31 @@ afterAll(async () => {
26
32
} ) ;
27
33
28
34
test ( 'import:credentials should import a credential' , async ( ) => {
29
- await createOwner ( ) ;
30
- const before = await getAllCredentials ( ) ;
31
- expect ( before . length ) . toBe ( 0 ) ;
32
- const importer = new ImportCredentialsCommand (
33
- [ '--input=./test/integration/commands/importCredentials/credentials.json' ] ,
34
- oclifConfig ,
35
- ) ;
36
- const mockExit = jest . spyOn ( process , 'exit' ) . mockImplementation ( ( ) => {
37
- throw new Error ( 'process.exit' ) ;
38
- } ) ;
35
+ //
36
+ // ARRANGE
37
+ //
38
+ const owner = await createOwner ( ) ;
39
39
40
- await importer . init ( ) ;
41
- try {
42
- await importer . run ( ) ;
43
- } catch ( error ) {
44
- expect ( error . message ) . toBe ( 'process.exit' ) ;
45
- }
46
- const after = await getAllCredentials ( ) ;
47
- expect ( after . length ) . toBe ( 1 ) ;
48
- expect ( after [ 0 ] . name ) . toBe ( 'cred-aws-test' ) ;
49
- expect ( after [ 0 ] . id ) . toBe ( '123' ) ;
50
- mockExit . mockRestore ( ) ;
40
+ //
41
+ // ACT
42
+ //
43
+ await importCredential ( [
44
+ '--input=./test/integration/commands/importCredentials/credentials.json' ,
45
+ ] ) ;
46
+
47
+ //
48
+ // ASSERT
49
+ //
50
+ const after = {
51
+ credentials : await getAllCredentials ( ) ,
52
+ sharings : await getAllSharedCredentials ( ) ,
53
+ } ;
54
+ expect ( after ) . toMatchObject ( {
55
+ credentials : [ expect . objectContaining ( { id : '123' , name : 'cred-aws-test' } ) ] ,
56
+ sharings : [
57
+ expect . objectContaining ( { credentialsId : '123' , userId : owner . id , role : 'credential:owner' } ) ,
58
+ ] ,
59
+ } ) ;
51
60
} ) ;
52
61
53
62
test ( 'import:credentials should import a credential from separated files' , async ( ) => {
@@ -60,12 +69,10 @@ test('import:credentials should import a credential from separated files', async
60
69
// ACT
61
70
//
62
71
// import credential the first time, assigning it to the owner
63
- const importer = new ImportCredentialsCommand (
64
- [ '--separate' , '--input=./test/integration/commands/importCredentials/separate' ] ,
65
- oclifConfig ,
66
- ) ;
67
- await importer . init ( ) ;
68
- await importer . run ( ) ;
72
+ await importCredential ( [
73
+ '--separate' ,
74
+ '--input=./test/integration/commands/importCredentials/separate' ,
75
+ ] ) ;
69
76
70
77
//
71
78
// ASSERT
@@ -100,15 +107,10 @@ test('`import:credentials --userId ...` should fail if the credential exists alr
100
107
const member = await createMember ( ) ;
101
108
102
109
// import credential the first time, assigning it to the owner
103
- const importer1 = new ImportCredentialsCommand (
104
- [
105
- '--input=./test/integration/commands/importCredentials/credentials.json' ,
106
- `--userId=${ owner . id } ` ,
107
- ] ,
108
- oclifConfig ,
109
- ) ;
110
- await importer1 . init ( ) ;
111
- await importer1 . run ( ) ;
110
+ await importCredential ( [
111
+ '--input=./test/integration/commands/importCredentials/credentials.json' ,
112
+ `--userId=${ owner . id } ` ,
113
+ ] ) ;
112
114
113
115
// making sure the import worked
114
116
const before = {
@@ -126,21 +128,18 @@ test('`import:credentials --userId ...` should fail if the credential exists alr
126
128
] ,
127
129
} ) ;
128
130
129
- // Prepare the second import, while updating the name we try to assign the
130
- // credential to another user.
131
- const importer2 = new ImportCredentialsCommand (
132
- [
133
- '--input=./test/integration/commands/importCredentials/credentials-updated.json' ,
134
- `--userId=${ member . id } ` ,
135
- ] ,
136
- oclifConfig ,
137
- ) ;
138
- await importer2 . init ( ) ;
139
-
140
131
//
141
132
// ACT
142
133
//
143
- await expect ( importer2 . run ( ) ) . rejects . toThrowError (
134
+
135
+ // Import again while updating the name we try to assign the
136
+ // credential to another user.
137
+ await expect (
138
+ importCredential ( [
139
+ '--input=./test/integration/commands/importCredentials/credentials-updated.json' ,
140
+ `--userId=${ member . id } ` ,
141
+ ] ) ,
142
+ ) . rejects . toThrowError (
144
143
`The credential with id "123" is already owned by the user with the id "${ owner . id } ". It can't be re-owned by the user with the id "${ member . id } "` ,
145
144
) ;
146
145
@@ -178,15 +177,10 @@ test("only update credential, don't create or update owner if `--userId` is not
178
177
const member = await createMember ( ) ;
179
178
180
179
// import credential the first time, assigning it to a member
181
- const importer1 = new ImportCredentialsCommand (
182
- [
183
- '--input=./test/integration/commands/importCredentials/credentials.json' ,
184
- `--userId=${ member . id } ` ,
185
- ] ,
186
- oclifConfig ,
187
- ) ;
188
- await importer1 . init ( ) ;
189
- await importer1 . run ( ) ;
180
+ await importCredential ( [
181
+ '--input=./test/integration/commands/importCredentials/credentials.json' ,
182
+ `--userId=${ member . id } ` ,
183
+ ] ) ;
190
184
191
185
// making sure the import worked
192
186
const before = {
@@ -204,17 +198,13 @@ test("only update credential, don't create or update owner if `--userId` is not
204
198
] ,
205
199
} ) ;
206
200
207
- // prepare the second import, only updating the name and omitting `--userId`
208
- const importer2 = new ImportCredentialsCommand (
209
- [ '--input=./test/integration/commands/importCredentials/credentials-updated.json' ] ,
210
- oclifConfig ,
211
- ) ;
212
- await importer2 . init ( ) ;
213
-
214
201
//
215
202
// ACT
216
203
//
217
- await importer2 . run ( ) ;
204
+ // Import again only updating the name and omitting `--userId`
205
+ await importCredential ( [
206
+ '--input=./test/integration/commands/importCredentials/credentials-updated.json' ,
207
+ ] ) ;
218
208
219
209
//
220
210
// ASSERT
0 commit comments