-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_holder_test.py
74 lines (61 loc) · 2.7 KB
/
key_holder_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import json
import sys
from io import StringIO
import unittest
from unittest.mock import MagicMock
import requests_mock
from mauth_client.key_holder import KeyHolder
from mauth_client.exceptions import InauthenticError
from .common import load_key
APP_UUID = "f5af50b2-bf7d-4c29-81db-76d086d4808a"
MAUTH_URL = "https://mauth.com"
MAUTH_API_VERSION = "v1"
MAUTH_PATH = "{}/mauth/v1/security_tokens/{}.json".format(MAUTH_URL, APP_UUID)
PUBLIC_KEY = load_key("rsapub")
MAUTH_RESPONSE = {
"security_token": {
"app_name": "awesome-app-sandbox",
"app_uuid": APP_UUID,
"public_key_str": PUBLIC_KEY,
"created_at": "2016-10-21T21:16:14Z",
}
}
CACHE_CONTROL = "max-age=60, private"
class TestKeyHolder(unittest.TestCase):
def setUp(self):
KeyHolder._MAUTH = {"auth": MagicMock(), "url": MAUTH_URL, "api_version": MAUTH_API_VERSION}
# redirect the output of stdout to self.captor
self.captor = StringIO()
sys.stdout = self.captor
def tearDown(self):
# reset the output of stdout to console
sys.stdout = sys.__stdout__
def test_get_request(self):
KeyHolder._CACHE = None
with requests_mock.mock() as requests:
requests.get(MAUTH_PATH, text=json.dumps(MAUTH_RESPONSE))
self.assertEqual(KeyHolder.get_public_key(APP_UUID), PUBLIC_KEY)
self.assertEqual(KeyHolder._CACHE.maxsize, 128)
self.assertEqual(KeyHolder._CACHE.ttl, 300)
def test_get_request_respect_cache_header(self):
KeyHolder._CACHE = None
with requests_mock.mock() as requests:
requests.get(MAUTH_PATH, text=json.dumps(MAUTH_RESPONSE), headers={"Cache-Control": CACHE_CONTROL})
self.assertEqual(KeyHolder.get_public_key(APP_UUID), PUBLIC_KEY)
self.assertEqual(KeyHolder._CACHE.ttl, 60)
def test_get_request_cache_expiration(self):
KeyHolder._CACHE = None
with requests_mock.mock() as requests:
requests.get(MAUTH_PATH, text=json.dumps(MAUTH_RESPONSE), headers={"Cache-Control": CACHE_CONTROL})
self.assertEqual(KeyHolder.get_public_key(APP_UUID), PUBLIC_KEY)
KeyHolder._CACHE.expire(KeyHolder._CACHE.timer() + 60)
self.assertEqual(KeyHolder._CACHE.get(APP_UUID), None)
def test_get_request_404_error(self):
KeyHolder._CACHE = None
with requests_mock.mock() as requests:
requests.get(MAUTH_PATH, status_code=404)
with self.assertRaises(InauthenticError) as exc:
KeyHolder.get_public_key(APP_UUID)
self.assertEqual(
str(exc.exception), "Failed to fetch the public key for {} from {}".format(APP_UUID, MAUTH_URL)
)