Rajeesh Nair avatar Rajeesh Nair committed 69ce4a5

Modified to enable app-level testing.

Comments (0)

Files changed (16)

Add a comment to this file

django_monitor/tests/apps/__init__.py

Empty file removed.

Add a comment to this file

django_monitor/tests/apps/testapp/__init__.py

Empty file removed.

django_monitor/tests/apps/testapp/admin.py

-from django.contrib import admin
-
-from django_monitor.tests.apps.testapp.models import (
-    Author, Book, EBook, Supplement, Publisher, Reader
-)
-from django_monitor.admin import MonitorAdmin
-
-class AuthorAdmin(MonitorAdmin):
-    """ Monitored model. So the admin inherited from MonitorAdmin."""
-    list_display = ('__unicode__',)
-
-class SuppInline(admin.TabularInline):
-    model = Supplement
-    fk_name = 'book'
-    extra = 2
-
-class BookAdmin(MonitorAdmin):
-    inlines = [SuppInline,]
-
-class EBookAdmin(MonitorAdmin):
-    inlines = [SuppInline,]
-
-class PubAdmin(admin.ModelAdmin):
-    """ Model not monitored. Use the built-in admin"""
-    pass
-
-class ReaderAdmin(MonitorAdmin):
-    """ To test the custom queryset """
-
-    def queryset(self, request):
-        """ Returns the reader that corresponds to user"""
-        qset = super(ReaderAdmin, self).queryset(request)
-        return qset.filter(user = request.user)
-
-admin.site.register(Author, AuthorAdmin)
-admin.site.register(Book, BookAdmin)
-admin.site.register(EBook, EBookAdmin)
-admin.site.register(Publisher, PubAdmin)
-admin.site.register(Reader, ReaderAdmin)
-

django_monitor/tests/apps/testapp/fixtures/test_monitor.json

-[
-    {
-        "pk": 1,
-        "model": "testapp.publisher",
-        "fields": {
-            "name": "Apress",
-            "num_awards": 3
-        }
-    },
-    {
-        "pk": 2,
-        "model": "testapp.publisher",
-        "fields": {
-            "name": "Sams",
-            "num_awards": 1
-        }
-    },
-    {
-        "pk": 3,
-        "model": "testapp.publisher",
-        "fields": {
-            "name": "Prentice Hall",
-            "num_awards": 7
-        }
-    },
-    {
-        "pk": 4,
-        "model": "testapp.publisher",
-        "fields": {
-            "name": "Morgan Kaufmann",
-            "num_awards": 9
-        }
-    },
-    {
-        "pk": 5,
-        "model": "testapp.publisher",
-        "fields": {
-            "name": "Jonno's House of Books",
-            "num_awards": 0
-        }
-    }
-]
-

django_monitor/tests/apps/testapp/models.py

-from django.db import models
-from django.contrib import admin
-from django.contrib.auth.models import User
-
-import django_monitor
-
-class Author(models.Model):
-    """ Moderated model """
-    name = models.CharField(max_length = 100)
-    age = models.IntegerField()
-    # To make sure that post_moderation signal is emitted.
-    signal_emitted = models.BooleanField(editable = False, default = False)
-
-    class Meta:
-        app_label = 'testapp'
-
-    def __unicode__(self):
-        return self.name
-
-    def collect_signal(self):
-        """Registers the emission of post_moderation signals"""
-        self.signal_emitted = True
-        self.save()
-
-django_monitor.nq(Author)
-
-def auth_moderation_handler(sender, instance, **kwargs):
-    """
-    Receives the post_moderation signal from Author & passes it to the instance
-    """
-    instance.collect_signal()
-
-django_monitor.post_moderation.connect(
-    auth_moderation_handler, sender = Author
-)
-
-class Publisher(models.Model):
-    """ Not moderated model """
-    name = models.CharField(max_length = 255)
-    num_awards = models.IntegerField()
-
-    class Meta:
-        app_label = 'testapp'
-
-    def __unicode__(self):
-        return self.name
-
-class WebPub(Publisher):
-    """ To check something with subclassed models """
-    pass
-
-class Book(models.Model):
-    """ Moderated model with related objects """
-    isbn = models.CharField(max_length = 9)
-    name = models.CharField(max_length = 255)
-    pages = models.IntegerField()
-    authors = models.ManyToManyField(Author)
-    publisher = models.ForeignKey(Publisher)
-    
-    class Meta:
-        app_label = 'testapp'
-
-    def __unicode__(self):
-        return self.name
-
-django_monitor.nq(Book, ['supplements', ])
-
-class EBook(Book):
-    """ Subclassing a moderated model """
-    pass
-
-django_monitor.nq(EBook, ['supplements', ])
-
-class Supplement(models.Model):
-    """ Objects of this model get moderated along with Book"""
-    serial_num = models.IntegerField()
-    book = models.ForeignKey(Book, related_name = 'supplements')
-
-    def __unicode__(self):
-        return 'Supplement %s to %s' % (self.serial_num, self.book)
-
-django_monitor.nq(Supplement)
-
-class Reader(models.Model):
-    """ To test an issue with custom querysets. See admin & tests """
-    name = models.CharField(max_length = 100)
-    user = models.ForeignKey(User)
-
-django_monitor.nq(Reader)
-

django_monitor/tests/apps/testapp/tests.py

