-
Notifications
You must be signed in to change notification settings - Fork 472
🛂(backend) stop throttling collaboration servers #1730
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
Merged
+212
−2
Merged
Changes from all commits
Commits
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
107 changes: 107 additions & 0 deletions
107
src/backend/core/tests/test_api_throttling_document_throttle.py
This file contains hidden or 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,107 @@ | ||
| """ | ||
| Test DocumentThrottle for regular throttling and y-provider bypass. | ||
| """ | ||
|
|
||
| import pytest | ||
| from rest_framework.test import APIClient | ||
|
|
||
| from core import factories | ||
|
|
||
| pytestmark = pytest.mark.django_db | ||
|
|
||
|
|
||
| def test_api_throttling_document_throttle_regular_requests(settings): | ||
| """Test that regular requests are throttled normally.""" | ||
|
|
||
| current_rate = settings.REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]["document"] | ||
| settings.REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]["document"] = "3/minute" | ||
| settings.Y_PROVIDER_API_KEY = "test-y-provider-key" | ||
|
|
||
| user = factories.UserFactory() | ||
| client = APIClient() | ||
| client.force_login(user) | ||
|
|
||
| document = factories.DocumentFactory() | ||
| factories.UserDocumentAccessFactory(document=document, user=user) | ||
|
|
||
| # Make 3 requests without the y-provider key | ||
| for _i in range(3): | ||
| response = client.get( | ||
| f"/api/v1.0/documents/{document.id!s}/", | ||
| ) | ||
| assert response.status_code == 200 | ||
|
|
||
| # 4th request should be throttled | ||
| response = client.get( | ||
| f"/api/v1.0/documents/{document.id!s}/", | ||
| ) | ||
| assert response.status_code == 429 | ||
|
|
||
| # A request with the y-provider key should NOT be throttled | ||
| response = client.get( | ||
| f"/api/v1.0/documents/{document.id!s}/", | ||
| HTTP_X_Y_PROVIDER_KEY="test-y-provider-key", | ||
| ) | ||
| assert response.status_code == 200 | ||
|
|
||
| # Restore original rate | ||
| settings.REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]["document"] = current_rate | ||
|
|
||
|
|
||
| def test_api_throttling_document_throttle_y_provider_exempted(settings): | ||
| """Test that y-provider requests are exempted from throttling.""" | ||
|
|
||
| current_rate = settings.REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]["document"] | ||
| settings.REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]["document"] = "3/minute" | ||
| settings.Y_PROVIDER_API_KEY = "test-y-provider-key" | ||
|
|
||
| user = factories.UserFactory() | ||
| client = APIClient() | ||
| client.force_login(user) | ||
|
|
||
| document = factories.DocumentFactory() | ||
| factories.UserDocumentAccessFactory(document=document, user=user) | ||
|
|
||
| # Make many requests with the y-provider API key | ||
| for _i in range(10): | ||
| response = client.get( | ||
| f"/api/v1.0/documents/{document.id!s}/", | ||
| HTTP_X_Y_PROVIDER_KEY="test-y-provider-key", | ||
| ) | ||
| assert response.status_code == 200 | ||
|
|
||
| # Restore original rate | ||
| settings.REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]["document"] = current_rate | ||
|
|
||
|
|
||
| def test_api_throttling_document_throttle_invalid_token(settings): | ||
| """Test that requests with invalid tokens are throttled.""" | ||
|
|
||
| current_rate = settings.REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]["document"] | ||
| settings.REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]["document"] = "3/minute" | ||
| settings.Y_PROVIDER_API_KEY = "test-y-provider-key" | ||
|
|
||
| user = factories.UserFactory() | ||
| client = APIClient() | ||
| client.force_login(user) | ||
|
|
||
| document = factories.DocumentFactory() | ||
| factories.UserDocumentAccessFactory(document=document, user=user) | ||
|
|
||
| # Make 3 requests with an invalid token | ||
| for _i in range(3): | ||
| response = client.get( | ||
| f"/api/v1.0/documents/{document.id!s}/", | ||
| HTTP_X_Y_PROVIDER_KEY="invalid-token", | ||
| ) | ||
| assert response.status_code == 200 | ||
|
|
||
| # 4th request should be throttled | ||
| response = client.get( | ||
| f"/api/v1.0/documents/{document.id!s}/", | ||
| HTTP_X_Y_PROVIDER_KEY="invalid-token", | ||
| ) | ||
| assert response.status_code == 429 | ||
|
|
||
| # Restore original rate | ||
| settings.REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]["document"] = current_rate | ||
66 changes: 66 additions & 0 deletions
66
src/frontend/servers/y-provider/__tests__/collaborationBackend.test.ts
This file contains hidden or 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,66 @@ | ||
| import axios from 'axios'; | ||
| import { describe, expect, test, vi } from 'vitest'; | ||
|
|
||
| vi.mock('../src/env', () => ({ | ||
| COLLABORATION_BACKEND_BASE_URL: 'http://app-dev:8000', | ||
| Y_PROVIDER_API_KEY: 'test-yprovider-key', | ||
| })); | ||
|
|
||
| describe('CollaborationBackend', () => { | ||
| test('fetchDocument sends X-Y-Provider-Key header', async () => { | ||
| const axiosGetSpy = vi.spyOn(axios, 'get').mockResolvedValue({ | ||
| status: 200, | ||
| data: { | ||
| id: 'test-doc-id', | ||
| abilities: { retrieve: true, update: true }, | ||
| }, | ||
| }); | ||
|
|
||
| const { fetchDocument } = await import('@/api/collaborationBackend'); | ||
| const documentId = 'test-document-123'; | ||
|
|
||
| await fetchDocument(documentId, { cookie: 'test-cookie' }); | ||
|
|
||
| expect(axiosGetSpy).toHaveBeenCalledWith( | ||
| `http://app-dev:8000/api/v1.0/documents/${documentId}/`, | ||
| expect.objectContaining({ | ||
| headers: expect.objectContaining({ | ||
| 'X-Y-Provider-Key': 'test-yprovider-key', | ||
| cookie: 'test-cookie', | ||
| }), | ||
| }), | ||
| ); | ||
|
|
||
| axiosGetSpy.mockRestore(); | ||
| }); | ||
|
|
||
| test('fetchCurrentUser sends X-Y-Provider-Key header', async () => { | ||
| const axiosGetSpy = vi.spyOn(axios, 'get').mockResolvedValue({ | ||
| status: 200, | ||
| data: { | ||
| id: 'test-user-id', | ||
| email: '[email protected]', | ||
| }, | ||
| }); | ||
|
|
||
| const { fetchCurrentUser } = await import('@/api/collaborationBackend'); | ||
|
|
||
| await fetchCurrentUser({ | ||
| cookie: 'test-cookie', | ||
| origin: 'http://localhost:3000', | ||
| }); | ||
|
|
||
| expect(axiosGetSpy).toHaveBeenCalledWith( | ||
| 'http://app-dev:8000/api/v1.0/users/me/', | ||
| expect.objectContaining({ | ||
| headers: expect.objectContaining({ | ||
| 'X-Y-Provider-Key': 'test-yprovider-key', | ||
| cookie: 'test-cookie', | ||
| origin: 'http://localhost:3000', | ||
| }), | ||
| }), | ||
| ); | ||
|
|
||
| axiosGetSpy.mockRestore(); | ||
| }); | ||
| }); |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.