示例#1
0
    def testFileModelSignature(self):
        """Try to sign a basic model and get a Signature
        """
        # Sign
        filepath = os.path.join(settings.MEDIA_ROOT, 'afile.txt')
        book1 = Book(name="A book", afile=filepath)
        book1.save()
        book1 = Book.objects.get(pk=1)
        signed = self.c_cert.make_signature(book1, self.c_pwd)
        self.assertTrue(isinstance(signed, Signature))
        content_type = ContentType.objects.get_for_model(book1)
        self.assertEqual(signed.content_type, content_type)
        self.assertEqual(signed.object_id, 1)
        signed.save()

        # Verify
        signed = Signature.objects.get(pk=1)
        result = signed.check_pkcs7()
        self.assertTrue(result)
        result = signed.check()
        self.assertTrue(result)

        # other file with same content
        filepath = os.path.join(settings.MEDIA_ROOT, 'otherfile.txt')
        book1.afile = filepath
        book1.save()
        signed = Signature.objects.get(pk=1)
        result = signed.check_pkcs7()
        self.assertTrue(result)
        result = signed.check()
        self.assertTrue(result)

        # other file with wrong content
        afile = "wrongfile.txt"
        filepath = os.path.join(settings.MEDIA_ROOT, 'wrongfile.txt')
        book1.afile = filepath
        book1.save()
        signed = Signature.objects.get(pk=1)
        result = signed.check()
        self.assertFalse(result)