File: test_base_service.py

package info (click to toggle)
python-ibm-cloud-sdk-core 3.19.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 672 kB
  • sloc: python: 4,428; makefile: 26; sh: 25
file content (1001 lines) | stat: -rw-r--r-- 40,022 bytes parent folder | download
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
# coding=utf-8
# pylint: disable=missing-docstring,protected-access,too-few-public-methods,too-many-lines
import gzip
import json
import os
import ssl
import tempfile
import time
from shutil import copyfile
from typing import Optional
from urllib3.exceptions import ConnectTimeoutError, MaxRetryError

import jwt
import pytest
import responses
import requests

from ibm_cloud_sdk_core import ApiException
from ibm_cloud_sdk_core import BaseService, DetailedResponse
from ibm_cloud_sdk_core import CP4DTokenManager
from ibm_cloud_sdk_core import get_authenticator_from_environment
from ibm_cloud_sdk_core.authenticators import (
    IAMAuthenticator,
    NoAuthAuthenticator,
    Authenticator,
    BasicAuthenticator,
    CloudPakForDataAuthenticator,
)


class IncludeExternalConfigService(BaseService):
    default_service_url = 'https://servicesthatincludeexternalconfig.com/api'

    def __init__(
        self, api_version: str, authenticator: Optional[Authenticator] = None, trace_id: Optional[str] = None
    ) -> None:
        BaseService.__init__(
            self, service_url=self.default_service_url, authenticator=authenticator, disable_ssl_verification=False
        )
        self.api_version = api_version
        self.trace_id = trace_id
        self.configure_service('include-external-config')


class AnyServiceV1(BaseService):
    default_url = 'https://gateway.watsonplatform.net/test/api'

    def __init__(
        self,
        version: str,
        service_url: str = default_url,
        authenticator: Optional[Authenticator] = None,
        disable_ssl_verification: bool = False,
    ) -> None:
        BaseService.__init__(
            self,
            service_url=service_url,
            authenticator=authenticator,
            disable_ssl_verification=disable_ssl_verification,
        )
        self.version = version

    def op_with_path_params(self, path0: str, path1: str) -> DetailedResponse:
        if path0 is None:
            raise ValueError('path0 must be provided')
        if path1 is None:
            raise ValueError('path1 must be provided')
        params = {'version': self.version}
        url = '/v1/foo/{0}/bar/{1}/baz'.format(*self._encode_path_vars(path0, path1))
        request = self.prepare_request(method='GET', url=url, params=params)
        response = self.send(request)
        return response

    def with_http_config(self, http_config: dict) -> DetailedResponse:
        self.set_http_config(http_config)
        request = self.prepare_request(method='GET', url='')
        response = self.send(request)
        return response

    def any_service_call(self) -> DetailedResponse:
        request = self.prepare_request(method='GET', url='')
        response = self.send(request)
        return response

    def head_request(self) -> DetailedResponse:
        request = self.prepare_request(method='HEAD', url='')
        response = self.send(request)
        return response

    def get_document_as_stream(self) -> DetailedResponse:
        params = {'version': self.version}
        url = '/v1/streamjson'
        request = self.prepare_request(method='GET', url=url, params=params)
        response = self.send(request, stream=True)
        return response


def get_access_token() -> str:
    access_token_layout = {
        "username": "dummy",
        "role": "Admin",
        "permissions": ["administrator", "manage_catalog"],
        "sub": "admin",
        "iss": "sss",
        "aud": "sss",
        "uid": "sss",
        "iat": 3600,
        "exp": int(time.time()),
    }

    access_token = jwt.encode(
        access_token_layout, 'secret', algorithm='HS256', headers={'kid': '230498151c214b788dd97f22b85410a5'}
    )
    return access_token


def test_invalid_authenticator():
    with pytest.raises(ValueError) as err:
        AnyServiceV1('2021-08-18')

    assert str(err.value) == 'authenticator must be provided'


@responses.activate
def test_url_encoding():
    service = AnyServiceV1('2017-07-07', authenticator=NoAuthAuthenticator())

    # All characters in path0 _must_ be encoded in path segments
    path0 = ' \"<>^`{}|/\\?#%[]'
    path0_encoded = '%20%22%3C%3E%5E%60%7B%7D%7C%2F%5C%3F%23%25%5B%5D'
    # All non-ASCII chars _must_ be encoded in path segments
    path1 = '比萨浇头'.encode('utf8')  # "pizza toppings"
    path1_encoded = '%E6%AF%94%E8%90%A8%E6%B5%87%E5%A4%B4'

    path_encoded = '/v1/foo/' + path0_encoded + '/bar/' + path1_encoded + '/baz'
    test_url = service.default_url + path_encoded

    responses.add(
        responses.GET, test_url, status=200, body=json.dumps({"foobar": "baz"}), content_type='application/json'
    )

    # Set Host as a default header on the service.
    service.set_default_headers({'Host': 'alternatehost.ibm.com:443'})

    response = service.op_with_path_params(path0, path1)

    assert response is not None
    assert len(responses.calls) == 1
    assert path_encoded in responses.calls[0].request.url
    assert 'version=2017-07-07' in responses.calls[0].request.url

    # Verify that the Host header was set in the request.
    assert responses.calls[0].request.headers.get('Host') == 'alternatehost.ibm.com:443'


