Commit cd9e27cd authored by Calum Mackervoy's avatar Calum Mackervoy

added initial test

parent 43efee48
Pipeline #7275 failed with stage
in 1 minute and 22 seconds
from django.conf import settings
from django.conf.urls import url, include
from djangoldp.tests.models import Skill, JobOffer, Message, Conversation, Dummy, Task
from djangoldp.tests.models import Skill, JobOffer, Message, Conversation, Dummy, PermissionlessDummy, Task
from djangoldp.views import LDPViewSet
urlpatterns = [
......@@ -9,5 +9,6 @@ urlpatterns = [
url(r'^conversations/', LDPViewSet.urls(model=Conversation, nested_fields=["message_set"], permission_classes=())),
url(r'^tasks/', LDPViewSet.urls(model=Task, permission_classes=())),
url(r'^dummys/', LDPViewSet.urls(model=Dummy, permission_classes=[], lookup_field='slug',)),
url(r'^permissionless-dummys/', LDPViewSet.urls(model=PermissionlessDummy, permission_classes=[], lookup_field='slug',)),
]
......@@ -110,6 +110,19 @@ class LDPDummy(Model):
owner_perms = ['inherit', 'change', 'delete', 'control']
# model used in django-guardian permission tests (no anonymous etc permissions set)
class PermissionlessDummy(Model):
some = models.CharField(max_length=255, blank=True, null=True)
class Meta:
anonymous_perms = []
authenticated_perms = []
owner_perms = []
permissions = (
('view_permissionless_dummy', 'View Permissionless Dummy'),
)
class Invoice(Model):
title = models.CharField(max_length=255, blank=True, null=True)
date = models.DateField(blank=True, null=True)
......
......@@ -65,6 +65,7 @@ failures = test_runner.run_tests([
'djangoldp.tests.tests_ldp_model',
'djangoldp.tests.tests_save',
'djangoldp.tests.tests_user_permissions',
'djangoldp.tests.tests_guardian',
'djangoldp.tests.tests_anonymous_permissions',
'djangoldp.tests.tests_update',
'djangoldp.tests.tests_auto_author',
......
from django.contrib.auth import get_user_model
from rest_framework.test import APIClient, APITestCase
from guardian.shortcuts import assign_perm
from .models import PermissionlessDummy
class TestGuardianPermissions(APITestCase):
def setUp(self):
self.user = get_user_model().objects.create_user(username='john', email='jlennon@beatles.com', password='glass onion')
self.client = APIClient(enforce_csrf_checks=True)
self.client.force_authenticate(user=self.user)
# optional setup for testing PermissionlessDummy model with parameterised perms
def setUpGuardianDummyWithPerms(self, perms=[]):
self.dummy = PermissionlessDummy.objects.create(some='test')
model_name = PermissionlessDummy.Meta.model_name
for perm in perms:
assign_perm(perm + '_' + model_name, self.user, self.dummy)
# test that dummy with no permissions set returns no permissions
def test_get_dummy_no_permissions(self):
self.setUpGuardianDummyWithPerms()
response = self.client.get('/permissionless-dummys/')
self.assertEqual(response.status_code, 403)
print('response was: ' + str(response))
# TODO: test that dummy with permissions set enforces these permissions
# TODO: test custom guardian permissions are enforced
\ No newline at end of file
from django.contrib.auth import get_user_model
from rest_framework.test import APIClient, APITestCase
from guardian.shortcuts import assign_perm
from djangoldp.permissions import LDPPermissions
from .models import JobOffer
from .models import JobOffer, PermissionlessDummy
from djangoldp.views import LDPViewSet
import json
......@@ -10,11 +11,19 @@ import json
class TestUserPermissions(APITestCase):
def setUp(self):
user = get_user_model().objects.create_user(username='john', email='jlennon@beatles.com', password='glass onion')
self.user = get_user_model().objects.create_user(username='john', email='jlennon@beatles.com', password='glass onion')
self.client = APIClient(enforce_csrf_checks=True)
self.client.force_authenticate(user=user)
self.client.force_authenticate(user=self.user)
self.job = JobOffer.objects.create(title="job", slug="slug1")
# optional setup for testing PermissionlessDummy model with parameterised perms
def setUpGuardianDummyWithPerms(self, perms=[]):
self.dummy = PermissionlessDummy.objects.create(some='test')
model_name = PermissionlessDummy.Meta.model_name
for perm in perms:
assign_perm(perm + '_' + model_name, self.user, self.dummy)
def test_get_for_authenticated_user(self):
response = self.client.get('/job-offers/')
self.assertEqual(response.status_code, 200)
......@@ -38,3 +47,7 @@ class TestUserPermissions(APITestCase):
response = self.client.patch('/job-offers/' + str(self.job.slug) + "/",
content_type='application/ld+json')
self.assertEqual(response.status_code, 200)
def test_temp(self):
print('hello')
self.assertTrue(True)
Markdown is supported
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