Skip to content

Commit a15ab00

Browse files
arkid15rkasya
andauthored
Make API endpoints available (#1908)
* Make API enpoints available * Update CI/CD * Update backend/tests/apps/common/utils_test.py --------- Co-authored-by: Kate Golovanova <[email protected]>
1 parent aeea306 commit a15ab00

File tree

13 files changed

+46
-25
lines changed

13 files changed

+46
-25
lines changed

.github/workflows/run-ci-cd.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ jobs:
176176

177177
- name: Run backend tests
178178
run: |
179-
docker run -e DJANGO_CONFIGURATION=Test owasp/nest:test-backend-latest pytest
179+
docker run -e DJANGO_SETTINGS_MODULE=settings.test --env-file backend/.env.example owasp/nest:test-backend-latest pytest
180180
181181
run-frontend-unit-tests:
182182
name: Run frontend unit tests

backend/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ test-backend:
116116
--cache-from nest-test-backend \
117117
-f backend/docker/Dockerfile.test backend \
118118
-t nest-test-backend
119-
@docker run -e DJANGO_CONFIGURATION=Test --rm nest-test-backend pytest
119+
@docker run \
120+
-e DJANGO_SETTINGS_MODULE=settings.test \
121+
--env-file backend/.env.example \
122+
--rm nest-test-backend pytest
120123

121124
update-backend-dependencies:
122125
@cd backend && poetry update

backend/apps/common/index.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"projects_updated_at_asc",
3030
"projects_updated_at_desc",
3131
)
32-
IS_LOCAL_BUILD = settings.ENVIRONMENT == "Local"
3332
LOCAL_INDEX_LIMIT = 1000
3433

3534

@@ -60,7 +59,11 @@ def is_indexable(self, name: str) -> bool:
6059
bool: True if the index is enabled, False otherwise.
6160
6261
"""
63-
return name.lower() not in self.excluded_local_index_names if IS_LOCAL_BUILD else True
62+
return (
63+
name.lower() not in self.excluded_local_index_names
64+
if settings.IS_LOCAL_ENVIRONMENT
65+
else True
66+
)
6467

6568
def load_excluded_local_index_names(self) -> IndexRegistry:
6669
"""Load excluded local index names from settings.
@@ -285,4 +288,4 @@ def get_queryset(self):
285288
"""
286289
qs = self.get_entities()
287290

288-
return qs[:LOCAL_INDEX_LIMIT] if IS_LOCAL_BUILD else qs
291+
return qs[:LOCAL_INDEX_LIMIT] if settings.IS_LOCAL_ENVIRONMENT else qs

backend/apps/common/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def get_user_ip_address(request) -> str:
7878
str: The user's IP address.
7979
8080
"""
81-
if settings.ENVIRONMENT == "Local":
81+
if settings.IS_LOCAL_ENVIRONMENT:
8282
return settings.PUBLIC_IP_ADDRESS
8383

8484
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")

backend/apps/owasp/models/mixins/project.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def idx_forks_count(self) -> int:
4040
def idx_health_score(self) -> float | None:
4141
"""Return health score for indexing."""
4242
# TODO(arkid15r): Enable real health score in production when ready.
43-
return 100 if settings.ENVIRONMENT == "Production" else self.health_score
43+
return 100 if settings.IS_PRODUCTION_ENVIRONMENT else self.health_score
4444

4545
@property
4646
def idx_is_active(self) -> bool:

backend/settings/api/v1.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
"""OWASP Nest API v1 configuration."""
22

3+
from django.conf import settings
34
from ninja import NinjaAPI, Swagger
45
from ninja.throttling import AuthRateThrottle
56

67
from apps.core.api.ninja import ApiKeyAuth
78
from apps.github.api.rest.v1.urls import router as github_router
89
from apps.owasp.api.rest.v1.urls import router as owasp_router
910

10-
api = NinjaAPI(
11-
auth=ApiKeyAuth(),
12-
description="API for OWASP related entities",
13-
docs=Swagger(settings={"persistAuthorization": True}),
14-
throttle=[
15-
AuthRateThrottle("10/s"),
16-
],
17-
title="OWASP Nest API",
18-
version="1.0.0",
19-
)
11+
api_settings = {
12+
"description": "Open Worldwide Application Security Project API",
13+
"docs": Swagger(settings={"persistAuthorization": True}),
14+
"title": "OWASP Nest",
15+
"version": "1.0.0",
16+
}
17+
if not settings.IS_LOCAL_ENVIRONMENT:
18+
api_settings.update(
19+
{
20+
"auth": ApiKeyAuth(),
21+
"throttle": [AuthRateThrottle("10/s")],
22+
}
23+
)
24+
25+
api = NinjaAPI(**api_settings)
2026

2127
api.add_router("github", github_router)
2228
api.add_router("owasp", owasp_router)

backend/settings/base.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ class Base(Configuration):
1212
BASE_DIR = Path(__file__).resolve().parent.parent
1313

1414
ENVIRONMENT = os.environ.get("DJANGO_CONFIGURATION", "Local")
15-
if ENVIRONMENT == "Test":
16-
from dotenv import load_dotenv
17-
18-
load_dotenv(BASE_DIR / ".env.example")
1915

2016
ALLOWED_HOSTS = values.ListValue()
2117
AUTH_USER_MODEL = "nest.User"
2218
CORS_ALLOW_CREDENTIALS = True
2319
DEBUG = False
20+
21+
IS_LOCAL_ENVIRONMENT = False
22+
IS_PRODUCTION_ENVIRONMENT = False
23+
IS_STAGING_ENVIRONMENT = False
24+
IS_TEST_ENVIRONMENT = False
25+
2426
RELEASE_VERSION = values.Value(environ_name="RELEASE_VERSION")
2527
SENTRY_DSN = values.SecretValue(environ_name="SENTRY_DSN")
2628

@@ -44,6 +46,7 @@ class Base(Configuration):
4446
THIRD_PARTY_APPS = (
4547
"algoliasearch_django",
4648
"corsheaders",
49+
"ninja",
4750
"storages",
4851
)
4952

backend/settings/local.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Local(Base):
1818
CSRF_TRUSTED_ORIGINS = ALLOWED_ORIGINS
1919

2020
DEBUG = True
21+
IS_LOCAL_ENVIRONMENT = True
2122
LOGGING = {}
2223
PUBLIC_IP_ADDRESS = values.Value()
2324
SLACK_COMMANDS_ENABLED = True

backend/settings/production.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,7 @@ class Production(Base):
4747
CORS_ALLOWED_ORIGINS = ALLOWED_ORIGINS
4848
CSRF_TRUSTED_ORIGINS = ALLOWED_ORIGINS
4949

50+
IS_PRODUCTION_ENVIRONMENT = True
51+
5052
SLACK_COMMANDS_ENABLED = True
5153
SLACK_EVENTS_ENABLED = True

backend/settings/staging.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,7 @@ class Staging(Base):
4747
CORS_ALLOWED_ORIGINS = ALLOWED_ORIGINS
4848
CSRF_TRUSTED_ORIGINS = ALLOWED_ORIGINS
4949

50+
IS_STAGING_ENVIRONMENT = True
51+
5052
SLACK_COMMANDS_ENABLED = True
5153
SLACK_EVENTS_ENABLED = True

0 commit comments

Comments
 (0)