浏览代码

Added tests for signing non-string values and updated docs.

Claude Paroz 5 年之前
父节点
当前提交
8c0c0235b6
共有 2 个文件被更改,包括 24 次插入0 次删除
  1. 9 0
      docs/topics/signing.txt
  2. 15 0
      tests/signing/tests.py

+ 9 - 0
docs/topics/signing.txt

@@ -53,6 +53,15 @@ You can retrieve the original value using the ``unsign`` method::
     >>> original
     'My string'
 
+If you pass a non-string value to ``sign``, the value will be forced to string
+before being signed, and the ``unsign`` result will give you that string
+value::
+
+    >>> signed = signer.sign(2.5)
+    >>> original = signer.unsign(signed)
+    >>> original
+    '2.5'
+
 If the signature or value have been altered in any way, a
 ``django.core.signing.BadSignature`` exception will be raised::
 

+ 15 - 0
tests/signing/tests.py

@@ -49,6 +49,21 @@ class TestSigner(SimpleTestCase):
             self.assertNotEqual(example, signed)
             self.assertEqual(example, signer.unsign(signed))
 
+    def test_sign_unsign_non_string(self):
+        signer = signing.Signer('predictable-secret')
+        values = [
+            123,
+            1.23,
+            True,
+            datetime.date.today(),
+        ]
+        for value in values:
+            with self.subTest(value):
+                signed = signer.sign(value)
+                self.assertIsInstance(signed, str)
+                self.assertNotEqual(signed, value)
+                self.assertEqual(signer.unsign(signed), str(value))
+
     def test_unsign_detects_tampering(self):
         "unsign should raise an exception if the value has been tampered with"
         signer = signing.Signer('predictable-secret')