Skip to content

Commit

Permalink
test: ✅ add api rates limit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nutfdt committed Jan 7, 2025
1 parent 7a89d79 commit 090671b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
run: IMAGE_TAG=${{ needs.build-backend.outputs.image_tag }} docker compose -f docker-compose.yml -f docker-compose.ci.yml up -d
- run: docker compose exec backend coverage run -m pytest
- run: docker compose exec backend coverage xml --ignore-errors
- run: docker compose exec backend locust -f tests/locustfile.py -t 5s --headless -H http://localhost:5000/api
- name: Produce the coverage report
uses: insightsengineering/coverage-action@v2
with:
Expand Down
3 changes: 2 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ numpy<3.0.0
# Dev
pytest==8.3.4
coverage==7.6.10
Faker==33.3.0
Faker==33.3.0
locust==2.32.5
6 changes: 1 addition & 5 deletions backend/src/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@
)
from .utils import get_current_user, send_mail, upload_image

DISABLE_RATE_LIMITS = os.environ.get("DISABLE_RATE_LIMITS", "false").lower() == "true"

router = APIRouter(prefix="/api")
limiter = Limiter(
key_func=get_remote_address if not DISABLE_RATE_LIMITS else lambda: None
)
limiter = Limiter(key_func=get_remote_address)


@router.get("/", response_class=PlainTextResponse)
Expand Down
6 changes: 6 additions & 0 deletions backend/tests/locust.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
locustfile = tests/locustfile.py
headless = true
host = http://localhost:5000/api
run-time = 5s
spawn-rate =
users = 60
18 changes: 18 additions & 0 deletions backend/tests/locustfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import time

from locust import HttpUser, task


class TestLocust(HttpUser):
@task
def rate_limit_version(self):
self.client.get("/version")

@task
def test_upload(self):
with open("./tests/images/revolver.jpg", "rb") as f:
self.client.post(
"/upload",
files={"image": f},
data={"date": time.time()},
)
2 changes: 0 additions & 2 deletions docker-compose.ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
services:
backend:
image: ${IMAGE_TAG}
environment:
- DISABLE_RATE_LIMITS=true

0 comments on commit 090671b

Please sign in to comment.