-
Notifications
You must be signed in to change notification settings - Fork 19
Implement request blocking for specific account ID; add tests for blo… #483
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8616,6 +8616,12 @@ | |
| if (_classPrivateFieldLooseBase(this, _dropRequestDueToOptOut)[_dropRequestDueToOptOut]()) { | ||
| this.logger.debug('req dropped due to optout cookie: ' + this.device.gcookie); | ||
| return; | ||
| } // Drop all requests for blocked account ID | ||
|
|
||
|
|
||
| if (this.account && this.account.id === '8R5-4K5-466Z') { | ||
| this.logger.debug('req dropped for blocked account: ' + this.account.id); | ||
| return; | ||
| } // set a request in progress | ||
| // so that if gcookie is not present, no other request can be made asynchronusly | ||
|
|
||
|
|
@@ -16546,6 +16552,13 @@ | |
| } | ||
|
|
||
| post(url, body) { | ||
| // Drop all requests for blocked account ID | ||
| if (_classPrivateFieldLooseBase(this, _account$3)[_account$3] && _classPrivateFieldLooseBase(this, _account$3)[_account$3].id === '8R5-4K5-466Z') { | ||
| _classPrivateFieldLooseBase(this, _logger$3)[_logger$3].debug('post req dropped for blocked account: ' + _classPrivateFieldLooseBase(this, _account$3)[_account$3].id); | ||
|
|
||
| return Promise.reject(new Error('Account blocked')); | ||
| } | ||
|
Comment on lines
+16555
to
+16560
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Duplicate blocking logic—consolidate with the check at lines 8619-8624. This is the same hardcoded account ID check that appears earlier in the Recommendation: Consolidate all blocked-account checks into a single, early validation layer—ideally in a centralized request dispatcher or guard function—so that all request paths (fireRequest, post, etc.) benefit from one authoritative check. This eliminates duplication and ensures consistent behavior across the SDK. See the refactor suggestion in the previous comment (lines 8619-8624) for extracting the account ID to a constant and creating a reusable helper function. 🤖 Prompt for AI Agents |
||
|
|
||
| return fetch(url, { | ||
| method: 'post', | ||
| headers: { | ||
|
|
||
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import RequestManager from '../../../src/modules/request' | ||
| import Account from '../../../src/modules/account' | ||
|
|
||
| describe('RequestManager - Blocked Account ID', () => { | ||
| let requestManager | ||
| let mockLogger | ||
| let mockDevice | ||
| let mockSession | ||
| let mockIsPersonalisationActive | ||
|
|
||
| beforeEach(() => { | ||
| mockLogger = { | ||
| debug: jest.fn(), | ||
| error: jest.fn(), | ||
| wzrkError: {} | ||
| } | ||
| mockDevice = { | ||
| gcookie: 'test-gcookie-123' | ||
| } | ||
| mockSession = { | ||
| getSessionCookieObject: jest.fn(() => ({ s: 'session-123', p: 1 })) | ||
| } | ||
| mockIsPersonalisationActive = jest.fn(() => false) | ||
| }) | ||
|
|
||
| test('should drop post requests for blocked account ID 8R5-4K5-466Z', () => { | ||
| const blockedAccount = new Account({ id: '8R5-4K5-466Z' }) | ||
| requestManager = new RequestManager({ | ||
| logger: mockLogger, | ||
| account: blockedAccount, | ||
| device: mockDevice, | ||
| session: mockSession, | ||
| isPersonalisationActive: mockIsPersonalisationActive | ||
| }) | ||
|
|
||
| const testUrl = 'https://example.com/api/sync' | ||
| const testBody = JSON.stringify({ data: 'test' }) | ||
|
|
||
| return requestManager.post(testUrl, testBody) | ||
| .then(() => { | ||
| // If we reach here, the test should fail | ||
| expect(true).toBe(false) | ||
| }) | ||
| .catch((error) => { | ||
| // Verify that the request was blocked | ||
| expect(error.message).toBe('Account blocked') | ||
| expect(mockLogger.debug).toHaveBeenCalledWith('post req dropped for blocked account: 8R5-4K5-466Z') | ||
| }) | ||
| }) | ||
|
|
||
| test('should allow post requests for non-blocked account IDs', () => { | ||
| const normalAccount = new Account({ id: 'TEST-ACCOUNT-123' }) | ||
| requestManager = new RequestManager({ | ||
| logger: mockLogger, | ||
| account: normalAccount, | ||
| device: mockDevice, | ||
| session: mockSession, | ||
| isPersonalisationActive: mockIsPersonalisationActive | ||
| }) | ||
|
|
||
| const testUrl = 'https://example.com/api/sync' | ||
| const testBody = JSON.stringify({ data: 'test' }) | ||
|
|
||
| // Mock fetch | ||
| global.fetch = jest.fn(() => | ||
| Promise.resolve({ | ||
| ok: true, | ||
| json: () => Promise.resolve({ success: true }) | ||
| }) | ||
| ) | ||
|
|
||
| return requestManager.post(testUrl, testBody) | ||
| .then((result) => { | ||
| // Verify that the request was not blocked | ||
| expect(mockLogger.debug).not.toHaveBeenCalledWith(expect.stringContaining('post req dropped for blocked account')) | ||
| expect(result).toEqual({ success: true }) | ||
| expect(global.fetch).toHaveBeenCalledWith(testUrl, { | ||
| method: 'post', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: testBody | ||
| }) | ||
| }) | ||
| }) | ||
| }) |
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.
🛠️ Refactor suggestion | 🟠 Major
🧩 Analysis chain
Extract hardcoded account ID to a constant and centralize blocking logic.
The account ID
'8R5-4K5-466Z'is hardcoded as a magic string and duplicated across multiple locations (also appears in thepostmethod below and in other files per the AI summary). This violates the DRY principle and makes maintenance difficult.Additionally, logging specific account IDs may raise privacy or compliance concerns, depending on your data governance policies.
Consider the following refactor:
Run the following script to verify all occurrences of the hardcoded account ID across the codebase:
🏁 Script executed:
Length of output: 1202
Length of output: 386
🏁 Script executed:
Length of output: 380
🏁 Script executed:
Length of output: 1360
🏁 Script executed:
Length of output: 68
Length of output: 1035
🏁 Script executed:
Length of output: 3286
Extract hardcoded account ID to a constant in src/util/constants.js and centralize blocking logic across src/util/requestDispatcher.js and src/modules/request.js.
The account ID
'8R5-4K5-466Z'is duplicated across two source files with identical checks:src/util/requestDispatcher.js:23(fireRequest method)src/modules/request.js:280(post method)This violates the DRY principle and makes maintenance difficult. Additionally, logging specific account IDs may raise privacy or compliance concerns.
Recommended refactor:
src/util/requestDispatcher.js:23– Replace the hardcoded check with the helper functionsrc/modules/request.js:280– Replace the hardcoded check with the helper function'req dropped for blocked account')This approach scales for blocking multiple accounts and centralizes the logic for future maintenance.