-import re
-from datetime import datetime
-
-from django.test import TestCase
-from django.contrib.auth.models import User, Permission
-from django.contrib.contenttypes.models import ContentType
-
-from django_monitor.conf import (
-    PENDING_STATUS, CHALLENGED_STATUS, APPROVED_STATUS
-)
-from django_monitor.models import MonitorEntry
-from django_monitor.tests.utils.testsettingsmanager import SettingsTestCase
-from django_monitor.tests.apps.testapp.models import (
-    Author, Book, EBook, Supplement, Publisher, Reader
-)
-
-def get_perm(Model, perm):
-    """Return the permission object, for the Model"""
-    ct = ContentType.objects.get_for_model(Model)
-    return Permission.objects.get(content_type = ct, codename = perm)
-
-def moderate_perm_exists(Model):
-    """ Returns whether moderate permission exists for the given model or not."""
-    ct = ContentType.objects.get_for_model(Model)
-    return Permission.objects.filter(
-        content_type = ct,
-        codename = 'moderate_%s' % Model._meta.object_name.lower()
-    ).exists()
-
-class ModPermTest(SettingsTestCase):
-    """ Make sure that moderate permissions are created for required models."""
-    test_settings = 'django_monitor.tests.settings'
-
-    def test_perms_for_author(self):
-        """ Testing moderate_perm exists for Author..."""
-        self.assertEquals(moderate_perm_exists(Author), True)
-
-    def test_perms_for_book(self):
-        """ Testing moderate_ perm exists for Book """
-        self.assertEquals(moderate_perm_exists(Book), True)
-
-    def test_perms_for_supplement(self):
-        """ Testing moderate_ perm exists for Supplement """
-        self.assertEquals(moderate_perm_exists(Supplement), True)
-
-    def test_perms_for_publisher(self):
-        """ Testing moderate_ perm exists for Publisher """
-        self.assertEquals(moderate_perm_exists(Publisher), False)
-
-class ModTest(SettingsTestCase):
-    """ Testing Moderation facility """
-    fixtures = ['test_monitor.json']
-    test_settings = 'django_monitor.tests.settings'
-
-    def get_csrf_token(self, url):
-        """ Scrape CSRF token """
-        response = self.client.get(url, follow = True)
-        csrf_regex = r'csrfmiddlewaretoken\'\s+value=\'([^\']+)\''
-        return re.search(csrf_regex, response.content).groups()[0]
-    
-    def setUp(self):
-        """ Two users, adder & moderator. """
-        # Permissions
-        add_auth_perm = get_perm(Author, 'add_author')
-        ch_auth_perm = get_perm(Author, 'change_author')
-        mod_auth_perm = get_perm(Author, 'moderate_author')
-        add_bk_perm = get_perm(Book, 'add_book')
-        ch_bk_perm = get_perm(Book, 'change_book')
-        mod_bk_perm = get_perm(Book, 'moderate_book')
-        add_ebk_perm = get_perm(EBook, 'add_ebook')
-        ch_ebk_perm = get_perm(EBook, 'change_ebook')
-        mod_ebk_perm = get_perm(EBook, 'moderate_ebook')
-        add_sup_perm = get_perm(Supplement, 'add_supplement')
-        mod_sup_perm = get_perm(Supplement, 'moderate_supplement')
-        ch_reader_perm = get_perm(Reader, 'change_reader')
-        ch_me_perm = get_perm(MonitorEntry, 'change_monitorentry')
-
-        self.adder = User.objects.create_user(
-            username = 'adder', email = 'adder@monitor.com',
-            password = 'adder'
-        )
-        self.adder.user_permissions = [
-            add_auth_perm, add_bk_perm, add_ebk_perm, add_sup_perm,
-            ch_auth_perm, ch_reader_perm, ch_me_perm
-        ]
-        self.adder.is_staff = True
-        self.adder.save()
-        self.moderator = User.objects.create_user(
-            username = 'moder', email = 'moder@monitor.com',
-            password = 'moder'
-        )
-        self.moderator.user_permissions = [
-            add_auth_perm, add_bk_perm, add_ebk_perm, add_sup_perm,
-            mod_auth_perm, mod_bk_perm, mod_ebk_perm, mod_sup_perm,
-            ch_auth_perm, ch_bk_perm, ch_ebk_perm, ch_reader_perm, ch_me_perm
-        ]
-        self.moderator.is_staff = True
-        self.moderator.save()
-
-    def tearDown(self):
-        self.adder.delete()
-        self.moderator.delete()
-
-    def test_1_check_additional_fields(self):
-        """
-        monitor puts some additional attrs to each moderated class.
-        Let's check for their existence.
-        """
-        import django_monitor
-        qd_book = django_monitor.model_from_queue(Book)
-        monitor_name = qd_book['monitor_name']
-        status_name = qd_book['status_name']
-        self.assertEquals(hasattr(Book, monitor_name), True) 
-        self.assertEquals(hasattr(Book, 'monitor_status'), True)
-        self.assertEquals(hasattr(Book, status_name), True)
-        self.assertEquals(hasattr(Book, 'get_monitor_status_display'), True)
-        self.assertEquals(hasattr(Book, 'moderate'), True)
-        self.assertEquals(hasattr(Book, 'approve'), True)
-        self.assertEquals(hasattr(Book, 'challenge'), True)
-        self.assertEquals(hasattr(Book, 'reset_to_pending'), True)
-        self.assertEquals(hasattr(Book, 'is_approved'), True)
-        self.assertEquals(hasattr(Book, 'is_challenged'), True)
-        self.assertEquals(hasattr(Book, 'is_pending'), True)
-        # monitor has changed the default manager, ``objects`` too.
-        self.assertEquals(
-            str(Book.objects)[:34], '<django_monitor.util.CustomManager'
-        )
-
-    def test_2_moderation(self):
-        """ 
-        Adder has permission to add only. All objects he creates are in Pending.
-        Moderator has permissions to add & moderate also. All objects he creates
-        are auto-approved.
-        """
-        # adder logs in. 
-        logged_in = self.client.login(username = 'adder', password='adder')
-        self.assertEquals(logged_in, True)
-        # Make sure that no objects are there 
-        self.assertEquals(Author.objects.count(), 0) 
-        self.assertEquals(Book.objects.count(), 0)
-        self.assertEquals(Supplement.objects.count(), 0)
-        # Adding 2 Author instances...
-        url = '/admin/testapp/author/add/'
-        # Author 1
-        data = {'age': 34, 'name': "Adrian Holovaty"}
-        response = self.client.post(url, data, follow = True)
-        # Author 2
-        data = {'age': 35, 'name': 'Jacob kaplan-Moss'}
-        response = self.client.post(url, data, follow = True)
-        self.assertEquals(response.status_code, 200)
-        # 2 Author instances added. Both are in pending (IP)
-        self.assertEquals(Author.objects.count(), 2)
-        self.assertEquals(Author.objects.get(pk=1).is_pending, True)
-        self.assertEquals(Author.objects.get(pk=2).is_pending, True)
-        # Adding 1 book instance with 2 supplements...
-        url = '/admin/testapp/book/add/'
-        data = {
-            'publisher': 1, 'isbn': '159059725', 'name': 'Definitive', 
-            'authors': [1, 2], 'pages': 447,
-            'supplements-TOTAL_FORMS': 2, 'supplements-INITIAL_FORMS': 0,
-            'supplements-0-serial_num': 1, 'supplements-1-serial_num': 2,
-        }
-        response = self.client.post(url, data, follow = True)
-        self.assertEquals(response.status_code, 200)
-        # 1 Book instance added. In pending (IP)
-        self.assertEquals(Book.objects.count(), 1)
-        self.assertEquals(Book.objects.get(pk=1).is_pending, True)
-        # 2 Supplement instances added. In Pending (IP)
-        self.assertEquals(Supplement.objects.count(), 2)
-        self.assertEquals(Supplement.objects.get(pk=1).is_pending, True)
-        self.assertEquals(Supplement.objects.get(pk=2).is_pending, True)
-
-        # Adder logs out
-        self.client.logout()
-    
-        # moderator logs in. 
-        logged_in = self.client.login(username = 'moder', password = 'moder')
-        self.assertEquals(logged_in, True)
-        # Adding one more author instance...
-        url = '/admin/testapp/author/add/'
-        # Author 3
-        data = {'age': 46, 'name': "Stuart Russel"}
-        response = self.client.post(url, data, follow = True)
-        # Author 3 added. Auto-Approved (AP)
-        self.assertEquals(Author.objects.count(), 3)
-        self.assertEquals(Author.objects.get(pk=3).is_approved, True)
-        # Approve Author 1 (created by adder)
-        url = '/admin/testapp/author/'
-        data = {'action': 'approve_selected', 'index': 0, '_selected_action': 1}
-        response = self.client.post(url, data, follow = True)
-        self.assertEquals(Author.objects.get(pk=1).is_approved, True)
-        # Challenge Author 2 (created by adder)
-        data = {'action': 'challenge_selected', 'index': 0, '_selected_action': 2}
-        response = self.client.post(url, data, follow = True)
-        self.assertEquals(Author.objects.get(pk=2).is_approved, False)
-        self.assertEquals(Author.objects.get(pk=2).is_challenged, True)
-        # Approve Book 1 (created by adder). Supplements also get approved.
-        url = '/admin/testapp/book/'
-        data = {'action': 'approve_selected', 'index': 0, '_selected_action': 1}
-        response = self.client.post(url, data, follow = True)
-        self.assertEquals(Book.objects.get(pk=1).is_approved, True)
-        self.assertEquals(Supplement.objects.get(pk=1).is_approved, True)
-        self.assertEquals(Supplement.objects.get(pk=2).is_approved, True)
-
-        # moderator logs out
-        self.client.logout()
-        # adder logs in again
-        logged_in = self.client.login(username = 'adder', password = 'adder')
-        self.assertEquals(logged_in, True)
-
-        # Edit the challenged Author, author 2.
-        self.failUnlessEqual(Author.objects.get(pk=2).age, 35)
-        url = '/admin/testapp/author/2/'
-        data = {'id': 2, 'age': 53, 'name': 'Stuart Russel'}
-        response = self.client.post(url, data)
-        self.assertRedirects(response, '/admin/testapp/author/', target_status_code = 200)
-        self.failUnlessEqual(Author.objects.get(pk=2).age, 53)
-        # Reset Author 2 back to pending
-        url = '/admin/testapp/author/'
-        data = {'action': 'reset_to_pending', 'index': 0, '_selected_action': 2}
-        response = self.client.post(url, data, follow = True)
-        self.failUnlessEqual(Author.objects.get(pk=2).is_pending, True)
-
-    def test_3_moderate_parents_from_shell(self):
-        """
-        Moderate instance of a sub-class model. Parents also must be moderated.
-        Run from the shell.
-        """
-        pub1 = Publisher.objects.create(
-            name = 'test_pub', num_awards = 3
-        )
-        auth1 = Author.objects.create(
-           name = 'test_auth', age = 34
-        )
-        eb1 = EBook.objects.create(
-            isbn='123456789', name='test_ebook', pages=300, publisher = pub1
-        )
-        eb1.authors = [auth1]
-        eb1.save()
-        # The parent instance book_ptr is available
-        book_ptr = getattr(eb1, 'book_ptr', None)
-        self.assertEquals(book_ptr is None, False)
-        # Both are in pending now
-        self.assertEquals(eb1.is_pending, True)
-        self.assertEquals(book_ptr.is_pending, True)
-        # Approve eb1.
-        eb1.approve()
-        # Load again from db. Else, cached value may be used.
-        eb1 = EBook.objects.get(pk = 1)
-        self.assertEquals(eb1.is_approved, True)
-        self.assertEquals(eb1.book_ptr.is_approved, True)
-
-    def test_4_moderate_parents_from_browser(self):
-        """
-        Moderate instance of a sub-class model. Parents also must be monitored.
-        User makes requests from a browser.
-        """
-        # The adder starts it as usual...
-        logged_in = self.client.login(username = 'adder', password = 'adder')
-        self.assertEquals(logged_in, True)
-
-        pub1 = Publisher.objects.create(
-            name = 'test_pub', num_awards = 3
-        )
-        auth1 = Author.objects.create(
-           name = 'test_auth', age = 34
-        )
-        url = '/admin/testapp/ebook/add/'
-        data = {
-            'publisher': 1, 'isbn': '159059725', 'name': 'Definitive', 
-            'authors': [1,], 'pages': 447,
-            'supplements-TOTAL_FORMS': 0, 'supplements-INITIAL_FORMS': 0,
-        }
-        response = self.client.post(url, data, follow = True)
-        self.assertEquals(response.status_code, 200)
-        # 1 EBook instance added. In pending (IP)
-        self.assertEquals(EBook.objects.count(), 1)
-        self.assertEquals(EBook.objects.get(pk=1).is_pending, True)
-        # 1 Book instance also added as parent. In Pending
-        self.assertEquals(Book.objects.count(), 1)
-        self.assertEquals(Book.objects.get(pk=1).is_pending, True)
-        # The Book instance is pointed by book_ptr in EBook instance
-        eb1 = EBook.objects.get(pk=1)
-        self.assertEquals(getattr(eb1, 'book_ptr', None) is None, False)
-
-        # Now moderator try moderation by http_request
-        self.client.logout()
-        logged_in = self.client.login(username = 'moder', password = 'moder')
-        self.assertEquals(logged_in, True)
-        url = '/admin/testapp/ebook/'
-        data = {'action': 'approve_selected', 'index': 0, '_selected_action': 1}
-        response = self.client.post(url, data, follow = True)
-        self.assertEquals(response.status_code, 200)
-        eb1 = EBook.objects.get(pk=1)
-        self.assertEquals(eb1.is_approved, True)
-        self.assertEquals(eb1.book_ptr.is_approved, True)
-
-    def test_5_auto_delete_monitor_entries(self):
-        """
-        When an instance of a moderated model is deleted, the monitor entries
-        corresponding to the instance and all of its parent instances also
-        should be deleted.
-        """
-        from django_monitor.models import MonitorEntry
-
-        self.assertEquals(MonitorEntry.objects.count(), 0)
-        ebook_ct = ContentType.objects.get_for_model(EBook)
-        book_ct = ContentType.objects.get_for_model(Book)
-
-        pub1 = Publisher.objects.create(
-            name = 'test_pub', num_awards = 3
-        )
-        auth1 = Author.objects.create(
-           name = 'test_auth', age = 34
-        )
-        eb1 = EBook.objects.create(
-            isbn='123456789', name='test_ebook', pages=300, publisher = pub1
-        )
-        eb1.authors = [auth1]
-        eb1.save()
-
-        # 1 monitor_entry each for auth1, eb1 & its parent, eb1.book_ptr.
-        self.assertEquals(MonitorEntry.objects.count(), 3)
-        ebook_mes = MonitorEntry.objects.filter(content_type = ebook_ct)
-        book_mes = MonitorEntry.objects.filter(content_type = book_ct)
-        self.assertEquals(ebook_mes.count(), 1)
-        self.assertEquals(book_mes.count(), 1)
-
-        eb1.delete()
-        # MonitorEntry object for auth1 remains. Others removed.
-        self.assertEquals(MonitorEntry.objects.count(), 1)
-        ebook_mes = MonitorEntry.objects.filter(content_type = ebook_ct)
-        book_mes = MonitorEntry.objects.filter(content_type = book_ct)
-        self.assertEquals(ebook_mes.count(), 0)
-        self.assertEquals(book_mes.count(), 0) 
-
-    def test_6_moderation_signal(self):
-        """
-        Make sure that a post_moderation signal is emitted.
-        We have an attribute, `signal_emitted` in the model, ``Author``.
-        By default, it will be set to False. When a ``post_moderation`` signal
-        is emitted, a function, ``auth_post_moderation_handler`` defined in
-        testapp.models will set it to True.
-        """
-        auth1 = Author.objects.create(
-            name = "test_auth", age = 34
-        )
-        # Creation itself invokes moderation which in turn emits the signal.
-        auth1 = Author.objects.get(pk=1)
-        self.assertEquals(auth1.signal_emitted, True)
-
-    def test_7_moderation_queue_count(self):
-        """
-        moderation queue counts all pending or challenged objects of each
-        monitored model and displays those counts in the changelist. The
-        developer may have written some of the model_admins in a way that a
-        user is able to view only a subset of all objects. eg, only those
-        objects the user has created herself. Our queue should respect such
-        customizations. So the counts must be prepared only after consulting
-        the model_admin.queryset.
-        """
-        reader1 = Reader.objects.create(name = 'r1', user = self.adder)
-        reader2 = Reader.objects.create(name = 'r2', user = self.adder)
-        reader3 = Reader.objects.create(name = 'r3', user = self.moderator)
-        reader4 = Reader.objects.create(name = 'r4', user = self.moderator)
-        reader5 = Reader.objects.create(name = 'r5', user = self.moderator)
-        # User 1, adder logs in
-        logged_in = self.client.login(username = 'adder', password = 'adder')
-        self.assertEquals(logged_in, True)
-        # model_admin allows user to view just 2 objects that belong to her.
-        response = self.client.get('/admin/testapp/reader/', follow = True)
-        self.assertEquals(response.status_code, 200)
-        result_count = len(response.context[-1]['cl'].result_list)
-        self.assertEquals(result_count, 2)
-        # Now is the time to test monitor_queue. Same result expected.
-        response = self.client.get(
-            '/admin/django_monitor/monitorentry/', follow = True
-        )
-        self.assertEquals(response.status_code, 200)
-        queued_reader = filter(
-            lambda x: x['model_name'] == 'reader',
-            response.context[-1]['model_list']
-        )[0]
-        self.assertEquals(queued_reader['pending'], 2)
-        self.assertEquals(queued_reader['challenged'], 0)
-        self.client.logout()
-
-        # User 2, moderator, repeats the same.
-        logged_in = self.client.login(username = 'moder', password = 'moder')
-        self.assertEquals(logged_in, True)
-        response = self.client.get(
-            '/admin/django_monitor/monitorentry/', follow = True
-        )
-        self.assertEquals(response.status_code, 200)
-        queued_reader = filter(
-            lambda x: x['model_name'] == 'reader',
-            response.context[-1]['model_list']
-        )[0]
-        self.assertEquals(queued_reader['pending'], 3)
-        self.assertEquals(queued_reader['challenged'], 0)
-        self.client.logout()

