Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sondre Malerud committed Apr 3, 2025
1 parent 15b2211 commit 6886305
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/deploy_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ on:
- "docker-compose.yml"

jobs:
test:
runs-on: self-hosted
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Build and run tests
run: |
docker build -f backend/Dockerfile.test -t secfit_test .
docker run --rm secfit_test
deploy:
runs-on: self-hosted
env:
Expand Down
5 changes: 3 additions & 2 deletions backend/tests/Dockerfile.test → backend/Dockerfile.test
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ WORKDIR /app

EXPOSE 8000

# Runs migrations and starts the server
CMD ["sh", "-c", "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"]
# Runs tests
CMD ["sh", "-c", "python manage.py test"]
#CMD ["sh", "-c", "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"]
48 changes: 47 additions & 1 deletion backend/tests/test_user.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from django.test import TestCase
from users.models import User
from users.serializers import UserSerializer
from workouts.serializers import ExerciseSerializer
from comments.serializers import CommentSerializer


class TestUser(TestCase):
#TC_001
def test_signing_up_with_invalid_email(self):
data = {
"username": "test",
Expand All @@ -19,6 +23,7 @@ def test_signing_up_with_invalid_email(self):
self.assertFalse(serializer.is_valid())
self.assertIn('email', serializer.errors)

#TC_002
def test_password_below_minimum_length(self):
data = {
"username": "test",
Expand All @@ -33,4 +38,45 @@ def test_password_below_minimum_length(self):
}
serializer = UserSerializer(data=data)
self.assertFalse(serializer.is_valid())
self.assertIn('password', serializer.errors)
self.assertIn('password', serializer.errors)

#New test case
def test_exercise_name_exceeds_max_length(self):
long_name = "A" * 100_000 # 100000 characters, going far over the 100 character limit
data = {
"name": long_name,
"description": "blablabla",
"unit": "reps"
}
serializer = ExerciseSerializer(data=data)
self.assertFalse(serializer.is_valid())
self.assertIn("name", serializer.errors)

#New test case
def test_duplicate_username(self):
User.objects.create_user(username="testuser", password="Tdt4141!?@abc", email="testuser@mail.com")

# creating a new user with thte same username
user_data = {
"username": "testuser", #duplicate
"password": "Tdt4141!?@abc",
"password1": "Tdt4141!?@abc",
"email": "notthesameuser@mail.com",
"isCoach": False,
}

serializer = UserSerializer(data=user_data)
self.assertFalse(serializer.is_valid())
self.assertIn("username", serializer.errors)

#New test case
def test_comment_without_content(self):
comment_data = {
"owner": "mock_user",
"workout": "mock_workout",
"content": "", #empty comment
}

serializer = CommentSerializer(data=comment_data)
self.assertFalse(serializer.is_valid())
self.assertIn("content", serializer.errors)

0 comments on commit 6886305

Please sign in to comment.