Skip to content

Commit a557d64

Browse files
committed
Add regression tests for change
1 parent 0b845af commit a557d64

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

server/lib/multipartRequest.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export default async function (
3030
Object.assign({}, options, { include_totals: true, page: 0 }),
3131
]);
3232

33-
// auth0 SDK v4 returns JSONApiResponse with .data property
3433
total = response.total || 0;
3534
pageCount = Math.ceil(total / perPage);
3635
const data = response[entity] || response || [];
@@ -42,8 +41,7 @@ export default async function (
4241
const response = await apiCall(client[entity], getter, [
4342
Object.assign({}, options, { page: page }),
4443
]);
45-
// auth0 SDK v4 returns JSONApiResponse with .data property
46-
const data = response.data || response || [];
44+
const data = response || [];
4745
data.forEach((item) => result.push(item));
4846
return null;
4947
};

tests/unit/server/groups-members-route.tests.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expect } from 'chai';
22
import { getServerData } from '../server';
33
import { getToken } from '../mocks/tokens';
4+
import * as auth0 from '../mocks/auth0';
45

56
describe('groups-members-route', async () => {
67
let server = null;
@@ -73,6 +74,15 @@ describe('groups-members-route', async () => {
7374

7475
it('should return members', async () => {
7576
const token = getToken('read:groups');
77+
auth0.get(`/api/v2/users/${encodeURIComponent(uid)}`, {
78+
user_id: uid,
79+
name: 'Test User',
80+
email: 'test@example.com',
81+
picture: 'https://example.com/pic.jpg',
82+
identities: [ { connection: 'Username-Password-Authentication', provider: 'auth0', user_id: 'some_user_id', isSocial: false } ],
83+
last_login: new Date().toISOString(),
84+
logins_count: 5
85+
});
7686
const options = {
7787
method: 'GET',
7888
url: `/api/groups/${guid}/members`,
@@ -85,8 +95,10 @@ describe('groups-members-route', async () => {
8595
expect(response.statusCode).to.be.equal(200);
8696
expect(response.result.users).to.be.a('array');
8797
expect(response.result.users[0].user_id).to.be.equal(uid);
88-
expect(response.result.users[1].user_id).to.be.equal('undefined');
98+
expect(response.result.users[0].identities).to.be.an('array');
99+
expect(response.result.users[0].identities[0].connection).to.be.a('string');
89100
// auth0 SDK v4 throws FetchError instead of APIError when user not found
101+
expect(response.result.users[1].user_id).to.be.equal('undefined');
90102
expect(response.result.users[1].name).to.be.equal('<Error: FetchError>');
91103
expect(response.result.total).to.be.equal(2);
92104
});
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { expect } from 'chai';
2+
import apiCall from '../../../../server/lib/apiCall';
3+
4+
describe('apiCall', () => {
5+
it('should unwrap .data from the SDK v4 response', async () => {
6+
const user = { user_id: 'auth0|123', identities: [ { connection: 'Username-Password-Authentication' } ] };
7+
const method = () => Promise.resolve({ data: user, status: 200, headers: {} });
8+
9+
const result = await apiCall({}, method, [ { id: 'auth0|123' } ]);
10+
11+
expect(result).to.deep.equal(user);
12+
expect(result.identities).to.be.an('array');
13+
expect(result.identities[0].connection).to.equal('Username-Password-Authentication');
14+
});
15+
16+
it('should throw non-retryable errors immediately', async () => {
17+
const error = new Error('Not found');
18+
error.originalError = { status: 404 };
19+
const method = () => Promise.reject(error);
20+
21+
try {
22+
await apiCall({}, method, []);
23+
expect.fail('Should have thrown');
24+
} catch (err) {
25+
expect(err).to.equal(error);
26+
}
27+
});
28+
29+
it('should not retry when rate limit reset exceeds max retry timeout', async () => {
30+
const ratelimitReset = Math.round(Date.now() / 1000) + 100;
31+
const error = new Error('Rate limit');
32+
error.originalError = {
33+
status: 429,
34+
response: { header: { 'x-ratelimit-reset': ratelimitReset } }
35+
};
36+
const method = () => Promise.reject(error);
37+
38+
try {
39+
await apiCall({}, method, []);
40+
expect.fail('Should have thrown');
41+
} catch (err) {
42+
expect(err).to.equal(error);
43+
}
44+
});
45+
46+
it('should retry on 429 and resolve on success', async function() {
47+
this.timeout(5000);
48+
49+
const user = { user_id: 'auth0|123', identities: [ { connection: 'Username-Password-Authentication' } ] };
50+
const ratelimitReset = Math.round(Date.now() / 1000);
51+
const error = new Error('Rate limit');
52+
error.originalError = {
53+
status: 429,
54+
response: { header: { 'x-ratelimit-reset': ratelimitReset } }
55+
};
56+
57+
let callCount = 0;
58+
const method = () => {
59+
callCount++;
60+
if (callCount === 1) return Promise.reject(error);
61+
return Promise.resolve({ data: user, status: 200, headers: {} });
62+
};
63+
64+
const result = await apiCall({}, method, [], 2);
65+
66+
expect(result).to.deep.equal(user);
67+
expect(callCount).to.equal(2);
68+
});
69+
});

0 commit comments

Comments
 (0)