django_monitor/tests/settings.py

-from django_monitor.tests.utils.testsettingsmanager import get_only_settings_locals
-
-DATABASE_ENGINE = 'sqlite3'
-
-INSTALLED_APPS = (
-    'django.contrib.auth',
-    'django.contrib.contenttypes',
-    'django.contrib.sessions',
-    'django.contrib.admin',
-    'django.contrib.sites',
-    'django_monitor',
-    'django_monitor.tests.apps.testapp',
-)
-
-SERIALIZATION_MODULES = {}
-
-ROOT_URLCONF = 'django_monitor.tests.urls'
-
-settings = get_only_settings_locals(locals().copy())
Add a comment to this file

django_monitor/tests/test_app/__init__.py

Empty file added.

django_monitor/tests/test_app/admin.py

+from django.contrib import admin
+
+from django_monitor.tests.test_app.models import (
+    Author, Book, EBook, Supplement, Publisher, Reader
+)
+from django_monitor.admin import MonitorAdmin
+
+class AuthorAdmin(MonitorAdmin):
+    """ Monitored model. So the admin inherited from MonitorAdmin."""
+    list_display = ('__unicode__',)
+
+class SuppInline(admin.TabularInline):
+    model = Supplement
+    fk_name = 'book'
+    extra = 2
+
+class BookAdmin(MonitorAdmin):
+    inlines = [SuppInline,]
+
+class EBookAdmin(MonitorAdmin):
+    inlines = [SuppInline,]
+
+class PubAdmin(admin.ModelAdmin):
+    """ Model not monitored. Use the built-in admin"""
+    pass
+
+class ReaderAdmin(MonitorAdmin):
+    """ To test the custom queryset """
+
+    def queryset(self, request):
+        """ Returns the reader that corresponds to user"""
+        qset = super(ReaderAdmin, self).queryset(request)
+        return qset.filter(user = request.user)
+
+admin.site.register(Author, AuthorAdmin)
+admin.site.register(Book, BookAdmin)
+admin.site.register(EBook, EBookAdmin)
+admin.site.register(Publisher, PubAdmin)
+admin.site.register(Reader, ReaderAdmin)
+

