-
Notifications
You must be signed in to change notification settings - Fork 23
/
workos.spec.ts
194 lines (165 loc) · 5.41 KB
/
workos.spec.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import axios from 'axios';
import MockAdapater from 'axios-mock-adapter';
import { WorkOS } from './workos';
import {
NoApiKeyProvidedException,
NotFoundException,
GenericServerException,
OauthException,
} from './common/exceptions';
const mock = new MockAdapater(axios);
describe('WorkOS', () => {
describe('constructor', () => {
const OLD_ENV = process.env;
beforeEach(() => {
jest.resetModules();
process.env = { ...OLD_ENV };
delete process.env.NODE_ENV;
});
afterEach(() => {
process.env = OLD_ENV;
});
describe('when no API key is provided', () => {
it('throws a NoApiKeyFoundException error', async () => {
expect(() => new WorkOS()).toThrowError(NoApiKeyProvidedException);
});
});
describe('when API key is provided with environment variable', () => {
it('initializes', async () => {
process.env.WORKOS_API_KEY = 'sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU';
expect(() => new WorkOS()).not.toThrow();
});
});
describe('when API key is provided with constructor', () => {
it('initializes', async () => {
expect(
() => new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU'),
).not.toThrow();
});
});
describe('with https option', () => {
it('sets baseURL', () => {
const workos = new WorkOS('foo', { https: false });
expect(workos.baseURL).toEqual('http://api.workos.com');
});
});
describe('with apiHostname option', () => {
it('sets baseURL', () => {
const workos = new WorkOS('foo', { apiHostname: 'localhost' });
expect(workos.baseURL).toEqual('https://localhost');
});
});
describe('with port option', () => {
it('sets baseURL', () => {
const workos = new WorkOS('foo', {
apiHostname: 'localhost',
port: 4000,
});
expect(workos.baseURL).toEqual('https://localhost:4000');
});
});
describe('when the `axios` option is provided', () => {
it('applies the configuration to the Axios client', async () => {
mock.onPost().reply(200, 'OK', { 'X-Request-ID': 'a-request-id' });
const workos = new WorkOS('sk_test', {
axios: {
headers: {
'X-My-Custom-Header': 'Hey there!',
},
},
});
await workos.post('/somewhere', {});
expect(mock.history.post[0].headers).toMatchObject({
'X-My-Custom-Header': 'Hey there!',
});
});
});
});
describe('post', () => {
describe('when the api responds with a 404', () => {
it('throws a NotFoundException', async () => {
const message = 'Not Found';
mock.onPost().reply(
404,
{
message,
},
{ 'X-Request-ID': 'a-request-id' },
);
const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
await expect(workos.post('/path', {})).rejects.toStrictEqual(
new NotFoundException({
message,
path: '/path',
requestID: 'a-request-id',
}),
);
});
it('preserves the error code, status, and message from the underlying response', async () => {
const message = 'The thing you are looking for is not here.';
const code = 'thing-not-found';
mock.onPost().reply(
404,
{
code,
message,
},
{ 'X-Request-ID': 'a-request-id' },
);
const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
await expect(workos.post('/path', {})).rejects.toMatchObject({
code,
message,
status: 404,
});
});
it('includes the path in the message if there is no message in the response', async () => {
const code = 'thing-not-found';
const path = '/path/to/thing/that-aint-there';
mock.onPost().reply(
404,
{
code,
},
{ 'X-Request-ID': 'a-request-id' },
);
const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
await expect(workos.post(path, {})).rejects.toMatchObject({
code,
message: `The requested path '${path}' could not be found.`,
status: 404,
});
});
});
describe('when the api responds with a 500 and no error/error_description', () => {
it('throws an GenericServerException', async () => {
mock.onPost().reply(
500,
{},
{
'X-Request-ID': 'a-request-id',
},
);
const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
await expect(workos.post('/path', {})).rejects.toStrictEqual(
new GenericServerException(500, undefined, 'a-request-id'),
);
});
});
describe('when the api responds with a 400 and an error/error_description', () => {
it('throws an OauthException', async () => {
mock.onPost().reply(
400,
{ error: 'error', error_description: 'error description' },
{
'X-Request-ID': 'a-request-id',
},
);
const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
await expect(workos.post('/path', {})).rejects.toStrictEqual(
new OauthException(400, 'a-request-id', 'error', 'error description'),
);
});
});
});
});