Skip to content

Commit c405363

Browse files
committed
Added tests for crypto-module
1 parent a435ec9 commit c405363

File tree

2 files changed

+139
-1
lines changed

2 files changed

+139
-1
lines changed

tests/test_crypto.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# -*- coding: utf-8 -*-
2+
"""Includes tests targeting the crypto abstraction layer.
3+
4+
- target file: auth_enhanced/crypto.py
5+
- included tags: 'crypto'"""
6+
7+
8+
# Python imports
9+
from unittest import skip # noqa
10+
11+
# Django imports
12+
from django.contrib.auth import get_user_model
13+
from django.core.signing import BadSignature, SignatureExpired
14+
from django.test import override_settings, tag # noqa
15+
16+
# app imports
17+
from auth_enhanced.crypto import EnhancedCrypto
18+
19+
# app imports
20+
from .utils.testcases import AuthEnhancedTestCase
21+
22+
try:
23+
from unittest import mock
24+
except ImportError:
25+
import mock # FIXME: How the fuck is this library called in P2.7?!?
26+
27+
28+
@tag('crypto')
29+
class EnhancedCryptoTests(AuthEnhancedTestCase):
30+
"""These tests target the EnhancedCrypto class."""
31+
32+
class MockedTimestampSigner(object):
33+
34+
def sign(self, item):
35+
return 'yyyy-mm-dd:{}:{}'.format(item, item)
36+
37+
def unsign_valid(self, token, max_age=None):
38+
return 'foo'
39+
40+
def unsign_expired(self, token, max_age=None):
41+
raise SignatureExpired('bar')
42+
43+
def unsign_bad_signature(self, token, max_age=None):
44+
raise BadSignature('bar')
45+
46+
def unsign_type_error(self, token, max_age=None):
47+
raise TypeError('bar')
48+
49+
@override_settings(DAE_VERIFICATION_TOKEN_MAX_AGE=5)
50+
def test_max_age_applied(self):
51+
"""Is the setting correctly read and applied?
52+
53+
See '__init__()'-method."""
54+
55+
c = EnhancedCrypto()
56+
57+
self.assertEqual(c.max_age, 5)
58+
59+
@mock.patch('django.core.signing.TimestampSigner.sign', MockedTimestampSigner.sign)
60+
def test_get_token_valid(self):
61+
"""Is a token generated for a valid user object?
62+
63+
See 'get_verification_token()'-method."""
64+
65+
# create a User object to pass along
66+
u = get_user_model().objects.create(username='foo')
67+
c = EnhancedCrypto()
68+
t = c.get_verification_token(u)
69+
70+
# asserts here!
71+
self.assertEqual(t, 'yyyy-mm-dd:foo:foo')
72+
73+
def test_get_token_invalid(self):
74+
"""Is a token generated for a valid user object?
75+
76+
See 'get_verification_token()'-method."""
77+
78+
c = EnhancedCrypto()
79+
80+
with self.assertRaisesMessage(
81+
EnhancedCrypto.EnhancedCryptoException,
82+
"Something went wrong during crypto operations. This error message "
83+
"is unspecific to prevent any fingerprinting."
84+
):
85+
t = c.get_verification_token(None) # noqa
86+
87+
@mock.patch('django.core.signing.TimestampSigner.unsign', MockedTimestampSigner.unsign_valid)
88+
def test_verify_token_valid(self):
89+
"""A valid token returns a username.
90+
91+
See 'verify_token()'-method."""
92+
93+
c = EnhancedCrypto()
94+
u = c.verify_token(token='foo')
95+
96+
self.assertEqual(u, 'foo')
97+
98+
@mock.patch('django.core.signing.TimestampSigner.unsign', MockedTimestampSigner.unsign_expired)
99+
def test_verify_token_expired(self):
100+
"""An expired token raises a specific exception.
101+
102+
See 'verify_token()'-method."""
103+
104+
c = EnhancedCrypto()
105+
106+
with self.assertRaisesMessage(SignatureExpired, 'bar'):
107+
u = c.verify_token(token='foo') # noqa
108+
109+
@mock.patch('django.core.signing.TimestampSigner.unsign', MockedTimestampSigner.unsign_bad_signature)
110+
def test_verify_token_bad_signature(self):
111+
"""'BadSignature' is caught and substituted by an own error.
112+
113+
See 'verify_token()'-method."""
114+
115+
c = EnhancedCrypto()
116+
117+
with self.assertRaisesMessage(
118+
EnhancedCrypto.EnhancedCryptoException,
119+
"Something went wrong during crypto operations. This error "
120+
"message is unspecific to prevent any fingerprinting."
121+
):
122+
u = c.verify_token(token='foo') # noqa
123+
124+
@mock.patch('django.core.signing.TimestampSigner.unsign', MockedTimestampSigner.unsign_type_error)
125+
def test_verify_token_type_error(self):
126+
"""'TypeError' is caught and substituted by an own error.
127+
128+
See 'verify_token()'-method."""
129+
130+
c = EnhancedCrypto()
131+
132+
with self.assertRaisesMessage(
133+
EnhancedCrypto.EnhancedCryptoException,
134+
"'verify_token()' was called without an actual token. "
135+
"You see this message, because this is probably a "
136+
"programming error/mistake."
137+
):
138+
u = c.verify_token(token='foo') # noqa

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ setenv =
1919
PYTHONDONTWRITEBYTECODE=1
2020
deps =
2121
coverage
22-
# py27: mock # currently unused
22+
py27: mock # currently unused
2323
django111: Django>=1.11, <2.0
2424
django20: Django>=2.0b1, <2.1
2525
commands =

0 commit comments

Comments
 (0)