@workos-inc/node 5.2.0 → 6.0.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,120 +12,114 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- const axios_1 = __importDefault(require("axios"));
16
- const axios_mock_adapter_1 = __importDefault(require("axios-mock-adapter"));
15
+ const jest_fetch_mock_1 = __importDefault(require("jest-fetch-mock"));
16
+ const test_utils_1 = require("../common/utils/test-utils");
17
17
  const workos_1 = require("../workos");
18
18
  const generate_link_invalid_json_1 = __importDefault(require("./fixtures/generate-link-invalid.json"));
19
19
  const generate_link_json_1 = __importDefault(require("./fixtures/generate-link.json"));
20
20
  const generate_portal_link_intent_interface_1 = require("./interfaces/generate-portal-link-intent.interface");
21
- const mock = new axios_mock_adapter_1.default(axios_1.default);
22
21
  const workos = new workos_1.WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
23
22
  describe('Portal', () => {
24
- afterEach(() => mock.resetHistory());
23
+ beforeEach(() => jest_fetch_mock_1.default.resetMocks());
25
24
  describe('generateLink', () => {
26
25
  describe('with a valid organization', () => {
27
26
  describe('with the sso intent', () => {
28
27
  it('returns an Admin Portal link', () => __awaiter(void 0, void 0, void 0, function* () {
29
- mock
30
- .onPost('/portal/generate_link', {
31
- intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.SSO,
32
- organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
33
- return_url: 'https://www.example.com',
34
- })
35
- .replyOnce(201, generate_link_json_1.default);
28
+ (0, test_utils_1.fetchOnce)(generate_link_json_1.default, { status: 201 });
36
29
  const { link } = yield workos.portal.generateLink({
37
30
  intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.SSO,
38
31
  organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
39
32
  returnUrl: 'https://www.example.com',
40
33
  });
34
+ expect((0, test_utils_1.fetchBody)()).toEqual({
35
+ intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.SSO,
36
+ organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
37
+ return_url: 'https://www.example.com',
38
+ });
41
39
  expect(link).toEqual('https://id.workos.com/portal/launch?secret=secret');
42
40
  }));
43
41
  });
44
42
  describe('with the domain_verification intent', () => {
45
43
  it('returns an Admin Portal link', () => __awaiter(void 0, void 0, void 0, function* () {
46
- mock
47
- .onPost('/portal/generate_link', {
48
- intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.DomainVerification,
49
- organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
50
- return_url: 'https://www.example.com',
51
- })
52
- .replyOnce(201, generate_link_json_1.default);
44
+ (0, test_utils_1.fetchOnce)(generate_link_json_1.default, { status: 201 });
53
45
  const { link } = yield workos.portal.generateLink({
54
46
  intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.DomainVerification,
55
47
  organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
56
48
  returnUrl: 'https://www.example.com',
57
49
  });
50
+ expect((0, test_utils_1.fetchBody)()).toEqual({
51
+ intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.DomainVerification,
52
+ organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
53
+ return_url: 'https://www.example.com',
54
+ });
58
55
  expect(link).toEqual('https://id.workos.com/portal/launch?secret=secret');
59
56
  }));
60
57
  });
61
58
  describe('with the dsync intent', () => {
62
59
  it('returns an Admin Portal link', () => __awaiter(void 0, void 0, void 0, function* () {
63
- mock
64
- .onPost('/portal/generate_link', {
65
- intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.DSync,
66
- organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
67
- return_url: 'https://www.example.com',
68
- })
69
- .reply(201, generate_link_json_1.default);
60
+ (0, test_utils_1.fetchOnce)(generate_link_json_1.default, { status: 201 });
70
61
  const { link } = yield workos.portal.generateLink({
71
62
  intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.DSync,
72
63
  organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
73
64
  returnUrl: 'https://www.example.com',
74
65
  });
66
+ expect((0, test_utils_1.fetchBody)()).toEqual({
67
+ intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.DSync,
68
+ organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
69
+ return_url: 'https://www.example.com',
70
+ });
75
71
  expect(link).toEqual('https://id.workos.com/portal/launch?secret=secret');
76
72
  }));
77
73
  });
78
74
  describe('with the `audit_logs` intent', () => {
79
75
  it('returns an Admin Portal link', () => __awaiter(void 0, void 0, void 0, function* () {
80
- mock
81
- .onPost('/portal/generate_link', {
82
- intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.AuditLogs,
83
- organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
84
- return_url: 'https://www.example.com',
85
- })
86
- .reply(201, generate_link_json_1.default);
76
+ (0, test_utils_1.fetchOnce)(generate_link_json_1.default, { status: 201 });
87
77
  const { link } = yield workos.portal.generateLink({
88
78
  intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.AuditLogs,
89
79
  organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
90
80
  returnUrl: 'https://www.example.com',
91
81
  });
82
+ expect((0, test_utils_1.fetchBody)()).toEqual({
83
+ intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.AuditLogs,
84
+ organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
85
+ return_url: 'https://www.example.com',
86
+ });
92
87
  expect(link).toEqual('https://id.workos.com/portal/launch?secret=secret');
93
88
  }));
94
89
  });
95
90
  describe('with the `log_streams` intent', () => {
96
91
  it('returns an Admin Portal link', () => __awaiter(void 0, void 0, void 0, function* () {
97
- mock
98
- .onPost('/portal/generate_link', {
99
- intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.LogStreams,
100
- organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
101
- return_url: 'https://www.example.com',
102
- })
103
- .reply(201, generate_link_json_1.default);
92
+ (0, test_utils_1.fetchOnce)(generate_link_json_1.default, { status: 201 });
104
93
  const { link } = yield workos.portal.generateLink({
105
94
  intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.LogStreams,
106
95
  organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
107
96
  returnUrl: 'https://www.example.com',
108
97
  });
98
+ expect((0, test_utils_1.fetchBody)()).toEqual({
99
+ intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.LogStreams,
100
+ organization: 'org_01EHQMYV6MBK39QC5PZXHY59C3',
101
+ return_url: 'https://www.example.com',
102
+ });
109
103
  expect(link).toEqual('https://id.workos.com/portal/launch?secret=secret');
110
104
  }));
111
105
  });
112
106
  });
113
107
  describe('with an invalid organization', () => {
114
108
  it('throws an error', () => __awaiter(void 0, void 0, void 0, function* () {
115
- mock
116
- .onPost('/portal/generate_link', {
117
- intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.SSO,
118
- organization: 'bogus-id',
119
- return_url: 'https://www.example.com',
120
- })
121
- .reply(400, generate_link_invalid_json_1.default, {
122
- 'X-Request-ID': 'a-request-id',
109
+ (0, test_utils_1.fetchOnce)(generate_link_invalid_json_1.default, {
110
+ status: 400,
111
+ headers: { 'X-Request-ID': 'a-request-id' },
123
112
  });
124
113
  yield expect(workos.portal.generateLink({
125
114
  intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.SSO,
126
115
  organization: 'bogus-id',
127
116
  returnUrl: 'https://www.example.com',
128
117
  })).rejects.toThrowError('Could not find an organization with the id, bogus-id.');
118
+ expect((0, test_utils_1.fetchBody)()).toEqual({
119
+ intent: generate_portal_link_intent_interface_1.GeneratePortalLinkIntent.SSO,
120
+ organization: 'bogus-id',
121
+ return_url: 'https://www.example.com',
122
+ });
129
123
  }));
130
124
  });
131
125
  });
@@ -12,11 +12,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- const axios_1 = __importDefault(require("axios"));
16
- const axios_mock_adapter_1 = __importDefault(require("axios-mock-adapter"));
15
+ const jest_fetch_mock_1 = __importDefault(require("jest-fetch-mock"));
16
+ const test_utils_1 = require("../common/utils/test-utils");
17
17
  const workos_1 = require("../workos");
18
18
  const interfaces_1 = require("./interfaces");
19
19
  describe('SSO', () => {
20
+ beforeEach(() => jest_fetch_mock_1.default.resetMocks());
20
21
  const connectionResponse = {
21
22
  object: 'connection',
22
23
  id: 'conn_123',
@@ -145,104 +146,66 @@ describe('SSO', () => {
145
146
  describe('getProfileAndToken', () => {
146
147
  describe('with all information provided', () => {
147
148
  it('sends a request to the WorkOS api for a profile', () => __awaiter(void 0, void 0, void 0, function* () {
148
- const mock = new axios_mock_adapter_1.default(axios_1.default);
149
- const expectedBody = new URLSearchParams({
150
- client_id: 'proj_123',
151
- client_secret: 'sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU',
152
- code: 'authorization_code',
153
- grant_type: 'authorization_code',
154
- });
155
- expectedBody.sort();
156
- mock.onPost('/sso/token').replyOnce((config) => {
157
- const actualBody = new URLSearchParams(config.data);
158
- actualBody.sort();
159
- if (actualBody.toString() === expectedBody.toString()) {
160
- return [
161
- 200,
162
- {
163
- access_token: '01DMEK0J53CVMC32CK5SE0KZ8Q',
164
- profile: {
165
- id: 'prof_123',
166
- idp_id: '123',
167
- organization_id: 'org_123',
168
- connection_id: 'conn_123',
169
- connection_type: 'OktaSAML',
170
- email: 'foo@test.com',
171
- first_name: 'foo',
172
- last_name: 'bar',
173
- groups: ['Admins', 'Developers'],
174
- raw_attributes: {
175
- email: 'foo@test.com',
176
- first_name: 'foo',
177
- last_name: 'bar',
178
- groups: ['Admins', 'Developers'],
179
- },
180
- },
181
- },
182
- ];
183
- }
184
- return [404];
149
+ (0, test_utils_1.fetchOnce)({
150
+ access_token: '01DMEK0J53CVMC32CK5SE0KZ8Q',
151
+ profile: {
152
+ id: 'prof_123',
153
+ idp_id: '123',
154
+ organization_id: 'org_123',
155
+ connection_id: 'conn_123',
156
+ connection_type: 'OktaSAML',
157
+ email: 'foo@test.com',
158
+ first_name: 'foo',
159
+ last_name: 'bar',
160
+ groups: ['Admins', 'Developers'],
161
+ raw_attributes: {
162
+ email: 'foo@test.com',
163
+ first_name: 'foo',
164
+ last_name: 'bar',
165
+ groups: ['Admins', 'Developers'],
166
+ },
167
+ },
185
168
  });
186
169
  const workos = new workos_1.WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
187
170
  const { accessToken, profile } = yield workos.sso.getProfileAndToken({
188
171
  code: 'authorization_code',
189
172
  clientId: 'proj_123',
190
173
  });
191
- expect(mock.history.post.length).toBe(1);
192
- const { data, headers } = mock.history.post[0];
193
- expect(data).toMatchSnapshot();
194
- expect(headers).toMatchSnapshot();
174
+ expect(jest_fetch_mock_1.default.mock.calls.length).toEqual(1);
175
+ expect((0, test_utils_1.fetchBody)()).toMatchSnapshot();
176
+ expect((0, test_utils_1.fetchHeaders)()).toMatchSnapshot();
195
177
  expect(accessToken).toBe('01DMEK0J53CVMC32CK5SE0KZ8Q');
196
178
  expect(profile).toMatchSnapshot();
197
179
  }));
198
180
  });
199
181
  describe('without a groups attribute', () => {
200
182
  it('sends a request to the WorkOS api for a profile', () => __awaiter(void 0, void 0, void 0, function* () {
201
- const mock = new axios_mock_adapter_1.default(axios_1.default);
202
- const expectedBody = new URLSearchParams({
203
- client_id: 'proj_123',
204
- client_secret: 'sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU',
205
- code: 'authorization_code',
206
- grant_type: 'authorization_code',
207
- });
208
- expectedBody.sort();
209
- mock.onPost('/sso/token').replyOnce((config) => {
210
- const actualBody = new URLSearchParams(config.data);
211
- actualBody.sort();
212
- if (actualBody.toString() === expectedBody.toString()) {
213
- return [
214
- 200,
215
- {
216
- access_token: '01DMEK0J53CVMC32CK5SE0KZ8Q',
217
- profile: {
218
- id: 'prof_123',
219
- idp_id: '123',
220
- organization_id: 'org_123',
221
- connection_id: 'conn_123',
222
- connection_type: 'OktaSAML',
223
- email: 'foo@test.com',
224
- first_name: 'foo',
225
- last_name: 'bar',
226
- raw_attributes: {
227
- email: 'foo@test.com',
228
- first_name: 'foo',
229
- last_name: 'bar',
230
- },
231
- },
232
- },
233
- ];
234
- }
235
- return [404];
183
+ (0, test_utils_1.fetchOnce)({
184
+ access_token: '01DMEK0J53CVMC32CK5SE0KZ8Q',
185
+ profile: {
186
+ id: 'prof_123',
187
+ idp_id: '123',
188
+ organization_id: 'org_123',
189
+ connection_id: 'conn_123',
190
+ connection_type: 'OktaSAML',
191
+ email: 'foo@test.com',
192
+ first_name: 'foo',
193
+ last_name: 'bar',
194
+ raw_attributes: {
195
+ email: 'foo@test.com',
196
+ first_name: 'foo',
197
+ last_name: 'bar',
198
+ },
199
+ },
236
200
  });
237
201
  const workos = new workos_1.WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
238
202
  const { accessToken, profile } = yield workos.sso.getProfileAndToken({
239
203
  code: 'authorization_code',
240
204
  clientId: 'proj_123',
241
205
  });
242
- expect(mock.history.post.length).toBe(1);
243
- const { data, headers } = mock.history.post[0];
244
- expect(data).toMatchSnapshot();
245
- expect(headers).toMatchSnapshot();
206
+ expect(jest_fetch_mock_1.default.mock.calls.length).toEqual(1);
207
+ expect((0, test_utils_1.fetchBody)()).toMatchSnapshot();
208
+ expect((0, test_utils_1.fetchHeaders)()).toMatchSnapshot();
246
209
  expect(accessToken).toBe('01DMEK0J53CVMC32CK5SE0KZ8Q');
247
210
  expect(profile).toMatchSnapshot();
248
211
  }));
@@ -250,12 +213,7 @@ describe('SSO', () => {
250
213
  });
251
214
  describe('getProfile', () => {
252
215
  it('calls the `/sso/profile` endpoint with the provided access token', () => __awaiter(void 0, void 0, void 0, function* () {
253
- const mock = new axios_mock_adapter_1.default(axios_1.default);
254
- mock
255
- .onGet('/sso/profile', {
256
- accessToken: 'access_token',
257
- })
258
- .replyOnce(200, {
216
+ (0, test_utils_1.fetchOnce)({
259
217
  id: 'prof_123',
260
218
  idp_id: '123',
261
219
  organization_id: 'org_123',
@@ -276,35 +234,33 @@ describe('SSO', () => {
276
234
  const profile = yield workos.sso.getProfile({
277
235
  accessToken: 'access_token',
278
236
  });
279
- expect(mock.history.get.length).toBe(1);
280
- const { headers } = mock.history.get[0];
281
- expect(headers === null || headers === void 0 ? void 0 : headers.Authorization).toBe(`Bearer access_token`);
237
+ expect(jest_fetch_mock_1.default.mock.calls.length).toEqual(1);
238
+ expect((0, test_utils_1.fetchHeaders)()).toMatchObject({
239
+ Authorization: 'Bearer access_token',
240
+ });
282
241
  expect(profile.id).toBe('prof_123');
283
242
  }));
284
243
  });
285
244
  describe('deleteConnection', () => {
286
245
  it('sends request to delete a Connection', () => __awaiter(void 0, void 0, void 0, function* () {
287
- const mock = new axios_mock_adapter_1.default(axios_1.default);
288
- mock.onDelete('/connections/conn_123').replyOnce(200, {});
246
+ (0, test_utils_1.fetchOnce)();
289
247
  const workos = new workos_1.WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
290
248
  yield workos.sso.deleteConnection('conn_123');
291
- expect(mock.history.delete[0].url).toEqual('/connections/conn_123');
249
+ expect((0, test_utils_1.fetchURL)()).toContain('/connections/conn_123');
292
250
  }));
293
251
  });
294
252
  describe('getConnection', () => {
295
253
  it(`requests a Connection`, () => __awaiter(void 0, void 0, void 0, function* () {
296
- const mock = new axios_mock_adapter_1.default(axios_1.default);
297
- mock.onGet('/connections/conn_123').replyOnce(200, connectionResponse);
254
+ (0, test_utils_1.fetchOnce)(connectionResponse);
298
255
  const workos = new workos_1.WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
299
256
  const subject = yield workos.sso.getConnection('conn_123');
300
- expect(mock.history.get[0].url).toEqual('/connections/conn_123');
257
+ expect((0, test_utils_1.fetchURL)()).toContain('/connections/conn_123');
301
258
  expect(subject.connectionType).toEqual('OktaSAML');
302
259
  }));
303
260
  });
304
261
  describe('listConnections', () => {
305
262
  it(`requests a list of Connections`, () => __awaiter(void 0, void 0, void 0, function* () {
306
- const mock = new axios_mock_adapter_1.default(axios_1.default);
307
- mock.onGet('/connections').replyOnce(200, {
263
+ (0, test_utils_1.fetchOnce)({
308
264
  data: [connectionResponse],
309
265
  list_metadata: {},
310
266
  });
@@ -312,7 +268,7 @@ describe('SSO', () => {
312
268
  const subject = yield workos.sso.listConnections({
313
269
  organizationId: 'org_1234',
314
270
  });
315
- expect(mock.history.get[0].url).toEqual('/connections');
271
+ expect((0, test_utils_1.fetchURL)()).toContain('/connections');
316
272
  expect(subject.data).toHaveLength(1);
317
273
  }));
318
274
  });