@responses.activate
def test_stream_json_response():
    service = AnyServiceV1('2017-07-07', authenticator=NoAuthAuthenticator())

    path = '/v1/streamjson'
    test_url = service.default_url + path

    expected_response = json.dumps({"id": 1, "rev": "v1", "content": "this is a document"})

    # print("Expected response: ", expected_response)

    # Simulate a JSON response
    responses.add(responses.GET, test_url, status=200, body=expected_response, content_type='application/json')

    # Invoke the operation and receive an "iterable" as the response
    response = service.get_document_as_stream()

    assert response is not None
    assert len(responses.calls) == 1

    # retrieve the requests.Response object from the DetailedResponse
    resp = response.get_result()
    assert isinstance(resp, requests.Response)
    assert hasattr(resp, "iter_content")

    # Retrieve the response body, one chunk at a time.
    actual_response = ''
    for chunk in resp.iter_content(chunk_size=3):
        actual_response += chunk.decode("utf-8")

    # print("Actual response: ", actual_response)
    assert actual_response == expected_response


@responses.activate
def test_http_config():
    service = AnyServiceV1('2017-07-07', authenticator=NoAuthAuthenticator())
    responses.add(
        responses.GET,
        service.default_url,
        status=200,
        body=json.dumps({"foobar": "baz"}),
        content_type='application/json',
    )

    response = service.with_http_config({'timeout': 100})
    assert response is not None
    assert len(responses.calls) == 1


def test_fail_http_config():
    service = AnyServiceV1('2017-07-07', authenticator=NoAuthAuthenticator())
    with pytest.raises(TypeError):
        service.with_http_config(None)


@responses.activate
def test_cwd():
    file_path = os.path.join(os.path.dirname(__file__), '../resources/ibm-credentials.env')
    # Try changing working directories to test getting creds from cwd
    cwd = os.getcwd()
    os.chdir(os.path.dirname(file_path))
    iam_authenticator = get_authenticator_from_environment('ibm_watson')
    service = AnyServiceV1('2017-07-07', authenticator=iam_authenticator)
    service.configure_service('ibm_watson')
    os.chdir(cwd)
    assert service.service_url == 'https://cwdserviceurl'
    assert service.authenticator is not None

    # Copy credentials file to cwd to test loading from current working directory
    temp_env_path = os.getcwd() + '/ibm-credentials.env'
    copyfile(file_path, temp_env_path)
    iam_authenticator = get_authenticator_from_environment('ibm_watson')
    service = AnyServiceV1('2017-07-07', authenticator=iam_authenticator)
    service.configure_service('ibm_watson')
    os.remove(temp_env_path)
    assert service.service_url == 'https://cwdserviceurl'
    assert service.authenticator is not None


@responses.activate
def test_iam():
    file_path = os.path.join(os.path.dirname(__file__), '../resources/ibm-credentials-iam.env')
    os.environ['IBM_CREDENTIALS_FILE'] = file_path
    iam_authenticator = get_authenticator_from_environment('ibm-watson')
    service = AnyServiceV1('2017-07-07', authenticator=iam_authenticator)
    assert service.service_url == 'https://gateway.watsonplatform.net/test/api'
    del os.environ['IBM_CREDENTIALS_FILE']
    assert service.authenticator is not None

    response = {
        "access_token": get_access_token(),
        "token_type": "Bearer",
        "expires_in": 3600,
        "expiration": int(time.time()),
        "refresh_token": "jy4gl91BQ",
    }
    responses.add(responses.POST, url='https://iam.cloud.ibm.com/identity/token', body=json.dumps(response), status=200)
    responses.add(
        responses.GET,
        url='https://gateway.watsonplatform.net/test/api',
        body=json.dumps({"foobar": "baz"}),
        content_type='application/json',
    )
    service.any_service_call()
    assert "grant-type%3Aapikey" in responses.calls[0].request.body


