/
cli-sync.test.ts
164 lines (135 loc) · 4.26 KB
/
cli-sync.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { Polly } from '@pollyjs/core';
import NodeHttpAdapter from '@pollyjs/adapter-node-http';
import FSPersister from '@pollyjs/persister-fs';
import { loadProjectStructure } from '@jupiterone/integration-sdk-private-test-utils';
import { SynchronizationJobStatus } from '@jupiterone/integration-sdk-core';
import { createCli } from '../index';
import {
generateSynchronizationJob,
setupSynchronizerApi,
} from './util/synchronization';
import * as log from '../log';
import { createTestPolly } from './util/recording';
jest.mock('../log');
Polly.register(NodeHttpAdapter);
Polly.register(FSPersister);
let polly: Polly;
beforeEach(() => {
process.env.JUPITERONE_API_KEY = 'testing-key';
process.env.JUPITERONE_ACCOUNT = 'mochi';
polly = createTestPolly('sync-cli');
loadProjectStructure('synchronization');
jest.spyOn(process, 'exit').mockImplementation((code: number | undefined) => {
throw new Error(`Process exited with code ${code}`);
});
});
afterEach(() => {
delete process.env.JUPITERONE_API_KEY;
delete process.env.JUPITERONE_DEV;
polly.disconnect();
});
test('uploads data to the synchronization api and displays the results', async () => {
const job = generateSynchronizationJob();
setupSynchronizerApi({ polly, job, baseUrl: 'https://api.us.jupiterone.io' });
await createCli().parseAsync([
'node',
'j1-integration',
'sync',
'--integrationInstanceId',
'test',
]);
expect(log.displaySynchronizationResults).toHaveBeenCalledTimes(1);
expect(log.displaySynchronizationResults).toHaveBeenCalledWith({
...job,
status: SynchronizationJobStatus.FINALIZE_PENDING,
// We arrive at these numbers because of what
// was written to disk in the 'synchronization' project fixture
numEntitiesUploaded: 6,
numRelationshipsUploaded: 3,
});
});
test('hits dev urls if JUPITERONE_DEV environment variable is set', async () => {
process.env.JUPITERONE_DEV = 'true';
const job = generateSynchronizationJob();
setupSynchronizerApi({
polly,
job,
baseUrl: 'https://api.dev.jupiterone.io',
});
await createCli().parseAsync([
'node',
'j1-integration',
'sync',
'--integrationInstanceId',
'test',
]);
expect(log.displaySynchronizationResults).toHaveBeenCalledTimes(1);
expect(log.displaySynchronizationResults).toHaveBeenCalledWith({
...job,
status: SynchronizationJobStatus.FINALIZE_PENDING,
// We arrive at these numbers because of what
// was written to disk in the 'synchronization' project fixture
numEntitiesUploaded: 6,
numRelationshipsUploaded: 3,
});
});
test('hits different url if --api-base-url is set', async () => {
const job = generateSynchronizationJob();
setupSynchronizerApi({
polly,
job,
baseUrl: 'https://api.TEST.jupiterone.io',
});
await createCli().parseAsync([
'node',
'j1-integration',
'sync',
'--integrationInstanceId',
'test',
'--api-base-url',
'https://api.TEST.jupiterone.io',
]);
expect(log.displaySynchronizationResults).toHaveBeenCalledTimes(1);
expect(log.displaySynchronizationResults).toHaveBeenCalledWith({
...job,
status: SynchronizationJobStatus.FINALIZE_PENDING,
// We arrive at these numbers because of what
// was written to disk in the 'synchronization' project fixture
numEntitiesUploaded: 6,
numRelationshipsUploaded: 3,
});
});
test('throws an error if --api-base-url is set with --development', async () => {
const job = generateSynchronizationJob();
setupSynchronizerApi({
polly,
job,
baseUrl: 'https://api.TEST.jupiterone.io',
});
await expect(
createCli().parseAsync([
'node',
'j1-integration',
'sync',
'--integrationInstanceId',
'test',
'--development',
'--api-base-url',
'https://api.TEST.jupiterone.io',
]),
).rejects.toThrow(
'Invalid configuration supplied. Cannot specify both --api-base-url and --development(-d) flags.',
);
});
test('throws if JUPITERONE_API_KEY is not set', async () => {
delete process.env.JUPITERONE_API_KEY;
await expect(
createCli().parseAsync([
'node',
'j1-integration',
'sync',
'--integrationInstanceId',
'test',
]),
).rejects.toThrow('JUPITERONE_API_KEY environment variable must be set');
});