django_monitor/tests/test_app/fixtures/test_monitor.json

+[
+    {
+        "pk": 1,
+        "model": "test_app.publisher",
+        "fields": {
+            "name": "Apress",
+            "num_awards": 3
+        }
+    },
+    {
+        "pk": 2,
+        "model": "test_app.publisher",
+        "fields": {
+            "name": "Sams",
+            "num_awards": 1
+        }
+    },
+    {
+        "pk": 3,
+        "model": "test_app.publisher",
+        "fields": {
+            "name": "Prentice Hall",
+            "num_awards": 7
+        }
+    },
+    {
+        "pk": 4,
+        "model": "test_app.publisher",
+        "fields": {
+            "name": "Morgan Kaufmann",
+            "num_awards": 9
+        }
+    },
+    {
+        "pk": 5,
+        "model": "test_app.publisher",
+        "fields": {
+            "name": "Jonno's House of Books",
+            "num_awards": 0
+        }
+    }
+]
+

django_monitor/tests/test_app/models.py

+from django.db import models
+from django.contrib import admin
+from django.contrib.auth.models import User
+
+import django_monitor
+
+class Author(models.Model):
+    """ Moderated model """
+    name = models.CharField(max_length = 100)
+    age = models.IntegerField()
+    # To make sure that post_moderation signal is emitted.
+    signal_emitted = models.BooleanField(editable = False, default = False)
+
+    def __unicode__(self):
+        return self.name
+
+    def collect_signal(self):
+        """Registers the emission of post_moderation signals"""
+        self.signal_emitted = True
+        self.save()
+
+django_monitor.nq(Author)
+
+def auth_moderation_handler(sender, instance, **kwargs):
+    """
+    Receives the post_moderation signal from Author & passes it to the instance
+    """
+    instance.collect_signal()
+
+django_monitor.post_moderation.connect(
+    auth_moderation_handler, sender = Author
+)
+
+class Publisher(models.Model):
+    """ Not moderated model """
+    name = models.CharField(max_length = 255)
+    num_awards = models.IntegerField()
+
+    def __unicode__(self):
+        return self.name
+
+class WebPub(Publisher):
+    """ To check something with subclassed models """
+    pass
+
+class Book(models.Model):
+    """ Moderated model with related objects """
+    isbn = models.CharField(max_length = 9)
+    name = models.CharField(max_length = 255)
+    pages = models.IntegerField()
+    authors = models.ManyToManyField(Author)
+    publisher = models.ForeignKey(Publisher)
+    
+    def __unicode__(self):
+        return self.name
+
+django_monitor.nq(Book, ['supplements', ])
+
+class EBook(Book):
+    """ Subclassing a moderated model """
+    pass
+
+django_monitor.nq(EBook, ['supplements', ])
+
+class Supplement(models.Model):
+    """ Objects of this model get moderated along with Book"""
+    serial_num = models.IntegerField()
+    book = models.ForeignKey(Book, related_name = 'supplements')
+
+    def __unicode__(self):
+        return 'Supplement %s to %s' % (self.serial_num, self.book)
+
+django_monitor.nq(Supplement)
+
+class Reader(models.Model):
+    """ To test an issue with custom querysets. See admin & tests """
+    name = models.CharField(max_length = 100)
+    user = models.ForeignKey(User)
+
+django_monitor.nq(Reader)
+

django_monitor/tests/tests.py

+import re
+from datetime import datetime
+
+from django.test import TestCase
+from django.contrib.auth.models import User, Permission
+from django.contrib.contenttypes.models import ContentType
+
+from django_monitor.conf import (
+    PENDING_STATUS, CHALLENGED_STATUS, APPROVED_STATUS
+)
+from django_monitor.models import MonitorEntry
+from django_monitor.tests.test_app.models import (
+    Author, Book, EBook, Supplement, Publisher, Reader
+)
+
+def get_perm(Model, perm):
+    """Return the permission object, for the Model"""
+    ct = ContentType.objects.get_for_model(Model)
+    return Permission.objects.get(content_type = ct, codename = perm)
+
+def moderate_perm_exists(Model):
+    """ Returns if moderate permission exists for the given model or not."""
+    ct = ContentType.objects.get_for_model(Model)
+    return Permission.objects.filter(
+        content_type = ct,
+        codename = 'moderate_%s' % Model._meta.object_name.lower()
+    ).exists()
+
+class ModPermTest(TestCase):
+    """Make sure that moderate permissions are created for required models."""
+
+    def test_perms_for_author(self):
+        """Testing if moderate_perm exists for Author"""
+        self.assertEquals(moderate_perm_exists(Author), True)
+
+    def test_perms_for_book(self):
+        """Testing if moderate_ perm exists for Book"""
+        self.assertEquals(moderate_perm_exists(Book), True)
+
+    def test_perms_for_supplement(self):
+        """Testing if moderate_ perm exists for Supplement"""
+        self.assertEquals(moderate_perm_exists(Supplement), True)
+
+    def test_perms_for_publisher(self):
+        """Testing if moderate_ perm exists for Publisher"""
+        self.assertEquals(moderate_perm_exists(Publisher), False)
+
+class ModTest(TestCase):
+    """Testing Moderation facility"""
+    fixtures = ['test_monitor.json']
+
+    def get_csrf_token(self, url):
+        """Scrape CSRF token"""
+        response = self.client.get(url, follow = True)
+        csrf_regex = r'csrfmiddlewaretoken\'\s+value=\'([^\']+)\''
+        return re.search(csrf_regex, response.content).groups()[0]
+    
+    def setUp(self):
+        """Two users, adder & moderator."""
+        # Permissions
+        add_auth_perm = get_perm(Author, 'add_author')
+        ch_auth_perm = get_perm(Author, 'change_author')
+        mod_auth_perm = get_perm(Author, 'moderate_author')
+        add_bk_perm = get_perm(Book, 'add_book')
+        ch_bk_perm = get_perm(Book, 'change_book')
+        mod_bk_perm = get_perm(Book, 'moderate_book')
+        add_ebk_perm = get_perm(EBook, 'add_ebook')
+        ch_ebk_perm = get_perm(EBook, 'change_ebook')
+        mod_ebk_perm = get_perm(EBook, 'moderate_ebook')
+        add_sup_perm = get_perm(Supplement, 'add_supplement')
+        mod_sup_perm = get_perm(Supplement, 'moderate_supplement')
+        ch_reader_perm = get_perm(Reader, 'change_reader')
+        ch_me_perm = get_perm(MonitorEntry, 'change_monitorentry')
+
+        self.adder = User.objects.create_user(
+            username = 'adder', email = 'adder@monitor.com',
+            password = 'adder'
+        )
+        self.adder.user_permissions = [
+            add_auth_perm, add_bk_perm, add_ebk_perm, add_sup_perm,
+            ch_auth_perm, ch_reader_perm, ch_me_perm
+        ]
+        self.adder.is_staff = True
+        self.adder.save()
+        self.moderator = User.objects.create_user(
+            username = 'moder', email = 'moder@monitor.com',
+            password = 'moder'
+        )
+        self.moderator.user_permissions = [
+            add_auth_perm, add_bk_perm, add_ebk_perm, add_sup_perm,
+            mod_auth_perm, mod_bk_perm, mod_ebk_perm, mod_sup_perm,
+            ch_auth_perm, ch_bk_perm, ch_ebk_perm, ch_reader_perm, ch_me_perm
+        ]
+        self.moderator.is_staff = True
+        self.moderator.save()
+
+    def tearDown(self):
+        self.adder.delete()
+        self.moderator.delete()
+
+    def test_1_check_additional_fields(self):
+        """
+        monitor puts some additional attrs to each moderated class.
+        Let's check for their existence.
+        """
+        import django_monitor
+        qd_book = django_monitor.model_from_queue(Book)
+        monitor_name = qd_book['monitor_name']
+        status_name = qd_book['status_name']
+        self.assertEquals(hasattr(Book, monitor_name), True) 
+        self.assertEquals(hasattr(Book, 'monitor_status'), True)
+        self.assertEquals(hasattr(Book, status_name), True)
+        self.assertEquals(hasattr(Book, 'get_monitor_status_display'), True)
+        self.assertEquals(hasattr(Book, 'moderate'), True)
+        self.assertEquals(hasattr(Book, 'approve'), True)
+        self.assertEquals(hasattr(Book, 'challenge'), True)
+        self.assertEquals(hasattr(Book, 'reset_to_pending'), True)
+        self.assertEquals(hasattr(Book, 'is_approved'), True)
+        self.assertEquals(hasattr(Book, 'is_challenged'), True)
+        self.assertEquals(hasattr(Book, 'is_pending'), True)
+        # monitor has changed the default manager, ``objects`` too.
+        self.assertEquals(
+            str(Book.objects)[:34], '<django_monitor.util.CustomManager'
+        )
+
+    def test_2_moderation(self):
+        """ 
+        Adder has permission to add only. All of his objects are in Pending.
+        Moderator has permissions to add & moderate also. All objects he 
+        creates are auto-approved.
+        """
+        # adder logs in. 
+        logged_in = self.client.login(username = 'adder', password = 'adder')
+        self.assertEquals(logged_in, True)
+        # Make sure that no objects are there 
+        self.assertEquals(Author.objects.count(), 0) 
+        self.assertEquals(Book.objects.count(), 0)
+        self.assertEquals(Supplement.objects.count(), 0)
+        # Adding 2 Author instances...
+        url = '/admin/test_app/author/add/'
+        # Author 1
+        data = {'age': 34, 'name': "Adrian Holovaty"}
+        response = self.client.post(url, data, follow = True)
+        # Author 2
+        data = {'age': 35, 'name': 'Jacob kaplan-Moss'}
+        response = self.client.post(url, data, follow = True)
+        self.assertEquals(response.status_code, 200)
+        # 2 Author instances added. Both are in pending (IP)
+        self.assertEquals(Author.objects.count(), 2)
+        self.assertEquals(Author.objects.get(pk = 1).is_pending, True)
+        self.assertEquals(Author.objects.get(pk = 2).is_pending, True)
+        # Adding 1 book instance with 2 supplements...
+        url = '/admin/test_app/book/add/'
+        data = {
+            'publisher': 1, 'isbn': '159059725', 'name': 'Definitive', 
+            'authors': [1, 2], 'pages': 447,
+            'supplements-TOTAL_FORMS': 2, 'supplements-INITIAL_FORMS': 0,
+            'supplements-0-serial_num': 1, 'supplements-1-serial_num': 2,
+        }
+        response = self.client.post(url, data, follow = True)
+        self.assertEquals(response.status_code, 200)
+        # 1 Book instance added. In pending (IP)
+        self.assertEquals(Book.objects.count(), 1)
+        self.assertEquals(Book.objects.get(pk = 1).is_pending, True)
+        # 2 Supplement instances added. In Pending (IP)
+        self.assertEquals(Supplement.objects.count(), 2)
+        self.assertEquals(Supplement.objects.get(pk = 1).is_pending, True)
+        self.assertEquals(Supplement.objects.get(pk = 2).is_pending, True)
+
+        # Adder logs out
+        self.client.logout()
+    
+        # moderator logs in. 
+        logged_in = self.client.login(username = 'moder', password = 'moder')
+        self.assertEquals(logged_in, True)
+        # Adding one more author instance...
+        url = '/admin/test_app/author/add/'
+        # Author 3
+        data = {'age': 46, 'name': "Stuart Russel"}
+        response = self.client.post(url, data, follow = True)
+        # Author 3 added. Auto-Approved (AP)
+        self.assertEquals(Author.objects.count(), 3)
+        self.assertEquals(Author.objects.get(pk = 3).is_approved, True)
+        # Approve Author 1 (created by adder)
+        url = '/admin/test_app/author/'
+        data = {'action': 'approve_selected', 'index': 0, '_selected_action': 1}
+        response = self.client.post(url, data, follow = True)
+        self.assertEquals(Author.objects.get(pk = 1).is_approved, True)
+        # Challenge Author 2 (created by adder)
+        data = {'action': 'challenge_selected', 'index': 0, '_selected_action': 2}
+        response = self.client.post(url, data, follow = True)
+        self.assertEquals(Author.objects.get(pk = 2).is_approved, False)
+        self.assertEquals(Author.objects.get(pk = 2).is_challenged, True)
+        # Approve Book 1 (created by adder). Supplements also get approved.
+        url = '/admin/test_app/book/'
+        data = {'action': 'approve_selected', 'index': 0, '_selected_action': 1}
+        response = self.client.post(url, data, follow = True)
+        self.assertEquals(Book.objects.get(pk = 1).is_approved, True)
+        self.assertEquals(Supplement.objects.get(pk = 1).is_approved, True)
+        self.assertEquals(Supplement.objects.get(pk = 2).is_approved, True)
+
+        # moderator logs out
+        self.client.logout()
+        # adder logs in again
+        logged_in = self.client.login(username = 'adder', password = 'adder')
+        self.assertEquals(logged_in, True)
+
+        # Edit the challenged Author, author 2.
+        self.failUnlessEqual(Author.objects.get(pk = 2).age, 35)
+        url = '/admin/test_app/author/2/'
+        data = {'id': 2, 'age': 53, 'name': 'Stuart Russel'}
+        response = self.client.post(url, data)
+        self.assertRedirects(
+            response, '/admin/test_app/author/', target_status_code = 200
+        )
+        self.failUnlessEqual(Author.objects.get(pk = 2).age, 53)
+        # Reset Author 2 back to pending
+        url = '/admin/test_app/author/'
+        data = {'action': 'reset_to_pending', 'index': 0, '_selected_action': 2}
+        response = self.client.post(url, data, follow = True)
+        self.failUnlessEqual(Author.objects.get(pk = 2).is_pending, True)
+
+    def test_3_moderate_parents_from_shell(self):
+        """
+        Moderate instance of a sub-class model. Parents also must be moderated.
+        Run from the shell.
+        """
+        pub1 = Publisher.objects.create(
+            name = 'test_pub', num_awards = 3
+        )
+        auth1 = Author.objects.create(
+           name = 'test_auth', age = 34
+        )
+        eb1 = EBook.objects.create(
+            isbn = '123456789', name = 'test_ebook', 
+            pages = 300, publisher = pub1
+        )
+        eb1.authors = [auth1]
+        eb1.save()
+        # The parent instance book_ptr is available
+        book_ptr = getattr(eb1, 'book_ptr', None)
+        self.assertEquals(book_ptr is None, False)
+        # Both are in pending now
+        self.assertEquals(eb1.is_pending, True)
+        self.assertEquals(book_ptr.is_pending, True)
+        # Approve eb1.
+        eb1.approve()
+        # Load again from db. Else, cached value may be used.
+        eb1 = EBook.objects.get(pk = 1)
+        self.assertEquals(eb1.is_approved, True)
+        self.assertEquals(eb1.book_ptr.is_approved, True)
+
+    def test_4_moderate_parents_from_browser(self):
+        """
+        Moderate instance of a sub-class model. Parents also must be monitored.
+        User makes requests from a browser.
+        """
+        # The adder starts it as usual...
+        logged_in = self.client.login(username = 'adder', password = 'adder')
+        self.assertEquals(logged_in, True)
+
+        pub1 = Publisher.objects.create(
+            name = 'test_pub', num_awards = 3
+        )
+        auth1 = Author.objects.create(
+           name = 'test_auth', age = 34
+        )
+        url = '/admin/test_app/ebook/add/'
+        data = {
+            'publisher': 1, 'isbn': '159059725', 'name': 'Definitive', 
+            'authors': [1,], 'pages': 447,
+            'supplements-TOTAL_FORMS': 0, 'supplements-INITIAL_FORMS': 0,
+        }
+        response = self.client.post(url, data, follow = True)
+        self.assertEquals(response.status_code, 200)
+        # 1 EBook instance added. In pending (IP)
+        self.assertEquals(EBook.objects.count(), 1)
+        self.assertEquals(EBook.objects.get(pk = 1).is_pending, True)
+        # 1 Book instance also added as parent. In Pending
+        self.assertEquals(Book.objects.count(), 1)
+        self.assertEquals(Book.objects.get(pk = 1).is_pending, True)
+        # The Book instance is pointed by book_ptr in EBook instance
+        eb1 = EBook.objects.get(pk = 1)
+        self.assertEquals(getattr(eb1, 'book_ptr', None) is None, False)
+
+        # Now moderator try moderation by http_request
+        self.client.logout()
+        logged_in = self.client.login(username = 'moder', password = 'moder')
+        self.assertEquals(logged_in, True)
+        url = '/admin/test_app/ebook/'
+        data = {'action': 'approve_selected', 'index': 0, '_selected_action': 1}
+        response = self.client.post(url, data, follow = True)
+        self.assertEquals(response.status_code, 200)
+        eb1 = EBook.objects.get(pk = 1)
+        self.assertEquals(eb1.is_approved, True)
+        self.assertEquals(eb1.book_ptr.is_approved, True)
+
+    def test_5_auto_delete_monitor_entries(self):
+        """
+        When an instance of a moderated model is deleted, the monitor entries
+        corresponding to the instance and all of its parent instances also
+        should be deleted.
+        """
+        from django_monitor.models import MonitorEntry
+
+        self.assertEquals(MonitorEntry.objects.count(), 0)
+        ebook_ct = ContentType.objects.get_for_model(EBook)
+        book_ct = ContentType.objects.get_for_model(Book)
+
+        pub1 = Publisher.objects.create(
+            name = 'test_pub', num_awards = 3
+        )
+        auth1 = Author.objects.create(
+           name = 'test_auth', age = 34
+        )
+        eb1 = EBook.objects.create(
+            isbn = '123456789', name = 'test_ebook', 
+            pages = 300, publisher = pub1
+        )
+        eb1.authors = [auth1]
+        eb1.save()
+
+        # 1 monitor_entry each for auth1, eb1 & its parent, eb1.book_ptr.
+        self.assertEquals(MonitorEntry.objects.count(), 3)
+        ebook_mes = MonitorEntry.objects.filter(content_type = ebook_ct)
+        book_mes = MonitorEntry.objects.filter(content_type = book_ct)
+        self.assertEquals(ebook_mes.count(), 1)
+        self.assertEquals(book_mes.count(), 1)
+
+        eb1.delete()
+        # MonitorEntry object for auth1 remains. Others removed.
+        self.assertEquals(MonitorEntry.objects.count(), 1)
+        ebook_mes = MonitorEntry.objects.filter(content_type = ebook_ct)
+        book_mes = MonitorEntry.objects.filter(content_type = book_ct)
+        self.assertEquals(ebook_mes.count(), 0)
+        self.assertEquals(book_mes.count(), 0) 
+
+    def test_6_moderation_signal(self):
+        """
+        Make sure that a post_moderation signal is emitted.
+        We have an attribute, `signal_emitted` in the model, ``Author``.
+        By default, it will be set to False. When a ``post_moderation`` signal
+        is emitted, a function, ``auth_post_moderation_handler`` defined in
+        test_app.models will set it to True.
+        """
+        auth1 = Author.objects.create(
+            name = "test_auth", age = 34
+        )
+        # Creation itself invokes moderation which in turn emits the signal.
+        auth1 = Author.objects.get(pk = 1)
+        self.assertEquals(auth1.signal_emitted, True)
+
+    def test_7_moderation_queue_count(self):
+        """
+        moderation queue counts all pending or challenged objects of each
+        monitored model and displays those counts in the changelist. The
+        developer may have written some of the model_admins in a way that a
+        user is able to view only a subset of all objects. eg, only those
+        objects the user has created herself. Our queue should respect such
+        customizations. So the counts must be prepared only after consulting
+        the model_admin.queryset.
+        """
+        reader1 = Reader.objects.create(name = 'r1', user = self.adder)
+        reader2 = Reader.objects.create(name = 'r2', user = self.adder)
+        reader3 = Reader.objects.create(name = 'r3', user = self.moderator)
+        reader4 = Reader.objects.create(name = 'r4', user = self.moderator)
+        reader5 = Reader.objects.create(name = 'r5', user = self.moderator)
+        # User 1, adder logs in
+        logged_in = self.client.login(username = 'adder', password = 'adder')
+        self.assertEquals(logged_in, True)
+        # model_admin allows user to view just 2 objects that belong to her.
+        response = self.client.get('/admin/test_app/reader/', follow = True)
+        self.assertEquals(response.status_code, 200)
+        result_count = len(response.context[-1]['cl'].result_list)
+        self.assertEquals(result_count, 2)
+        # Now is the time to test monitor_queue. Same result expected.
+        response = self.client.get(
+            '/admin/django_monitor/monitorentry/', follow = True
+        )
+        self.assertEquals(response.status_code, 200)
+        queued_reader = filter(
+            lambda x: x['model_name'] == 'reader',
+            response.context[-1]['model_list']
+        )[0]
+        self.assertEquals(queued_reader['pending'], 2)
+        self.assertEquals(queued_reader['challenged'], 0)
+        self.client.logout()
+
+        # User 2, moderator, repeats the same.
+        logged_in = self.client.login(username = 'moder', password = 'moder')
+        self.assertEquals(logged_in, True)
+        response = self.client.get(
+            '/admin/django_monitor/monitorentry/', follow = True
+        )
+        self.assertEquals(response.status_code, 200)
+        queued_reader = filter(
+            lambda x: x['model_name'] == 'reader',
+            response.context[-1]['model_list']
+        )[0]
+        self.assertEquals(queued_reader['pending'], 3)
+        self.assertEquals(queued_reader['challenged'], 0)
+        self.client.logout()
Add a comment to this file

