Skip to content

Commit

Permalink
split tests into modules, use pytest, 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Oct 17, 2018
1 parent ccc67a1 commit df24810
Show file tree
Hide file tree
Showing 13 changed files with 522 additions and 366 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ docs/_build/
.coverage
.coverage.*
htmlcov/
.hypothesis/
4 changes: 2 additions & 2 deletions src/itsdangerous/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
number_types = (numbers.Real, decimal.Decimal)


def constant_time_compare(val1, val2):
def _constant_time_compare(val1, val2):
"""Return ``True`` if the two strings are equal, ``False``
otherwise.
Expand All @@ -43,4 +43,4 @@ def constant_time_compare(val1, val2):

# Starting with 2.7/3.3 the standard library has a c-implementation for
# constant time string compares.
constant_time_compare = getattr(hmac, "compare_digest", constant_time_compare)
constant_time_compare = getattr(hmac, "compare_digest", _constant_time_compare)
8 changes: 4 additions & 4 deletions src/itsdangerous/jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ def loads(self, s, salt=None, return_header=False):
if "exp" not in header:
raise BadSignature("Missing expiry date", payload=payload)

int_date_error = BadHeader("Expiry date is not an IntDate", payload=payload)
try:
header["exp"] = int(header["exp"])
except ValueError:
raise BadHeader("Expiry date is not valid timestamp", payload=payload)

if not (isinstance(header["exp"], number_types) and header["exp"] > 0):
raise BadSignature("expiry date is not an IntDate", payload=payload)
raise int_date_error
if header["exp"] < 0:
raise int_date_error

if header["exp"] < self.now():
raise SignatureExpired(
Expand Down
Empty file added tests/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions tests/test_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest

from itsdangerous._compat import _constant_time_compare


@pytest.mark.parametrize(
("a", "b", "expect"),
((b"a", b"a", True), (b"a", b"b", False), (b"a", b"aa", False)),
)
def test_python_constant_time_compare(a, b, expect):
assert _constant_time_compare(a, b) == expect
38 changes: 38 additions & 0 deletions tests/test_encoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
import pytest

from itsdangerous.encoding import base64_decode
from itsdangerous.encoding import base64_encode
from itsdangerous.encoding import bytes_to_int
from itsdangerous.encoding import int_to_bytes
from itsdangerous.encoding import want_bytes
from itsdangerous.exc import BadData


@pytest.mark.parametrize("value", (u"mañana", b"tomorrow"))
def test_want_bytes(value):
out = want_bytes(value)
assert isinstance(out, bytes)


@pytest.mark.parametrize("value", (u"無限", b"infinite"))
def test_base64(value):
enc = base64_encode(value)
assert isinstance(enc, bytes)
dec = base64_decode(enc)
assert dec == want_bytes(value)


def test_base64_bad():
with pytest.raises(BadData):
base64_decode("12345")


@pytest.mark.parametrize(
("value", "expect"), ((0, b""), (192, b"\xc0"), (18446744073709551615, b"\xff" * 8))
)
def test_int_bytes(value, expect):
enc = int_to_bytes(value)
assert enc == expect
dec = bytes_to_int(enc)
assert dec == value
Loading

0 comments on commit df24810

Please sign in to comment.