def test_no_auth():
    class MadeUp:
        def __init__(self):
            self.lazy = 'made up'

    with pytest.raises(ValueError) as err:
        service = AnyServiceV1('2017-07-07', authenticator=MadeUp())
        service.prepare_request(
            responses.GET,
            url='https://gateway.watsonplatform.net/test/api',
        )
        assert str(err.value) == 'authenticator should be of type Authenticator'

    service = AnyServiceV1('2017-07-07', authenticator=NoAuthAuthenticator())
    service.prepare_request(
        responses.GET,
        url='https://gateway.watsonplatform.net/test/api',
    )
    assert service.authenticator is not None
    assert isinstance(service.authenticator, Authenticator)


def test_for_cp4d():
    cp4d_authenticator = CloudPakForDataAuthenticator('my_username', 'my_password', 'my_url')
    service = AnyServiceV1('2017-07-07', authenticator=cp4d_authenticator)
    assert service.authenticator.token_manager is not None
    assert service.authenticator.token_manager.username == 'my_username'
    assert service.authenticator.token_manager.password == 'my_password'
    assert service.authenticator.token_manager.url == 'my_url/v1/authorize'
    assert isinstance(service.authenticator.token_manager, CP4DTokenManager)


def test_disable_ssl_verification():
    service1 = AnyServiceV1('2017-07-07', authenticator=NoAuthAuthenticator(), disable_ssl_verification=True)
    assert service1.disable_ssl_verification is True

    service1.set_disable_ssl_verification(False)
    assert service1.disable_ssl_verification is False

    cp4d_authenticator = CloudPakForDataAuthenticator('my_username', 'my_password', 'my_url')
    service2 = AnyServiceV1('2017-07-07', authenticator=cp4d_authenticator)
    assert service2.disable_ssl_verification is False
    cp4d_authenticator.set_disable_ssl_verification(True)
    assert service2.authenticator.token_manager.disable_ssl_verification is True


@responses.activate
def test_http_head():
    service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
    expected_headers = {'Test-Header1': 'value1', 'Test-Header2': 'value2'}
    responses.add(responses.HEAD, service.default_url, status=200, headers=expected_headers, content_type=None)

    response = service.head_request()
    assert response is not None
    assert len(responses.calls) == 1
    assert response.headers is not None
    assert response.headers == expected_headers


@responses.activate
def test_response_with_no_body():
    service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
    responses.add(responses.GET, service.default_url, status=200, body=None)

    response = service.any_service_call()
    assert response is not None
    assert len(responses.calls) == 1
    assert response.get_result() is None


def test_has_bad_first_or_last_char():
    with pytest.raises(ValueError) as err:
        basic_authenticator = BasicAuthenticator('{my_username}', 'my_password')
        AnyServiceV1('2018-11-20', authenticator=basic_authenticator).prepare_request(
            responses.GET, 'https://gateway.watsonplatform.net/test/api'
        )
    assert (
        str(err.value) == 'The username and password shouldn\'t start or end with curly brackets or quotes. '
        'Please remove any surrounding {, }, or \" characters.'
    )


@responses.activate
def test_request_server_error():
    with pytest.raises(ApiException, match=r'internal server error') as err:
        responses.add(
            responses.GET,
            'https://gateway.watsonplatform.net/test/api',
            status=500,
            body=json.dumps({'error': 'internal server error'}),
            content_type='application/json',
        )
        service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
        prepped = service.prepare_request('GET', url='')
        service.send(prepped)
    assert err.value.code == 500
    assert err.value.status_code == 500
    assert err.value.http_response.headers['Content-Type'] == 'application/json'
    assert err.value.message == 'internal server error'


@responses.activate
def test_request_success_json():
    responses.add(
        responses.GET,
        'https://gateway.watsonplatform.net/test/api',
        status=200,
        body=json.dumps({'foo': 'bar'}),
        content_type='application/json',
    )
    service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
    prepped = service.prepare_request('GET', url='')
    detailed_response = service.send(prepped)
    assert detailed_response.get_result() == {'foo': 'bar'}

    service = AnyServiceV1('2018-11-20', authenticator=BasicAuthenticator('my_username', 'my_password'))
    service.set_default_headers({'test': 'header'})
    service.set_disable_ssl_verification(True)
    prepped = service.prepare_request('GET', url='')
    detailed_response = service.send(prepped)
    assert detailed_response.get_result() == {'foo': 'bar'}


@responses.activate
def test_request_success_invalid_json():
    # expect an ApiException with JSONDecodeError as the cause when a "success"
    # response contains invalid JSON in response body.
    with pytest.raises(ApiException, match=r'Error processing the HTTP response') as err:
        responses.add(
            responses.GET,
            'https://gateway.watsonplatform.net/test/api',
            status=200,
            body='{ "invalid": "json", "response"',
            content_type='application/json; charset=utf8',
        )
        service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
        prepped = service.prepare_request('GET', url='')
        service.send(prepped)
    assert err.value.code == 200
    assert err.value.status_code == 200
    assert err.value.http_response.headers['Content-Type'] == 'application/json; charset=utf8'
    assert isinstance(err.value.__cause__, requests.exceptions.JSONDecodeError)
    assert "Expecting ':' delimiter: line 1" in str(err.value.__cause__)


@responses.activate
def test_request_success_response():
    expected_body = '{"foo": "bar", "description": "this\nis\na\ndescription"}'
    responses.add(
        responses.GET,
        'https://gateway.watsonplatform.net/test/api',
        status=200,
        body=expected_body,
        content_type='application/json',
    )
    service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
    prepped = service.prepare_request('GET', url='')
    detailed_response = service.send(prepped)
    assert detailed_response.get_result() == {"foo": "bar", "description": "this\nis\na\ndescription"}


@responses.activate
def test_request_success_nonjson():
    responses.add(
        responses.GET,
        'https://gateway.watsonplatform.net/test/api',
        status=200,
        body='<h1>Hola, amigo!</h1>',
        content_type='text/html',
    )
    service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
    prepped = service.prepare_request('GET', url='')
    detailed_response = service.send(prepped)
    # It's odd that we have to call ".text" to get the string value
    # (see issue 3557)
    assert detailed_response.get_result().text == '<h1>Hola, amigo!</h1>'


@responses.activate
def test_request_fail_401_nonerror_json():
    # response body not an error object, so we expect the default error message.
    error_msg = 'Unauthorized: Access is denied due to invalid credentials'
    with pytest.raises(ApiException, match=error_msg) as err:
        responses.add(
            responses.GET,
            'https://gateway.watsonplatform.net/test/api',
            status=401,
            body=json.dumps({'foo': 'bar'}),
            content_type='application/json',
        )
        service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
        prepped = service.prepare_request('GET', url='')
        service.send(prepped)
    assert err.value.code == 401
    assert err.value.status_code == 401
    assert err.value.http_response.headers['Content-Type'] == 'application/json'
    assert err.value.message == error_msg


@responses.activate
def test_request_fail_401_error_json():
    # response body is an error object, so we expect to get the message from there.
    error_msg = 'You dont need to know...'
    with pytest.raises(ApiException, match=error_msg) as err:
        responses.add(
            responses.GET,
            'https://gateway.watsonplatform.net/test/api',
            status=401,
            body=json.dumps({'message': error_msg}),
            content_type='application/json',
        )
        service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
        prepped = service.prepare_request('GET', url='')
        service.send(prepped)
    assert err.value.code == 401
    assert err.value.status_code == 401
    assert err.value.http_response.headers['Content-Type'] == 'application/json'
    assert err.value.message == error_msg


@responses.activate
def test_request_fail_401_nonjson():
    response_body = 'You dont have a need to know...'
    with pytest.raises(ApiException, match=response_body) as err:
        responses.add(
            responses.GET,
            'https://gateway.watsonplatform.net/test/api',
            status=401,
            body=response_body,
            content_type='text/plain',
        )
        service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
        prepped = service.prepare_request('GET', url='')
        service.send(prepped)
    assert err.value.code == 401
    assert err.value.status_code == 401
    assert err.value.http_response.headers['Content-Type'] == 'text/plain'
    assert err.value.message == response_body


@responses.activate
def test_request_fail_401_badjson():
    # if an error response contains invalid JSON, then we should
    # end up with 'Unknown error' as the message since we couldn't get
    # the actual error message from the response body.
    response_body = 'This is not a JSON object'
    with pytest.raises(ApiException, match=response_body) as err:
        responses.add(
            responses.GET,
            'https://gateway.watsonplatform.net/test/api',
            status=401,
            body=response_body,
            content_type='application/json',
        )
        service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
        prepped = service.prepare_request('GET', url='')
        service.send(prepped)
    assert err.value.code == 401
    assert err.value.status_code == 401
    assert err.value.http_response.headers['Content-Type'] == 'application/json'
    assert err.value.message == response_body


def test_misc_methods():
    class MockModel:
        def __init__(self, xyz=None):
            self.xyz = xyz

        def _to_dict(self):
            _dict = {}
            if hasattr(self, 'xyz') and self.xyz is not None:
                _dict['xyz'] = self.xyz
            return _dict

        @classmethod
        def _from_dict(cls, _dict):
            args = {}
            if 'xyz' in _dict:
                args['xyz'] = _dict.get('xyz')
            return cls(**args)

    mock = MockModel('foo')
    service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
    model1 = service._convert_model(mock)
    assert model1 == {'xyz': 'foo'}

    model2 = service._convert_model("{\"xyz\": \"foo\"}")
    assert model2 is not None
    assert model2['xyz'] == 'foo'

    temp = ['default', '123']
    res_str = service._convert_list(temp)
    assert res_str == 'default,123'

    temp2 = 'default123'
    res_str2 = service._convert_list(temp2)
    assert res_str2 == temp2


