]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/tests/test_rgw.py
import 15.2.9
[ceph.git] / ceph / src / pybind / mgr / dashboard / tests / test_rgw.py
1 try:
2 import mock
3 except ImportError:
4 import unittest.mock as mock
5
6 from .. import mgr
7 from ..controllers.rgw import Rgw, RgwUser
8 from . import ControllerTestCase # pylint: disable=no-name-in-module
9
10
11 class RgwControllerTestCase(ControllerTestCase):
12 @classmethod
13 def setup_server(cls):
14 Rgw._cp_config['tools.authenticate.on'] = False # pylint: disable=protected-access
15 cls.setup_controllers([Rgw], '/test')
16
17 def test_status_no_service(self):
18 mgr.list_servers.return_value = []
19 self._get('/test/api/rgw/status')
20 self.assertStatus(200)
21 self.assertJsonBody({'available': False, 'message': 'No RGW service is running.'})
22
23
24 class RgwUserControllerTestCase(ControllerTestCase):
25 @classmethod
26 def setup_server(cls):
27 RgwUser._cp_config['tools.authenticate.on'] = False # pylint: disable=protected-access
28 cls.setup_controllers([RgwUser], '/test')
29
30 @mock.patch('dashboard.controllers.rgw.RgwRESTController.proxy')
31 def test_user_list(self, mock_proxy):
32 mock_proxy.side_effect = [{
33 'count': 3,
34 'keys': ['test1', 'test2', 'test3'],
35 'truncated': False
36 }]
37 self._get('/test/api/rgw/user')
38 self.assertStatus(200)
39 mock_proxy.assert_has_calls([
40 mock.call('GET', 'user?list', {})
41 ])
42 self.assertJsonBody(['test1', 'test2', 'test3'])
43
44 @mock.patch('dashboard.controllers.rgw.RgwRESTController.proxy')
45 def test_user_list_marker(self, mock_proxy):
46 mock_proxy.side_effect = [{
47 'count': 3,
48 'keys': ['test1', 'test2', 'test3'],
49 'marker': 'foo:bar',
50 'truncated': True
51 }, {
52 'count': 1,
53 'keys': ['admin'],
54 'truncated': False
55 }]
56 self._get('/test/api/rgw/user')
57 self.assertStatus(200)
58 mock_proxy.assert_has_calls([
59 mock.call('GET', 'user?list', {}),
60 mock.call('GET', 'user?list', {'marker': 'foo:bar'})
61 ])
62 self.assertJsonBody(['test1', 'test2', 'test3', 'admin'])
63
64 @mock.patch('dashboard.controllers.rgw.RgwRESTController.proxy')
65 def test_user_list_duplicate_marker(self, mock_proxy):
66 mock_proxy.side_effect = [{
67 'count': 3,
68 'keys': ['test1', 'test2', 'test3'],
69 'marker': 'foo:bar',
70 'truncated': True
71 }, {
72 'count': 3,
73 'keys': ['test4', 'test5', 'test6'],
74 'marker': 'foo:bar',
75 'truncated': True
76 }, {
77 'count': 1,
78 'keys': ['admin'],
79 'truncated': False
80 }]
81 self._get('/test/api/rgw/user')
82 self.assertStatus(500)
83
84 @mock.patch('dashboard.controllers.rgw.RgwRESTController.proxy')
85 def test_user_list_invalid_marker(self, mock_proxy):
86 mock_proxy.side_effect = [{
87 'count': 3,
88 'keys': ['test1', 'test2', 'test3'],
89 'marker': 'foo:bar',
90 'truncated': True
91 }, {
92 'count': 3,
93 'keys': ['test4', 'test5', 'test6'],
94 'marker': '',
95 'truncated': True
96 }, {
97 'count': 1,
98 'keys': ['admin'],
99 'truncated': False
100 }]
101 self._get('/test/api/rgw/user')
102 self.assertStatus(500)
103
104 @mock.patch('dashboard.controllers.rgw.RgwRESTController.proxy')
105 @mock.patch.object(RgwUser, '_keys_allowed')
106 def test_user_get_with_keys(self, keys_allowed, mock_proxy):
107 keys_allowed.return_value = True
108 mock_proxy.return_value = {
109 'tenant': '',
110 'user_id': 'my_user_id',
111 'keys': [],
112 'swift_keys': []
113 }
114 self._get('/test/api/rgw/user/testuser')
115 self.assertStatus(200)
116 self.assertInJsonBody('keys')
117 self.assertInJsonBody('swift_keys')
118
119 @mock.patch('dashboard.controllers.rgw.RgwRESTController.proxy')
120 @mock.patch.object(RgwUser, '_keys_allowed')
121 def test_user_get_without_keys(self, keys_allowed, mock_proxy):
122 keys_allowed.return_value = False
123 mock_proxy.return_value = {
124 'tenant': '',
125 'user_id': 'my_user_id',
126 'keys': [],
127 'swift_keys': []
128 }
129 self._get('/test/api/rgw/user/testuser')
130 self.assertStatus(200)
131 self.assertNotIn('keys', self.json_body())
132 self.assertNotIn('swift_keys', self.json_body())