-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli_api.py
More file actions
136 lines (106 loc) · 6.02 KB
/
Copy pathtest_cli_api.py
File metadata and controls
136 lines (106 loc) · 6.02 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
import os
import unittest
from unittest.mock import patch
import cloudinary.provisioning
from click.testing import CliRunner
from cloudinary_cli.cli import cli
from test.helper_test import api_response_mock, uploader_response_mock, URLLIB3_REQUEST, \
CONFIG_PRESENT, REQUIRES_CONFIG
API_MOCK_RESPONSE = api_response_mock()
UPLOAD_MOCK_RESPONSE = uploader_response_mock()
CONFIRM_ACTION_PATCH = "cloudinary_cli.utils.api_utils.confirm_action"
class TestCLIApi(unittest.TestCase):
runner = CliRunner()
@unittest.skipUnless(CONFIG_PRESENT, REQUIRES_CONFIG)
@patch(URLLIB3_REQUEST)
def test_admin(self, mocker):
mocker.return_value = API_MOCK_RESPONSE
result = self.runner.invoke(cli, ['ping'])
self.assertEqual(0, result.exit_code, result.output)
self.assertIn('"foo": "bar"', result.output)
@unittest.skipUnless(CONFIG_PRESENT, REQUIRES_CONFIG)
@patch(URLLIB3_REQUEST)
def test_upload(self, mocker):
mocker.return_value = UPLOAD_MOCK_RESPONSE
result = self.runner.invoke(cli, ['upload', os.path.abspath(__file__)])
self.assertEqual(0, result.exit_code, result.output)
self.assertIn('"foo": "bar"', result.output)
@patch(URLLIB3_REQUEST)
@unittest.skipUnless(cloudinary.provisioning.account_config().account_id, "requires account_id")
def test_provisioning(self, mocker):
mocker.return_value = API_MOCK_RESPONSE
result = self.runner.invoke(cli, ['provisioning', 'users'])
self.assertEqual(0, result.exit_code, result.output)
self.assertIn('"foo": "bar"', result.output)
class TestDestructiveBulkConfirmation(unittest.TestCase):
runner = CliRunner()
@patch(CONFIRM_ACTION_PATCH, return_value=False)
@patch(URLLIB3_REQUEST)
def test_delete_all_resources_decline_skips_call(self, http_mock, confirm_mock):
http_mock.return_value = API_MOCK_RESPONSE
result = self.runner.invoke(cli, ['admin', 'delete_all_resources'])
self.assertEqual(0, result.exit_code, result.output)
confirm_mock.assert_called_once()
self.assertFalse(http_mock.called, "SDK should not be called when user declines")
@unittest.skipUnless(CONFIG_PRESENT, REQUIRES_CONFIG)
@patch(CONFIRM_ACTION_PATCH, return_value=True)
@patch(URLLIB3_REQUEST)
def test_delete_all_resources_accept_calls_sdk(self, http_mock, confirm_mock):
http_mock.return_value = API_MOCK_RESPONSE
result = self.runner.invoke(cli, ['admin', 'delete_all_resources'])
self.assertEqual(0, result.exit_code, result.output)
confirm_mock.assert_called_once()
self.assertTrue(http_mock.called, "SDK should be called when user accepts")
@unittest.skipUnless(CONFIG_PRESENT, REQUIRES_CONFIG)
@patch(CONFIRM_ACTION_PATCH, return_value=False)
@patch(URLLIB3_REQUEST)
def test_delete_all_resources_force_skips_prompt(self, http_mock, confirm_mock):
http_mock.return_value = API_MOCK_RESPONSE
result = self.runner.invoke(cli, ['admin', '-F', 'delete_all_resources'])
self.assertEqual(0, result.exit_code, result.output)
self.assertFalse(confirm_mock.called, "--force should bypass the confirmation prompt")
self.assertTrue(http_mock.called, "SDK should be called when --force is set")
@patch(CONFIRM_ACTION_PATCH, return_value=False)
@patch(URLLIB3_REQUEST)
def test_delete_resources_by_tag_decline_skips_call(self, http_mock, confirm_mock):
http_mock.return_value = API_MOCK_RESPONSE
result = self.runner.invoke(cli, ['admin', 'delete_resources_by_tag', 'mytag'])
self.assertEqual(0, result.exit_code, result.output)
confirm_mock.assert_called_once()
self.assertFalse(http_mock.called, "SDK should not be called when user declines")
@unittest.skipUnless(CONFIG_PRESENT, REQUIRES_CONFIG)
@patch(CONFIRM_ACTION_PATCH, return_value=False)
@patch(URLLIB3_REQUEST)
def test_delete_resources_explicit_ids_no_prompt(self, http_mock, confirm_mock):
http_mock.return_value = API_MOCK_RESPONSE
result = self.runner.invoke(cli, ['admin', 'delete_resources', 'public_id1', 'public_id2'])
self.assertEqual(0, result.exit_code, result.output)
self.assertFalse(confirm_mock.called, "Explicit-ID delete must not prompt")
self.assertTrue(http_mock.called, "SDK should be called for explicit-ID delete")
@unittest.skipUnless(CONFIG_PRESENT, REQUIRES_CONFIG)
@patch(CONFIRM_ACTION_PATCH, return_value=False)
@patch(URLLIB3_REQUEST)
def test_uploader_add_tag_no_prompt(self, http_mock, confirm_mock):
http_mock.return_value = UPLOAD_MOCK_RESPONSE
result = self.runner.invoke(cli, ['uploader', 'add_tag', 'mytag', 'public_id1'])
self.assertEqual(0, result.exit_code, result.output)
self.assertFalse(confirm_mock.called, "Non-destructive bulk methods must not prompt")
self.assertTrue(http_mock.called, "SDK should be called for non-destructive bulk methods")
@unittest.skipUnless(CONFIG_PRESENT, REQUIRES_CONFIG)
@patch(CONFIRM_ACTION_PATCH, return_value=False)
@patch(URLLIB3_REQUEST)
def test_admin_resources_read_no_prompt(self, http_mock, confirm_mock):
http_mock.return_value = API_MOCK_RESPONSE
result = self.runner.invoke(cli, ['admin', 'resources'])
self.assertEqual(0, result.exit_code, result.output)
self.assertFalse(confirm_mock.called, "Read commands must not prompt")
self.assertTrue(http_mock.called, "SDK should be called for read commands")
@unittest.skipUnless(CONFIG_PRESENT, REQUIRES_CONFIG)
@patch(CONFIRM_ACTION_PATCH, return_value=False)
@patch(URLLIB3_REQUEST)
def test_admin_resources_read_with_force_no_prompt(self, http_mock, confirm_mock):
http_mock.return_value = API_MOCK_RESPONSE
result = self.runner.invoke(cli, ['admin', '-F', 'resources'])
self.assertEqual(0, result.exit_code, result.output)
self.assertFalse(confirm_mock.called, "Read commands must not prompt regardless of --force")
self.assertTrue(http_mock.called)