def test_default_headers():
    service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
    service.set_default_headers({'xxx': 'yyy'})
    assert service.default_headers == {'xxx': 'yyy'}
    with pytest.raises(TypeError):
        service.set_default_headers('xxx')


def test_set_service_url():
    service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
    with pytest.raises(ValueError) as err:
        service.set_service_url('{url}')
    assert (
        str(err.value) == 'The service url shouldn\'t start or end with curly brackets or quotes. '
        'Be sure to remove any {} and \" characters surrounding your service url'
    )

    service.set_service_url('my_url')


def test_http_client():
    auth = BasicAuthenticator('my_username', 'my_password')
    service = AnyServiceV1('2018-11-20', authenticator=auth)
    assert isinstance(service.get_http_client(), requests.sessions.Session)
    assert service.get_http_client().headers.get('Accept-Encoding') == 'gzip, deflate'

    new_http_client = requests.Session()
    new_http_client.headers.update({'Accept-Encoding': 'gzip'})
    service.set_http_client(http_client=new_http_client)
    assert service.get_http_client().headers.get('Accept-Encoding') == 'gzip'

    with pytest.raises(TypeError):
        service.set_http_client("bad_argument_type")


def test_get_authenticator():
    auth = BasicAuthenticator('my_username', 'my_password')
    service = AnyServiceV1('2018-11-20', authenticator=auth)
    assert service.get_authenticator() is not None


def test_gzip_compression():
    # Should return uncompressed data when gzip is off
    service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
    assert not service.get_enable_gzip_compression()
    prepped = service.prepare_request('GET', url='', data=json.dumps({"foo": "bar"}))
    assert prepped['data'] == b'{"foo": "bar"}'
    assert prepped['headers'].get('content-encoding') != 'gzip'

    # Should return compressed data when gzip is on
    service.set_enable_gzip_compression(True)
    assert service.get_enable_gzip_compression()
    prepped = service.prepare_request('GET', url='', data=json.dumps({"foo": "bar"}))
    assert prepped['data'] == gzip.compress(b'{"foo": "bar"}')
    assert prepped['headers'].get('content-encoding') == 'gzip'

    # Should return compressed data when gzip is on for non-json data
    assert service.get_enable_gzip_compression()
    prepped = service.prepare_request('GET', url='', data=b'rawdata')
    assert prepped['data'] == gzip.compress(b'rawdata')
    assert prepped['headers'].get('content-encoding') == 'gzip'

    # Should return compressed data when gzip is on for gzip file data
    assert service.get_enable_gzip_compression()
    with tempfile.TemporaryFile(mode='w+b') as t_f:
        with gzip.GzipFile(mode='wb', fileobj=t_f) as gz_f:
            gz_f.write(json.dumps({"foo": "bar"}).encode())
        with gzip.GzipFile(mode='rb', fileobj=t_f) as gz_f:
            gzip_data = gz_f.read()
        prepped = service.prepare_request('GET', url='', data=gzip_data)
        assert prepped['data'] == gzip.compress(t_f.read())
        assert prepped['headers'].get('content-encoding') == 'gzip'

    # Should return compressed json data when gzip is on for gzip file json data
    assert service.get_enable_gzip_compression()
    with tempfile.TemporaryFile(mode='w+b') as t_f:
        with gzip.GzipFile(mode='wb', fileobj=t_f) as gz_f:
            gz_f.write("rawdata".encode())
        with gzip.GzipFile(mode='rb', fileobj=t_f) as gz_f:
            gzip_data = gz_f.read()
        prepped = service.prepare_request('GET', url='', data=gzip_data)
        assert prepped['data'] == gzip.compress(t_f.read())
        assert prepped['headers'].get('content-encoding') == 'gzip'

    # Should return uncompressed data when content-encoding is set
    assert service.get_enable_gzip_compression()
    prepped = service.prepare_request(
        'GET', url='', headers={"content-encoding": "gzip"}, data=json.dumps({"foo": "bar"})
    )
    assert prepped['data'] == b'{"foo": "bar"}'
    assert prepped['headers'].get('content-encoding') == 'gzip'


