Commit f41f3eea authored by Håvard Farestveit's avatar Håvard Farestveit
Browse files

Added tests for workouts/permissions.py #2

parent 12af25f4
Pipeline #111954 passed with stages
in 1 minute and 6 seconds
......@@ -13,7 +13,6 @@ test-backend:
image: python:3.8.5-slim
before_script:
- apt-get clean
- python -V
- pip install -r backend/secfit/requirements.txt
script:
- cd backend/secfit
......
"""
Tests for the workouts application.
"""
from rest_framework import permissions
from workouts.permissions import *
from users.models import User
from workouts.models import Workout
from django.test import TestCase
import datetime
import pytz
# Create your tests here.
class MockRequest():
def __init__(self):
self.method = ""
self.data = ""
self.user = None
class MockWorkout():
def __init__(self):
try:
user = User.objects.get(pk='1')
except:
user = User.objects.create()
workout_data = {
"name": "Test Workout",
"date": datetime.datetime(2021, 2, 19, 9, 33, 0, 0, pytz.UTC),
"notes": "notes",
"visibility": "PU",
"owner": user
}
self.workout = Workout.objects.create(**workout_data)
self.workout.owner.coach = User()
class IsOwnerTestCase(TestCase):
def setUp(self):
x = 1;
self.is_owner = IsOwner()
self.request = MockRequest()
self.workout = MockWorkout()
self.request.user = User()
def test_has_object_permission(self):
self.assertFalse(self.is_owner.has_object_permission(self.request, None, self.workout.workout))
self.request.user = self.workout.workout.owner
self.assertTrue(self.is_owner.has_object_permission(self.request, None, self.workout.workout))
class IsOwnerOfWorkoutTestCase(TestCase):
def setUp(self):
self.is_owner_of_workout = IsOwnerOfWorkout()
self.request = MockRequest()
self.workout = MockWorkout()
def test_has_permission_method(self):
request = MockRequest()
request.method = "GET"
self.assertTrue(self.is_owner_of_workout.has_permission(request, None))
def test_has_permission_workout(self):
request = MockRequest()
request.method = "POST"
request.data = {"workout": ""}
self.assertFalse(self.is_owner_of_workout.has_permission(request, None))
def test_has_permission_user(self):
request = MockRequest()
request.method = "POST"
request.user = self.workout.workout.owner
request.data = {"workout": "http://localhost:8000/api/workouts/1/"}
self.assertTrue(self.is_owner_of_workout.has_permission(request, None))
def test_has_object_permission(self):
self.assertFalse(self.is_owner_of_workout.has_object_permission(self.request, None, self.workout))
self.request.user = self.workout.workout.owner
self.assertTrue(self.is_owner_of_workout.has_object_permission(self.request, None, self.workout))
class IsCoachAndVisibleToCoachTestCase(TestCase):
def setUp(self):
self.is_coach_and_visable_to_coach = IsCoachAndVisibleToCoach()
self.request = MockRequest()
self.request.user = User()
self.workout = MockWorkout()
def test_has_object_permission(self):
self.assertFalse(self.is_coach_and_visable_to_coach.has_object_permission(
self.request,
None,
self.workout.workout
))
self.workout.workout.owner.coach = self.request.user
self.assertTrue(self.is_coach_and_visable_to_coach.has_object_permission(
self.request,
None,
self.workout.workout
))
class IsCoachOfWorkoutAndVisibleToCoachTestCase(TestCase):
def setUp(self):
self.is_coach_of_workout_and_visable_to_coach = IsCoachOfWorkoutAndVisibleToCoach()
self.request = MockRequest()
self.request.user = User()
self.workout = MockWorkout()
def test_has_object_permission(self):
self.assertFalse(self.is_coach_of_workout_and_visable_to_coach.has_object_permission(
self.request,
None,
self.workout
))
self.workout.workout.owner.coach = self.request.user
self.assertTrue(self.is_coach_of_workout_and_visable_to_coach.has_object_permission(
self.request,
None,
self.workout
))
class IsPublicTestCase(TestCase):
def setUp(self):
self.workout = MockWorkout()
self.is_public = IsPublic()
def test_has_object_permission(self):
self.assertTrue(self.is_public.has_object_permission(
None,
None,
self.workout.workout)
)
self.workout.workout.visibility = "CO"
self.assertFalse(self.is_public.has_object_permission(
None,
None,
self.workout.workout)
)
class IsWorkoutPublicTestCase(TestCase):
def setUp(self):
self.workout = MockWorkout()
self.is_workout_public = IsWorkoutPublic()
def test_has_object_permission(self):
self.assertTrue(self.is_workout_public.has_object_permission(
None,
None,
self.workout)
)
self.workout.workout.visibility = "N"
self.assertFalse(self.is_workout_public.has_object_permission(
None,
None,
self.workout)
)
class IsReadOnlyTestCase(TestCase):
def setUp(self):
self.is_read_only = IsReadOnly()
self.request = MockRequest()
self.request.method = permissions.SAFE_METHODS.__getitem__(1)
def test_has_object_permission(self):
self.assertTrue(self.is_read_only.has_object_permission(self.request, None, None))
def test_assert_true(self):
self.assertTrue(1==1)
\ No newline at end of file
self.request.method = None
self.assertFalse(self.is_read_only.has_object_permission(self.request, None, None))
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