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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
#------------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation.
# All rights reserved.
#
# This code is licensed under the MIT License.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files(the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions :
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#------------------------------------------------------------------------------
import sys
import requests
import httpretty
try:
import unittest2 as unittest
except ImportError:
import unittest
try:
from unittest import mock
except ImportError:
import mock
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
import adal
from tests import util
from tests.util import parameters as cp
class TestAuthenticationParameters(unittest.TestCase):
def setUp(self):
testHost = 'https://this.is.my.domain.com'
testPath = '/path/to/resource'
testQuery = 'a=query&string=really'
self.testUrl = testHost + testPath + '?' + testQuery
return super(TestAuthenticationParameters, self).setUp()
def run_data(self, test_data, test_func):
for index, test_case in enumerate(test_data):
parameters = None
error = None
test_input = test_case[0]
test_params = test_case[1]
try:
parameters = test_func(test_input)
except Exception as exp:
error = exp
prefix = "Test case: {0} - ".format(index)
if test_params:
self.assertIsNone(error, "{0}Parse failed but should have succeeded. {1}".format(prefix, error))
self.assertEqual(parameters.authorization_uri, test_params.get('authorizationUri'),
"{0}Parsed authorizationUri did not match expected value.: {1}".format(prefix, parameters.authorization_uri))
self.assertEqual(parameters.resource, test_params.get('resource'),
"{0}Parsed resource did not match expected value.: {1}".format(prefix, parameters.resource))
else:
self.assertIsNotNone(error, "{0}Parse succeeded but should have failed.".format(prefix))
def test_create_from_header(self):
test_data = [
[
'Bearer authorization_uri="foobar,lkfj,;l,", fruitcake="f",resource="clark, &^()- q32,shark" , f="foo"',
{
'authorizationUri' : 'foobar,lkfj,;l,',
'resource' : 'clark, &^()- q32,shark',
}
],
[
'Bearer resource="clark, &^()- q32,shark", authorization_uri="foobar,lkfj,;l,"',
{
'authorizationUri' : 'foobar,lkfj,;l,',
'resource' : 'clark, &^()- q32,shark',
}
],
[
'Bearer authorization_uri="' + cp['authorityTenant'] + '", resource="' + cp['resource'] + '"',
{
'authorizationUri' : cp['authorityTenant'],
'resource' : cp['resource'],
}
],
[
'Bearer authorization_uri="' + cp['authorizeUrl'] + '", resource="' + cp['resource'] + '"',
{
'authorizationUri' : cp['authorizeUrl'],
'resource' : cp['resource'],
}
],
# Add second = sign on first pair.
[
'Bearer authorization_uri=="foobar,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" , f="foo"',
None
],
# Add second = sign on second pair.
[
'Bearer authorization_uri="foobar,lkfj,;l,", resource=="clark, &^()- q32,shark",fruitcake="f" , f="foo"',
None
],
# Add second quote on first pair.
[
'Bearer authorization_uri=""foobar,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" , f="foo"',
None
],
# Add second quote on second pair.
[
'Bearer authorization_uri=foobar,lkfj,;l,", resource="clark, &^()- q32,shark"",fruitcake="f" , f="foo"',
None
],
# Add trailing quote.
[
'Bearer authorization_uri=foobar,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" , f="foo""',
None
],
# Add trailing comma at end of string.
[
'Bearer authorization_uri=foobar,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" , f="foo",',
None
],
# Add second comma between 2 and 3 pairs.
[
'Bearer authorization_uri=foobar,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" ,, f="foo"',
None
],
# Add second comma between 1 and 2 pairs.
[
'Bearer authorization_uri=foobar,lkfj,;l,", , resource="clark, &^()- q32,shark",fruitcake="f" , f="foo"',
None
],
# Add random letter between Bearer and first pair.
[
'Bearer f authorization_uri=foobar,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" , f="foo"',
None
],
# Add random letter between 2 and 3 pair.
[
'Bearer authorization_uri=foobar,lkfj,;l,", a resource="clark, &^()- q32,shark",fruitcake="f" , f="foo"',
None
],
# Add random letter between 3 and 2 pair.
[
'Bearer authorization_uri=foobar,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" a, f="foo"',
None
],
# Mispell Bearer
[
'Berer authorization_uri=foobar,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" , f="foo"',
None
],
# Missing resource.
[
'Bearer authorization_uri="foobar,lkfj,;l,"',
{
'authorizationUri' : 'foobar,lkfj,;l,'
}
],
# Missing authoritzation uri.
[
'Bearer resource="clark, &^()- q32,shark",fruitcake="f" , f="foo"',
None
],
# Boris's test.
[
'Bearer foo="bar" ANYTHING HERE, ANYTHING PRESENT HERE, foo1="bar1"',
None
],
[
'Bearerauthorization_uri="authuri", resource="resourceHere"',
None
],
]
self.run_data(test_data, adal.authentication_parameters.create_authentication_parameters_from_header)
def test_create_from_response(self):
test_data = [
[
mock.Mock(status_code=401, headers={ 'www-authenticate' : 'Bearer authorization_uri="foobar,lkfj,;l,", fruitcake="f",resource="clark, &^()- q32,shark" , f="foo"' }),
{
'authorizationUri' : 'foobar,lkfj,;l,',
'resource' : 'clark, &^()- q32,shark',
}
],
[
mock.Mock(status_code=200, headers={ 'www-authenticate' : 'Bearer authorization_uri="foobar,lkfj,;l,", fruitcake="f",resource="clark, &^()- q32,shark" , f="foo"' }),
None
],
[
mock.Mock(status_code=401),
None
],
[
mock.Mock(status_code=401, headers={ 'foo' : 'this is not the www-authenticate header' }),
None
],
[
mock.Mock(status_code=401, headers={ 'www-authenticate' : 'Berer authorization_uri=foobar,lkfj,;l,", resource="clark, &^()- q32,shark",fruitcake="f" , f="foo"' }),
None
],
[
mock.Mock(status_code=401, headers={ 'www-authenticate' : None }),
None
],
[
mock.Mock(headers={ 'www-authenticate' : None }),
None
],
[
None,
None
]
]
self.run_data(test_data, adal.authentication_parameters.create_authentication_parameters_from_response)
@httpretty.activate
def test_create_from_url_happy_string_url(self):
httpretty.register_uri(httpretty.GET, uri=self.testUrl, body='foo', status=401, **{'www-authenticate':'Bearer authorization_uri="foobar,lkfj,;l,", fruitcake="f",resource="clark, &^()- q32,shark" , f="foo"'})
# maybe try-catch here to catch the error
parameters = adal.authentication_parameters.create_authentication_parameters_from_url(self.testUrl)
test_params = {
'authorizationUri' : 'foobar,lkfj,;l,',
'resource' : 'clark, &^()- q32,shark',
}
self.assertEqual(parameters.authorization_uri, test_params['authorizationUri'],
'Parsed authorizationUri did not match expected value.: {0}'.format(parameters.authorization_uri))
self.assertEqual(parameters.resource, test_params['resource'],
'Parsed resource did not match expected value.: {0}'.format(parameters.resource))
req = httpretty.last_request()
util.match_standard_request_headers(req)
@httpretty.activate
def test_create_from_url_happy_path_url_object(self):
httpretty.register_uri(httpretty.GET, uri=self.testUrl, body='foo', status=401, **{'www-authenticate':'Bearer authorization_uri="foobar,lkfj,;l,", fruitcake="f",resource="clark, &^()- q32,shark" , f="foo"'})
url_obj = urlparse(self.testUrl)
try:
parameters = adal.authentication_parameters.create_authentication_parameters_from_url(url_obj)
test_params = {
'authorizationUri' : 'foobar,lkfj,;l,',
'resource' : 'clark, &^()- q32,shark',
}
self.assertEqual(parameters.authorization_uri, test_params['authorizationUri'],
'Parsed authorizationUri did not match expected value.: {0}'.format(parameters.authorization_uri))
self.assertEqual(parameters.resource, test_params['resource'],
'Parsed resource did not match expected value.: {0}'.format(parameters.resource))
except Exception as err:
self.assertIsNone(err, "An error was raised during function {0}".format(err))
req = httpretty.last_request()
util.match_standard_request_headers(req)
def test_create_from_url_bad_object(self):
try:
parameters = adal.authentication_parameters.create_authentication_parameters_from_url({})
except Exception as exp:
self.assertIsNotNone(exp, "Did not receive expected error.")
pass
def test_create_from_url_not_passed(self):
try:
parameters = adal.authentication_parameters.create_authentication_parameters_from_url(None)
except Exception as exp:
self.assertIsNotNone(exp, "Did not receive expected error.")
pass
@httpretty.activate
def test_create_from_url_no_header(self):
httpretty.register_uri(httpretty.GET, uri=self.testUrl, body='foo', status=401)
receivedException = False
try:
adal.authentication_parameters.create_authentication_parameters_from_url(self.testUrl)
except Exception as err:
receivedException = True
self.assertTrue(str(err).find('header') >= 0, 'Error did not include message about missing header')
pass
finally:
self.assertTrue(receivedException)
req = httpretty.last_request()
util.match_standard_request_headers(req)
def test_create_from_url_network_error(self):
try:
adal.authentication_parameters.create_authentication_parameters_from_url('https://0.0.0.0/foobar')
except Exception as err:
self.assertIsNotNone(err, "Did not receive expected error.")
if __name__ == '__main__':
unittest.main()
|