def test_gzip_compression_file_input():
    service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
    service.set_enable_gzip_compression(True)

    # Should return file-like object with the compressed data when compression is on
    # and the input is a file, opened for reading in binary mode.
    raw_data = b'rawdata'
    with tempfile.TemporaryFile(mode='w+b') as tmp_file:
        tmp_file.write(raw_data)
        tmp_file.seek(0)

        prepped = service.prepare_request('GET', url='', data=tmp_file)
        assert prepped['data'].read() == gzip.compress(raw_data)
        assert prepped['headers'].get('content-encoding') == 'gzip'
        assert prepped['data'].read() == b''

    # Simulate the requests (urllib3) package reading method for binary files.
    with tempfile.TemporaryFile(mode='w+b') as tmp_file:
        tmp_file.write(raw_data)
        tmp_file.seek(0)

        prepped = service.prepare_request('GET', url='', data=tmp_file)
        compressed = b''
        for chunk in prepped['data']:
            compressed += chunk

        assert compressed == gzip.compress(raw_data)

    # Make sure the decompression works fine.
    assert gzip.decompress(compressed) == raw_data

    # Should return file-like object with the compressed data when compression is on
    # and the input is a file, opened for reading in text mode.
    assert service.get_enable_gzip_compression()
    text_data = 'textdata'
    with tempfile.TemporaryFile(mode='w+') as tmp_file:
        tmp_file.write(text_data)
        tmp_file.seek(0)

        prepped = service.prepare_request('GET', url='', data=tmp_file)
        assert prepped['data'].read() == gzip.compress(text_data.encode())
        assert prepped['headers'].get('content-encoding') == 'gzip'
        assert prepped['data'].read() == b''

    # Simulate the requests (urllib3) package reading method for text files.
    with tempfile.TemporaryFile(mode='w+') as tmp_file:
        tmp_file.write(text_data)
        tmp_file.seek(0)

        prepped = service.prepare_request('GET', url='', data=tmp_file)
        compressed = b''
        for chunk in prepped['data']:
            compressed += chunk

        assert compressed == gzip.compress(text_data.encode())

    # Make sure the decompression works fine.
    assert gzip.decompress(compressed).decode() == text_data


def test_gzip_compression_external():
    # Should set gzip compression from external config
    file_path = os.path.join(os.path.dirname(__file__), '../resources/ibm-credentials-gzip.env')
    os.environ['IBM_CREDENTIALS_FILE'] = file_path
    service = IncludeExternalConfigService('v1', authenticator=NoAuthAuthenticator())
    assert service.service_url == 'https://mockurl'
    assert service.get_enable_gzip_compression() is True
    prepped = service.prepare_request('GET', url='', data=json.dumps({"foo": "bar"}))
    assert prepped['data'] == gzip.compress(b'{"foo": "bar"}')
    assert prepped['headers'].get('content-encoding') == 'gzip'


def test_retry_config_default():
    service = BaseService(service_url='https://mockurl/', authenticator=NoAuthAuthenticator())
    service.enable_retries()
    assert service.retry_config.total == 4
    assert service.retry_config.backoff_factor == 1.0
    assert service.retry_config.backoff_max == 30.0
    assert service.http_client.get_adapter('https://').max_retries.total == 4

    # Ensure retries fail after 4 retries
    error = ConnectTimeoutError()
    retry = service.http_client.get_adapter('https://').max_retries
    retry = retry.increment(error=error)
    retry = retry.increment(error=error)
    retry = retry.increment(error=error)
    retry = retry.increment(error=error)
    with pytest.raises(MaxRetryError) as retry_err:
        retry.increment(error=error)
    assert retry_err.value.reason == error


def test_retry_config_disable():
    # Test disabling retries
    service = BaseService(service_url='https://mockurl/', authenticator=NoAuthAuthenticator())
    service.enable_retries()
    service.disable_retries()
    assert service.retry_config is None
    assert service.http_client.get_adapter('https://').max_retries.total == 0

    # Ensure retries are not started after one connection attempt
    error = ConnectTimeoutError()
    retry = service.http_client.get_adapter('https://').max_retries
    with pytest.raises(MaxRetryError) as retry_err:
        retry.increment(error=error)
    assert retry_err.value.reason == error


def test_retry_config_non_default():
    service = BaseService(service_url='https://mockurl/', authenticator=NoAuthAuthenticator())
    service.enable_retries(2, 10.0)
    assert service.retry_config.total == 2
    assert service.retry_config.backoff_factor == 1.0
    assert service.retry_config.backoff_max == 10.0

    # Ensure retries fail after 2 retries
    error = ConnectTimeoutError()
    retry = service.http_client.get_adapter('https://').max_retries
    retry = retry.increment(error=error)
    retry = retry.increment(error=error)
    with pytest.raises(MaxRetryError) as retry_err:
        retry.increment(error=error)
    assert retry_err.value.reason == error


