def test_csrf_token(self):
        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=User.objects.get(username='******'))

        profile.save()

        if django_19():
            user = User.objects.get(username='******')
            self.client.force_login(user)
        else:
            self.client.login(username='******', password='******')

        url = profile.moderated_object.get_admin_moderate_url()

        post_data = {'approve': 'Approve'}

        response = self.client.post(url, post_data)

        self.assertEqual(response.status_code, 302)

        profile = UserProfile.objects.get(pk=profile.pk)

        self.assertEqual(profile.moderated_object.status,
                         MODERATION_STATUS_APPROVED)
Ejemplo n.º 2
0
    def test_csrf_token(self):
        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=User.objects.get(username='******'))

        profile.save()

        if django_19():
            user = User.objects.get(username='******')
            self.client.force_login(user)
        else:
            self.client.login(username='******', password='******')

        url = profile.moderated_object.get_admin_moderate_url()

        post_data = {'approve': 'Approve'}

        response = self.client.post(url, post_data)

        self.assertEqual(response.status_code, 302)

        profile = UserProfile.objects.get(pk=profile.pk)

        self.assertEqual(profile.moderated_object.status,
                         MODERATION_STATUS_APPROVED)
Ejemplo n.º 3
0
    def test_csrf_token(self):
        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=User.objects.get(username='******'))

        profile.save()

        self.client.login(username='******', password='******')

        url = profile.moderated_object.get_admin_moderate_url()

        if django_version == '1.1':
            from django.contrib.csrf.middleware import _make_token

            csrf_token = _make_token(self.client.session.session_key)
            post_data = {'approve': 'Approve',
                         'csrfmiddlewaretoken': csrf_token}
        else:
            post_data = {'approve': 'Approve'}

        response = self.client.post(url, post_data)

        self.assertEqual(response.status_code, 302)

        profile = UserProfile.objects.get(pk=profile.pk)

        self.assertEqual(profile.moderated_object.moderation_status,
                         MODERATION_STATUS_APPROVED)
Ejemplo n.º 4
0
    def test_objects_with_same_object_id_django(self):
        model1 = ModelWithSlugField2(slug='test')
        model1.save()

        model2 = UserProfile(description='Profile for new user',
                             url='http://www.yahoo.com',
                             user=User.objects.get(username='******'))

        model2.save()

        self.assertRaises(MultipleObjectsReturned,
                          ModeratedObject.objects.get,
                          object_pk=model2.pk)

        moderated_obj1 = ModeratedObject.objects.get_for_instance(model1)
        moderated_obj2 = ModeratedObject.objects.get_for_instance(model2)

        self.assertEqual(repr(moderated_obj2),
                         '<ModeratedObject: user1 - http://www.yahoo.com>')
        if VERSION < (2, 0):
            self.assertEqual(repr(moderated_obj1),
                             "<ModeratedObject: ModelWithSlugField2 object>")
        else:
            self.assertEqual(repr(moderated_obj1),
                             "<ModeratedObject: ModelWithSlugField2 object (1)>")
Ejemplo n.º 5
0
    def test_csrf_token(self):
        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=User.objects.get(username='******'))

        profile.save()

        self.client.login(username='******', password='******')

        url = profile.moderated_object.get_admin_moderate_url()

        if django_version == '1.1':
            from django.contrib.csrf.middleware import _make_token

            csrf_token = _make_token(self.client.session.session_key)
            post_data = {
                'approve': 'Approve',
                'csrfmiddlewaretoken': csrf_token
            }
        else:
            post_data = {'approve': 'Approve'}

        response = self.client.post(url, post_data)

        self.assertEqual(response.status_code, 302)

        profile = UserProfile.objects.get(pk=profile.pk)

        self.assertEqual(profile.moderated_object.moderation_status,
                         MODERATION_STATUS_APPROVED)
    def test_if_form_is_initialized_new_object(self):
        profile = UserProfile(description="New description",
                              url='http://test.com',
                              user=self.user)
        profile.save()

        form = self.ModeratedObjectForm(instance=profile)
        self.assertEqual(form.initial['description'], 'New description')
Ejemplo n.º 7
0
    def test_if_form_is_initialized_new_object(self):
        profile = UserProfile(description='New description',
                              url='http://test.com',
                              user=self.user)
        profile.save()

        form = self.ModeratedObjectForm(instance=profile)
        self.assertEqual(form.initial['description'], 'New description')
