-
Notifications
You must be signed in to change notification settings - Fork 303
/
auth0-rest-client.tests.js
263 lines (225 loc) · 7.89 KB
/
auth0-rest-client.tests.js
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const { expect } = require('chai');
const nock = require('nock');
const { ArgumentError } = require('rest-facade');
const Auth0RestClient = require('../src/Auth0RestClient');
const API_URL = 'https://tenant.auth0.com';
describe('Auth0RestClient', () => {
before(function () {
this.providerMock = {
getAccessToken() {
return Promise.resolve('access_token');
},
};
});
it('should raise an error when no resource Url is provided', () => {
expect(() => {
new Auth0RestClient();
}).to.throw(ArgumentError, 'Must provide a Resource Url');
});
it('should raise an error when resource Url is invalid', () => {
expect(() => {
new Auth0RestClient('');
}).to.throw(ArgumentError, 'The provided Resource Url is invalid');
});
it('should raise an error when no options is provided', () => {
expect(() => {
new Auth0RestClient('/some-resource');
}).to.throw(ArgumentError, 'Must provide options');
});
describe('`get`', () => {
it('should encode params.id on `get` requests', function (done) {
nock(API_URL).get('/some-resource/auth0%7C1234').reply(200, { data: 'value' });
const options = {
headers: {},
};
const client = new Auth0RestClient(
`${API_URL}/some-resource/:id`,
options,
this.providerMock
);
client.get({ id: 'auth0|1234' }, (err, data) => {
expect(data).to.deep.equal({ data: 'value' });
done();
nock.cleanAll();
});
});
it('should not encode other params on `get` requests', function (done) {
nock(API_URL)
.get('/some-resource/1234?otherEncoded=auth0%7C6789&other=foobar')
.reply(200, { data: 'value' });
const options = {
headers: {},
};
const client = new Auth0RestClient(
`${API_URL}/some-resource/:id`,
options,
this.providerMock
);
client.get({ id: '1234', other: 'foobar', otherEncoded: 'auth0|6789' }, (err, data) => {
expect(err).to.be.null;
expect(data).to.deep.equal({ data: 'value' });
done();
nock.cleanAll();
});
});
it('should accept only a callback', function (done) {
nock(API_URL).get('/some-resource').reply(200, { data: 'value' });
const options = {
headers: {},
};
const client = new Auth0RestClient(`${API_URL}/some-resource`, options, this.providerMock);
client.get({}, (err, data) => {
expect(data).to.deep.equal({ data: 'value' });
done();
nock.cleanAll();
});
});
it('should return a promise if no callback is provided', async function () {
nock(API_URL).get('/some-resource').reply(200, { data: 'value' });
const options = {
headers: {},
};
const client = new Auth0RestClient(`${API_URL}/some-resource`, options, this.providerMock);
const returnValue = client.get();
expect(returnValue).instanceOf(Promise);
const data = await returnValue;
expect(data).to.deep.equal({ data: 'value' });
nock.cleanAll();
});
});
describe('`patch`', () => {
it('should encode params.id on `patch` requests', function (done) {
nock(API_URL).patch('/some-resource/auth0%7C1234%2F5678').reply(200);
const client = new Auth0RestClient(
`${API_URL}/some-resource/:id`,
{ headers: {} },
this.providerMock
);
client.patch({ id: 'auth0|1234/5678' }, { data: 'udpate ' }, (err) => {
expect(err).to.be.null;
done();
nock.cleanAll();
});
});
});
describe('`update`', () => {
it('should encode params.id on `update` requests', function (done) {
nock(API_URL).put('/some-resource/auth0%7C1234%2F5678').reply(200);
const client = new Auth0RestClient(
`${API_URL}/some-resource/:id`,
{ headers: {} },
this.providerMock
);
client.update({ id: 'auth0|1234/5678' }, { data: 'udpate ' }, (err) => {
expect(err).to.be.null;
done();
nock.cleanAll();
});
});
});
describe('`delete`', () => {
it('should encode params.id on `delete` requests', function (done) {
nock(API_URL).delete('/some-resource/auth0%7C1234%2F5678').reply(200);
const client = new Auth0RestClient(
`${API_URL}/some-resource/:id`,
{ headers: {} },
this.providerMock
);
client.delete({ id: 'auth0|1234/5678' }, (err) => {
expect(err).to.be.null;
done();
nock.cleanAll();
});
});
});
it('should return a promise if no callback is given', async function () {
nock(API_URL).get('/some-resource').reply(200, { data: 'value' });
const options = {
headers: {},
};
const client = new Auth0RestClient(`${API_URL}/some-resource`, options, this.providerMock);
const data = await client.getAll();
expect(data).to.deep.equal({ data: 'value' });
nock.cleanAll();
});
it('should accept a callback and handle errors', (done) => {
const providerMock = {
getAccessToken() {
return Promise.reject(new Error('Some Error'));
},
};
nock(API_URL).get('/some-resource').reply(500);
const options = {
headers: {},
};
const client = new Auth0RestClient(`${API_URL}/some-resource`, options, providerMock);
client.getAll((err) => {
expect(err).to.not.null;
expect(err.message).to.be.equal('Some Error');
done();
nock.cleanAll();
});
});
it('should set access token as Authorization header in options object', async function () {
nock(API_URL).get('/some-resource').reply(200);
const options = {
headers: {},
};
const client = new Auth0RestClient(`${API_URL}/some-resource`, options, this.providerMock);
await client.getAll();
expect(client.restClient.options.headers['Authorization']).to.be.equal('Bearer access_token');
nock.cleanAll();
});
it('should sanitize error when access token is in authorization header', function (done) {
nock(API_URL).get('/some-resource').reply(401);
const options = {
headers: {},
};
const client = new Auth0RestClient(`${API_URL}/some-resource`, options, this.providerMock);
client.getAll().catch((err) => {
const originalRequestHeader = err.originalError.response.request._header;
expect(originalRequestHeader.authorization).to.equal('[REDACTED]');
done();
nock.cleanAll();
});
});
it('should catch error when provider.getAccessToken throws an error', (done) => {
const providerMock = {
getAccessToken() {
return Promise.reject(new Error('Some Error'));
},
};
const client = new Auth0RestClient('/some-resource', {}, providerMock);
client.getAll().catch((err) => {
expect(err).to.not.null;
expect(err.message).to.be.equal('Some Error');
done();
});
});
it('should include response headers in promise response', async function () {
nock(API_URL).get('/some-resource').reply(200, { data: 'value' });
const options = {
includeResponseHeaders: true,
headers: {},
};
const client = new Auth0RestClient(`${API_URL}/some-resource`, options, this.providerMock);
const { data, headers } = await client.getAll();
expect(data).to.deep.equal({ data: 'value' });
expect(headers).to.deep.equal({ 'content-type': 'application/json' });
nock.cleanAll();
});
it('should include response headers in callback response', function (done) {
nock(API_URL).get('/some-resource').reply(200, { data: 'value' });
const options = {
includeResponseHeaders: true,
headers: {},
};
const client = new Auth0RestClient(`${API_URL}/some-resource`, options, this.providerMock);
client.getAll((err, { data, headers }) => {
expect(data).to.deep.equal({ data: 'value' });
expect(headers).to.deep.equal({ 'content-type': 'application/json' });
nock.cleanAll();
done();
});
});
});