def test_retry_config_external():
    file_path = os.path.join(os.path.dirname(__file__), '../resources/ibm-credentials-retry.env')
    os.environ['IBM_CREDENTIALS_FILE'] = file_path
    service = IncludeExternalConfigService('v1', authenticator=NoAuthAuthenticator())
    assert service.retry_config.total == 3
    assert service.retry_config.backoff_factor == 1.0
    assert service.retry_config.backoff_max == 25.0

    # Ensure retries fail after 3 retries
    error = ConnectTimeoutError()
    retry = service.http_client.get_adapter('https://').max_retries
    retry = retry.increment(error=error)
    retry = retry.increment(error=error)
    retry = retry.increment(error=error)
    with pytest.raises(MaxRetryError) as retry_err:
        retry.increment(error=error)
    assert retry_err.value.reason == error


@responses.activate
def test_user_agent_header():
    service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
    user_agent_header = service.user_agent_header
    assert user_agent_header is not None
    assert user_agent_header['User-Agent'] is not None

    responses.add(responses.GET, 'https://gateway.watsonplatform.net/test/api', status=200, body='some text')
    prepped = service.prepare_request('GET', url='', headers={'user-agent': 'my_user_agent'})
    response = service.send(prepped)
    assert response.get_result().request.headers.get('user-agent') == 'my_user_agent'

    prepped = service.prepare_request('GET', url='', headers=None)
    response = service.send(prepped)
    assert response.get_result().request.headers.get('user-agent') == user_agent_header['User-Agent']


@responses.activate
def test_reserved_keys(caplog):
    service = AnyServiceV1('2021-07-02', authenticator=NoAuthAuthenticator())
    responses.add(responses.GET, 'https://gateway.watsonplatform.net/test/api', status=200, body='some text')
    prepped = service.prepare_request('GET', url='', headers={'key': 'OK'})
    response = service.send(
        prepped, headers={'key': 'bad'}, method='POST', url='localhost', cookies=None, hooks={'response': []}
    )
    assert response.get_result().request.headers.get('key') == 'OK'
    assert response.get_result().request.url == 'https://gateway.watsonplatform.net/test/api'
    assert response.get_result().request.method == 'GET'
    assert response.get_result().request.hooks == {'response': []}
    assert caplog.record_tuples[0][2] == '"method" has been removed from the request'
    assert caplog.record_tuples[1][2] == '"url" has been removed from the request'
    assert caplog.record_tuples[2][2] == '"cookies" has been removed from the request'


@responses.activate
def test_ssl_error():
    responses.add(responses.GET, 'https://gateway.watsonplatform.net/test/api', body=requests.exceptions.SSLError())
    service = AnyServiceV1('2021-08-18', authenticator=NoAuthAuthenticator())
    with pytest.raises(requests.exceptions.SSLError):
        prepped = service.prepare_request('GET', url='')
        service.send(prepped)


def test_files_dict():
    service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())

    form_data = {}
    with open(
        os.path.join(os.path.dirname(__file__), '../resources/ibm-credentials-iam.env'), 'r', encoding='utf-8'
    ) as file:
        form_data['file1'] = (None, file, 'application/octet-stream')
    form_data['string1'] = (None, 'hello', 'text/plain')
    request = service.prepare_request('GET', url='', headers={'X-opt-out': True}, files=form_data)
    files = request['files']
    assert isinstance(files, list)
    assert len(files) == 2
    files_dict = dict(files)
    file1 = files_dict['file1']
    assert file1[0] == 'ibm-credentials-iam.env'
    string1 = files_dict['string1']
    assert string1[0] is None


def test_files_list():
    service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())

    form_data = []
    with open(
        os.path.join(os.path.dirname(__file__), '../resources/ibm-credentials-iam.env'), 'r', encoding='utf-8'
    ) as file:
        form_data.append(('file1', (None, file, 'application/octet-stream')))
    form_data.append(('string1', (None, 'hello', 'text/plain')))
    request = service.prepare_request('GET', url='', headers={'X-opt-out': True}, files=form_data)
    files = request['files']
    assert isinstance(files, list)
    assert len(files) == 2
    files_dict = dict(files)
    file1 = files_dict['file1']
    assert file1[0] == 'ibm-credentials-iam.env'
    string1 = files_dict['string1']
    assert string1[0] is None


