Commit 898fd899 authored by Tomas Vaagen Gule's avatar Tomas Vaagen Gule
Browse files

#10 Update tests

parent 990960aa
......@@ -4,7 +4,6 @@ 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 *
......@@ -34,12 +33,25 @@ class WorkoutPermissionTestCase(TestCase):
'street_address': ''
}
other_values = {
'email': 'test@test.com',
'username': 'other',
'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.other = get_user_model().objects.create_user(**other_values)
self.other.save()
self.workout_not_public = Workout(owner=self.user, date="2022-12-01 12:12")
self.workout_not_public.save()
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="")
......@@ -47,48 +59,53 @@ class WorkoutPermissionTestCase(TestCase):
self.factory = APIRequestFactory()
self.request = self.factory.get('/workout/1/')
self.request.user=self.user
self.request_user = self.factory.get('/workout/1/')
self.request_user.user=self.user
self.request_coach = self.factory.get('/workout/1/')
self.request_coach.user=self.coach
self.request_other = self.factory.get('/workout/1/')
self.request_other.user=self.other
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))
self.assertTrue(IsOwner.has_object_permission(IsOwner, view ="", request=self.request_user, obj=self.workout))
self.assertFalse(IsOwner.has_object_permission(IsOwner, view ="", request=self.request_coach, obj=self.workout))
self.assertFalse(IsOwner.has_object_permission(IsOwner, view ="", request=self.request_other, 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/', format='json')
request_get = self.factory.get('/workout/1/', 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.assertFalse(IsOwnerOfWorkout.has_object_permission(IsOwnerOfWorkout, view ="", request=self.request_other, obj=self.workoutFile))
self.assertFalse(IsOwnerOfWorkout.has_object_permission(IsOwnerOfWorkout, view ="", request=self.request_coach, obj=self.workoutFile))
self.assertTrue(IsOwnerOfWorkout.has_object_permission(IsOwnerOfWorkout, view ="", request=request_get, obj=self.workoutFile))
self.assertTrue(IsOwnerOfWorkout.has_object_permission(IsOwnerOfWorkout, view ="", request=request_post, obj=self.workoutFile))
self.assertTrue(IsOwnerOfWorkout.has_permission(IsOwnerOfWorkout, view ="", request=request_get))
self.assertTrue(IsOwnerOfWorkout.has_permission(IsOwnerOfWorkout, view ="", request=request_post))
self.assertTrue(IsOwnerOfWorkout.has_permission(IsOwnerOfWorkout, view ="", request=self.request_coach))
self.assertTrue(IsOwnerOfWorkout.has_permission(IsOwnerOfWorkout, view ="", request=self.request_other))
request_post.data = {}
self.assertFalse(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=self.request_coach, obj=self.workout))
self.assertFalse(IsCoachAndVisibleToCoach.has_object_permission(IsCoachAndVisibleToCoach, view ="", request=self.request_user, obj=self.workout))
self.assertFalse(IsCoachAndVisibleToCoach.has_object_permission(IsCoachAndVisibleToCoach, view ="", request=self.request_other, obj=self.workout))
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))
self.assertTrue(IsCoachOfWorkoutAndVisibleToCoach.has_object_permission(IsCoachOfWorkoutAndVisibleToCoach, view ="", request=self.request_coach, obj=self.workoutFile))
self.assertFalse(IsCoachOfWorkoutAndVisibleToCoach.has_object_permission(IsCoachOfWorkoutAndVisibleToCoach, view ="", request=self.request_user, obj=self.workoutFile))
self.assertFalse(IsCoachOfWorkoutAndVisibleToCoach.has_object_permission(IsCoachOfWorkoutAndVisibleToCoach, view ="", request=self.request_other, obj=self.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))
self.assertTrue(IsPublic.has_object_permission(IsPublic, view ="", request=self.request_other, obj=self.workout))
self.assertFalse(IsPublic.has_object_permission(IsPublic, view ="", request=self.request_other, obj=self.workout_not_public))
def test_is_workout_public(self):
request = self.factory.get('/workout/1/')
......@@ -96,4 +113,4 @@ class WorkoutPermissionTestCase(TestCase):
self.assertTrue(IsWorkoutPublic.has_object_permission(IsWorkoutPublic, view ="", request=request, obj=self.workoutFile))
def test_is_read_only(self):
self.assertTrue(IsReadOnly.has_object_permission(IsReadOnly, view ="", request=self.request, obj=self.workoutFile))
\ No newline at end of file
self.assertTrue(IsReadOnly.has_object_permission(IsReadOnly, view ="", request=self.request_user, obj=self.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