Ejemplo n.º 8
0
    def test_moderated_object_is_created_when_not_loaded_from_fixture(self):
        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=User.objects.get(username='******'))

        profile.save()

        moderated_objs = ModeratedObject.objects.filter(object_pk=profile.pk)
        self.assertEqual(moderated_objs.count(), 1)
Ejemplo n.º 9
0
    def test_form_when_obj_has_no_moderated_obj(self):
        self.moderation.unregister(UserProfile)
        profile = UserProfile(description='old description',
                              url='http://test.com',
                              user=self.user)
        profile.save()
        self.moderation.register(UserProfile)

        form = self.ModeratedObjectForm(instance=profile)

        self.assertEqual(form.initial['description'], 'old description')
    def test_form_when_obj_has_no_moderated_obj(self):
        self.moderation.unregister(UserProfile)
        profile = UserProfile(description="old description",
                              url='http://test.com',
                              user=self.user)
        profile.save()
        self.moderation.register(UserProfile)

        form = self.ModeratedObjectForm(instance=profile)

        self.assertEqual(form.initial['description'], 'old description')
Ejemplo n.º 11
0
    def test_if_form_is_initialized_new_object_with_initial(self):
        profile = UserProfile(description="New description",
                              url='http://test.com',
                              user=self.user)
        profile.save()

        form = self.ModeratedObjectForm(initial={'extra': 'value'},
                                        instance=profile)

        self.assertEqual(form.initial['description'], 'New description')
        self.assertEqual(form.initial['extra'], 'value')
Ejemplo n.º 12
0
    def test_approve_moderated_object_new_model_instance(self):
        profile = UserProfile(description='Profile for new user',
                              url='http://www.test.com',
                              user=User.objects.get(username='******'))

        profile.save()

        profile.moderated_object.approve(self.user)

        user_profile = UserProfile.objects.get(url='http://www.test.com')

        self.assertEqual(user_profile.description, 'Profile for new user')
Ejemplo n.º 13
0
    def test_after_disconnecting_signals_moderation_object(self):
        self.moderation._connect_signals(UserProfile)
        self.moderation._disconnect_signals(UserProfile)

        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=User.objects.get(username='******'))

        profile.save()

        self.assertRaises(ObjectDoesNotExist, ModeratedObject.objects.get,
                          object_pk=profile.pk)
Ejemplo n.º 14
0
    def test_approve_moderated_object_new_model_instance(self):
        profile = UserProfile(description='Profile for new user',
                              url='http://www.test.com',
                              user=User.objects.get(username='******'))

        profile.save()

        profile.moderated_object.approve(self.user)

        user_profile = UserProfile.objects.get(url='http://www.test.com')

        self.assertEqual(user_profile.description, 'Profile for new user')
Ejemplo n.º 15
0
    def test_creation_of_moderated_object(self):
        """
        Test if after create of new object moderated object should be created
        """
        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=User.objects.get(username='******'))

        profile.save()

        moderated_object = ModeratedObject.objects.get_for_instance(profile)

        self.assertEqual(str(moderated_object), "user1 - http://www.yahoo.com")
Ejemplo n.º 16
0
    def test_get_of_new_object_should_raise_exception(self):
        """Tests if after register of model class with moderation, 
           when new object is created and getting of object 
           raise ObjectDoesNotExist"""

        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=User.objects.get(username='******'))

        profile.save()

        self.assertRaises(ObjectDoesNotExist, UserProfile.objects.get,
                          pk=profile.pk)
Ejemplo n.º 17
0
    def test_creation_of_moderated_object(self):
        """
        Test if after create of new object moderated object should be created
        """
        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=User.objects.get(username='******'))

        profile.save()

        moderated_object = ModeratedObject.objects.get_for_instance(profile)

        self.assertEqual(str(moderated_object),
                         "user1 - http://www.yahoo.com")
Ejemplo n.º 18
0
    def test_unregister(self):
        """Tests if model class is successfully unregistered from moderation"""
        from django.db.models import signals

        old_pre_save_receivers = [r for r in signals.pre_save.receivers]
        old_post_save_receivers = [r for r in signals.post_save.receivers]

        signals.pre_save.receivers = []
        signals.post_save.receivers = []
        self.moderation.register(UserProfile)

        self.assertNotEqual(signals.pre_save.receivers, [])
        self.assertNotEqual(signals.post_save.receivers, [])

        UserProfile(description='Profile for new user',
                    url='http://www.yahoo.com',
                    user=User.objects.get(username='******')).save()

        self.moderation.unregister(UserProfile)

        self.assertEqual(signals.pre_save.receivers, [])
        self.assertEqual(signals.post_save.receivers, [])

        self.assertEqual(UserProfile.objects.__class__, Manager)
        self.assertEqual(hasattr(UserProfile, 'moderated_object'), False)

        signals.pre_save.receivers = old_pre_save_receivers
        signals.post_save.receivers = old_post_save_receivers

        UserProfile.objects.get(user__username='******')

        User.objects.get(username='******')
        management.call_command('loaddata', 'test_moderation.json',
                                verbosity=0)
