Skip to content

Commit

Permalink
test: add anon token ratelimit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Oct 10, 2024
1 parent c0e9ad5 commit 8d443a6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
20 changes: 18 additions & 2 deletions test/tests/integration/ratelimit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ describe('rate limiter', () => {

await waitForProbesUpdate();

await client(GP_TOKENS_TABLE).insert({
await client(GP_TOKENS_TABLE).insert([{
name: 'test token',
user_created: '89da69bd-a236-4ab7-9c5d-b5f52ce09959',
value: 'Xj6kuKFEQ6zI60mr+ckHG7yQcIFGMJFzvtK9PBQ69y8=', // token: qz5kdukfcr3vggv3xbujvjwvirkpkkpx
});
}, {
name: 'anon token',
user_created: null,
value: '1CJTN06QAyM2JYA3r2FwaSytXEWg1r50xNlUyC1G98w=', // token: t6jy4n6nw5jdqxhs5wlkvw7tqsabt734
}]);
});


Expand Down Expand Up @@ -279,6 +283,18 @@ describe('rate limiter', () => {

expect(response.headers['retry-after']).to.equal('5');
});

it('should use hashed token as a key for anonymous tokens', async () => {
await requestAgent.post('/v1/measurements')
.set('Authorization', 'Bearer t6jy4n6nw5jdqxhs5wlkvw7tqsabt734')
.send({
type: 'ping',
target: 'jsdelivr.com',
}).expect(202) as Response;

const rateLimiterRes = await authenticatedPostRateLimiter.get(`1CJTN06QAyM2JYA3r2FwaSytXEWg1r50xNlUyC1G98w=`);
expect(rateLimiterRes?.remainingPoints).to.equal(249);
});
});

describe('access with credits', () => {
Expand Down
28 changes: 24 additions & 4 deletions test/tests/unit/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ describe('Auth', () => {
await clock.tickAsync(60_000);

const user1 = await auth.validate('hf2fnprguymlgliirdk7qv23664c2xcr', 'https://jsdelivr.com');
expect(user1).to.deep.equal({ userId: 'user1', scopes: [] });
expect(user1).to.deep.equal({
userId: 'user1',
scopes: [],
hashedToken: '/bSluuDrAPX9zIiZZ/hxEKARwOg+e//EdJgCFpmApbg=',
});

const user2 = await auth.validate('vumzijbzihrskmc2hj34yw22batpibmt', 'https://jsdelivr.com');
expect(user2).to.equal(null);

Expand All @@ -53,7 +58,12 @@ describe('Auth', () => {
const user1afterSync = await auth.validate('hf2fnprguymlgliirdk7qv23664c2xcr', 'https://jsdelivr.com');
expect(user1afterSync).to.equal(null);
const user2afterSync = await auth.validate('vumzijbzihrskmc2hj34yw22batpibmt', 'https://jsdelivr.com');
expect(user2afterSync).to.deep.equal({ userId: 'user2', scopes: [] });
expect(user2afterSync).to.deep.equal({
userId: 'user2',
scopes: [],
hashedToken: '8YZ2pZoGQxfOeEGvUUkagX1yizZckq3weL+IN0chvU0=',
});

auth.unscheduleSync();
});

Expand All @@ -72,7 +82,12 @@ describe('Auth', () => {
await auth.validate('hf2fnprguymlgliirdk7qv23664c2xcr', 'https://jsdelivr.com');
await auth.validate('hf2fnprguymlgliirdk7qv23664c2xcr', 'https://jsdelivr.com');

expect(user).to.deep.equal({ userId: 'user1', scopes: [] });
expect(user).to.deep.equal({
userId: 'user1',
scopes: [],
hashedToken: '/bSluuDrAPX9zIiZZ/hxEKARwOg+e//EdJgCFpmApbg=',
});

expect(selectStub.callCount).to.equal(1);
});

Expand All @@ -89,7 +104,12 @@ describe('Auth', () => {
await auth.validate('hf2fnprguymlgliirdk7qv23664c2xcr', 'https://jsdelivr.com');
await auth.validate('hf2fnprguymlgliirdk7qv23664c2xcr', 'https://jsdelivr.com');

expect(user).to.deep.equal({ userId: 'user1', scopes: [] });
expect(user).to.deep.equal({
userId: 'user1',
scopes: [],
hashedToken: '/bSluuDrAPX9zIiZZ/hxEKARwOg+e//EdJgCFpmApbg=',
});

expect(selectStub.callCount).to.equal(1);
});

Expand Down

0 comments on commit 8d443a6

Please sign in to comment.