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 a8c7d1b
Show file tree
Hide file tree
Showing 17 changed files with 533 additions and 372 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/
9 changes: 6 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/asottile/reorder_python_imports
rev: v1.2.0
rev: v1.3.1
hooks:
- id: reorder-python-imports
args: ["--application-directories", "src"]
Expand All @@ -9,7 +9,10 @@ repos:
hooks:
- id: black
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v1.4.0-1
rev: v2.0.0
hooks:
- id: check-byte-order-marker
- id: trailing-whitespace
- id: end-of-file-fixer
- id: flake8
additional_dependencies: [flake8-bugbear]
additional_dependencies: [flake8-bugbear]
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ script:
- tox

cache:
- pip
directories:
- $HOME/.cache/pip
- $HOME/.cache/pre-commit

branches:
only:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ The initial implementation of It's Dangerous was inspired by Django's
signing module.

Copyright © Django Software Foundation and individual contributors.
All rights reserved.
All rights reserved.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ ignore = E203, E501, W503
# up to 88 allowed by bugbear B950
max-line-length = 80
# init is used to export public API, ignore import warnings
exclude = src/itsdangerous/__init__.py
exclude = src/itsdangerous/__init__.py
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 a8c7d1b

Please sign in to comment.