Skip to content
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run-ci-cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ jobs:

- name: Run frontend end-to-end tests
run: |
docker run --env-file frontend/.env.example owasp/nest:test-frontend-e2e-latest pnpm run test:e2e
docker run --env-file frontend/.env.e2e.example owasp/nest:test-frontend-e2e-latest pnpm run test:e2e

set-release-version:
name: Set release version
Expand Down
23 changes: 14 additions & 9 deletions .github/workflows/setup-e2e-environment/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ runs:
steps:
- name: Wait for database to be ready
run: |
until docker exec ${{ job.services.db.id }} pg_isready -U nest_user_e2e -d nest_db_e2e; do
echo "Waiting for database..."
sleep 5
done
timeout 1m bash -c '
until docker exec ${{ job.services.db.id }} pg_isready -U nest_user_e2e -d nest_db_e2e; do
echo "Waiting for database..."
sleep 5
done
'
shell: bash

- name: Install PostgreSQL client
Expand All @@ -21,7 +23,7 @@ runs:
env:
PGPASSWORD: nest_user_e2e_password
run: |
gunzip -c backend/data/nest-e2e.sql.gz | psql -h localhost -U nest_user_e2e -d nest_db_e2e
gunzip -c backend/data/nest.sql.gz | psql -h localhost -U nest_user_e2e -d nest_db_e2e
shell: bash

- name: Build backend e2e image
Expand All @@ -43,6 +45,7 @@ runs:
--env-file backend/.env.e2e.example \
--network host \
-p 9000:9000 \
-e DJANGO_DB_HOST=localhost \
owasp/nest:test-backend-e2e-latest \
sh -c '
gunicorn wsgi:application --bind 0.0.0.0:9000
Expand All @@ -51,9 +54,11 @@ runs:

