forked from chrisfreeman-volunteer-apps/osbridge-volunteer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refacturing of the permissions, set up new tests, disabled template u…
…rls for now
- Loading branch information
1 parent
a98cd21
commit e20159b
Showing
11 changed files
with
174 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ coverage==3.7.1 | |
flake8==2.2.5 | ||
python-coveralls | ||
pytest-cov | ||
pytest-flakes | ||
pytest-flakes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
volunteerapp/volunteer/migrations/0009_auto_20150129_1840.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models, migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('volunteer', '0008_auto_20150125_1630'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='event', | ||
name='name', | ||
field=models.CharField(help_text='Please add a descriptive name.', max_length=100, unique=True, null=True), | ||
preserve_default=True, | ||
), | ||
migrations.AlterField( | ||
model_name='organization', | ||
name='name', | ||
field=models.CharField(help_text='Please add a descriptive name.', max_length=100, unique=True, null=True), | ||
preserve_default=True, | ||
), | ||
migrations.AlterField( | ||
model_name='shift', | ||
name='name', | ||
field=models.CharField(help_text='Please add a descriptive name.', max_length=100, unique=True, null=True), | ||
preserve_default=True, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
from rest_framework import permissions | ||
|
||
|
||
class IsAdminOrReadOnly(permissions.BasePermission): | ||
class IsObjectAdminOrReadOnly(permissions.BasePermission): | ||
""" | ||
Custom permission to only allow owners of an object to edit it. | ||
If the user is authenticated, (s)he can create new objects. | ||
""" | ||
|
||
def has_object_permission(self, request, view, obj): | ||
# Read permissions are allowed to any request, | ||
# so we'll always allow GET, HEAD or OPTIONS requests. | ||
if request.method in permissions.SAFE_METHODS: | ||
return True | ||
|
||
# For all other submissions | ||
# Write permissions are only allowed if user is admin of the object. | ||
return obj.admin.filter(id=request.user.id).exists() | ||
return (request.user.is_authenticated() | ||
and obj.admin.filter(id=request.user.id).exists()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,87 @@ | ||
from django.test import TestCase | ||
import json | ||
from django.core.urlresolvers import reverse | ||
|
||
# Create your tests here. | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
|
||
from volunteer.models import Organization # , Event, Shift, Task | ||
from users.models import Users | ||
|
||
|
||
class OrganizationTestCase(APITestCase): | ||
|
||
def setUp(self): # noqa | ||
""" | ||
Set up test user and a sample organization | ||
for testing the duplicate error message | ||
""" | ||
Users.objects.create_user( | ||
username='john', | ||
last_name='John', | ||
first_name='Crane', | ||
email='john@gmail.com', | ||
password='123' | ||
) | ||
Organization.objects.create( | ||
name='Duplicate Test Organization', | ||
description='Duplicate Description', | ||
) | ||
|
||
def test_create_organization_witout_auth(self): | ||
""" | ||
Test the status code if a unauthenticated POST | ||
request is submitted | ||
""" | ||
url = reverse('organization-list') | ||
data = { | ||
'name': 'Test Organization', | ||
'description': 'Test Description', | ||
} | ||
response = self.client.post(url, data, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) | ||
|
||
def test_create_organization_logged_in(self): | ||
""" | ||
Test the status code and returned dataif an | ||
authenticated POST request is submitted | ||
""" | ||
self.client.login(username='john', password='123') | ||
url = reverse('organization-list') | ||
data = { | ||
'name': 'Test Organization', | ||
'description': 'Test Description', | ||
} | ||
response_data = { | ||
'description': 'Test Description', | ||
'get_num_events': 0, | ||
'get_status_display': 'Draft Mode', | ||
'id': 3, | ||
'location': None, | ||
'name': 'Test Organization', | ||
'slug': 'test-organization', | ||
'status': 'DR' | ||
} | ||
|
||
response = self.client.post(url, data, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
self.assertJSONEqual(json.dumps(response.data), json.dumps(response_data)) | ||
self.client.logout() | ||
|
||
def test_create_duplicate_organization(self): | ||
""" | ||
Test the status code if an | ||
authenticated POST, but duplicated object | ||
is submitted | ||
""" | ||
self.client.login(username='john', password='123') | ||
url = reverse('organization-list') | ||
data = { | ||
'name': 'Duplicate Test Organization', | ||
'description': 'Duplicate Description', | ||
} | ||
response_data = {u'name': [u'This field must be unique.']} | ||
|
||
response = self.client.post(url, data, format='json') | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertJSONEqual(json.dumps(response.data), json.dumps(response_data)) | ||
self.client.logout() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters