|
| 1 | +import time |
1 | 2 | from twilio import jwt
|
2 | 3 | import sys
|
3 | 4 | if sys.version_info < (2, 7):
|
|
7 | 8 |
|
8 | 9 | from twilio.util import TwilioCapability
|
9 | 10 |
|
10 |
| -class TokenTest(unittest.TestCase): |
| 11 | +class JwtTest(unittest.TestCase): |
11 | 12 |
|
12 | 13 | def assertIn(self, foo, bar, msg=None):
|
13 | 14 | """backport for 2.6"""
|
@@ -80,3 +81,43 @@ def test_decode(self):
|
80 | 81
8000
code> | self.assertIn(outgoing_uri, scope)
|
81 | 82 | self.assertIn(incoming_uri, scope)
|
82 | 83 | self.assertIn(event_uri, scope)
|
| 84 | + |
| 85 | + def setUp(self): |
| 86 | + self.payload = {"iss": "jeff", "exp": int(time.time()), "claim": "insanity"} |
| 87 | + |
| 88 | + def test_encode_decode(self): |
| 89 | + secret = 'secret' |
| 90 | + jwt_message = jwt.encode(self.payload, secret) |
| 91 | + decoded_payload = jwt.decode(jwt_message, secret) |
| 92 | + self.assertEqual(decoded_payload, self.payload) |
| 93 | + |
| 94 | + def test_bad_secret(self): |
| 95 | + right_secret = 'foo' |
| 96 | + bad_secret = 'bar' |
| 97 | + jwt_message = jwt.encode(self.payload, right_secret) |
| 98 | + self.assertRaises(jwt.DecodeError, jwt.decode, jwt_message, bad_secret) |
| 99 | + |
| 100 | + def test_decodes_valid_jwt(self): |
| 101 | + example_payload = {"hello": "world"} |
| 102 | + example_secret = "secret" |
| 103 | + example_jwt = "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJoZWxsbyI6ICJ3b3JsZCJ9.tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8" |
| 104 | + decoded_payload = jwt.decode(example_jwt, example_secret) |
| 105 | + self.assertEqual(decoded_payload, example_payload) |
| 106 | + |
| 107 | + def test_allow_skip_verification(self): |
| 108 | + right_secret = 'foo' |
| 109 | + bad_secret = 'bar' |
| 110 | + jwt_message = jwt.encode(self.payload, right_secret) |
| 111 | + decoded_payload = jwt.decode(jwt_message, verify=False) |
| 112 | + self.assertEqual(decoded_payload, self.payload) |
| 113 | + |
| 114 | + def test_no_secret(self): |
| 115 | + right_secret = 'foo' |
| 116 | + bad_secret = 'bar' |
| 117 | + jwt_message = jwt.encode(self.payload, right_secret) |
| 118 | + self.assertRaises(jwt.DecodeError, jwt.decode, jwt_message) |
| 119 | + |
| 120 | + def test_invalid_crypto_alg(self): |
| 121 | + self.assertRaises(NotImplementedError, jwt.encode, self.payload, "secret", "HS1024") |
| 122 | + |
| 123 | + |
0 commit comments