Ejemplo n.º 19
0
    def test_pre_save_handler_for_new_object(self):
        from django.db.models import signals

        signals.pre_save.connect(self.moderation.pre_save_handler,
                                 sender=UserProfile)
        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=User.objects.get(username='******'))

        profile.save()

        self.assertRaises(ObjectDoesNotExist,
                          ModeratedObject.objects.get_for_instance, profile)

        signals.pre_save.disconnect(self.moderation.pre_save_handler,
                                    UserProfile)
Ejemplo n.º 20
0
    def test_pre_save_handler_for_new_object(self):
        from django.db.models import signals

        signals.pre_save.connect(self.moderation.pre_save_handler,
                                 sender=UserProfile)
        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=User.objects.get(username='******'))

        profile.save()

        self.assertRaises(ObjectDoesNotExist,
                          ModeratedObject.objects.get_for_instance,
                          profile)

        signals.pre_save.disconnect(self.moderation.pre_save_handler,
                                    UserProfile)
Ejemplo n.º 21
0
    def test_instance_with_many_moderations(self):
        # Register a moderator that keeps the history
        # un-register UserProfile
        from moderation import moderation

        class KeepHistoryModerator(GenericModerator):
            keep_history = True

        moderation.unregister(UserProfile)
        moderation.register(UserProfile, KeepHistoryModerator)

        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=self.user)
        profile.save()

        # There should only be one ModeratedObject
        self.assertEqual(1, ModeratedObject.objects.count())

        profile.url = 'http://www.google.com'
        profile.save()

        # Should now be two
        self.assertEqual(2, ModeratedObject.objects.count())

        # Getting for instance should return the one with the google url
        moderated_object = ModeratedObject.objects.get_for_instance(profile)
        self.assertEqual(profile.url, moderated_object.changed_object.url)

        # Get the first object, and does it have the yahoo address
        moderated_object_pk1 = ModeratedObject.objects.get(pk=1)
        self.assertEqual('http://www.yahoo.com',
                         moderated_object_pk1.changed_object.url)
Ejemplo n.º 22
0
    def test_save_new_instance_after_add_and_remove_fields_from_class(self):
        """Test if after removing moderation from model class new 
        instance of model can be created"""
        class CustomManager(Manager):
            pass

        moderator = GenericModerator(UserProfile)
        self.moderation._and_fields_to_model_class(moderator)

        self.moderation._remove_fields(moderator)

        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=User.objects.get(username='******'))

        profile.save()

        up = UserProfile._default_manager.filter(url='http://www.yahoo.com')
        self.assertEqual(up.count(), 1)
    def test_excluded_field_should_not_be_moderated_when_obj_is_created(self):
        '''
        Create new object, only non excluded fields are used
        by moderation system
        '''
        profile = UserProfile(description='Profile for new user',
                              url='http://www.dominno.com',
                              user=User.objects.get(username='******'))
        profile.save()

        url = reverse('admin:moderation_moderatedobject_change',
                      args=(profile.moderated_object.pk,))

        response = self.client.get(url, {})

        changes = [change.change for change in response.context['changes']]

        self.assertFalse(('http://www.dominno.com',
                          'http://www.dominno.com') in changes)
Ejemplo n.º 24
0
    def test_excluded_field_should_not_be_moderated_when_obj_is_created(self):
        '''
        Create new object, only non excluded fields are used
        by moderation system
        '''
        profile = UserProfile(description='Profile for new user',
                              url='http://www.dominno.com',
                              user=User.objects.get(username='******'))
        profile.save()

        url = reverse('admin:moderation_moderatedobject_change',
                      args=(profile.moderated_object.pk, ))

        response = self.client.get(url, {})

        changes = [change.change for change in response.context['changes']]

        self.assertFalse(('http://www.dominno.com',
                          'http://www.dominno.com') in changes)
Ejemplo n.º 25
0
    def test_save_new_instance_after_add_and_remove_fields_from_class(self):
        """Test if after removing moderation from model class new 
        instance of model can be created"""

        class CustomManager(Manager):
            pass

        moderator = GenericModerator(UserProfile)
        self.moderation._add_fields_to_model_class(moderator)

        self.moderation._remove_fields(moderator)

        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=User.objects.get(username='******'))

        profile.save()

        up = UserProfile._default_manager.filter(url='http://www.yahoo.com')
        self.assertEqual(up.count(), 1)
Ejemplo n.º 26
0
    def test_register(self):
        """Tests if after creation of new model instance new 
        moderation object is created"""
        UserProfile(description='Profile for new user',
                    url='http://www.yahoo.com',
                    user=User.objects.get(username='******')).save()

        self.assertEqual(ModeratedObject.objects.all().count(), 1,
                         "New moderation object was not created"
                         " after creation of new model instance "
                         "from model class that is registered with moderation")
Ejemplo n.º 27
0
    def test_post_save_handler_for_new_object(self):
        from django.db.models import signals

        signals.pre_save.connect(self.moderation.pre_save_handler,
                                 sender=UserProfile)
        signals.post_save.connect(self.moderation.post_save_handler,
                                  sender=UserProfile)
        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=User.objects.get(username='******'))

        profile.save()

        moderated_object = ModeratedObject.objects.get_for_instance(profile)

        self.assertEqual(moderated_object.content_object, profile)

        signals.pre_save.disconnect(self.moderation.pre_save_handler,
                                    UserProfile)
        signals.post_save.disconnect(self.moderation.post_save_handler,
                                     UserProfile)
Ejemplo n.º 28
0
    def test_objects_with_same_object_id(self):
        model1 = ModelWithSlugField2(slug='test')
        model1.save()

        model2 = UserProfile(description='Profile for new user',
                             url='http://www.yahoo.com',
                             user=User.objects.get(username='******'))

        model2.save()

        self.assertRaises(MultipleObjectsReturned,
                          ModeratedObject.objects.get,
                          object_pk=model2.pk)

        moderated_obj1 = ModeratedObject.objects.get_for_instance(model1)
        moderated_obj2 = ModeratedObject.objects.get_for_instance(model2)

        self.assertEqual(repr(moderated_obj1),
                         u"<ModeratedObject: ModelWithSlugField2 object>")
        self.assertEqual(repr(moderated_obj2),
                         u'<ModeratedObject: user1 - http://www.yahoo.com>')
Ejemplo n.º 29
0
    def test_instance_with_many_moderations(self):
        # Register a moderator that keeps the history
        # un-register UserProfile
        from moderation import moderation

        class KeepHistoryModerator(GenericModerator):
            keep_history = True

        moderation.unregister(UserProfile)
        moderation.register(UserProfile, KeepHistoryModerator)

        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=self.user)
        profile.save()

        # There should only be one ModeratedObject
        self.assertEqual(1, ModeratedObject.objects.count())

        profile.url = 'http://www.google.com'
        profile.save()

        # Should now be two
        self.assertEqual(2, ModeratedObject.objects.count())

        # Getting for instance should return the one with the google url
        moderated_object = ModeratedObject.objects.get_for_instance(profile)
        self.assertEqual(profile.url, moderated_object.changed_object.url)

        # Get the first object, and does it have the yahoo address
        moderated_object_pk1 = ModeratedObject.objects.get(pk=1)
        self.assertEqual('http://www.yahoo.com',
                         moderated_object_pk1.changed_object.url)
Ejemplo n.º 30
0
    def test_bypass_moderation_after_approval(self):
        profile = UserProfile(description='Profile for new user',
                              url='http://www.test.com',
                              user=User.objects.get(username='******'))
        profile.save()

        profile.moderated_object.approve(self.user)

        profile.description = 'New description'
        profile.save()

        self.assertEqual(profile.moderated_object.moderation_status,
                         MODERATION_STATUS_APPROVED)
Ejemplo n.º 31
0
    def test_save_handler_keep_history(self):
        # de-register current Moderator and replace it with one that
        # has keep_history set to True
        from moderation import moderation

        class KeepHistoryModerator(GenericModerator):
            keep_history = True
            notify_moderator = False

        moderation.unregister(UserProfile)
        moderation.register(UserProfile, KeepHistoryModerator)

        from django.db.models import signals

        signals.pre_save.connect(self.moderation.pre_save_handler,
                                 sender=UserProfile)
        signals.post_save.connect(self.moderation.post_save_handler,
                                  sender=UserProfile)
        profile = UserProfile(description='Profile for new user',
                              url='http://www.yahoo.com',
                              user=User.objects.get(username='******'))

        profile.save()

        moderated_object = ModeratedObject.objects.get_for_instance(profile)

        self.assertEqual(moderated_object.content_object, profile)

        # Now update it and make sure it gets the right history object...
        profile.url = 'http://www.google.com'
        profile.save()

        moderated_object = ModeratedObject.objects.get_for_instance(profile)
        self.assertEqual(moderated_object.content_object, profile)

        # There should only be two moderated objects
        self.assertEqual(2, ModeratedObject.objects.count())

        # Approve the change
        moderated_object.approve(by=self.user,
                                 reason='Testing post save handlers')

        # There should *still* only be two moderated objects
        self.assertEqual(2, ModeratedObject.objects.count())

        signals.pre_save.disconnect(self.moderation.pre_save_handler,
                                    UserProfile)
        signals.post_save.disconnect(self.moderation.post_save_handler,
                                     UserProfile)

        self.moderation = False
Ejemplo n.º 32
0
    def test_bypass_moderation_after_approval(self):
        profile = UserProfile(description='Profile for new user',
                              url='http://www.test.com',
                              user=User.objects.get(username='******'))
        profile.save()

        profile.moderated_object.approve(self.user)

        profile.description = 'New description'
        profile.save()

        self.assertEqual(profile.moderated_object.moderation_status,
                         MODERATION_STATUS_APPROVED)
Ejemplo n.º 33
0
    def test_if_form_is_initialized_existing_object(self):
        profile = UserProfile(description='old description',
                              url='http://test.com',
                              user=self.user)
        profile.save()

        profile.moderated_object.approve(by=self.user)

        profile.description = 'Changed description'
        profile.save()

        form = self.ModeratedObjectForm(instance=profile)

        profile = UserProfile.objects.get(id=1)

        self.assertEqual(profile.description, 'old description')
        self.assertEqual(form.initial['description'], 'Changed description')
    def test_if_form_is_initialized_existing_object(self):
        profile = UserProfile(description="old description",
                              url='http://test.com',
                              user=self.user)
        profile.save()

        profile.moderated_object.approve(moderated_by=self.user)

        profile.description = "Changed description"
        profile.save()

        form = self.ModeratedObjectForm(instance=profile)

        profile = UserProfile.objects.get(id=1)

        self.assertEqual(profile.description, "old description")
        self.assertEqual(form.initial['description'], 'Changed description')
Ejemplo n.º 35
0
    def test_excluded_field_should_always_be_saved(self):
        '''
        Ensure that excluded fields are always saved to the object.
        '''
        profile = UserProfile(description='Profile for new user',
                              url='http://www.dominno.com',
                              user=User.objects.get(username='******'))
        profile.save()
        profile.moderated_object.approve()

        profile = UserProfile.objects.get(id=profile.id)
        profile.description = 'New profile'
        profile.save()
        profile.url = 'http://dominno.pl'
        profile.save()
        profile.moderated_object.approve()
        profile = UserProfile.objects.get(id=profile.id)

        self.assertEqual(profile.description, 'New profile')
        self.assertEqual(profile.url, 'http://dominno.pl')

        profile.url = 'http://www.google.com'
        profile.save()
        profile.moderated_object.approve()
        profile = UserProfile.objects.get(id=profile.id)

        self.assertEqual(profile.url, 'http://www.google.com')
Ejemplo n.º 36
0
    def test_excluded_field_should_always_be_saved(self):
        '''
        Ensure that excluded fields are always saved to the object.
        '''
        profile = UserProfile(description='Profile for new user',
                              url='http://www.dominno.com',
                              user=User.objects.get(username='******'))
        profile.save()
        profile.moderated_object.approve()

        profile = UserProfile.objects.get(id=profile.id)
        profile.description = 'New profile'
        profile.save()
        profile.url = 'http://dominno.pl'
        profile.save()
        profile.moderated_object.approve()
        profile = UserProfile.objects.get(id=profile.id)

        self.assertEqual(profile.description, 'New profile')
        self.assertEqual(profile.url, 'http://dominno.pl')

        profile.url = 'http://www.google.com'
        profile.save()
        profile.moderated_object.approve()
        profile = UserProfile.objects.get(id=profile.id)

        self.assertEqual(profile.url, 'http://www.google.com')