Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
Bug 1116335 - Use transaction test case for contentflagging tests.
Browse files Browse the repository at this point in the history
This also adds some refactored base test cases that offer transaction support.
  • Loading branch information
jezdez committed Apr 24, 2015
1 parent e32cc97 commit f3c6126
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 56 deletions.
14 changes: 7 additions & 7 deletions kuma/contentflagging/models.py
Expand Up @@ -53,13 +53,13 @@ def flag(self, request, object, flag_type, explanation, recipients=None):
user, ip, user_agent, unique_hash = get_unique(content_type, object.pk,
request=request)

cf = ContentFlag.objects.get_or_create(
unique_hash=unique_hash,
defaults=dict(content_type=content_type,
object_pk=object.pk, ip=ip,
user_agent=user_agent, user=user,
flag_type=flag_type,
explanation=explanation))
defaults = dict(content_type=content_type,
object_pk=object.pk, ip=ip,
user_agent=user_agent, user=user,
flag_type=flag_type,
explanation=explanation)
cf = ContentFlag.objects.get_or_create(unique_hash=unique_hash,
defaults=defaults)

if recipients:
subject = _("{object} Flagged")
Expand Down
73 changes: 33 additions & 40 deletions kuma/contentflagging/tests.py
@@ -1,8 +1,8 @@
from django.core.exceptions import MultipleObjectsReturned

from django.contrib.auth.models import AnonymousUser

from django.contrib.contenttypes.models import ContentType
from django.db import IntegrityError
from django.http import HttpRequest

from nose.tools import eq_, ok_
Expand All @@ -13,36 +13,39 @@
from kuma.demos.models import Submission
from kuma.demos.tests.test_models import save_valid_submission
from kuma.wiki.models import Document
from kuma.users.tests import UserTestCase
from kuma.users.tests import UserTransactionTestCase

from .models import ContentFlag
from .utils import get_unique


class DemoPackageTest(UserTestCase):
fixtures = UserTestCase.fixtures + ['wiki/documents.json']
def _mock_request(user=None, ip='192.168.123.123',
user_agent='FakeBrowser 1.0'):
request = HttpRequest()
request.user = user and user or AnonymousUser()
request.method = 'GET'
request.META['REMOTE_ADDR'] = ip
request.META['HTTP_USER_AGENT'] = user_agent
return request


class DemoPackageTest(UserTransactionTestCase):
fixtures = UserTransactionTestCase.fixtures + ['wiki/documents.json']

def setUp(self):
super(DemoPackageTest, self).setUp()
self.user1 = self.user_model.objects.create_user('tester1',
'tester2@tester.com', 'tester1')
'tester2@tester.com',
'tester1')
self.user2 = self.user_model.objects.create_user('tester2',
'tester2@tester.com', 'tester2')

def mk_request(self, user=None, ip='192.168.123.123',
user_agent='FakeBrowser 1.0'):
request = HttpRequest()
request.user = user and user or AnonymousUser()
request.method = 'GET'
request.META['REMOTE_ADDR'] = ip
request.META['HTTP_USER_AGENT'] = user_agent
return request
'tester2@tester.com',
'tester2')

@attr('bug694544')
def test_bug694544(self):
"""Bug 694544: unicode character in request details should not break"""
try:
request = self.mk_request(user_agent=u"Some\xef\xbf\xbdbrowser")
request = _mock_request(user_agent=u"Some\xef\xbf\xbdbrowser")

obj_1 = self.user2
obj_1_ct = ContentType.objects.get_for_model(obj_1)
Expand All @@ -57,7 +60,7 @@ def test_bad_multiple_flags(self):
Force multiple flags, possibly result of race condition,
ensure graceful handling
"""
request = self.mk_request()
request = _mock_request()

obj_1 = self.user2
obj_1_ct = ContentType.objects.get_for_model(obj_1)
Expand All @@ -66,17 +69,17 @@ def test_bad_multiple_flags(self):

# Create an initial record directly.
f1 = ContentFlag(content_type=obj_1_ct, object_pk=obj_1.pk,
flag_type="Broken thing",
ip=ip, user_agent=user_agent, user=user)
flag_type="Broken thing",
ip=ip, user_agent=user_agent, user=user)
f1.save()

# Adding a duplicate should be prevented at the model level.
try:
f2 = ContentFlag(content_type=obj_1_ct, object_pk=obj_1.pk,
flag_type="Broken thing",
ip=ip, user_agent=user_agent, user=user)
flag_type="Broken thing",
ip=ip, user_agent=user_agent, user=user)
f2.save()
except:
except IntegrityError:
pass

# Try flag, which should turn up the single unique record created
Expand All @@ -96,7 +99,7 @@ def test_basic_flag(self):
request per unique object
"""
# Submit a flag.
request = self.mk_request()
request = _mock_request()
flag, created = ContentFlag.objects.flag(
request=request, object=self.user2, flag_type='notworking',
explanation="It not go.")
Expand All @@ -109,21 +112,20 @@ def test_basic_flag(self):
eq_(False, created)

