-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathunique-grafeas-client.yaml
More file actions
2126 lines (2002 loc) · 98.6 KB
/
unique-grafeas-client.yaml
File metadata and controls
2126 lines (2002 loc) · 98.6 KB
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
# Copyright 2023 Google LLC
#
# 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.
description: Grafeas client is unique and requires customizations as described in PR 8186 below
url: https://github.com/googleapis/gapic-generator-python/issues/1830
replacements:
- paths: [
packages/grafeas/grafeas/grafeas_v1/services/grafeas/async_client.py
]
# Use backslashes to preserve leading spaces
before: |
\ # Copy defaults from the synchronous client for use here.
\ # Note: DEFAULT_ENDPOINT is deprecated. Use _DEFAULT_ENDPOINT_TEMPLATE instead.
\ DEFAULT_ENDPOINT = GrafeasClient.DEFAULT_ENDPOINT
\ DEFAULT_MTLS_ENDPOINT = GrafeasClient.DEFAULT_MTLS_ENDPOINT
\ _DEFAULT_ENDPOINT_TEMPLATE = GrafeasClient._DEFAULT_ENDPOINT_TEMPLATE
\ _DEFAULT_UNIVERSE = GrafeasClient._DEFAULT_UNIVERSE\n
after: ""
count: 1
- paths: [
packages/grafeas/grafeas/grafeas_v1/services/grafeas/client.py
]
# Use backslashes to preserve leading spaces
before: |
\ # Note: DEFAULT_ENDPOINT is deprecated. Use _DEFAULT_ENDPOINT_TEMPLATE instead.
\ DEFAULT_ENDPOINT = "containeranalysis.googleapis.com"
\ DEFAULT_MTLS_ENDPOINT = _get_default_mtls_endpoint.__func__\( # type: ignore
\ DEFAULT_ENDPOINT
\ \)
\
\ _DEFAULT_ENDPOINT_TEMPLATE = "containeranalysis.{UNIVERSE_DOMAIN}"
\ _DEFAULT_UNIVERSE = "googleapis.com"\n
after: ""
count: 1
- paths: [
packages/grafeas/grafeas/grafeas_v1/services/grafeas/async_client.py
]
# Use backslashes to preserve leading spaces
before: |
\ @classmethod
\ def from_service_account_file\(cls, filename: str, \*args, \*\*kwargs\):
\ """Creates an instance of this client using the provided credentials
\ file.
\
\ Args:
\ filename \(str\): The path to the service account private key json
\ file.
\ args: Additional arguments to pass to the constructor.
\ kwargs: Additional arguments to pass to the constructor.
\
\ Returns:
\ GrafeasAsyncClient: The constructed client.
\ """
\ return GrafeasClient.from_service_account_file.__func__\(GrafeasAsyncClient, filename, \*args, \*\*kwargs\) # type: ignore
\
\ from_service_account_json = from_service_account_file
\
\ @classmethod
\ def get_mtls_endpoint_and_cert_source\(
\ cls, client_options: Optional\[ClientOptions\] = None
\ \):
\ """Return the API endpoint and client cert source for mutual TLS.
\
\ The client cert source is determined in the following order:
\ \(1\) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
\ client cert source is None.
\ \(2\) if `client_options.client_cert_source` is provided, use the provided one; if the
\ default client cert source exists, use the default one; otherwise the client cert
\ source is None.
\
\ The API endpoint is determined in the following order:
\ \(1\) if `client_options.api_endpoint` if provided, use the provided one.
\ \(2\) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
\ default mTLS endpoint; if the environment variable is "never", use the default API
\ endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
\ use the default API endpoint.
\
\ More details can be found at https://google.aip.dev/auth/4114.
\
\ Args:
\ client_options \(google.api_core.client_options.ClientOptions\): Custom options for the
\ client. Only the `api_endpoint` and `client_cert_source` properties may be used
\ in this method.
\
\ Returns:
\ Tuple\[str, Callable\[\[\], Tuple\[bytes, bytes\]\]\]: returns the API endpoint and the
\ client cert source to use.
\
\ Raises:
\ google.auth.exceptions.MutualTLSChannelError: If any errors happen.
\ """
\ return GrafeasClient.get_mtls_endpoint_and_cert_source\(client_options\) # type: ignore\n
after: ""
count: 1
- paths: [
packages/grafeas/grafeas/grafeas_v1/services/grafeas/client.py,
]
# Use backslashes to preserve leading spaces
before: |
\ @classmethod
\ def from_service_account_file\(cls, filename: str, \*args, \*\*kwargs\):
\ """Creates an instance of this client using the provided credentials
\ file.
\
\ Args:
\ filename \(str\): The path to the service account private key json
\ file.
\ args: Additional arguments to pass to the constructor.
\ kwargs: Additional arguments to pass to the constructor.
\
\ Returns:
\ GrafeasClient: The constructed client.
\ """
\ credentials = service_account.Credentials.from_service_account_file\(filename\)
\ kwargs\["credentials"\] = credentials
\ return cls\(\*args, \*\*kwargs\)
\
\ from_service_account_json = from_service_account_file\n
after: ""
count: 1
- paths: [
packages/grafeas/tests/unit/gapic/grafeas_v1/test_grafeas.py,
]
before: |
\)\n\n\n@pytest.mark.parametrize\(
"client_class,transport_class",
\[
\(GrafeasClient, transports.GrafeasGrpcTransport\),
\(GrafeasAsyncClient, transports.GrafeasGrpcAsyncIOTransport\),
\],
\)
def test_api_key_credentials\(client_class, transport_class\):
with mock.patch.object\(
google.auth._default, "get_api_key_credentials", create=True
\) as get_api_key_credentials:
mock_cred = mock.Mock\(\)
get_api_key_credentials.return_value = mock_cred
options = client_options.ClientOptions\(\)
options.api_key = "api_key"
with mock.patch.object\(transport_class, "__init__"\) as patched:
patched.return_value = None
client = client_class\(client_options=options\)
patched.assert_called_once_with\(
credentials=mock_cred,
credentials_file=None,
host=client._DEFAULT_ENDPOINT_TEMPLATE.format\(
UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE
\),
scopes=None,
client_cert_source_for_mtls=None,
quota_project_id=None,
client_info=transports.base.DEFAULT_CLIENT_INFO,
always_use_jwt_access=True,
api_audience=None,
\)
after: ")\n"
count: 1
- paths: [
packages/grafeas/tests/unit/gapic/grafeas_v1/test_grafeas.py,
]
# Use backslashes to preserve leading spaces
before: |
def test_client_with_default_client_info\(\):
client_info = gapic_v1.client_info.ClientInfo\(\)
with mock.patch.object\(
transports.GrafeasTransport, "_prep_wrapped_messages"
\) as prep:
client = GrafeasClient\(
credentials=ga_credentials.AnonymousCredentials\(\),
client_info=client_info,
\)
prep.assert_called_once_with\(client_info\)
with mock.patch.object\(
transports.GrafeasTransport, "_prep_wrapped_messages"
\) as prep:
transport_class = GrafeasClient.get_transport_class\(\)
transport = transport_class\(
credentials=ga_credentials.AnonymousCredentials\(\),
client_info=client_info,
\)
prep.assert_called_once_with\(client_info\)\n\n
after: ""
count: 1
- paths: [
packages/grafeas/tests/unit/gapic/grafeas_v1/test_grafeas.py,
]
before: |
@pytest.mark.parametrize\(
"transport_name",
\[
"grpc",
"grpc_asyncio",
"rest",
\],
\)
def test_grafeas_host_no_port\(transport_name\):
client = GrafeasClient\(
credentials=ga_credentials.AnonymousCredentials\(\),
client_options=client_options.ClientOptions\(
api_endpoint="containeranalysis.googleapis.com"
\),
transport=transport_name,
\)
assert client.transport._host == \(
"containeranalysis.googleapis.com:443"
if transport_name in \["grpc", "grpc_asyncio"\]
else "https://containeranalysis.googleapis.com"
\)
@pytest.mark.parametrize\(
"transport_name",
\[
"grpc",
"grpc_asyncio",
"rest",
\],
\)
def test_grafeas_host_with_port\(transport_name\):
client = GrafeasClient\(
credentials=ga_credentials.AnonymousCredentials\(\),
client_options=client_options.ClientOptions\(
api_endpoint="containeranalysis.googleapis.com:8000"
\),
transport=transport_name,
\)
assert client.transport._host == \(
"containeranalysis.googleapis.com:8000"
if transport_name in \["grpc", "grpc_asyncio"\]
else "https://containeranalysis.googleapis.com:8000"
\)
@pytest.mark.parametrize\(
"transport_name",
\[
"rest",
\],
\)
def test_grafeas_client_transport_session_collision\(transport_name\):
creds1 = ga_credentials.AnonymousCredentials\(\)
creds2 = ga_credentials.AnonymousCredentials\(\)
client1 = GrafeasClient\(
credentials=creds1,
transport=transport_name,
\)
client2 = GrafeasClient\(
credentials=creds2,
transport=transport_name,
\)
session1 = client1.transport.get_occurrence._session
session2 = client2.transport.get_occurrence._session
assert session1 != session2
session1 = client1.transport.list_occurrences._session
session2 = client2.transport.list_occurrences._session
assert session1 != session2
session1 = client1.transport.delete_occurrence._session
session2 = client2.transport.delete_occurrence._session
assert session1 != session2
session1 = client1.transport.create_occurrence._session
session2 = client2.transport.create_occurrence._session
assert session1 != session2
session1 = client1.transport.batch_create_occurrences._session
session2 = client2.transport.batch_create_occurrences._session
assert session1 != session2
session1 = client1.transport.update_occurrence._session
session2 = client2.transport.update_occurrence._session
assert session1 != session2
session1 = client1.transport.get_occurrence_note._session
session2 = client2.transport.get_occurrence_note._session
assert session1 != session2
session1 = client1.transport.get_note._session
session2 = client2.transport.get_note._session
assert session1 != session2
session1 = client1.transport.list_notes._session
session2 = client2.transport.list_notes._session
assert session1 != session2
session1 = client1.transport.delete_note._session
session2 = client2.transport.delete_note._session
assert session1 != session2
session1 = client1.transport.create_note._session
session2 = client2.transport.create_note._session
assert session1 != session2
session1 = client1.transport.batch_create_notes._session
session2 = client2.transport.batch_create_notes._session
assert session1 != session2
session1 = client1.transport.update_note._session
session2 = client2.transport.update_note._session
assert session1 != session2
session1 = client1.transport.list_note_occurrences._session
session2 = client2.transport.list_note_occurrences._session
assert session1 != session2\n\n
after: ""
count: 1
- paths: [
packages/grafeas/tests/unit/gapic/grafeas_v1/test_grafeas.py,
]
before: |
def test_grafeas_base_transport_error\(\):
# Passing both a credentials object and credentials_file should raise an error
with pytest.raises\(core_exceptions.DuplicateCredentialArgs\):
transport = transports.GrafeasTransport\(
credentials=ga_credentials.AnonymousCredentials\(\),
credentials_file="credentials.json",
\)\n\n
after: ""
count: 1
- paths: [
packages/grafeas/tests/unit/gapic/grafeas_v1/test_grafeas.py,
]
before: |
def test_credentials_transport_error\(\):
# It is an error to provide credentials and a transport instance.
transport = transports.GrafeasGrpcTransport\(
credentials=ga_credentials.AnonymousCredentials\(\),
\)
with pytest.raises\(ValueError\):
client = GrafeasClient\(
credentials=ga_credentials.AnonymousCredentials\(\),
transport=transport,
\)
# It is an error to provide a credentials file and a transport instance.
transport = transports.GrafeasGrpcTransport\(
credentials=ga_credentials.AnonymousCredentials\(\),
\)
with pytest.raises\(ValueError\):
client = GrafeasClient\(
client_options={"credentials_file": "credentials.json"},
transport=transport,
\)
# It is an error to provide an api_key and a transport instance.
transport = transports.GrafeasGrpcTransport\(
credentials=ga_credentials.AnonymousCredentials\(\),
\)
options = client_options.ClientOptions\(\)
options.api_key = "api_key"
with pytest.raises\(ValueError\):
client = GrafeasClient\(
client_options=options,
transport=transport,
\)
# It is an error to provide an api_key and a credential.
options = client_options.ClientOptions\(\)
options.api_key = "api_key"
with pytest.raises\(ValueError\):
client = GrafeasClient\(
client_options=options, credentials=ga_credentials.AnonymousCredentials\(\)
\)
# It is an error to provide scopes and a transport instance.
transport = transports.GrafeasGrpcTransport\(
credentials=ga_credentials.AnonymousCredentials\(\),
\)
with pytest.raises\(ValueError\):
client = GrafeasClient\(
client_options={"scopes": \["1", "2"\]},
transport=transport,
\)\n\n
after: ""
count: 1
- paths: [
packages/grafeas/tests/unit/gapic/grafeas_v1/test_grafeas.py,
]
before: |
# If default endpoint is localhost, then default mtls endpoint will be the same.
# This method modifies the default endpoint so the client can produce a different
# mtls endpoint for endpoint testing purposes.
def modify_default_endpoint\(client\):
return \(
"foo.googleapis.com"
if \("localhost" in client.DEFAULT_ENDPOINT\)
else client.DEFAULT_ENDPOINT
\)
# If default endpoint template is localhost, then default mtls endpoint will be the same.
# This method modifies the default endpoint template so the client can produce a different
# mtls endpoint for endpoint testing purposes.
def modify_default_endpoint_template\(client\):
return \(
"test.{UNIVERSE_DOMAIN}"
if \("localhost" in client._DEFAULT_ENDPOINT_TEMPLATE\)
else client._DEFAULT_ENDPOINT_TEMPLATE
\)
def test__get_default_mtls_endpoint\(\):
api_endpoint = "example.googleapis.com"
api_mtls_endpoint = "example.mtls.googleapis.com"
sandbox_endpoint = "example.sandbox.googleapis.com"
sandbox_mtls_endpoint = "example.mtls.sandbox.googleapis.com"
non_googleapi = "api.example.com"
assert GrafeasClient._get_default_mtls_endpoint\(None\) is None
assert GrafeasClient._get_default_mtls_endpoint\(api_endpoint\) == api_mtls_endpoint
assert \(
GrafeasClient._get_default_mtls_endpoint\(api_mtls_endpoint\) == api_mtls_endpoint
\)
assert \(
GrafeasClient._get_default_mtls_endpoint\(sandbox_endpoint\)
== sandbox_mtls_endpoint
\)
assert \(
GrafeasClient._get_default_mtls_endpoint\(sandbox_mtls_endpoint\)
== sandbox_mtls_endpoint
\)
assert GrafeasClient._get_default_mtls_endpoint\(non_googleapi\) == non_googleapi
def test__read_environment_variables\(\):
assert GrafeasClient._read_environment_variables\(\) == \(False, "auto", None\)
with mock.patch.dict\(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}\):
assert GrafeasClient._read_environment_variables\(\) == \(True, "auto", None\)
with mock.patch.dict\(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}\):
assert GrafeasClient._read_environment_variables\(\) == \(False, "auto", None\)
with mock.patch.dict\(
os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"}
\):
if not hasattr\(google.auth.transport.mtls, "should_use_client_cert"\):
with pytest.raises\(ValueError\) as excinfo:
GrafeasClient._read_environment_variables\(\)
assert \(
str\(excinfo.value\)
== "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
\)
else:
assert GrafeasClient._read_environment_variables\(\) == \(
False,
"auto",
None,
\)
with mock.patch.dict\(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}\):
assert GrafeasClient._read_environment_variables\(\) == \(False, "never", None\)
with mock.patch.dict\(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}\):
assert GrafeasClient._read_environment_variables\(\) == \(False, "always", None\)
with mock.patch.dict\(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}\):
assert GrafeasClient._read_environment_variables\(\) == \(False, "auto", None\)
with mock.patch.dict\(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}\):
with pytest.raises\(MutualTLSChannelError\) as excinfo:
GrafeasClient._read_environment_variables\(\)
assert \(
str\(excinfo.value\)
== "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
\)
with mock.patch.dict\(os.environ, {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"}\):
assert GrafeasClient._read_environment_variables\(\) == \(False, "auto", "foo.com"\)
def test_use_client_cert_effective\(\):
# Test case 1: Test when `should_use_client_cert` returns True.
# We mock the `should_use_client_cert` function to simulate a scenario where
# the google-auth library supports automatic mTLS and determines that a
# client certificate should be used.
if hasattr\(google.auth.transport.mtls, "should_use_client_cert"\):
with mock.patch\(
"google.auth.transport.mtls.should_use_client_cert", return_value=True
\):
assert GrafeasClient._use_client_cert_effective\(\) is True
# Test case 2: Test when `should_use_client_cert` returns False.
# We mock the `should_use_client_cert` function to simulate a scenario where
# the google-auth library supports automatic mTLS and determines that a
# client certificate should NOT be used.
if hasattr\(google.auth.transport.mtls, "should_use_client_cert"\):
with mock.patch\(
"google.auth.transport.mtls.should_use_client_cert", return_value=False
\):
assert GrafeasClient._use_client_cert_effective\(\) is False
# Test case 3: Test when `should_use_client_cert` is unavailable and the
# `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true".
if not hasattr\(google.auth.transport.mtls, "should_use_client_cert"\):
with mock.patch.dict\(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}\):
assert GrafeasClient._use_client_cert_effective\(\) is True
# Test case 4: Test when `should_use_client_cert` is unavailable and the
# `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "false".
if not hasattr\(google.auth.transport.mtls, "should_use_client_cert"\):
with mock.patch.dict\(
os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}
\):
assert GrafeasClient._use_client_cert_effective\(\) is False
# Test case 5: Test when `should_use_client_cert` is unavailable and the
# `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "True".
if not hasattr\(google.auth.transport.mtls, "should_use_client_cert"\):
with mock.patch.dict\(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "True"}\):
assert GrafeasClient._use_client_cert_effective\(\) is True
# Test case 6: Test when `should_use_client_cert` is unavailable and the
# `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "False".
if not hasattr\(google.auth.transport.mtls, "should_use_client_cert"\):
with mock.patch.dict\(
os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "False"}
\):
assert GrafeasClient._use_client_cert_effective\(\) is False
# Test case 7: Test when `should_use_client_cert` is unavailable and the
# `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "TRUE".
if not hasattr\(google.auth.transport.mtls, "should_use_client_cert"\):
with mock.patch.dict\(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "TRUE"}\):
assert GrafeasClient._use_client_cert_effective\(\) is True
# Test case 8: Test when `should_use_client_cert` is unavailable and the
# `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "FALSE".
if not hasattr\(google.auth.transport.mtls, "should_use_client_cert"\):
with mock.patch.dict\(
os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "FALSE"}
\):
assert GrafeasClient._use_client_cert_effective\(\) is False
# Test case 9: Test when `should_use_client_cert` is unavailable and the
# `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not set.
# In this case, the method should return False, which is the default value.
if not hasattr\(google.auth.transport.mtls, "should_use_client_cert"\):
with mock.patch.dict\(os.environ, clear=True\):
assert GrafeasClient._use_client_cert_effective\(\) is False
# Test case 10: Test when `should_use_client_cert` is unavailable and the
# `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value.
# The method should raise a ValueError as the environment variable must be either
# "true" or "false".
if not hasattr\(google.auth.transport.mtls, "should_use_client_cert"\):
with mock.patch.dict\(
os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}
\):
with pytest.raises\(ValueError\):
GrafeasClient._use_client_cert_effective\(\)
# Test case 11: Test when `should_use_client_cert` is available and the
# `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value.
# The method should return False as the environment variable is set to an invalid value.
if hasattr\(google.auth.transport.mtls, "should_use_client_cert"\):
with mock.patch.dict\(
os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}
\):
assert GrafeasClient._use_client_cert_effective\(\) is False
# Test case 12: Test when `should_use_client_cert` is available and the
# `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also,
# the GOOGLE_API_CONFIG environment variable is unset.
if hasattr\(google.auth.transport.mtls, "should_use_client_cert"\):
with mock.patch.dict\(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}\):
with mock.patch.dict\(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""}\):
assert GrafeasClient._use_client_cert_effective\(\) is False
def test__get_client_cert_source\(\):
mock_provided_cert_source = mock.Mock\(\)
mock_default_cert_source = mock.Mock\(\)
assert GrafeasClient._get_client_cert_source\(None, False\) is None
assert \(
GrafeasClient._get_client_cert_source\(mock_provided_cert_source, False\) is None
\)
assert \(
GrafeasClient._get_client_cert_source\(mock_provided_cert_source, True\)
== mock_provided_cert_source
\)
with mock.patch\(
"google.auth.transport.mtls.has_default_client_cert_source", return_value=True
\):
with mock.patch\(
"google.auth.transport.mtls.default_client_cert_source",
return_value=mock_default_cert_source,
\):
assert \(
GrafeasClient._get_client_cert_source\(None, True\)
is mock_default_cert_source
\)
assert \(
GrafeasClient._get_client_cert_source\(mock_provided_cert_source, "true"\)
is mock_provided_cert_source
\)
@mock.patch.object\(
GrafeasClient,
"_DEFAULT_ENDPOINT_TEMPLATE",
modify_default_endpoint_template\(GrafeasClient\),
\)
@mock.patch.object\(
GrafeasAsyncClient,
"_DEFAULT_ENDPOINT_TEMPLATE",
modify_default_endpoint_template\(GrafeasAsyncClient\),
\)
def test__get_api_endpoint\(\):
api_override = "foo.com"
mock_client_cert_source = mock.Mock\(\)
default_universe = GrafeasClient._DEFAULT_UNIVERSE
default_endpoint = GrafeasClient._DEFAULT_ENDPOINT_TEMPLATE.format\(
UNIVERSE_DOMAIN=default_universe
\)
mock_universe = "bar.com"
mock_endpoint = GrafeasClient._DEFAULT_ENDPOINT_TEMPLATE.format\(
UNIVERSE_DOMAIN=mock_universe
\)
assert \(
GrafeasClient._get_api_endpoint\(
api_override, mock_client_cert_source, default_universe, "always"
\)
== api_override
\)
assert \(
GrafeasClient._get_api_endpoint\(
None, mock_client_cert_source, default_universe, "auto"
\)
== GrafeasClient.DEFAULT_MTLS_ENDPOINT
\)
assert \(
GrafeasClient._get_api_endpoint\(None, None, default_universe, "auto"\)
== default_endpoint
\)
assert \(
GrafeasClient._get_api_endpoint\(None, None, default_universe, "always"\)
== GrafeasClient.DEFAULT_MTLS_ENDPOINT
\)
assert \(
GrafeasClient._get_api_endpoint\(
None, mock_client_cert_source, default_universe, "always"
\)
== GrafeasClient.DEFAULT_MTLS_ENDPOINT
\)
assert \(
GrafeasClient._get_api_endpoint\(None, None, mock_universe, "never"\)
== mock_endpoint
\)
assert \(
GrafeasClient._get_api_endpoint\(None, None, default_universe, "never"\)
== default_endpoint
\)
with pytest.raises\(MutualTLSChannelError\) as excinfo:
GrafeasClient._get_api_endpoint\(
None, mock_client_cert_source, mock_universe, "auto"
\)
assert \(
str\(excinfo.value\)
== "mTLS is not supported in any universe other than googleapis.com."
\)
def test__get_universe_domain\(\):
client_universe_domain = "foo.com"
universe_domain_env = "bar.com"
assert \(
GrafeasClient._get_universe_domain\(client_universe_domain, universe_domain_env\)
== client_universe_domain
\)
assert \(
GrafeasClient._get_universe_domain\(None, universe_domain_env\)
== universe_domain_env
\)
assert \(
GrafeasClient._get_universe_domain\(None, None\)
== GrafeasClient._DEFAULT_UNIVERSE
\)
with pytest.raises\(ValueError\) as excinfo:
GrafeasClient._get_universe_domain\("", None\)
assert str\(excinfo.value\) == "Universe Domain cannot be an empty string."
@pytest.mark.parametrize\(
"error_code,cred_info_json,show_cred_info",
\[
\(401, CRED_INFO_JSON, True\),
\(403, CRED_INFO_JSON, True\),
\(404, CRED_INFO_JSON, True\),
\(500, CRED_INFO_JSON, False\),
\(401, None, False\),
\(403, None, False\),
\(404, None, False\),
\(500, None, False\),
\],
\)
def test__add_cred_info_for_auth_errors\(error_code, cred_info_json, show_cred_info\):
cred = mock.Mock\(\["get_cred_info"\]\)
cred.get_cred_info = mock.Mock\(return_value=cred_info_json\)
client = GrafeasClient\(credentials=cred\)
client._transport._credentials = cred
error = core_exceptions.GoogleAPICallError\("message", details=\["foo"\]\)
error.code = error_code
client._add_cred_info_for_auth_errors\(error\)
if show_cred_info:
assert error.details == \["foo", CRED_INFO_STRING\]
else:
assert error.details == \["foo"\]
@pytest.mark.parametrize\("error_code", \[401, 403, 404, 500\]\)
def test__add_cred_info_for_auth_errors_no_get_cred_info\(error_code\):
cred = mock.Mock\(\[\]\)
assert not hasattr\(cred, "get_cred_info"\)
client = GrafeasClient\(credentials=cred\)
client._transport._credentials = cred
error = core_exceptions.GoogleAPICallError\("message", details=\[\]\)
error.code = error_code
client._add_cred_info_for_auth_errors\(error\)
assert error.details == \[\]
@pytest.mark.parametrize\(
"client_class,transport_name",
\[
\(GrafeasClient, "grpc"\),
\(GrafeasAsyncClient, "grpc_asyncio"\),
\(GrafeasClient, "rest"\),
\],
\)
def test_grafeas_client_from_service_account_info\(client_class, transport_name\):
creds = ga_credentials.AnonymousCredentials\(\)
with mock.patch.object\(
service_account.Credentials, "from_service_account_info"
\) as factory:
factory.return_value = creds
info = {"valid": True}
client = client_class.from_service_account_info\(info, transport=transport_name\)
assert client.transport._credentials == creds
assert isinstance\(client, client_class\)
assert client.transport._host == \(
"containeranalysis.googleapis.com:443"
if transport_name in \["grpc", "grpc_asyncio"\]
else "https://containeranalysis.googleapis.com"
\)
@pytest.mark.parametrize\(
"transport_class,transport_name",
\[
\(transports.GrafeasGrpcTransport, "grpc"\),
\(transports.GrafeasGrpcAsyncIOTransport, "grpc_asyncio"\),
\(transports.GrafeasRestTransport, "rest"\),
\],
\)
def test_grafeas_client_service_account_always_use_jwt\(transport_class, transport_name\):
with mock.patch.object\(
service_account.Credentials, "with_always_use_jwt_access", create=True
\) as use_jwt:
creds = service_account.Credentials\(None, None, None\)
transport = transport_class\(credentials=creds, always_use_jwt_access=True\)
use_jwt.assert_called_once_with\(True\)
with mock.patch.object\(
service_account.Credentials, "with_always_use_jwt_access", create=True
\) as use_jwt:
creds = service_account.Credentials\(None, None, None\)
transport = transport_class\(credentials=creds, always_use_jwt_access=False\)
use_jwt.assert_not_called\(\)
@pytest.mark.parametrize\(
"client_class,transport_name",
\[
\(GrafeasClient, "grpc"\),
\(GrafeasAsyncClient, "grpc_asyncio"\),
\(GrafeasClient, "rest"\),
\],
\)
def test_grafeas_client_from_service_account_file\(client_class, transport_name\):
creds = ga_credentials.AnonymousCredentials\(\)
with mock.patch.object\(
service_account.Credentials, "from_service_account_file"
\) as factory:
factory.return_value = creds
client = client_class.from_service_account_file\(
"dummy/file/path.json", transport=transport_name
\)
assert client.transport._credentials == creds
assert isinstance\(client, client_class\)
client = client_class.from_service_account_json\(
"dummy/file/path.json", transport=transport_name
\)
assert client.transport._credentials == creds
assert isinstance\(client, client_class\)
assert client.transport._host == \(
"containeranalysis.googleapis.com:443"
if transport_name in \["grpc", "grpc_asyncio"\]
else "https://containeranalysis.googleapis.com"
\)
def test_grafeas_client_get_transport_class\(\):
transport = GrafeasClient.get_transport_class\(\)
available_transports = \[
transports.GrafeasGrpcTransport,
transports.GrafeasRestTransport,
\]
assert transport in available_transports
transport = GrafeasClient.get_transport_class\("grpc"\)
assert transport == transports.GrafeasGrpcTransport
@pytest.mark.parametrize\(
"client_class,transport_class,transport_name",
\[
\(GrafeasClient, transports.GrafeasGrpcTransport, "grpc"\),
\(GrafeasAsyncClient, transports.GrafeasGrpcAsyncIOTransport, "grpc_asyncio"\),
\(GrafeasClient, transports.GrafeasRestTransport, "rest"\),
\],
\)
@mock.patch.object\(
GrafeasClient,
"_DEFAULT_ENDPOINT_TEMPLATE",
modify_default_endpoint_template\(GrafeasClient\),
\)
@mock.patch.object\(
GrafeasAsyncClient,
"_DEFAULT_ENDPOINT_TEMPLATE",
modify_default_endpoint_template\(GrafeasAsyncClient\),
\)
def test_grafeas_client_client_options\(client_class, transport_class, transport_name\):
# Check that if channel is provided we won't create a new one.
with mock.patch.object\(GrafeasClient, "get_transport_class"\) as gtc:
transport = transport_class\(credentials=ga_credentials.AnonymousCredentials\(\)\)
client = client_class\(transport=transport\)
gtc.assert_not_called\(\)
# Check that if channel is provided via str we will create a new one.
with mock.patch.object\(GrafeasClient, "get_transport_class"\) as gtc:
client = client_class\(transport=transport_name\)
gtc.assert_called\(\)
# Check the case api_endpoint is provided.
options = client_options.ClientOptions\(api_endpoint="squid.clam.whelk"\)
with mock.patch.object\(transport_class, "__init__"\) as patched:
patched.return_value = None
client = client_class\(transport=transport_name, client_options=options\)
patched.assert_called_once_with\(
credentials=None,
credentials_file=None,
host="squid.clam.whelk",
scopes=None,
client_cert_source_for_mtls=None,
quota_project_id=None,
client_info=transports.base.DEFAULT_CLIENT_INFO,
always_use_jwt_access=True,
api_audience=None,
\)
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
# "never".
with mock.patch.dict\(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}\):
with mock.patch.object\(transport_class, "__init__"\) as patched:
patched.return_value = None
client = client_class\(transport=transport_name\)
patched.assert_called_once_with\(
credentials=None,
credentials_file=None,
host=client._DEFAULT_ENDPOINT_TEMPLATE.format\(
UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE
\),
scopes=None,
client_cert_source_for_mtls=None,
quota_project_id=None,
client_info=transports.base.DEFAULT_CLIENT_INFO,
always_use_jwt_access=True,
api_audience=None,
\)
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
# "always".
with mock.patch.dict\(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}\):
with mock.patch.object\(transport_class, "__init__"\) as patched:
patched.return_value = None
client = client_class\(transport=transport_name\)
patched.assert_called_once_with\(
credentials=None,
credentials_file=None,
host=client.DEFAULT_MTLS_ENDPOINT,
scopes=None,
client_cert_source_for_mtls=None,
quota_project_id=None,
client_info=transports.base.DEFAULT_CLIENT_INFO,
always_use_jwt_access=True,
api_audience=None,
\)
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
# unsupported value.
with mock.patch.dict\(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}\):
with pytest.raises\(MutualTLSChannelError\) as excinfo:
client = client_class\(transport=transport_name\)
assert \(
str\(excinfo.value\)
== "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
\)
# Check the case quota_project_id is provided
options = client_options.ClientOptions\(quota_project_id="octopus"\)
with mock.patch.object\(transport_class, "__init__"\) as patched:
patched.return_value = None
client = client_class\(client_options=options, transport=transport_name\)
patched.assert_called_once_with\(
credentials=None,
credentials_file=None,
host=client._DEFAULT_ENDPOINT_TEMPLATE.format\(
UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE
\),
scopes=None,
client_cert_source_for_mtls=None,
quota_project_id="octopus",
client_info=transports.base.DEFAULT_CLIENT_INFO,
always_use_jwt_access=True,
api_audience=None,
\)
# Check the case api_endpoint is provided
options = client_options.ClientOptions\(
api_audience="https://language.googleapis.com"
\)
with mock.patch.object\(transport_class, "__init__"\) as patched:
patched.return_value = None
client = client_class\(client_options=options, transport=transport_name\)
patched.assert_called_once_with\(
credentials=None,
credentials_file=None,
host=client._DEFAULT_ENDPOINT_TEMPLATE.format\(
UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE
\),
scopes=None,
client_cert_source_for_mtls=None,
quota_project_id=None,
client_info=transports.base.DEFAULT_CLIENT_INFO,
always_use_jwt_access=True,
api_audience="https://language.googleapis.com",
\)
@pytest.mark.parametrize\(
"client_class,transport_class,transport_name,use_client_cert_env",
\[
\(GrafeasClient, transports.GrafeasGrpcTransport, "grpc", "true"\),
\(
GrafeasAsyncClient,
transports.GrafeasGrpcAsyncIOTransport,
"grpc_asyncio",
"true",
\),
\(GrafeasClient, transports.GrafeasGrpcTransport, "grpc", "false"\),
\(
GrafeasAsyncClient,
transports.GrafeasGrpcAsyncIOTransport,
"grpc_asyncio",
"false",
\),
\(GrafeasClient, transports.GrafeasRestTransport, "rest", "true"\),
\(GrafeasClient, transports.GrafeasRestTransport, "rest", "false"\),
\],
\)
@mock.patch.object\(
GrafeasClient,
"_DEFAULT_ENDPOINT_TEMPLATE",
modify_default_endpoint_template\(GrafeasClient\),
\)
@mock.patch.object\(
GrafeasAsyncClient,
"_DEFAULT_ENDPOINT_TEMPLATE",
modify_default_endpoint_template\(GrafeasAsyncClient\),
\)
@mock.patch.dict\(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}\)
def test_grafeas_client_mtls_env_auto\(
client_class, transport_class, transport_name, use_client_cert_env
\):
# This tests the endpoint autoswitch behavior. Endpoint is autoswitched to the default
# mtls endpoint, if GOOGLE_API_USE_CLIENT_CERTIFICATE is "true" and client cert exists.