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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
# Copyright (c) 2018 The Johns Hopkins University/Applied Physics Laboratory
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
import requests
import testtools
from kmip.core import exceptions
from kmip.services.server import auth
class TestSLUGSConnector(testtools.TestCase):
"""
Test suite for the SLUGSConnector.
"""
def setUp(self):
super(TestSLUGSConnector, self).setUp()
def tearDown(self):
super(TestSLUGSConnector, self).tearDown()
def test_init(self):
"""
Test that a SLUGSConnector can be constructed without arguments.
"""
auth.SLUGSConnector()
def test_init_with_args(self):
"""
Test that a SLUGSConnector can be constructed with arguments.
"""
connector = auth.SLUGSConnector(url='http://127.0.0.1:8080/slugs/')
self.assertEqual('http://127.0.0.1:8080/slugs/', connector.url)
self.assertEqual(
'http://127.0.0.1:8080/slugs/users/{}',
connector.users_url
)
self.assertEqual(
'http://127.0.0.1:8080/slugs/users/{}/groups',
connector.groups_url
)
def test_url_formatting(self):
"""
Test that a URL without a trailing slash is handled properly when used
to set the URL of a SLUGSConnector.
"""
connector = auth.SLUGSConnector(url="http://127.0.0.1:8080/slugs")
self.assertEqual('http://127.0.0.1:8080/slugs/', connector.url)
self.assertEqual(
'http://127.0.0.1:8080/slugs/users/{}',
connector.users_url
)
self.assertEqual(
'http://127.0.0.1:8080/slugs/users/{}/groups',
connector.groups_url
)
connector = auth.SLUGSConnector()
self.assertEqual(None, connector.url)
self.assertEqual(None, connector.users_url)
self.assertEqual(None, connector.groups_url)
connector.url = "http://127.0.0.1:8080/slugs"
self.assertEqual('http://127.0.0.1:8080/slugs/', connector.url)
self.assertEqual(
'http://127.0.0.1:8080/slugs/users/{}',
connector.users_url
)
self.assertEqual(
'http://127.0.0.1:8080/slugs/users/{}/groups',
connector.groups_url
)
def test_invalid_url(self):
"""
Test that a TypeError is raised when an invalid value is used to set
the URL of a SLUGSConnector.
"""
kwargs = {'url': 0}
self.assertRaisesRegex(
TypeError,
"URL must be a string.",
auth.SLUGSConnector,
**kwargs
)
connector = auth.SLUGSConnector()
args = (connector, "url", 0)
self.assertRaisesRegex(
TypeError,
"URL must be a string.",
setattr,
*args
)
@mock.patch('requests.get')
@mock.patch(
'kmip.services.server.auth.utils.get_client_identity_from_certificate'
)
def test_authenticate(self, mock_get_client_identity, mock_request_get):
"""
Test that a call to authenticate with the SLUGSConnector triggers the
right utility and SLUGS API calls.
"""
mock_get_client_identity.return_value = "John Doe"
users_response = mock.MagicMock(requests.Response)
users_response.status_code = 200
groups_response = mock.MagicMock(requests.Response)
groups_response.status_code = 200
groups_response.json.return_value = {'groups': ['Group A', 'Group B']}
mock_request_get.side_effect = [users_response, groups_response]
connector = auth.SLUGSConnector(
url="http://127.0.0.1:8080/test/slugs/"
)
result = connector.authenticate("test")
mock_get_client_identity.assert_called_once_with("test")
mock_request_get.assert_any_call(
"http://127.0.0.1:8080/test/slugs/users/John Doe"
)
mock_request_get.assert_any_call(
"http://127.0.0.1:8080/test/slugs/users/John Doe/groups"
)
self.assertEqual(('John Doe', ['Group A', 'Group B']), result)
@mock.patch('requests.get')
@mock.patch(
'kmip.services.server.auth.utils.get_client_identity_from_certificate'
)
def test_authenticate_with_url_unset(self,
mock_get_client_identity,
mock_request_get):
"""
Test that a ConfigurationError is raised when attempting to
authenticate with an unset URL.
"""
connector = auth.SLUGSConnector()
args = ("test", )
self.assertRaisesRegex(
exceptions.ConfigurationError,
"The SLUGS URL must be specified.",
connector.authenticate,
*args
)
@mock.patch('requests.get')
@mock.patch(
'kmip.services.server.auth.utils.get_client_identity_from_certificate'
)
def test_authenticate_with_connection_failure(self,
mock_get_client_identity,
mock_request_get):
"""
Test that a ConfigurationError is raised when attempting to
authenticate with an invalid URL.
"""
mock_get_client_identity.return_value = "John Doe"
mock_request_get.side_effect = [requests.exceptions.ConnectionError()]
connector = auth.SLUGSConnector(
url="http://127.0.0.1:8080/test/slugs/"
)
args = ("test", )
self.assertRaisesRegex(
exceptions.ConfigurationError,
"A connection could not be established using the SLUGS URL.",
connector.authenticate,
*args
)
@mock.patch('requests.get')
@mock.patch(
'kmip.services.server.auth.utils.get_client_identity_from_certificate'
)
def test_authenticate_with_users_failure(self,
mock_get_client_identity,
mock_request_get):
"""
Test that a PermissionDenied error is raised when an invalid user ID
is used to query SLUGS.
"""
mock_get_client_identity.return_value = "John Doe"
users_response = mock.MagicMock(requests.Response)
users_response.status_code = 404
mock_request_get.return_value = users_response
connector = auth.SLUGSConnector(
url="http://127.0.0.1:8080/test/slugs/"
)
args = ("test", )
self.assertRaisesRegex(
exceptions.PermissionDenied,
"Unrecognized user ID: John Doe",
connector.authenticate,
*args
)
@mock.patch('requests.get')
@mock.patch(
'kmip.services.server.auth.utils.get_client_identity_from_certificate'
)
def test_authenticate_with_groups_failure(self,
mock_get_client_identity,
mock_request_get):
"""
Test that a PermissionDenied error is raised when a groups request to
SLUGS fails.
"""
mock_get_client_identity.return_value = "John Doe"
users_response = mock.MagicMock(requests.Response)
users_response.status_code = 200
groups_response = mock.MagicMock(requests.Response)
groups_response.status_code = 404
mock_request_get.side_effect = [users_response, groups_response]
connector = auth.SLUGSConnector(
url="http://127.0.0.1:8080/test/slugs/"
)
args = ("test", )
self.assertRaisesRegex(
exceptions.PermissionDenied,
"Group information could not be retrieved for user ID: John Doe",
connector.authenticate,
*args
)
|