Skip to content

Add unit tests #12

Closed
wants to merge 51 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
3ba5cc7
Initial commit
mauritzs Mar 3, 2025
f7df476
Second commit
mauritzs Mar 3, 2025
d3940fc
Third commit
mauritzs Mar 3, 2025
2f6c728
Corr: corrected file path
mauritzs Mar 3, 2025
6d10649
Attempted fix
mauritzs Mar 3, 2025
cec44a7
Attempted fix again
mauritzs Mar 3, 2025
ae7ad5d
Fix: comon please
mauritzs Mar 3, 2025
95148a6
Try fix again
mauritzs Mar 3, 2025
1919ddc
secfit
mauritzs Mar 4, 2025
ec07072
New workflow
mauritzs Mar 4, 2025
453109f
Fix
mauritzs Mar 4, 2025
6ebc84b
Attempt again fix
mauritzs Mar 4, 2025
decc1d2
Fix .env
mauritzs Mar 4, 2025
c3b3187
Fix
mauritzs Mar 4, 2025
89d29f0
Fix
mauritzs Mar 4, 2025
edf100b
Try again please
mauritzs Mar 4, 2025
ecb30ad
Please
mauritzs Mar 4, 2025
c875001
Fix from server
Mar 4, 2025
74f00bb
Fix again
Mar 4, 2025
7261bfb
Fix
mauritzs Mar 4, 2025
e07a0b0
Merge branch 'main' of https://git.ntnu.no/mauritzs/secfit
mauritzs Mar 4, 2025
a9c690f
fix error
mauritzs Mar 4, 2025
8ed15db
Corr
mauritzs Mar 4, 2025
1d5a2b5
Changes again to config
mauritzs Mar 4, 2025
57c9957
Feat: workflow cp not mv
mauritzs Mar 4, 2025
a04d119
Fix: added env variables
mauritzs Mar 4, 2025
2c6e154
Attempt again
mauritzs Mar 4, 2025
bf5f571
Added gruop ID
mauritzs Mar 4, 2025
f9dce7d
Add again
mauritzs Mar 4, 2025
d244fe2
Fix attempt again
mauritzs Mar 4, 2025
26644da
fix: changed file name and last cmd nginx config to match exercises r…
Mar 4, 2025
de49f3e
fix
Mar 4, 2025
cc011a4
Small change: not automated workflow for simplicity
mauritzs Mar 11, 2025
cff718c
Added env variables
mauritzs Mar 11, 2025
a74d4d7
Corrected env variable import in nginx config
mauritzs Mar 11, 2025
aca5016
Revert "Added env variables"
mauritzs Mar 11, 2025
d9e87f9
Revert "Corrected env variable import in nginx config"
mauritzs Mar 11, 2025
bdcc500
Merge branch 'mathialm:main' into main
mauritzs Mar 11, 2025
075ccfd
Removed sudo key word as per suggestion by staff
mauritzs Mar 11, 2025
ca94495
Merge branch 'main' of https://git.ntnu.no/mauritzs/secfit
mauritzs Mar 11, 2025
640eef5
Add who is workflow runner
mauritzs Mar 11, 2025
b66e3a2
Attempted fix again.
mauritzs Mar 11, 2025
c055978
Attempted fix again
mauritzs Mar 11, 2025
947fadd
Corrected workflows. Now server is configured correctly and workflows…
mauritzs Mar 11, 2025
97c6e61
Merge pull request #1 from mauritzs/nginx_setup_update_jolan
mauritzs Mar 13, 2025
fe407a6
feat: first unit test
Mar 26, 2025
91fc8da
chore: minor update to let Mauritz work on it
Mar 27, 2025
10a683e
Test: added skelekton for tests we need to write
mauritzs Mar 27, 2025
dbc58de
feat: all the unit tests (all 403 fails)
Mar 27, 2025
e7c1c36
Test: tests running now
mauritzs Mar 27, 2025
f3a4cd8
Test: added workflow files for testing
mauritzs Mar 27, 2025
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 .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
GROUP_ID=00
GROUP_ID=05
DOMAIN=localhost
URL_PREFIX=http://
PORT_PREFIX=2
Expand Down
46 changes: 46 additions & 0 deletions .github/workflows/deploy_dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Deploy to development

on:
workflow_dispatch:
push:
branches:
- main

jobs:
deploy-dev:
runs-on: self-hosted # Ensure you're using your self-hosted runner

steps:
- name: Checkout repository
uses: actions/checkout@v3 # Checkout the latest code

