-
Notifications
You must be signed in to change notification settings - Fork 106
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
[OTE-132] new compliance logic for comlink address-related endpoints #1048
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b6c3664
update rejectRestrictedCountries
dydxwill c566833
Merge branch 'main' of https://github.com/dydxprotocol/v4-chain into …
dydxwill 149491d
implement
dydxwill 2c9183b
add back no address check
dydxwill 8743a2c
Merge branch 'main' of https://github.com/dydxprotocol/v4-chain into …
dydxwill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
239 changes: 239 additions & 0 deletions
239
indexer/services/comlink/__tests__/lib/compliance-and-geo-check.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
import express from 'express'; | ||
|
||
import { BlockedCode, RequestMethod } from '../../src/types'; | ||
import Server from '../../src/request-helpers/server'; | ||
import { sendRequestToApp } from '../helpers/helpers'; | ||
import { complianceAndGeoCheck } from '../../src/lib/compliance-and-geo-check'; | ||
import { handleValidationErrors } from '../../src/request-helpers/error-handler'; | ||
import { checkSchema } from 'express-validator'; | ||
import { | ||
ComplianceStatus, | ||
ComplianceStatusTable, | ||
dbHelpers, | ||
testConstants, | ||
testMocks, | ||
} from '@dydxprotocol-indexer/postgres'; | ||
import request from 'supertest'; | ||
import { | ||
INDEXER_COMPLIANCE_BLOCKED_PAYLOAD, | ||
INDEXER_GEOBLOCKED_PAYLOAD, | ||
isRestrictedCountryHeaders, | ||
} from '@dydxprotocol-indexer/compliance'; | ||
import config from '../../src/config'; | ||
|
||
jest.mock('@dydxprotocol-indexer/compliance'); | ||
|
||
// Create a router to test the middleware with | ||
const router: express.Router = express.Router(); | ||
|
||
const restrictedHeaders = { | ||
'cf-ipcountry': 'US', | ||
}; | ||
|
||
const nonRestrictedHeaders = { | ||
'cf-ipcountry': 'SA', | ||
}; | ||
|
||
router.get( | ||
'/check-compliance-query', | ||
checkSchema({ | ||
address: { | ||
in: ['query'], | ||
isString: true, | ||
optional: true, | ||
}, | ||
}), | ||
handleValidationErrors, | ||
complianceAndGeoCheck, | ||
(req: express.Request, res: express.Response) => { | ||
res.sendStatus(200); | ||
}, | ||
); | ||
|
||
router.get( | ||
'/check-compliance-param/:address', | ||
checkSchema({ | ||
address: { | ||
in: ['params'], | ||
isString: true, | ||
}, | ||
}), | ||
handleValidationErrors, | ||
complianceAndGeoCheck, | ||
(req: express.Request, res: express.Response) => { | ||
res.sendStatus(200); | ||
}, | ||
); | ||
|
||
export const complianceCheckApp = Server(router); | ||
|
||
describe('compliance-check', () => { | ||
let isRestrictedCountrySpy: jest.SpyInstance; | ||
|
||
beforeAll(async () => { | ||
config.INDEXER_LEVEL_GEOBLOCKING_ENABLED = true; | ||
await dbHelpers.migrate(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
isRestrictedCountrySpy = isRestrictedCountryHeaders as unknown as jest.Mock; | ||
await testMocks.seedData(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await dbHelpers.teardown(); | ||
}); | ||
|
||
afterEach(async () => { | ||
jest.restoreAllMocks(); | ||
await dbHelpers.clearData(); | ||
}); | ||
|
||
it('does not return 403 if no address in request', async () => { | ||
isRestrictedCountrySpy.mockReturnValueOnce(false); | ||
await sendRequestToApp({ | ||
type: RequestMethod.GET, | ||
path: '/v4/check-compliance-query', | ||
expressApp: complianceCheckApp, | ||
expectedStatus: 200, | ||
}); | ||
}); | ||
|
||
it.each([ | ||
['query', '/v4/check-compliance-query?address=random'], | ||
['param', '/v4/check-compliance-param/random'], | ||
])('does not return 403 if address in request is not in database (%s)', async ( | ||
_name: string, | ||
path: string, | ||
) => { | ||
isRestrictedCountrySpy.mockReturnValueOnce(false); | ||
await sendRequestToApp({ | ||
type: RequestMethod.GET, | ||
path, | ||
expressApp: complianceCheckApp, | ||
expectedStatus: 200, | ||
}); | ||
}); | ||
|
||
it.each([ | ||
['query', '/v4/check-compliance-query?address=random'], | ||
['param', '/v4/check-compliance-param/random'], | ||
])('does not return 403 if address in request is not in database (%s) and non-restricted country', async ( | ||
_name: string, | ||
path: string, | ||
) => { | ||
isRestrictedCountrySpy.mockReturnValueOnce(false); | ||
await sendRequestToApp({ | ||
type: RequestMethod.GET, | ||
path, | ||
expressApp: complianceCheckApp, | ||
expectedStatus: 200, | ||
headers: nonRestrictedHeaders, | ||
}); | ||
}); | ||
|
||
it.each([ | ||
['query', `/v4/check-compliance-query?address=${testConstants.defaultAddress}`], | ||
['param', `/v4/check-compliance-param/${testConstants.defaultAddress}`], | ||
])('does not return 403 if address in request is not blocked (%s)', async ( | ||
_name: string, | ||
path: string, | ||
) => { | ||
isRestrictedCountrySpy.mockReturnValueOnce(false); | ||
await ComplianceStatusTable.create(testConstants.compliantStatusData); | ||
await sendRequestToApp({ | ||
type: RequestMethod.GET, | ||
path, | ||
expressApp: complianceCheckApp, | ||
expectedStatus: 200, | ||
}); | ||
}); | ||
|
||
it.each([ | ||
['query', `/v4/check-compliance-query?address=${testConstants.defaultAddress}`], | ||
['param', `/v4/check-compliance-param/${testConstants.defaultAddress}`], | ||
])('does not return 403 if address in request is in CLOSE_ONLY (%s)', async ( | ||
_name: string, | ||
path: string, | ||
) => { | ||
isRestrictedCountrySpy.mockReturnValueOnce(false); | ||
await ComplianceStatusTable.create({ | ||
...testConstants.compliantStatusData, | ||
status: ComplianceStatus.CLOSE_ONLY, | ||
}); | ||
await sendRequestToApp({ | ||
type: RequestMethod.GET, | ||
path, | ||
expressApp: complianceCheckApp, | ||
expectedStatus: 200, | ||
}); | ||
}); | ||
|
||
it.each([ | ||
['query', `/v4/check-compliance-query?address=${testConstants.defaultAddress}`], | ||
['param', `/v4/check-compliance-param/${testConstants.defaultAddress}`], | ||
])('does not return 403 if address in request is in CLOSE_ONLY and from restricted country (%s)', async ( | ||
_name: string, | ||
path: string, | ||
) => { | ||
isRestrictedCountrySpy.mockReturnValueOnce(true); | ||
await ComplianceStatusTable.create({ | ||
...testConstants.compliantStatusData, | ||
status: ComplianceStatus.CLOSE_ONLY, | ||
}); | ||
await sendRequestToApp({ | ||
type: RequestMethod.GET, | ||
path, | ||
expressApp: complianceCheckApp, | ||
expectedStatus: 200, | ||
}); | ||
}); | ||
|
||
it.each([ | ||
['query', `/v4/check-compliance-query?address=${testConstants.defaultAddress}`], | ||
['param', `/v4/check-compliance-param/${testConstants.defaultAddress}`], | ||
])('does return 403 if request is from restricted country (%s)', async ( | ||
_name: string, | ||
path: string, | ||
) => { | ||
isRestrictedCountrySpy.mockReturnValueOnce(true); | ||
const response: request.Response = await sendRequestToApp({ | ||
type: RequestMethod.GET, | ||
path, | ||
expressApp: complianceCheckApp, | ||
expectedStatus: 403, | ||
headers: restrictedHeaders, | ||
}); | ||
|
||
expect(response.body).toEqual(expect.objectContaining({ | ||
errors: expect.arrayContaining([{ | ||
msg: INDEXER_GEOBLOCKED_PAYLOAD, | ||
code: BlockedCode.GEOBLOCKED, | ||
}]), | ||
})); | ||
}); | ||
|
||
it.each([ | ||
['query', `/v4/check-compliance-query?address=${testConstants.blockedAddress}`], | ||
['param', `/v4/check-compliance-param/${testConstants.blockedAddress}`], | ||
])('does return 403 if address in request is blocked (%s)', async ( | ||
_name: string, | ||
path: string, | ||
) => { | ||
isRestrictedCountrySpy.mockReturnValueOnce(false); | ||
await ComplianceStatusTable.create(testConstants.noncompliantStatusData); | ||
const response: request.Response = await sendRequestToApp({ | ||
type: RequestMethod.GET, | ||
path, | ||
expressApp: complianceCheckApp, | ||
expectedStatus: 403, | ||
}); | ||
|
||
expect(response.body).toEqual(expect.objectContaining({ | ||
errors: expect.arrayContaining([{ | ||
msg: INDEXER_COMPLIANCE_BLOCKED_PAYLOAD, | ||
code: BlockedCode.COMPLIANCE_BLOCKED, | ||
}]), | ||
})); | ||
}); | ||
}); |
131 changes: 0 additions & 131 deletions
131
indexer/services/comlink/__tests__/lib/compliance-check.test.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
await
withreq.set(headers)
is unnecessary sinceset
does not return a promise. Removeawait
to avoid confusion.Committable suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this seems legit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Christopher-Li: Great, I'm glad you find the suggestion helpful! If you haven't already, please go ahead and apply the change. If there's anything else you need, feel free to ask.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it returns a Promise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.