Skip to content

Commit

Permalink
Fix permissions handling when testing in other projects (Fix #16)
Browse files Browse the repository at this point in the history
  • Loading branch information
noirbizarre committed Apr 30, 2013
1 parent 3dc749d commit acc00ee
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
12 changes: 12 additions & 0 deletions djangojs/fake/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
from django.db import models


class FakeModel(models.Model):
something = models.CharField(max_length=256)

class Meta:
permissions = (
("do_something", "Can do something"),
("do_something_else", "Can do something else"),
)
3 changes: 1 addition & 2 deletions djangojs/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@
)

ROOT_URLCONF = 'djangojs.test_urls'
ROOT_URLCONF = 'djangojs.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'djangojs.wsgi.application'
Expand All @@ -124,7 +123,7 @@

PROJECT_APPS = (
'djangojs',
'djangojs.fake',
# 'djangojs.fake',
)

INSTALLED_APPS = (
Expand Down
36 changes: 20 additions & 16 deletions djangojs/tests/test_context.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# -*- coding: utf-8 -*-
import json

from django.conf import global_settings
from django.contrib.auth.management import create_permissions
from django.contrib.auth.models import User
from django.contrib.contenttypes.management import update_contenttypes
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models import get_app
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
Expand All @@ -13,17 +16,6 @@
from djangojs.utils import ContextSerializer


class FakeModel(models.Model):
something = models.CharField(max_length=256)

class Meta:
app_label = 'fake'
permissions = (
("do_smething", "Can do something"),
("do_something_else", "Can do something else"),
)


class ContextTestMixin(object):

def setUp(self):
Expand Down Expand Up @@ -120,14 +112,17 @@ def test_super_user(self):

def test_user_permissions(self):
'''Should list permissions'''
self.fake_permissions()
result = self.process_request(True)

self.assertIn('permissions', result['user'])
self.assertTrue(isinstance(result['user']['permissions'], (list, tuple)))

# Default permissions
for perm in ('add', 'change', 'delete'):
self.assertIn('fake.%s_fakemodel' % perm, result['user']['permissions'])
# Custom permissions
for perm in ('do_smething', 'do_something_else'):
for perm in ('do_something', 'do_something_else'):
self.assertIn('fake.%s' % perm, result['user']['permissions'])

def test_user_without_permissions(self):
Expand All @@ -137,11 +132,18 @@ def test_user_without_permissions(self):
self.assertTrue(isinstance(result['user']['permissions'], (list, tuple)))
self.assertEqual(len(result['user']['permissions']), 0)

def fake_permissions(self):
''' Add fake app missing content types and permissions'''
app = get_app('fake')
update_contenttypes(app, None, 0)
create_permissions(app, None, 0)


@override_settings(
TEMPLATE_CONTEXT_PROCESSORS=global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
'djangojs.tests.custom_processor',
)
),
INSTALLED_APPS=['djangojs', 'djangojs.fake']
)
class ContextAsDictTest(ContextTestMixin, TestCase):

Expand All @@ -152,7 +154,8 @@ def get_result(self, request):
@override_settings(
TEMPLATE_CONTEXT_PROCESSORS=global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
'djangojs.tests.custom_processor',
)
),
INSTALLED_APPS=['djangojs', 'djangojs.fake']
)
class ContextAsJsonTest(ContextTestMixin, TestCase):

Expand All @@ -163,7 +166,8 @@ def get_result(self, request):
@override_settings(
TEMPLATE_CONTEXT_PROCESSORS=global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
'djangojs.tests.custom_processor',
)
),
INSTALLED_APPS=['djangojs', 'djangojs.fake']
)
class ContextJsonViewTest(TestCase):
urls = 'djangojs.test_urls'
Expand Down

0 comments on commit acc00ee

Please sign in to comment.