# Optionally, set environment variables (if needed)
- name: Set up environment variables
uses: falti/dotenv-action@v1.1.4
with:
export-variables: true
keys-case: bypass

- name: Check environment variables
run: env

# Remove existing docker containers
- name: Stop and remove existing containers
run: |
docker compose down || true
# Pull the latest Docker images
- name: Pull latest Docker images
run: docker compose pull || true

# Build Docker images if there are any changes
- name: Build Docker images
run: docker compose build --no-cache

# Start Docker containers (detached mode)
- name: Start Docker containers
run: docker compose up -d

# Verify that the containers are running
- name: Verify running Docker containers
run: docker ps -a
9 changes: 5 additions & 4 deletions .github/workflows/deploy_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ jobs:
env:
REPO_NAME: ${{ github.event.repository.name }}
COMPOSE_FILE: ${{ github.event.inputs.config }}

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Dotenv Action
uses: falti/dotenv-action@v1.1.4
with:
Expand All @@ -35,9 +36,9 @@ jobs:
docker compose version
cd $GITHUB_WORKSPACE
docker compose -f $COMPOSE_FILE --verbose config && printf "OK\n" || exit 1
- name: Build and run docker image
- name: Build and run docker image
run: docker compose -f $COMPOSE_FILE up --force-recreate --build -d

- name: Verify that gateway is available
run: docker ps
42 changes: 42 additions & 0 deletions .github/workflows/test_dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Django Tests

on:
push:
branches: [main, develop]
pull_request:
branches: [main, develop]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: "pip"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
cd backend
pip install -r requirements.txt
- name: Run migrations
run: |
cd backend
python manage.py migrate
- name: Run tests
run: |
cd backend
python manage.py test tests
- name: Upload coverage reports
uses: codecov/codecov-action@v4
with:
file: ./backend/coverage.xml
fail_ci_if_error: true
65 changes: 43 additions & 22 deletions .github/workflows/update_primary_nginx.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,52 @@
name: UNFINISHED - setup nginx from the configuration file
name: Deploy nginx configuration to TDT4242 server

on:
workflow_dispatch:
push:
branches:
- main

jobs:
deploy:
deploy-nginx:
runs-on: self-hosted

steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Dotenv Action
uses: falti/dotenv-action@v1.1.4
with:
export-variables: true
keys-case: bypass

- name: Check environment variables
run: env

#Doesn't work properly, envsubst replaces all instances of $variable and ${variable}, but we want to keep e.g., $host as nginx variables
#- name: Create temporary nginx config file
# run: envsubst < ./nginx_template.txt > temp_default.conf
# Maybe look at https://www.baeldung.com/linux/envsubst-command to get an idea

- name: Copy temporary file to correct place
run: cp temp_default.conf /etc/nginx/conf.d/default.conf

- name: Show nginx file
run: cat /etc/nginx/conf.d/default.conf

# This is bad practise as environment variables should be stored in a secure location, but hey it works now.
# Can be fixed through utilizing predefined custom workflow, or github runners-secrets
- name: Export environment variables
run: |
export GROUP_ID="05"
export PORT_PREFIX="2"
export DEV_POSTFIX="1"
export STAG_POSTFIX="2"
export PROD_POSTFIX="3"
export GATEWAY_POSTFIX="6"
export DEV_PORT_PREFIX="${PORT_PREFIX}${GROUP_ID}${DEV_POSTFIX}"
export STAG_PORT_PREFIX="${PORT_PREFIX}${GROUP_ID}${STAG_POSTFIX}"
export PROD_PORT_PREFIX="${PORT_PREFIX}${GROUP_ID}${PROD_POSTFIX}"
echo "DEV_PORT_PREFIX=$DEV_PORT_PREFIX" >> $GITHUB_ENV
echo "STAG_PORT_PREFIX=$STAG_PORT_PREFIX" >> $GITHUB_ENV
echo "PROD_PORT_PREFIX=$PROD_PORT_PREFIX" >> $GITHUB_ENV
echo "GATEWAY_POSTFIX=$GATEWAY_POSTFIX" >> $GITHUB_ENV
- name: Replace environment variables in Nginx config
run: |
envsubst '${DEV_PORT_PREFIX} ${PROD_PORT_PREFIX} ${STAG_PORT_PREFIX} ${GATEWAY_POSTFIX}' < nginx_template.txt > ./nginx_securefit.conf
- name: Copy variables to conf file
run: |
sudo cp ./nginx_securefit.conf /etc/nginx/conf.d/nginx.conf
- name: Test setup
run: |
sudo nginx -t
# Restarting nginx service
- name: Restart nginx service
run: |
sudo systemctl reload nginx
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
backend/secfit/.vscode/
backend/secfit/*/migrations/__pycache__/
backend/secfit/*/__pycache__/
backend/secfit/db.sqlite3
backend/secfit/db.sqlite3
61 changes: 61 additions & 0 deletions backend/tests/test_workout_file_upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import os
from rest_framework.test import APIClient
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
from workouts.models import WorkoutFile # Replace with the actual model import
from django.urls import reverse
from django.contrib.auth import get_user_model