django_monitor/tests/utils/__init__.py

Empty file removed.

django_monitor/tests/utils/request_factory.py

-"""
-RequestFactory mock class,
-snippet taken from http://www.djangosnippets.org/snippets/963/
-"""
-from django.test import Client
-from django.core.handlers.wsgi import WSGIRequest
-
-
-class RequestFactory(Client):
-    """
-    Class that lets you create mock Request objects for use in testing.
-    
-    Usage:
-    
-    rf = RequestFactory()
-    get_request = rf.get('/hello/')
-    post_request = rf.post('/submit/', {'foo': 'bar'})
-    
-    This class re-uses the django.test.client.Client interface, docs here:
-    http://www.djangoproject.com/documentation/testing/#the-test-client
-    
-    Once you have a request object you can pass it to any view function, 
-    just as if that view had been hooked up using a URLconf.
-    
-    """
-    
-    def request(self, **request):
-        """
-        Similar to parent class, but returns the request object as soon as it
-        has created it.
-        """
-        environ = {
-            'HTTP_COOKIE': self.cookies,
-            'PATH_INFO': '/',
-            'QUERY_STRING': '',
-            'REQUEST_METHOD': 'GET',
-            'SCRIPT_NAME': '',
-            'SERVER_NAME': 'testserver',
-            'SERVER_PORT': 80,
-            'SERVER_PROTOCOL': 'HTTP/1.1',
-        }
-        environ.update(self.defaults)
-        environ.update(request)
-        return WSGIRequest(environ)