def test_files_duplicate_parts():
    service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())

    form_data = []
    with open(
        os.path.join(os.path.dirname(__file__), '../resources/ibm-credentials-iam.env'), 'r', encoding='utf-8'
    ) as file:
        form_data.append(('creds_file', (None, file, 'application/octet-stream')))
    with open(
        os.path.join(os.path.dirname(__file__), '../resources/ibm-credentials-basic.env'), 'r', encoding='utf-8'
    ) as file:
        form_data.append(('creds_file', (None, file, 'application/octet-stream')))
    with open(
        os.path.join(os.path.dirname(__file__), '../resources/ibm-credentials-bearer.env'), 'r', encoding='utf-8'
    ) as file:
        form_data.append(('creds_file', (None, file, 'application/octet-stream')))
    request = service.prepare_request('GET', url='', headers={'X-opt-out': True}, files=form_data)
    files = request['files']
    assert isinstance(files, list)
    assert len(files) == 3
    for part_name, file_tuple in files:
        assert part_name == 'creds_file'
        assert file_tuple[0] is not None


def test_json():
    service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator())
    req = service.prepare_request('POST', url='', headers={'X-opt-out': True}, data={'hello': 'world', 'fóó': 'bår'})
    assert req.get('data') == b'{"hello": "world", "f\\u00f3\\u00f3": "b\\u00e5r"}'


def test_service_url_handling():
    service = AnyServiceV1('2018-11-20', service_url='https://host///////', authenticator=NoAuthAuthenticator())
    assert service.service_url == 'https://host'

    service.set_service_url('https://host/')
    assert service.service_url == 'https://host'

    req = service.prepare_request('POST', url='/path/', headers={'X-opt-out': True}, data={'hello': 'world'})
    assert req.get('url') == 'https://host/path/'

    service = AnyServiceV1('2018-11-20', service_url='https://host/', authenticator=NoAuthAuthenticator())
    assert service.service_url == 'https://host'

    service.set_service_url('https://host/')
    assert service.service_url == 'https://host'

    req = service.prepare_request('POST', url='/', headers={'X-opt-out': True}, data={'hello': 'world'})
    assert req.get('url') == 'https://host/'

    req = service.prepare_request('POST', url='////', headers={'X-opt-out': True}, data={'hello': 'world'})
    assert req.get('url') == 'https://host/'

    service.set_service_url(None)
    assert service.service_url is None

    service = AnyServiceV1('2018-11-20', service_url='/', authenticator=NoAuthAuthenticator())
    assert service.service_url == ''

    service.set_service_url('/')
    assert service.service_url == ''

    with pytest.raises(ValueError) as err:
        service.prepare_request('POST', url='/', headers={'X-opt-out': True}, data={'hello': 'world'})
    assert str(err.value) == 'The service_url is required'


def test_service_url_slash():
    service = AnyServiceV1('2018-11-20', service_url='/', authenticator=NoAuthAuthenticator())
    assert service.service_url == ''
    with pytest.raises(ValueError) as err:
        service.prepare_request('POST', url='/', headers={'X-opt-out': True}, data={'hello': 'world'})
    assert str(err.value) == 'The service_url is required'


def test_service_url_not_set():
    service = BaseService(service_url='', authenticator=NoAuthAuthenticator())
    with pytest.raises(ValueError) as err:
        service.prepare_request('POST', url='')
    assert str(err.value) == 'The service_url is required'


def test_setting_proxy():
    service = BaseService(service_url='test', authenticator=IAMAuthenticator('wonder woman'))
    assert service.authenticator is not None
    assert service.authenticator.token_manager.http_config == {}

    http_config = {"proxies": {"http": "user:password@host:port"}}
    service.set_http_config(http_config)
    assert service.authenticator.token_manager.http_config == http_config

    service2 = BaseService(service_url='test', authenticator=BasicAuthenticator('marvellous', 'mrs maisel'))
    service2.set_http_config(http_config)
    assert service2.authenticator is not None


def test_configure_service():
    file_path = os.path.join(os.path.dirname(__file__), '../resources/ibm-credentials-external.env')
    os.environ['IBM_CREDENTIALS_FILE'] = file_path
    service = IncludeExternalConfigService('v1', authenticator=NoAuthAuthenticator())
    assert service.service_url == 'https://externallyconfigured.com/api'
    assert service.disable_ssl_verification is True
    # The authenticator should not be changed as a result of configure_service()
    assert isinstance(service.get_authenticator(), NoAuthAuthenticator)


def test_configure_service_error():
    service = BaseService(service_url='v1', authenticator=NoAuthAuthenticator())
    with pytest.raises(ValueError) as err:
        service.configure_service(None)
    assert str(err.value) == 'Service_name must be of type string.'


def test_min_ssl_version():
    service = AnyServiceV1('2022-03-08', authenticator=NoAuthAuthenticator())
    adapter = service.http_client.get_adapter('https://')
    ssl_context = adapter.poolmanager.connection_pool_kw.get('ssl_context', None)
    assert ssl_context is not None
    assert ssl_context.minimum_version == ssl.TLSVersion.TLSv1_2