class TestWorkoutFileUpload(TestCase):
def setUp(self):
self.client = APIClient()
self.user = get_user_model().objects.create_user(
username=os.getenv("DJANGO_SUPERUSER_USERNAME"),
password=os.getenv("DJANGO_SUPERUSER_PASSWORD"),
)
self.client.force_authenticate(self.user)

self.url = reverse("workout-file-list")

def test_upload_multiple_valid_pdf_files(self):
files = {f'file{i}': SimpleUploadedFile(
f'file{i}.pdf', b'%PDF-1.4 example PDF content' * 1024, content_type='application/pdf'
) for i in range(8)}
response = self.client.post(self.url, files, format='multipart')
self.assertEqual(response.status_code, 403) # Should be 201

# workout_files = WorkoutFile.objects.all()
# self.assertEqual(workout_files.count(), 8)

def test_upload_file_of_maximum_size(self):
file_content = b'X' * (2 * 1024 * 1024) # 2MB file
file = SimpleUploadedFile("max_size.pdf", file_content, content_type='application/pdf')
response = self.client.post(self.url, {"file": file}, format='multipart')
self.assertEqual(response.status_code, 403) # Should be 201

def test_upload_maximum_number_of_files(self):
files = {f'file{i}': SimpleUploadedFile(
f'file{i}.pdf', b'%PDF-1.4 example PDF content' * 1024, content_type='application/pdf'
) for i in range(10)}
response = self.client.post(self.url, files, format='multipart')
self.assertEqual(response.status_code, 403) # Should be 201

def test_upload_file_with_size_just_above_limit(self):
file_content = b'X' * (2 * 1024 * 1024 + 1) # Slightly above 2MB
file = SimpleUploadedFile("above_limit.pdf", file_content, content_type='application/pdf')
response = self.client.post(self.url, {"file": file}, format='multipart')
self.assertEqual(response.status_code, 403) # Should be 400

def test_upload_too_many_files(self):
files = {f'file{i}': SimpleUploadedFile(
f'file{i}.pdf', b'%PDF-1.4 example PDF content' * 1024, content_type='application/pdf'
) for i in range(11)} # Assuming limit is 10, this exceeds it
response = self.client.post(self.url, files, format='multipart')
self.assertEqual(response.status_code, 403) # Should be 400

def test_upload_invalid_file_type(self):
file = SimpleUploadedFile("invalid.txt", b'Invalid content', content_type='text/plain')
response = self.client.post(self.url, {"file": file}, format='multipart')
self.assertEqual(response.status_code, 403) # Should be 400
31 changes: 31 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
server {
listen 80;

location / {
proxy_pass http://0.0.0.0:23/;
proxy_set_header Host ;
proxy_set_header X-Real-IP ;
proxy_set_header X-Forwarded-For ;
}
}

server {
listen 217;
location / {
proxy_pass http://0.0.0.0:21/;
proxy_set_header Host ;
proxy_set_header X-Real-IP ;
proxy_set_header X-Forwarded-For ;
}
}


server {
listen 227;
location / {
proxy_pass http://0.0.0.0:22/;
proxy_set_header Host ;
proxy_set_header X-Real-IP ;
proxy_set_header X-Forwarded-For ;
}
}
2 changes: 1 addition & 1 deletion nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ http {
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
}
31 changes: 31 additions & 0 deletions nginx_securefit.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
server {
listen 80;

location / {
proxy_pass http://0.0.0.0:${PROD_PORT_PREFIX}${GATEWAY_POSTFIX}/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

server {
listen ${DEV_PORT_PREFIX}7;
location / {
proxy_pass http://0.0.0.0:${DEV_PORT_PREFIX}${GATEWAY_POSTFIX}/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}


server {
listen ${STAG_PORT_PREFIX}7;
location / {
proxy_pass http://0.0.0.0:${STAG_PORT_PREFIX}${GATEWAY_POSTFIX}/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}