Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Filter API error handling and types #141

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/api/services/api-filter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { APIService } from './api.service';
import AbortController from 'abort-controller';

const handleFailover = (
userId: string,
reason: string,
configuration: Configuration,
err?: Error
Expand All @@ -18,7 +17,6 @@ const handleFailover = (
if (configuration.failoverStrategy === FailoverStrategy.throw) {
throw err;
}

return {
policy: {
action: configuration.failoverStrategy,
Expand Down Expand Up @@ -52,9 +50,9 @@ export const APIFilterService = {
);
} catch (e) {
if (isTimeoutError(e)) {
return handleFailover(options.user.id, 'timeout', configuration, e);
return handleFailover('timeout', configuration, e);
} else if (e instanceof InternalServerError) {
return handleFailover(options.user.id, 'server error', configuration);
return handleFailover('server error', configuration, e);
} else {
throw e;
}
Expand Down
6 changes: 6 additions & 0 deletions src/payload/models/filter_payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ export interface FilterPayload {
ip: string;
headers: IncomingHttpHeaders | { [key: string]: string | boolean };
};
matching_user_id?: string;
params?: {
email?: string;
phone?: string;
username?: string;
}
}
2 changes: 1 addition & 1 deletion src/payload/models/risk_payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface RiskPayload {
status: string;
user: {
id: string;
email: string;
email?: string;
registered_at?: string;
traits?: object;
name?: string;
Expand Down
57 changes: 49 additions & 8 deletions test/api/services/api-filter.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { APIFilterService } from '../../../src/api/api.module';
import { Configuration } from '../../../src/configuraton';
import { FailoverStrategy } from '../../../src/failover/models';
import type { FilterPayload } from '../../../src/payload/payload.module';
import MockDate from 'mockdate';
import fetchMock from 'fetch-mock';
Expand All @@ -16,11 +17,8 @@ describe('APIFilterService', () => {
const sampleRequestData: FilterPayload = {
event: '$login',
request_token: 'token',
status: '$succeeded',
user: {
id: 'userid',
email: 'myemail',
},
status: '$failed',
matching_user_id: 'userid',
context: {
ip: '8.8.8.8',
headers: {},
Expand All @@ -38,7 +36,7 @@ describe('APIFilterService', () => {
const config = new Configuration({
apiSecret: 'test',
overrideFetch: fetch,
logger: { info: () => {} },
logger: { info: () => { } },
});

const response = await (<any>(
Expand All @@ -58,7 +56,7 @@ describe('APIFilterService', () => {
const config = new Configuration({
apiSecret: 'test',
overrideFetch: fetch,
logger: { info: () => {} },
logger: { info: () => { } },
});

const response = await APIFilterService.call(sampleRequestData, config);
Expand All @@ -82,7 +80,7 @@ describe('APIFilterService', () => {
const config = new Configuration({
apiSecret: 'test',
overrideFetch: fetch,
logger: { info: () => {} },
logger: { info: () => { } },
});

const response = await (<any>(
Expand All @@ -97,5 +95,48 @@ describe('APIFilterService', () => {
);
expect(response.policy).toHaveProperty('name', 'Block Users from X');
});

it('handles timeouts with failover strategy', async () => {
jest.useFakeTimers();
const fetch = fetchMock.sandbox().mock('*', {
action: 'deny',
device: {
token: 'device_token',
},
policy: {
id: 'q-rbeMzBTdW2Fd09sbz55A',
revision_id: 'pke4zqO2TnqVr-NHJOAHEg',
name: 'Block Users from X',
},
}, {
delay: 2000
});

const config = new Configuration({
apiSecret: 'test',
overrideFetch: fetch,
failoverStrategy: FailoverStrategy.allow,
timeout: 1000,
logger: { info: () => { } },
});

const filterCall = (<any>(
APIFilterService.call(sampleRequestData, config)
));
jest.runAllTimers();

const response = await filterCall

expect(response).toEqual({
policy: {
action: 'allow',
},
action: 'allow',
failover: true,
failover_reason: 'timeout'
});

jest.useRealTimers();
});
});
});