# Submit a flag on another object.
request = self.mk_request()
request = _mock_request()
flag, created = ContentFlag.objects.flag(
request=request, object=self.user1, flag_type='notworking',
explanation="It not go.")
eq_(True, created)

# Try another unique request
request = self.mk_request(ip='192.168.123.1')
request = _mock_request(ip='192.168.123.1')
flag, created = ContentFlag.objects.flag(
request=request, object=self.user2, flag_type='inappropriate',
explanation="This is porn.")
eq_(True, created)

request = self.mk_request(ip='192.168.123.50',
user_agent='Mozilla 1.0')
request = _mock_request(ip='192.168.123.50', user_agent='Mozilla 1.0')
flag, created = ContentFlag.objects.flag(
request=request, object=self.user2, flag_type='inappropriate',
explanation="This is porn.")
Expand All @@ -132,7 +134,7 @@ def test_basic_flag(self):
eq_(4, ContentFlag.objects.count())

def test_flag_dict(self):
request = self.mk_request()
request = _mock_request()
objects_to_flag = [
{'obj': save_valid_submission(),
'flag_type': 'notworking',
Expand Down Expand Up @@ -168,21 +170,12 @@ def test_flag_dict(self):
flag_dict[doc][1].content_object.title)


class ViewTests(UserTestCase):
fixtures = UserTestCase.fixtures + ['wiki/documents.json']
class ViewTests(UserTransactionTestCase):
fixtures = UserTransactionTestCase.fixtures + ['wiki/documents.json']
localizing_client = True

def mk_request(self, user=None, ip='192.168.123.123',
user_agent='FakeBrowser 1.0'):
request = HttpRequest()
request.user = user and user or AnonymousUser()
request.method = 'GET'
request.META['REMOTE_ADDR'] = ip
request.META['HTTP_USER_AGENT'] = user_agent
return request

def test_flagged_view(self):
request = self.mk_request()
request = _mock_request()
objects_to_flag = [
{'obj': save_valid_submission(),
'flag_type': 'notworking',
Expand Down
18 changes: 12 additions & 6 deletions kuma/core/tests/__init__.py
@@ -1,17 +1,15 @@
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.messages.storage.fallback import FallbackStorage
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django import test
from django.test import TestCase
from django.test import TestCase, TransactionTestCase
from django.test.client import Client
from django.utils.functional import wraps
from django.utils.importlib import import_module
from django.utils.translation import trans_real

from constance import config
from constance.backends import database as constance_database
from nose import SkipTest
from nose.tools import eq_

Expand Down Expand Up @@ -159,7 +157,7 @@ class LocalizingClient(LocalizingMixin, SessionAwareClient):
JINJA_INSTRUMENTED = False


class KumaTestCase(TestCase):
class KumaTestMixin(object):
client_class = SessionAwareClient
localizing_client = False
skipme = False
Expand All @@ -170,10 +168,10 @@ def setUpClass(cls):
raise SkipTest
if cls.localizing_client:
cls.client_class = LocalizingClient
super(KumaTestCase, cls).setUpClass()
super(KumaTestMixin, cls).setUpClass()

def _pre_setup(self):
super(KumaTestCase, self)._pre_setup()
super(KumaTestMixin, self)._pre_setup()

# Clean the slate.
cache.clear()
Expand Down Expand Up @@ -206,5 +204,13 @@ def get_messages(self, request):
return messages


class KumaTestCase(KumaTestMixin, TestCase):
pass


class KumaTransactionTestCase(KumaTestMixin, TransactionTestCase):
pass


class SkippedTestCase(KumaTestCase):
skipme = True
14 changes: 11 additions & 3 deletions kuma/users/tests/__init__.py
Expand Up @@ -4,20 +4,28 @@
from django.utils.crypto import get_random_string
from allauth.account.models import EmailAddress

from kuma.core.tests import KumaTestCase
from kuma.core.tests import KumaTestCase, KumaTransactionTestCase

from ..models import UserProfile


class UserTestCase(KumaTestCase):
class UserTestMixin(object):
"""Base TestCase for the users app test cases."""
fixtures = ['test_users.json']

def setUp(self):
super(UserTestCase, self).setUp()
super(UserTestMixin, self).setUp()
self.user_model = get_user_model()


class UserTestCase(UserTestMixin, KumaTestCase):
pass


class UserTransactionTestCase(UserTestMixin, KumaTransactionTestCase):
pass


def profile(user, **kwargs):
"""Return a saved profile for a given user."""
return UserProfile.objects.get(user=user)
Expand Down

0 comments on commit f3c6126

Please sign in to comment.