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'], u'New description')
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)
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'], u'old description')
def test_send_message_when_object_has_no_moderated_object(self): profile = UserProfile(description='Profile for new user', url='http://www.yahoo.com', user=User.objects.get(username='******')) profile.save() self.admin.send_message(self.request, profile.pk) message = self.request.user.message_set.get() self.assertEqual(unicode(message), u"This object is not registered "\ u"with the moderation system.")
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)
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)
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() modearated_object = ModeratedObject.objects.get_for_instance(profile) self.assertEqual(unicode(modearated_object), u"user1 - http://www.yahoo.com")
def test_serialize_of_many_objects(self): """Test if object is propertly serialized to json""" profile = UserProfile(description='Profile for new user', url='http://www.test.com', user=User.objects.get(username='******')) profile.save() json_field = SerializedObjectField() self.assertEqual(json_field._serialize(UserProfile.objects.all()), '[{"pk": 1, "model": "test_app.userprofile", '\ '"fields": {"url": "http://www.google.com",'\ ' "user": 1, "description": "Profile description"}},'\ ' {"pk": 2, "model": "test_app.userprofile", "fields":'\ ' {"url": "http://www.test.com", "user": 2, '\ '"description": "Profile for new user"}}]')
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>")
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 = u"Changed description" profile.save() form = self.ModeratedObjectForm(instance=profile) profile = UserProfile.objects.get(id=1) self.assertEqual(profile.description, u"old description") self.assertEqual(form.initial['description'], u'Changed description')
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)
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)
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""" from django.db.models import signals 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)