Permalink
Browse files

[bug 741789] Stop using fixtures in access tests.

  • Loading branch information...
rlr committed Jul 9, 2013
1 parent 7269326 commit 499c3e971c39b6da8d6a5742d0e69f75e5a50079
@@ -1,13 +1,13 @@
-from django.contrib.auth.models import User
+from django.contrib.contenttypes.models import ContentType
import test_utils
from authority.models import Permission
from nose.tools import eq_
from kitsune import access
-from kitsune.forums.models import Forum, Thread
from kitsune.sumo.tests import TestCase, with_save
from kitsune.sumo.urlresolvers import reverse
+from kitsune.users.tests import user
@with_save
@@ -16,54 +16,66 @@ def permission(**kwargs):
kwargs['approved'] = True
return Permission(**kwargs)
+
class AccessTests(TestCase):
"""Test stuff in access/__init__.py"""
- fixtures = ['users.json', 'posts.json', 'forums_permissions.json']
-
def setUp(self):
url = reverse('forums.threads', args=[u'test-forum'])
self.context = {'request': test_utils.RequestFactory().get(url)}
- self.forum_1 = Forum.objects.get(pk=1)
- self.forum_2 = Forum.objects.get(pk=2)
def test_admin_perm_thread(self):
"""Super user can do anything on any forum."""
- admin = User.objects.get(pk=1)
+ from kitsune.forums.tests import restricted_forum
+ f1 = restricted_forum()
+ f2 = restricted_forum()
+
+ admin = user(is_staff=True, is_superuser=True, save=True)
# Loop over all forums perms and both forums
perms = ('thread_edit_forum', 'thread_delete_forum', 'post_edit_forum',
'thread_sticky_forum', 'thread_locked_forum',
- 'post_delete_forum')
- forums = (self.forum_1, self.forum_2)
+ 'post_delete_forum', 'view_in_forum')
for perm in perms:
- for forum in forums:
+ for forum in [f1, f2]:
assert access.has_perm(admin, 'forums_forum.' + perm, forum)
def test_util_has_perm_or_owns_sanity(self):
"""Sanity check for access.has_perm_or_owns."""
- me = User.objects.get(pk=118533)
- my_t = Thread.objects.filter(creator=me)[0]
- other_t = Thread.objects.exclude(creator=me)[0]
+ from kitsune.forums.tests import thread
+ me = user(save=True)
+ my_t = thread(creator=me, save=True)
+ other_t = thread(save=True)
perm = 'forums_forum.thread_edit_forum'
- allowed = access.has_perm_or_owns(me, perm, my_t, self.forum_1)
+ allowed = access.has_perm_or_owns(me, perm, my_t, my_t.forum)
eq_(allowed, True)
- allowed = access.has_perm_or_owns(me, perm, other_t, self.forum_1)
+ allowed = access.has_perm_or_owns(me, perm, other_t, other_t.forum)
eq_(allowed, False)
def test_has_perm_per_object(self):
"""Assert has_perm checks per-object permissions correctly."""
- user = User.objects.get(pk=47963)
- perm = 'forums_forum.thread_edit_forum'
- assert access.has_perm(user, perm, self.forum_1)
- assert not access.has_perm(user, perm, self.forum_2)
+ from kitsune.forums.tests import restricted_forum
+ f1 = restricted_forum()
+ f2 = restricted_forum()
+
+ # Give user permission to one of the forums
+ u = user(save=True)
+ perm = 'forums_forum.view_in_forum'
+ ct = ContentType.objects.get_for_model(f1)
+ permission(codename=perm, content_type=ct,
+ object_id=f1.id, user=u, save=True)
+ assert access.has_perm(u, perm, f1)
+ assert not access.has_perm(u, perm, f2)
def test_perm_is_defined_on(self):
"""Test permission relationship
Test whether we check for permission relationship, independent
of whether the permission is actually assigned to anyone.
"""
+ from kitsune.forums.tests import forum, restricted_forum
+ f1 = restricted_forum()
+ f2 = forum(save=True)
perm = 'forums_forum.view_in_forum'
- assert access.perm_is_defined_on(perm, Forum.objects.get(pk=3))
- assert not access.perm_is_defined_on(perm, Forum.objects.get(pk=2))
+ assert access.perm_is_defined_on(perm, f1)
+ assert not access.perm_is_defined_on(perm, f2)
@@ -1,4 +1,4 @@
-from django.contrib.auth.models import User, AnonymousUser
+from django.contrib.auth.models import AnonymousUser
from django.http import HttpResponse
import test_utils
@@ -7,15 +7,14 @@
from kitsune.access.decorators import (
logout_required, login_required, permission_required)
from kitsune.sumo.tests import TestCase
+from kitsune.users.tests import user
def simple_view(request):
return HttpResponse()
class LogoutRequiredTestCase(TestCase):
- fixtures = ['users.json']
-
def test_logged_out_default(self):
request = test_utils.RequestFactory().get('/foo')
request.user = AnonymousUser()
@@ -25,14 +24,14 @@ def test_logged_out_default(self):
def test_logged_in_default(self):
request = test_utils.RequestFactory().get('/foo')
- request.user = User.objects.get(username='jsocol')
+ request.user = user(save=True)
view = logout_required(simple_view)
response = view(request)
eq_(302, response.status_code)
def test_logged_in_argument(self):
request = test_utils.RequestFactory().get('/foo')
- request.user = User.objects.get(username='jsocol')
+ request.user = user(save=True)
view = logout_required('/bar')(simple_view)
response = view(request)
eq_(302, response.status_code)
@@ -42,15 +41,13 @@ def test_no_redirect_ajax(self):
"""Ajax requests should not redirect."""
request = test_utils.RequestFactory().get('/foo')
request.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
- request.user = User.objects.get(username='jsocol')
+ request.user = user(save=True)
view = logout_required(simple_view)
response = view(request)
eq_(403, response.status_code)
class LoginRequiredTestCase(TestCase):
- fixtures = ['users.json']
-
def test_logged_out_default(self):
request = test_utils.RequestFactory().get('/foo')
request.user = AnonymousUser()
@@ -61,29 +58,23 @@ def test_logged_out_default(self):
def test_logged_in_default(self):
"""Active user login."""
request = test_utils.RequestFactory().get('/foo')
- request.user = User.objects.get(username='jsocol')
+ request.user = user(save=True)
view = login_required(simple_view)
response = view(request)
eq_(200, response.status_code)
def test_logged_in_inactive(self):
"""Inactive user login not allowed by default."""
request = test_utils.RequestFactory().get('/foo')
- user = User.objects.get(username='rrosario')
- user.is_active = False
- user.save()
- request.user = user
+ request.user = user(is_active=False, save=True)
view = login_required(simple_view)
response = view(request)
eq_(302, response.status_code)
def test_logged_in_inactive_allow(self):
"""Inactive user login explicitly allowed."""
request = test_utils.RequestFactory().get('/foo')
- user = User.objects.get(username='rrosario')
- user.is_active = False
- user.save()
- request.user = user
+ request.user = user(is_active=False, save=True)
view = login_required(simple_view, only_active=False)
response = view(request)
eq_(200, response.status_code)
@@ -99,8 +90,6 @@ def test_no_redirect_ajax(self):
class PermissionRequiredTestCase(TestCase):
- fixtures = ['users.json']
-
def test_logged_out_default(self):
request = test_utils.RequestFactory().get('/foo')
request.user = AnonymousUser()
@@ -110,25 +99,22 @@ def test_logged_out_default(self):
def test_logged_in_default(self):
request = test_utils.RequestFactory().get('/foo')
- request.user = User.objects.get(username='jsocol')
+ request.user = user(save=True)
view = permission_required('perm')(simple_view)
response = view(request)
eq_(403, response.status_code)
def test_logged_in_inactive(self):
"""Inactive user is denied access."""
request = test_utils.RequestFactory().get('/foo')
- user = User.objects.get(username='admin')
- user.is_active = False
- user.save()
- request.user = user
+ request.user = user(is_active=False, save=True)
view = permission_required('perm')(simple_view)
response = view(request)
eq_(403, response.status_code)
def test_logged_in_admin(self):
request = test_utils.RequestFactory().get('/foo')
- request.user = User.objects.get(username='admin')
+ request.user = user(is_staff=True, is_superuser=True, save=True)
view = permission_required('perm')(simple_view)
response = view(request)
eq_(200, response.status_code)
@@ -1,139 +0,0 @@
-[
- {
- "pk": 1,
- "model": "authority.permission",
- "fields": {
- "date_requested": "2010-05-20 10:37:22",
- "group": 1,
- "creator": 1,
- "object_id": 1,
- "content_type": ["forums", "forum"],
- "codename": "forums_forum.thread_edit_forum",
- "approved": 1,
- "date_approved": "2010-05-20 10:39:57"
- }
- },
- {
- "pk": 2,
- "model": "authority.permission",
- "fields": {
- "date_requested": "2010-05-20 10:37:22",
- "group": 1,
- "creator": 1,
- "object_id": 1,
- "content_type": ["forums", "forum"],
- "codename": "forums_forum.post_edit_forum",
- "approved": 1,
- "date_approved": "2010-05-20 10:37:22"
- }
- },
- {
- "pk": 3,
- "model": "authority.permission",
- "fields": {
- "date_requested": "2010-05-20 10:37:22",
- "group": 1,
- "creator": 1,
- "object_id": 1,
- "content_type": ["forums", "forum"],
- "codename": "forums_forum.post_delete_forum",
- "approved": 1,
- "date_approved": "2010-05-20 10:37:22"
- }
- },
- {
- "pk": 4,
- "model": "authority.permission",
- "fields": {
- "date_requested": "2010-05-20 10:37:22",
- "group": 1,
- "creator": 1,
- "object_id": 1,
- "content_type": ["forums", "forum"],
- "codename": "forums_forum.thread_delete_forum",
- "approved": 1,
- "date_approved": "2010-05-20 10:37:22"
- }
- },
- {
- "pk": 5,
- "model": "authority.permission",
- "fields": {
- "date_requested": "2010-05-20 10:37:22",
- "group": 1,
- "creator": 1,
- "object_id": 1,
- "content_type": ["forums", "forum"],
- "codename": "forums_forum.thread_sticky_forum",
- "approved": 1,
- "date_approved": "2010-05-20 10:37:22"
- }
- },
- {
- "pk": 6,
- "model": "authority.permission",
- "fields": {
- "date_requested": "2010-08-10 10:37:22",
- "group": 1,
- "creator": 1,
- "object_id": 1,
- "content_type": ["forums", "forum"],
- "codename": "forums_forum.thread_move_forum",
- "approved": 1,
- "date_approved": "2010-08-10 10:37:22"
- }
- },
- {
- "pk": 7,
- "model": "authority.permission",
- "fields": {
- "date_requested": "2010-08-10 10:37:22",
- "group": 1,
- "creator": 1,
- "object_id": 2,
- "content_type": ["forums", "forum"],
- "codename": "forums_forum.thread_move_forum",
- "approved": 1,
- "date_approved": "2010-08-10 10:37:22"
- }
- },
- {
- "pk": 8,
- "model": "authority.permission",
- "fields": {
- "date_requested": "2010-05-20 10:37:22",
- "creator": 1,
- "object_id": 3,
- "content_type": ["forums", "forum"],
- "codename": "forums_forum.view_in_forum",
- "approved": 1,
- "date_approved": "2010-05-20 10:37:22"
- }
- },
- {
- "pk": 9,
- "model": "authority.permission",
- "fields": {
- "date_requested": "2010-05-20 10:37:22",
- "creator": 1,
- "object_id": 3,
- "content_type": ["forums", "forum"],
- "codename": "forums_forum.post_in_forum",
- "approved": 1,
- "date_approved": "2010-05-20 10:37:22"
- }
- },
- {
- "pk": 10,
- "model": "authority.permission",
- "fields": {
- "date_requested": "2010-05-20 10:37:22",
- "creator": 1,
- "object_id": 4,
- "content_type": ["forums", "forum"],
- "codename": "forums_forum.post_in_forum",
- "approved": 1,
- "date_approved": "2010-05-20 10:37:22"
- }
- }
-]
Oops, something went wrong.

0 comments on commit 499c3e9

Please sign in to comment.