Commit 9cf896fb authored by Tomas Vaagen Gule's avatar Tomas Vaagen Gule
Browse files

#10 First draft of tests

parent 73a0e0ae
Pipeline #164909 passed with stages
in 2 minutes and 8 seconds
from django.test import TestCase
from rest_framework.test import APIRequestFactory
from .serializers import UserSerializer
from .models import User
# Create your tests here.
class UserSerializerTestCase(TestCase):
def setUp(self):
self.user_values = {
'email': 'test@test.com',
'username': 'tes99566t',
'password': 'password1',
'phone_number': '12345678',
'country': 'norway',
'city': 't',
'street_address': ''
}
self.factory = APIRequestFactory()
self.request = self.factory.post('/users/', self.user_values, format='json')
self.serializer = UserSerializer
def test_create(self):
user = self.serializer.create(self.serializer, self.user_values)
print(user)
......@@ -2,5 +2,97 @@
Tests for the workouts application.
"""
from django.test import TestCase
from django.contrib.auth import get_user_model
from rest_framework.test import APIRequestFactory
from rest_framework.test import force_authenticate
from .models import Workout, WorkoutFile
from .permissions import *
# Create your tests here.
class WorkoutPermissionTestCase(TestCase):
def setUp(self):
user_values = {
'email': 'test@test.com',
'username': 'user',
'password': 'password1',
'phone_number': '',
'country': '',
'city': '',
'street_address': '',
}
coach_values = {
'email': 'test@test.com',
'username': 'coach',
'password': 'password1',
'phone_number': '',
'country': '',
'city': '',
'street_address': ''
}
self.coach = get_user_model().objects.create_user(**coach_values)
self.coach.save()
self.user = get_user_model().objects.create_user(**user_values, coach=self.coach)
self.user.save()
self.workout = Workout(owner=self.user, date="2022-12-01 12:12")
self.workout = Workout(owner=self.user, date="2022-12-01 12:12", visibility="PU")
self.workout.save()
self.workoutFile = WorkoutFile.objects.create(owner=self.user, workout=self.workout, file="")
self.workoutFile.save()
self.factory = APIRequestFactory()
def test_is_owner(self):
request = self.factory.get('/workout/1/')
request.user=self.user
self.assertTrue(IsOwner.has_object_permission(IsOwner, view ="", request=request, obj=self.workout))
def test_owner_of_workout(self):
workoutFile = WorkoutFile.objects.create(owner=self.user, workout=self.workout, file="")
request_post = self.factory.post('/workout/1/', {'workout': 2}, format='json')
request_get = self.factory.get('/workout/1/', {'workout': 2}, format='json')
request_post.user=self.user
request_get.user=self.user
request_post.data={"workout": "/workoiut/1/"}
self.assertEqual(request_get.method, "GET")
self.assertTrue(IsOwnerOfWorkout.has_object_permission(IsOwnerOfWorkout, view ="", request=request_get, obj=workoutFile))
self.assertTrue(IsOwnerOfWorkout.has_object_permission(IsOwnerOfWorkout, view ="", request=request_post, obj=workoutFile))
self.assertTrue(IsOwnerOfWorkout.has_permission(IsOwnerOfWorkout, view ="", request=request_get))
self.assertTrue(IsOwnerOfWorkout.has_permission(IsOwnerOfWorkout, view ="", request=request_post))
def test_is_coach_and_visible_to_coach(self):
request = self.factory.get('/workout/1/')
request.user=self.coach
self.assertTrue(IsCoachAndVisibleToCoach.has_object_permission(IsCoachAndVisibleToCoach, view ="", request=request, obj=self.workout))
def test_is_coach_of_workout_and_visible_to_coach(self):
workoutFile = WorkoutFile.objects.create(owner=self.user, workout=self.workout, file="")
request = self.factory.get('/workout/1/')
request.user=self.coach
self.assertTrue(IsCoachOfWorkoutAndVisibleToCoach.has_object_permission(IsCoachOfWorkoutAndVisibleToCoach, view ="", request=request, obj=workoutFile))
request.user=self.user
self.assertFalse(IsCoachOfWorkoutAndVisibleToCoach.has_object_permission(IsCoachOfWorkoutAndVisibleToCoach, view ="", request=request, obj=workoutFile))
def test_is_public(self):
request = self.factory.get('/workout/1/')
request.user=self.coach
self.assertTrue(IsPublic.has_object_permission(IsPublic, view ="", request=request, obj=self.workout))
def test_is_workout_public(self):
request = self.factory.get('/workout/1/')
request.user=self.coach
self.assertTrue(IsWorkoutPublic.has_object_permission(IsWorkoutPublic, view ="", request=request, obj=self.workoutFile))
def test_is_read_only(self):
workoutFile = WorkoutFile.objects.create(owner=self.user, workout=self.workout, file="")
request = self.factory.get('/workout/1/')
request.user=self.user
IsReadOnly.has_object_permission(IsReadOnly, view ="", request=request, obj=workoutFile)
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment