Exemplo n.º 1
0
    def test_creating_an_item_with_unrecognized_mime_type(self):
        """Don't accept files without a recognized MIME Type."""
        # Start by creating a new Item object
        item = Item()

        item.title = "Bar Item"
        item.slug = "bar-item"
        item.description = "File with unrecognized extension and unknown MIME."
        item.creation_time = timezone.now()

        # This filename with '.foo' extension is not a recognized MIME type.
        # TODO: Look for magic numbers etc. to determine MIME type?
        item.storage = 'deleteme.foo'

        # The .clean() method will guess the MIME type.
        # This should raise a ValidationError if it cannot guess.
        self.assertRaises(ValidationError, item.clean)

        # Trying to save this unclean object should be prevented by the DB.
        self.assertRaises(IntegrityError, item.save)

        # Specifying a blank MIME type should also raise an exception.
        item.mime_type = ''

        # Trying to save this file should raise a ValidationError.
        self.assertRaises(ValidationError, item.clean)

        # Now "manually" specify a valid MIME type.
        item.mime_type = 'multipart/encrypted'

        # This should allow it to clean and save normally.
        item.clean()
        item.save()

        my_id = item.id

        # Now check we can find it in the database again.
        my_item_in_database = Item.objects.get(id=my_id)
        self.assertEquals(my_item_in_database, item)

        # And check that it's properly saved its attributes
        self.assertEquals(my_item_in_database.title, "Bar Item")
        self.assertEquals(my_item_in_database.slug, "bar-item")
        self.assertEquals(
            my_item_in_database.description,
            "File with unrecognized extension and unknown MIME.")
        self.assertEquals(my_item_in_database.creation_time,
                          item.creation_time)

        # And test that the MIME type was correctly written.
        self.assertEquals(my_item_in_database.mime_type, 'multipart/encrypted')
Exemplo n.º 2
0
    def test_creating_an_item_with_unrecognized_mime_type(self):
        """Don't accept files without a recognized MIME Type."""
        # Start by creating a new Item object
        item = Item()
        
        item.title = "Bar Item"
        item.slug = "bar-item"
        item.description = "File with unrecognized extension and unknown MIME."
        item.creation_time = timezone.now()

        # This filename with '.foo' extension is not a recognized MIME type.
        # TODO: Look for magic numbers etc. to determine MIME type?
        item.storage = 'deleteme.foo'

        # The .clean() method will guess the MIME type.
        # This should raise a ValidationError if it cannot guess.
        self.assertRaises(ValidationError, item.clean)

        # Trying to save this unclean object should be prevented by the DB.
        self.assertRaises(IntegrityError, item.save)

        # Specifying a blank MIME type should also raise an exception.
        item.mime_type = ''

        # Trying to save this file should raise a ValidationError.
        self.assertRaises(ValidationError, item.clean)

        # Now "manually" specify a valid MIME type.
        item.mime_type = 'multipart/encrypted'

        # This should allow it to clean and save normally.
        item.clean()
        item.save()

        my_id = item.id

        # Now check we can find it in the database again.
        my_item_in_database = Item.objects.get(id=my_id)
        self.assertEquals(my_item_in_database, item)

        # And check that it's properly saved its attributes
        self.assertEquals(my_item_in_database.title, "Bar Item")
        self.assertEquals(my_item_in_database.slug, "bar-item")
        self.assertEquals(my_item_in_database.description,
            "File with unrecognized extension and unknown MIME.")
        self.assertEquals(my_item_in_database.creation_time, item.creation_time)

        # And test that the MIME type was correctly written.
        self.assertEquals(my_item_in_database.mime_type, 'multipart/encrypted')