- name: Waiting for the backend to be ready
run: |
until wget --spider http://localhost:9000/a; do
echo "Waiting for backend..."
sleep 5
done
timeout 1m bash -c '
until wget --spider http://localhost:9000/a; do
echo "Waiting for backend..."
sleep 5
done
'
echo "Backend is up!"
shell: bash
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ Ensure that all `.env` files are saved in **UTF-8 format without BOM (Byte Order

1. **Load Initial Data**:

- Make sure you have `gzip` installed on your machine.

- Open a new terminal session and run the following command to populate the database with initial data from fixtures:

```bash
Expand Down
2 changes: 1 addition & 1 deletion backend/.env.e2e.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DJANGO_AWS_ACCESS_KEY_ID=None
DJANGO_AWS_SECRET_ACCESS_KEY=None
DJANGO_SETTINGS_MODULE=settings.e2e
DJANGO_CONFIGURATION=E2E
DJANGO_DB_HOST=None
DJANGO_DB_HOST=db
DJANGO_DB_NAME=nest_db_e2e
DJANGO_DB_USER=nest_user_e2e
DJANGO_DB_PASSWORD=nest_user_e2e_password
Expand Down
27 changes: 9 additions & 18 deletions backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,7 @@ django-shell:

dump-data:
@echo "Dumping Nest data"
@CMD="python manage.py dumpdata \
github \
owasp \
slack.Conversation \
slack.Member \
slack.Message \
slack.Workspace \
--indent=4 \
--natural-foreign \
--natural-primary -o data/nest.json" $(MAKE) exec-backend-command
@CMD="sed -E -i 's/(\"[^\"]*email\"): *\"([^\"]|\\\")*\"/\1: \"\"/g' data/nest.json" $(MAKE) exec-backend-command
@CMD="gzip -f data/nest.json" $(MAKE) exec-backend-command

dump-data-e2e:
@echo "Dumping Nest e2e data"
@CMD="pg_dumpall -U nest_user_e2e --clean | gzip -9 > backend/data/nest-e2e.sql.gz" $(MAKE) exec-db-command-e2e
@CMD="./data/dump.sh" $(MAKE) exec-backend-command-it

enrich-data: \
github-enrich-issues \
Expand All @@ -83,12 +68,18 @@ index-data:
@CMD="python manage.py algolia_update_synonyms" $(MAKE) exec-backend-command

load-data:
@echo "Recreating Nest schema"
@CMD="psql -U nest_user_dev -d nest_db_dev -c 'DROP SCHEMA public CASCADE; CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO nest_user_dev'" \
$(MAKE) exec-db-command-it 2>/dev/null
@echo "Loading Nest data"
@CMD="python manage.py load_data" $(MAKE) exec-backend-command
@gunzip -c backend/data/nest.sql.gz | docker exec -i nest-db psql -U nest_user_dev -d nest_db_dev

load-data-e2e:
@echo "Recreating Nest e2e schema"
@CMD="psql -U nest_user_e2e -d nest_db_e2e -c 'DROP SCHEMA public CASCADE; CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO nest_user_e2e'" \
$(MAKE) exec-db-command-e2e 2>/dev/null
@echo "Loading Nest e2e data"
@gunzip -c backend/data/nest-e2e.sql.gz | docker exec -i e2e-nest-db psql -U nest_user_e2e -d nest_db_e2e
@gunzip -c backend/data/nest.sql.gz | docker exec -i e2e-nest-db psql -U nest_user_e2e -d nest_db_e2e

merge-migrations:
@CMD="python manage.py makemigrations --merge" $(MAKE) exec-backend-command
Expand Down
2 changes: 2 additions & 0 deletions backend/apps/api/decorators/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def cache_response(
def decorator(view_func):
@wraps(view_func)
def _wrapper(request, *args, **kwargs):
if settings.IS_E2E_ENVIRONMENT:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why e2e needs this change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We didn't setup cache for e2e, so when it tries to access cache it gives 500 internal error. I think there is an option to setup redis cache in CI/CD. Maybe we can do that or keep it simple.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's add cache service for e2e instead. The closer to production architecture the better -- for both local and CI/CD cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I will add the cache in another PR after this one.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's configure the cache backed for e2e via Django settings (locmem for now)

return view_func(request, *args, **kwargs)
if request.method not in ("GET", "HEAD"):
return view_func(request, *args, **kwargs)

Expand Down
13 changes: 13 additions & 0 deletions backend/apps/api/rest/v0/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@
],
"throttle": [],
}

elif settings.IS_E2E_ENVIRONMENT:
api_settings_customization = {
"auth": None,
"servers": [
{
"description": "E2E",
"url": settings.SITE_URL,
}
],
"throttle": [],
}

elif settings.IS_STAGING_ENVIRONMENT:
api_settings_customization = {
"servers": [
Expand Down
53 changes: 53 additions & 0 deletions backend/data/dump.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/sh

set -e

# Cleanup function to ensure temp DB is dropped even on error
cleanup() {

Check warning on line 6 in backend/data/dump.sh

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add an explicit return statement at the end of the function.

See more on https://sonarcloud.io/project/issues?id=OWASP_Nest&issues=AZrF2NveBsDskKfEapS9&open=AZrF2NveBsDskKfEapS9&pullRequest=2710
if [ -n "$TEMP_DB" ]; then
echo "Cleaning up temporary database $TEMP_DB..."
psql -h "$DJANGO_DB_HOST" -U "$DJANGO_DB_USER" -d postgres -c "DROP DATABASE IF EXISTS $TEMP_DB;" 2>/dev/null || true
fi
}
trap cleanup EXIT

export PGPASSWORD="$DJANGO_DB_PASSWORD"
export TEMP_DB="temp_$DJANGO_DB_NAME"

# 1. Create a temporary copy of the database
echo "Creating temporary database $TEMP_DB"

psql -h "$DJANGO_DB_HOST" -U "$DJANGO_DB_USER" -d postgres -c \
"CREATE DATABASE $TEMP_DB TEMPLATE $DJANGO_DB_NAME;"

# 2. Generate all UPDATE statements dynamically
UPDATES=$(psql -h "$DJANGO_DB_HOST" -U "$DJANGO_DB_USER" -d "$TEMP_DB" -Atqc "
SELECT 'UPDATE '
|| quote_ident(n.nspname) || '.' || quote_ident(c.relname)
|| ' SET ' || quote_ident(a.attname)
|| ' = '''';'
FROM pg_attribute a
JOIN pg_class c ON c.oid = a.attrelid
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE a.attname = 'email'
AND a.attnum > 0
AND NOT a.attisdropped
AND n.nspname NOT IN ('pg_catalog','information_schema');
")

if [ -z "$UPDATES" ]; then
echo "No email fields found to hide."
else
echo "Hiding email addresses…"
echo "$UPDATES" | psql -h "$DJANGO_DB_HOST" -U "$DJANGO_DB_USER" -d "$TEMP_DB"
fi

# 3. Dump the DB
echo "Creating dump…"
pg_dump -h "$DJANGO_DB_HOST" -U "$DJANGO_DB_USER" -d "$TEMP_DB" | gzip -9 > ./data/nest.sql.gz

# 4. Drop the temporary database
echo "Dropping temporary database $TEMP_DB"
psql -h "$DJANGO_DB_HOST" -U "$DJANGO_DB_USER" -d postgres -c "DROP DATABASE $TEMP_DB;"

echo "Dump created: data/nest.sql.gz"
Binary file removed backend/data/nest-e2e.sql.gz
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion backend/settings/e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class E2E(Base):
"""End-to-end testing configuration."""

APP_NAME = "OWASP Nest E2E Testing"
SITE_URL = "http://localhost:9000"

ALLOWED_ORIGINS = (
"http://frontend:3000", # NOSONAR
Expand All @@ -17,7 +18,6 @@ class E2E(Base):
CORS_ALLOWED_ORIGINS = ALLOWED_ORIGINS
CSRF_TRUSTED_ORIGINS = ALLOWED_ORIGINS

DEBUG = False
IS_E2E_ENVIRONMENT = True
LOGGING = {}
PUBLIC_IP_ADDRESS = values.Value()
4 changes: 4 additions & 0 deletions cspell/custom-dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Agentic
Agsoc
Aichi
Aissue
Atqc
Aupdated
BOTTOMPADDING
CCSP
Expand All @@ -19,6 +20,7 @@ NOASSERTION
NOSONAR
Nadu
Nominatim
PGPASSWORD
PLR
PYTHONUNBUFFERED
RUF
Expand All @@ -44,6 +46,7 @@ apk
arithmatex
arkid15r
askowasp
attisdropped
bangbang
bsky
certbot
Expand Down Expand Up @@ -96,6 +99,7 @@ navlink
nestbot
noinput
nosniff
nspname
openstreetmap
owasppcitoolkit
owtf
Expand Down