Skip to content

Commit

Permalink
Added test cases for the change
Browse files Browse the repository at this point in the history
  • Loading branch information
gullpong committed Mar 14, 2014
1 parent fbd3a25 commit 66daa82
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
4 changes: 2 additions & 2 deletions jwt/__init__.py
Expand Up @@ -81,9 +81,9 @@ def prepare_HS_key(key):
})

def prepare_RS_key(key):
if isinstance(key, basestring) and key.startswith('-----BEGIN '):
if isinstance(key, basestring):
if isinstance(key, unicode):
key = key.encode('utf-8')
key = key.encode('utf-8')
key = RSA.importKey(key)
elif isinstance(key, RSA._RSAobj):
pass
Expand Down
68 changes: 66 additions & 2 deletions tests/test_jwt.py
Expand Up @@ -35,16 +35,26 @@ def test_encode_bad_type(self):
for t in types:
self.assertRaises(TypeError, lambda: jwt.encode(t, 'secret'))

def test_encode_expiration_datetime(self):
def test_encode_datetime(self):
secret = "secret"
current_datetime = datetime.utcnow()
payload = {"exp": current_datetime}
payload = {
"exp": current_datetime,
"iat": current_datetime,
"nbf": current_datetime
}
jwt_message = jwt.encode(payload, secret)
decoded_payload = jwt.decode(jwt_message, secret, leeway=1)

self.assertEqual(
decoded_payload['exp'],
timegm(current_datetime.utctimetuple()))
self.assertEqual(
decoded_payload['iat'],
timegm(current_datetime.utctimetuple()))
self.assertEqual(
decoded_payload['nbf'],
timegm(current_datetime.utctimetuple()))

def test_bad_secret(self):
right_secret = 'foo'
Expand Down Expand Up @@ -301,6 +311,7 @@ def test_encode_decode_with_rsa_sha256(self):
try:
from Crypto.PublicKey import RSA

# RSA-formatted key
with open('tests/testkey', 'r') as rsa_priv_file:
priv_rsakey = RSA.importKey(rsa_priv_file.read())
jwt_message = jwt.encode(self.payload, priv_rsakey,
Expand All @@ -312,13 +323,28 @@ def test_encode_decode_with_rsa_sha256(self):

load_output = jwt.load(jwt_message)
jwt.verify_signature(key=pub_rsakey, *load_output)

# string-formatted key
with open('tests/testkey', 'r') as rsa_priv_file:
priv_rsakey = rsa_priv_file.read()
jwt_message = jwt.encode(self.payload, priv_rsakey,
algorithm='RS256')

with open('tests/testkey.pub', 'r') as rsa_pub_file:
pub_rsakey = rsa_pub_file.read()
assert jwt.decode(jwt_message, pub_rsakey)

load_output = jwt.load(jwt_message)
jwt.verify_signature(key=pub_rsakey, *load_output)

except ImportError:
pass

def test_encode_decode_with_rsa_sha384(self):
try:
from Crypto.PublicKey import RSA

# RSA-formatted key
with open('tests/testkey', 'r') as rsa_priv_file:
priv_rsakey = RSA.importKey(rsa_priv_file.read())
jwt_message = jwt.encode(self.payload, priv_rsakey,
Expand All @@ -328,6 +354,19 @@ def test_encode_decode_with_rsa_sha384(self):
pub_rsakey = RSA.importKey(rsa_pub_file.read())
assert jwt.decode(jwt_message, pub_rsakey)

load_output = jwt.load(jwt_message)
jwt.verify_signature(key=pub_rsakey, *load_output)

# string-formatted key
with open('tests/testkey', 'r') as rsa_priv_file:
priv_rsakey = rsa_priv_file.read()
jwt_message = jwt.encode(self.payload, priv_rsakey,
algorithm='RS384')

with open('tests/testkey.pub', 'r') as rsa_pub_file:
pub_rsakey = rsa_pub_file.read()
assert jwt.decode(jwt_message, pub_rsakey)

load_output = jwt.load(jwt_message)
jwt.verify_signature(key=pub_rsakey, *load_output)
except ImportError:
Expand All @@ -337,6 +376,7 @@ def test_encode_decode_with_rsa_sha512(self):
try:
from Crypto.PublicKey import RSA

# RSA-formatted key
with open('tests/testkey', 'r') as rsa_priv_file:
priv_rsakey = RSA.importKey(rsa_priv_file.read())
jwt_message = jwt.encode(self.payload, priv_rsakey,
Expand All @@ -346,6 +386,19 @@ def test_encode_decode_with_rsa_sha512(self):
pub_rsakey = RSA.importKey(rsa_pub_file.read())
assert jwt.decode(jwt_message, pub_rsakey)

load_output = jwt.load(jwt_message)
jwt.verify_signature(key=pub_rsakey, *load_output)

# string-formatted key
with open('tests/testkey', 'r') as rsa_priv_file:
priv_rsakey = rsa_priv_file.read()
jwt_message = jwt.encode(self.payload, priv_rsakey,
algorithm='RS512')

with open('tests/testkey.pub', 'r') as rsa_pub_file:
pub_rsakey = rsa_pub_file.read()
assert jwt.decode(jwt_message, pub_rsakey)

load_output = jwt.load(jwt_message)
jwt.verify_signature(key=pub_rsakey, *load_output)
except ImportError:
Expand Down Expand Up @@ -373,6 +426,17 @@ def test_crypto_related_verify_methods(self):
self.assertFalse('RS384' in jwt.verify_methods)
self.assertFalse('RS512' in jwt.verify_methods)

def test_crypto_related_key_preparation_methods(self):
try:
import Crypto
self.assertTrue('RS256' in jwt.prepare_key_methods)
self.assertTrue('RS384' in jwt.prepare_key_methods)
self.assertTrue('RS512' in jwt.prepare_key_methods)
except ImportError:
self.assertFalse('RS256' in jwt.prepare_key_methods)
self.assertFalse('RS384' in jwt.prepare_key_methods)
self.assertFalse('RS512' in jwt.prepare_key_methods)


if __name__ == '__main__':
unittest.main()

0 comments on commit 66daa82

Please sign in to comment.