Skip to content

Commit 1af89f9

Browse files
committed
split tests into modules, use pytest, 100% coverage
1 parent ccc67a1 commit 1af89f9

17 files changed

+532
-371
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ docs/_build/
1616
.coverage
1717
.coverage.*
1818
htmlcov/
19+
.hypothesis/

.pre-commit-config.yaml

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/asottile/reorder_python_imports
3-
rev: v1.2.0
3+
rev: v1.3.1
44
hooks:
55
- id: reorder-python-imports
66
args: ["--application-directories", "src"]
@@ -9,7 +9,10 @@ repos:
99
hooks:
1010
- id: black
1111
- repo: https://github.com/pre-commit/pre-commit-hooks
12-
rev: v1.4.0-1
12+
rev: v2.0.0
1313
hooks:
14+
- id: check-byte-order-marker
15+
- id: trailing-whitespace
16+
- id: end-of-file-fixer
1417
- id: flake8
15-
additional_dependencies: [flake8-bugbear]
18+
additional_dependencies: [flake8-bugbear]

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ script:
2626

2727
cache:
2828
- pip
29+
directories:
30+
- $HOME/.cache/pre-commit
2931

3032
branches:
3133
only:

LICENSE.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ The initial implementation of It's Dangerous was inspired by Django's
4444
signing module.
4545

4646
Copyright © Django Software Foundation and individual contributors.
47-
All rights reserved.
47+
All rights reserved.

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ ignore = E203, E501, W503
3333
# up to 88 allowed by bugbear B950
3434
max-line-length = 80
3535
# init is used to export public API, ignore import warnings
36-
exclude = src/itsdangerous/__init__.py
36+
exclude = src/itsdangerous/__init__.py

src/itsdangerous/_compat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
number_types = (numbers.Real, decimal.Decimal)
1717

1818

19-
def constant_time_compare(val1, val2):
19+
def _constant_time_compare(val1, val2):
2020
"""Return ``True`` if the two strings are equal, ``False``
2121
otherwise.
2222
@@ -43,4 +43,4 @@ def constant_time_compare(val1, val2):
4343

4444
# Starting with 2.7/3.3 the standard library has a c-implementation for
4545
# constant time string compares.
46-
constant_time_compare = getattr(hmac, "compare_digest", constant_time_compare)
46+
constant_time_compare = getattr(hmac, "compare_digest", _constant_time_compare)

src/itsdangerous/jws.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,13 @@ def loads(self, s, salt=None, return_header=False):
190190
if "exp" not in header:
191191
raise BadSignature("Missing expiry date", payload=payload)
192192

193+
int_date_error = BadHeader("Expiry date is not an IntDate", payload=payload)
193194
try:
194195
header["exp"] = int(header["exp"])
195196
except ValueError:
196-
raise BadHeader("Expiry date is not valid timestamp", payload=payload)
197-
198-
if not (isinstance(header["exp"], number_types) and header["exp"] > 0):
199-
raise BadSignature("expiry date is not an IntDate", payload=payload)
197+
raise int_date_error
198+
if header["exp"] < 0:
199+
raise int_date_error
200200

201201
if header["exp"] < self.now():
202202
raise SignatureExpired(

tests/__init__.py

Whitespace-only changes.

tests/test_compat.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
3+
from itsdangerous._compat import _constant_time_compare
4+
5+
6+
@pytest.mark.parametrize(
7+
("a", "b", "expect"),
8+
((b"a", b"a", True), (b"a", b"b", False), (b"a", b"aa", False)),
9+
)
10+
def test_python_constant_time_compare(a, b, expect):
11+
assert _constant_time_compare(a, b) == expect

tests/test_encoding.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
import pytest
3+
4+
from itsdangerous.encoding import base64_decode
5+
from itsdangerous.encoding import base64_encode
6+
from itsdangerous.encoding import bytes_to_int
7+
from itsdangerous.encoding import int_to_bytes
8+
from itsdangerous.encoding import want_bytes
9+
from itsdangerous.exc import BadData
10+
11+
12+
@pytest.mark.parametrize("value", (u"mañana", b"tomorrow"))
13+
def test_want_bytes(value):
14+
out = want_bytes(value)
15+
assert isinstance(out, bytes)
16+
17+
18+
@pytest.mark.parametrize("value", (u"無限", b"infinite"))
19+
def test_base64(value):
20+
enc = base64_encode(value)
21+
assert isinstance(enc, bytes)
22+
dec = base64_decode(enc)
23+
assert dec == want_bytes(value)
24+
25+
26+
def test_base64_bad():
27+
with pytest.raises(BadData):
28+
base64_decode("12345")
29+
30+
31+
@pytest.mark.parametrize(
32+
("value", "expect"), ((0, b""), (192, b"\xc0"), (18446744073709551615, b"\xff" * 8))
33+
)
34+
def test_int_bytes(value, expect):
35+
enc = int_to_bytes(value)
36+
assert enc == expect
37+
dec = bytes_to_int(enc)
38+
assert dec == value

0 commit comments

Comments
 (0)