-
Notifications
You must be signed in to change notification settings - Fork 138
/
test_api_authorization.py
124 lines (85 loc) · 3.78 KB
/
test_api_authorization.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import unittest
import six
import cloudinary
from cloudinary import api
from cloudinary import uploader
from test.helper_test import TEST_IMAGE, get_headers, get_params, URLLIB3_REQUEST, patch
from test.test_api import MOCK_RESPONSE
from test.test_config import OAUTH_TOKEN, CLOUD_NAME, API_KEY, API_SECRET
from test.test_uploader import API_TEST_PRESET
class ApiAuthorizationTest(unittest.TestCase):
def setUp(self):
self.config = cloudinary.config(cloud_name=CLOUD_NAME, api_key=API_KEY, api_secret=API_SECRET)
@patch(URLLIB3_REQUEST)
def test_oauth_token_admin_api(self, mocker):
self.config.oauth_token = OAUTH_TOKEN
mocker.return_value = MOCK_RESPONSE
api.ping()
headers = get_headers(mocker)
self.assertTrue("authorization" in headers)
self.assertEqual("Bearer {}".format(OAUTH_TOKEN), headers["authorization"])
@patch(URLLIB3_REQUEST)
def test_oauth_token_as_an_option_admin_api(self, mocker):
mocker.return_value = MOCK_RESPONSE
api.ping(oauth_token=OAUTH_TOKEN)
headers = get_headers(mocker)
self.assertTrue("authorization" in headers)
self.assertEqual("Bearer {}".format(OAUTH_TOKEN), headers["authorization"])
@patch(URLLIB3_REQUEST)
def test_key_and_secret_admin_api(self, mocker):
self.config.oauth_token = None
mocker.return_value = MOCK_RESPONSE
api.ping()
headers = get_headers(mocker)
self.assertTrue("authorization" in headers)
self.assertEqual("Basic a2V5OnNlY3JldA==", headers["authorization"])
@patch(URLLIB3_REQUEST)
def test_missing_credentials_admin_api(self, mocker):
self.config.oauth_token = None
self.config.api_key = None
self.config.api_secret = None
mocker.return_value = MOCK_RESPONSE
with six.assertRaisesRegex(self, Exception, "Must supply api_key"):
api.ping()
@patch(URLLIB3_REQUEST)
def test_oauth_token_upload_api(self, mocker):
self.config.oauth_token = OAUTH_TOKEN
mocker.return_value = MOCK_RESPONSE
uploader.upload(TEST_IMAGE)
headers = get_headers(mocker)
self.assertTrue("authorization" in headers)
self.assertEqual("Bearer {}".format(OAUTH_TOKEN), headers["authorization"])
params = get_params(mocker)
self.assertNotIn("signature", params)
@patch(URLLIB3_REQUEST)
def test_oauth_token_as_an_option_upload_api(self, mocker):
mocker.return_value = MOCK_RESPONSE
uploader.upload(TEST_IMAGE, oauth_token=OAUTH_TOKEN)
headers = get_headers(mocker)
self.assertTrue("authorization" in headers)
self.assertEqual("Bearer {}".format(OAUTH_TOKEN), headers["authorization"])
@patch(URLLIB3_REQUEST)
def test_key_and_secret_upload_api(self, mocker):
self.config.oauth_token = None
mocker.return_value = MOCK_RESPONSE
uploader.upload(TEST_IMAGE)
headers = get_headers(mocker)
self.assertNotIn("authorization", headers)
params = get_params(mocker)
self.assertIn("signature", params)
self.assertIn("api_key", params)
@patch(URLLIB3_REQUEST)
def test_missing_credentials_upload_api(self, mocker):
self.config.oauth_token = None
self.config.api_key = None
self.config.api_secret = None
mocker.return_value = MOCK_RESPONSE
with six.assertRaisesRegex(self, Exception, "Must supply api_key"):
uploader.upload(TEST_IMAGE)
# no credentials required for unsigned upload
uploader.unsigned_upload(TEST_IMAGE, upload_preset=API_TEST_PRESET)
args, _ = mocker.call_args
params = get_params(mocker)
self.assertTrue("upload_preset" in params)
if __name__ == '__main__':
unittest.main()