Skip to content

Commit 795c630

Browse files
committed
fix(value error): Throw error if authenticator is not set
1 parent 866f279 commit 795c630

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

ibm_cloud_sdk_core/base_service.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ def __init__(self,
5656

5757
self._set_user_agent_header(self._build_user_agent())
5858

59-
if self.authenticator:
60-
if not isinstance(self.authenticator, Authenticator):
61-
raise ValueError(
62-
'authenticator should be of type Authenticator')
59+
if not self.authenticator:
60+
raise ValueError('authenticator must be provided')
61+
62+
if not isinstance(self.authenticator, Authenticator):
63+
raise ValueError(
64+
'authenticator should be of type Authenticator')
6365

6466
if display_name:
6567
service_name = display_name.replace(' ', '_').lower()

test/test_base_service.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def get_access_token():
8181

8282
@responses.activate
8383
def test_url_encoding():
84-
service = AnyServiceV1('2017-07-07')
84+
service = AnyServiceV1('2017-07-07', authenticator=NoauthAuthenticator())
8585

8686
# All characters in path0 _must_ be encoded in path segments
8787
path0 = ' \"<>^`{}|/\\?#%[]'
@@ -112,7 +112,7 @@ def test_url_encoding():
112112

113113
@responses.activate
114114
def test_http_config():
115-
service = AnyServiceV1('2017-07-07')
115+
service = AnyServiceV1('2017-07-07', authenticator=NoauthAuthenticator())
116116
responses.add(
117117
responses.GET,
118118
service.default_url,
@@ -128,7 +128,7 @@ def test_http_config():
128128

129129

130130
def test_fail_http_config():
131-
service = AnyServiceV1('2017-07-07')
131+
service = AnyServiceV1('2017-07-07', authenticator=NoauthAuthenticator())
132132
with pytest.raises(TypeError):
133133
service.with_http_config(None)
134134

@@ -196,7 +196,7 @@ def test_for_cp4d():
196196

197197

198198
def test_disable_ssl_verification():
199-
service1 = AnyServiceV1('2017-07-07', disable_ssl_verification=True)
199+
service1 = AnyServiceV1('2017-07-07', authenticator=NoauthAuthenticator(), disable_ssl_verification=True)
200200
assert service1.disable_ssl_verification is True
201201

202202
service1.set_disable_ssl_verification(False)
@@ -212,7 +212,7 @@ def test_disable_ssl_verification():
212212

213213
@responses.activate
214214
def test_http_head():
215-
service = AnyServiceV1('2018-11-20')
215+
service = AnyServiceV1('2018-11-20', authenticator=NoauthAuthenticator())
216216
expectedHeaders = {'Test-Header1': 'value1', 'Test-Header2': 'value2'}
217217
responses.add(
218218
responses.HEAD,
@@ -230,7 +230,7 @@ def test_http_head():
230230

231231
@responses.activate
232232
def test_response_with_no_body():
233-
service = AnyServiceV1('2018-11-20')
233+
service = AnyServiceV1('2018-11-20', authenticator=NoauthAuthenticator())
234234
responses.add(responses.GET, service.default_url, status=200, body=None)
235235

236236
response = service.any_service_call()
@@ -257,7 +257,7 @@ def test_request_server_error():
257257
'error': 'internal server error'
258258
}),
259259
content_type='application/json')
260-
service = AnyServiceV1('2018-11-20')
260+
service = AnyServiceV1('2018-11-20', authenticator=NoauthAuthenticator())
261261
try:
262262
prepped = service.prepare_request('GET', url='')
263263
service.send(prepped)
@@ -274,7 +274,7 @@ def test_request_success_json():
274274
'foo': 'bar'
275275
}),
276276
content_type='application/json')
277-
service = AnyServiceV1('2018-11-20')
277+
service = AnyServiceV1('2018-11-20', authenticator=NoauthAuthenticator())
278278
prepped = service.prepare_request('GET', url='')
279279
detailed_response = service.send(prepped)
280280
assert detailed_response.get_result() == {'foo': 'bar'}
@@ -296,7 +296,7 @@ def test_request_success_response():
296296
'foo': 'bar'
297297
}),
298298
content_type='application/json')
299-
service = AnyServiceV1('2018-11-20')
299+
service = AnyServiceV1('2018-11-20', authenticator=NoauthAuthenticator())
300300
prepped = service.prepare_request('GET', url='')
301301
detailed_response = service.send(prepped)
302302
assert detailed_response.get_result() == {"foo": "bar"}
@@ -311,7 +311,7 @@ def test_request_fail_401():
311311
'foo': 'bar'
312312
}),
313313
content_type='application/json')
314-
service = AnyServiceV1('2018-11-20')
314+
service = AnyServiceV1('2018-11-20', authenticator=NoauthAuthenticator())
315315
try:
316316
prepped = service.prepare_request('GET', url='')
317317
service.send(prepped)
@@ -339,7 +339,7 @@ def _from_dict(cls, _dict):
339339
return cls(**args)
340340

341341
mock = MockModel('foo')
342-
service = AnyServiceV1('2018-11-20')
342+
service = AnyServiceV1('2018-11-20', authenticator=NoauthAuthenticator())
343343
model1 = service._convert_model(mock)
344344
assert model1 == {'x': 'foo'}
345345

@@ -352,14 +352,14 @@ def _from_dict(cls, _dict):
352352
assert res_str == 'default,123'
353353

354354
def test_default_headers():
355-
service = AnyServiceV1('2018-11-20')
355+
service = AnyServiceV1('2018-11-20', authenticator=NoauthAuthenticator())
356356
service.set_default_headers({'xxx': 'yyy'})
357357
assert service.default_headers == {'xxx': 'yyy'}
358358
with pytest.raises(TypeError):
359359
service.set_default_headers('xxx')
360360

361361
def test_set_url():
362-
service = AnyServiceV1('2018-11-20')
362+
service = AnyServiceV1('2018-11-20', authenticator=NoauthAuthenticator())
363363
with pytest.raises(ValueError) as err:
364364
service.set_url('{url}')
365365
assert str(err.value) == 'The url shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your url'
@@ -373,7 +373,7 @@ def test_get_authenticator():
373373

374374
@responses.activate
375375
def test_user_agent_header():
376-
service = AnyServiceV1('2018-11-20')
376+
service = AnyServiceV1('2018-11-20', authenticator=NoauthAuthenticator())
377377
user_agent_header = service.user_agent_header
378378
assert user_agent_header is not None
379379
assert user_agent_header['User-Agent'] is not None
@@ -397,7 +397,7 @@ def test_user_agent_header():
397397

398398
@responses.activate
399399
def test_files():
400-
service = AnyServiceV1('2018-11-20')
400+
service = AnyServiceV1('2018-11-20', authenticator=NoauthAuthenticator())
401401

402402
responses.add(
403403
responses.GET,

0 commit comments

Comments
 (0)