django_monitor/tests/utils/testsettingsmanager.py

-"""
-TestSettingsManager class for making temporary changes to 
-settings for the purposes of a unittest or doctest. 
-It will keep track of the original settings and let 
-easily revert them back when you're done.
-
-Snippet taken from: http://www.djangosnippets.org/snippets/1011/
-"""
-
-from django.conf import settings
-from django.core.management import call_command
-from django.db.models import loading
-from django.test import TestCase
-from django.utils.importlib import import_module
-
-NO_SETTING = ('!', None)
-
-
-class TestSettingsManager(object):
-    """
-    A class which can modify some Django settings temporarily for a
-    test and then revert them to their original values later.
-
-    Automatically handles resyncing the DB if INSTALLED_APPS is
-    modified.
-
-    """
-    
-    def __init__(self):
-        self._original_settings = {}
-
-    def set(self, **kwargs):
-        for k, v in kwargs.iteritems():
-            self._original_settings.setdefault(k, getattr(settings, k,
-                                                          NO_SETTING))
-            setattr(settings, k, v)
-        if 'INSTALLED_APPS' in kwargs:
-            self.syncdb()
-
-    def syncdb(self):
-        loading.cache.loaded = False
-        call_command('syncdb', verbosity=0)
-
-    def revert(self):
-        for k, v in self._original_settings.iteritems():
-            if v == NO_SETTING:
-                try:
-                    delattr(settings, k)
-                except AttributeError:
-                    pass
-            else:
-                setattr(settings, k, v)
-        if 'INSTALLED_APPS' in self._original_settings:
-            self.syncdb()
-        self._original_settings = {}
-
-
-class SettingsTestCase(TestCase):
-    """
-    A subclass of the Django TestCase with a settings_manager
-    attribute which is an instance of TestSettingsManager.
-
-    """
-    test_settings = ''
-    
-    def __init__(self, *args, **kwargs):
-        super(SettingsTestCase, self).__init__(*args, **kwargs)
-        self.settings_manager = TestSettingsManager()
-        
-    def _pre_setup(self):
-        if self.test_settings:
-            settings_module = import_module(self.test_settings)
-            self.settings_manager.set(**settings_module.settings)
-        super(SettingsTestCase, self)._pre_setup()
-    
-    def _post_teardown(self):
-        if self.test_settings:
-            self.settings_manager.revert()
-        
-        super(SettingsTestCase, self)._post_teardown()
-
-
-def get_only_settings_locals(locals):
-    for key in locals.keys():
-        if key.islower():
-            del locals[key]
-    return locals
+#!/usr/bin/env python
+import os
+import sys
+
+from django.conf import settings
+try:
+    from django.utils.functional import empty
+except ImportError:
+    empty = None
+
+def setup_test_environment():
+    """Reset settings to be just enough to test this app."""
+    settings._wrapped = empty
+    MIDDLEWARES = (
+        'django.middleware.common.CommonMiddleware',
+        'django.contrib.sessions.middleware.SessionMiddleware',
+        'django.middleware.csrf.CsrfViewMiddleware',
+        'django.contrib.auth.middleware.AuthenticationMiddleware',
+        'django.contrib.messages.middleware.MessageMiddleware',
+        # We are concerned about this middleware only.
+        'django_monitor.middleware.MonitorMiddleware',
+    )
+    apps = [
+        'django.contrib.auth',
+        'django.contrib.contenttypes',
+        'django.contrib.sessions',
+        'django.contrib.admin',
+        'django.contrib.sites',
+        'django_monitor',
+        'django_monitor.tests.test_app',
+    ]
+    settings_dict = {
+        'DATABASES': {
+            'default': {
+                'ENGINE': 'django.db.backends.sqlite3',
+                'NAME': 'test_dm.db'
+            }
+        },
+        'MIDDLEWARE_CLASSES': MIDDLEWARES,
+        'INSTALLED_APPS': apps,
+        'STATIC_URL': '/static/',
+        'ROOT_URLCONF': 'django_monitor.tests.urls'
+    }
+    settings.configure(**settings_dict)
+
+def runtests(*test_args):
+    """
+    Build a test environment & a test_runner. Run the tests & return the 
+    number of failures to terminal.
+    """
+    setup_test_environment()
+    parent = os.path.dirname(os.path.abspath(__file__))
+    sys.path.insert(0, parent)
+    try:
+        from django.test.simple import DjangoTestSuiteRunner
+
+        def run_tests(test_args, verbosity, interactive):
+            runner = DjangoTestSuiteRunner(
+                verbosity = verbosity, interactive = interactive, 
+                failfast = True
+            )
+            return runner.run_tests(test_args)
+    except ImportError:
+        from django.test.simple import run_tests
+
+    failures = run_tests(test_args, verbosity = 1, interactive = True)
+    sys.exit(failures)
+
+if __name__ == "__main__":
+    """Unit tests of the app, finance_schedule, are run."""
+    runtests('django_monitor')
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.