Skip to content

Commit 9872849

Browse files
committed
simplify and unify the tests
1 parent a6d0eef commit 9872849

File tree

2 files changed

+55
-79
lines changed

2 files changed

+55
-79
lines changed

packages/cli/test/integration/commands/credentials.cmd.test.ts

Lines changed: 55 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import { createMember, createOwner } from '../shared/db/users';
1111

1212
const oclifConfig = new Config({ root: __dirname });
1313

14+
async function importCredential(argv: string[]) {
15+
const importer = new ImportCredentialsCommand(argv, oclifConfig);
16+
await importer.init();
17+
await importer.run();
18+
}
19+
1420
beforeAll(async () => {
1521
mockInstance(InternalHooks);
1622
mockInstance(LoadNodesAndCredentials);
@@ -26,28 +32,31 @@ afterAll(async () => {
2632
});
2733

2834
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();
3939

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+
});
5160
});
5261

5362
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
6069
// ACT
6170
//
6271
// 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+
]);
6976

7077
//
7178
// ASSERT
@@ -100,15 +107,10 @@ test('`import:credentials --userId ...` should fail if the credential exists alr
100107
const member = await createMember();
101108

102109
// 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+
]);
112114

113115
// making sure the import worked
114116
const before = {
@@ -126,21 +128,18 @@ test('`import:credentials --userId ...` should fail if the credential exists alr
126128
],
127129
});
128130

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-
140131
//
141132
// ACT
142133
//
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(
144143
`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}"`,
145144
);
146145

@@ -178,15 +177,10 @@ test("only update credential, don't create or update owner if `--userId` is not
178177
const member = await createMember();
179178

180179
// 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+
]);
190184

191185
// making sure the import worked
192186
const before = {
@@ -204,17 +198,13 @@ test("only update credential, don't create or update owner if `--userId` is not
204198
],
205199
});
206200

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-
214201
//
215202
// ACT
216203
//
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+
]);
218208

219209
//
220210
// ASSERT

packages/cli/test/integration/commands/import.cmd.test.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ test('import:workflow should import active workflow and deactivate it', async ()
3737
//
3838
const owner = await createOwner();
3939

40-
const before = {
41-
workflows: await getAllWorkflows(),
42-
sharings: await getAllSharedWorkflows(),
43-
};
44-
expect(before.workflows).toHaveLength(0);
45-
expect(before.sharings).toHaveLength(0);
46-
4740
//
4841
// ACT
4942
//
@@ -77,13 +70,6 @@ test('import:workflow should import active workflow from combined file and deact
7770
//
7871
const owner = await createOwner();
7972

80-
const before = {
81-
workflows: await getAllWorkflows(),
82-
sharings: await getAllSharedWorkflows(),
83-
};
84-
expect(before.workflows).toHaveLength(0);
85-
expect(before.sharings).toHaveLength(0);
86-
8773
//
8874
// ACT
8975
//

0 commit comments

Comments
 (0)