forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_vpcsc.py
More file actions
168 lines (124 loc) · 5.13 KB
/
test_vpcsc.py
File metadata and controls
168 lines (124 loc) · 5.13 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
# -*- coding: utf-8 -*-
#
# Copyright 2019 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
#
# https://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.
"""Unit tests for VPC-SC."""
import pytest
from google.api_core import exceptions
from google.cloud import translate_v3beta1
from test_utils.vpcsc_config import vpcsc_config
_VPCSC_PROHIBITED_MESSAGE = "Request is prohibited by organization's policy"
@pytest.fixture(scope="module")
def client():
return translate_v3beta1.TranslationServiceClient()
@pytest.fixture(scope="module")
def parent_inside(client):
return client.location_path(vpcsc_config.project_inside, "us-central1")
@pytest.fixture(scope="module")
def parent_outside(client):
return client.location_path(vpcsc_config.project_outside, "us-central1")
@pytest.fixture(scope="module")
def glossary_name_inside(client):
return client.glossary_path(
vpcsc_config.project_inside, "us-central1", "fake_glossary"
)
@pytest.fixture(scope="module")
def glossary_name_outside(client):
return client.glossary_path(
vpcsc_config.project_outside, "us-central1", "fake_glossary"
)
def _make_glossary(name):
return {
"name": name,
"language_codes_set": {"language_codes": ["en", "ja"]},
"input_config": {
"gcs_source": {"input_uri": "gs://fake-bucket/fake_glossary.csv"}
},
}
@pytest.fixture(scope="module")
def glossary_inside(glossary_name_inside):
return _make_glossary(glossary_name_inside)
@pytest.fixture(scope="module")
def glossary_outside(glossary_name_outside):
return _make_glossary(glossary_name_outside)
@vpcsc_config.skip_unless_inside_vpcsc
def test_create_glossary_w_inside(client, parent_inside, glossary_inside):
client.create_glossary(parent_inside, glossary_inside)
@vpcsc_config.skip_unless_inside_vpcsc
def test_create_glossary_w_outside(client, parent_outside, glossary_outside):
with pytest.raises(exceptions.PermissionDenied) as exc:
client.create_glossary(parent_outside, glossary_outside)
assert exc.value.message.startswith(_VPCSC_PROHIBITED_MESSAGE)
@vpcsc_config.skip_unless_inside_vpcsc
def test_list_glossaries_w_inside(client, parent_inside):
list(client.list_glossaries(parent_inside))
@vpcsc_config.skip_unless_inside_vpcsc
def test_list_glossaries_w_outside(client, parent_outside):
with pytest.raises(exceptions.PermissionDenied) as exc:
list(client.list_glossaries(parent_outside))
assert exc.value.message.startswith(_VPCSC_PROHIBITED_MESSAGE)
@vpcsc_config.skip_unless_inside_vpcsc
def test_get_glossary_w_inside(client, glossary_name_inside):
try:
client.get_glossary(glossary_name_inside)
except exceptions.NotFound: # no perms issue
pass
@vpcsc_config.skip_unless_inside_vpcsc
def test_get_glossary_w_outside(client, glossary_name_outside):
with pytest.raises(exceptions.PermissionDenied) as exc:
client.get_glossary(glossary_name_outside)
assert exc.value.message.startswith(_VPCSC_PROHIBITED_MESSAGE)
@vpcsc_config.skip_unless_inside_vpcsc
def test_delete_glossary_w_inside(client, glossary_name_inside):
try:
client.delete_glossary(glossary_name_inside)
except exceptions.NotFound: # no perms issue
pass
@vpcsc_config.skip_unless_inside_vpcsc
def test_delete_glossary_w_outside(client, glossary_name_outside):
with pytest.raises(exceptions.PermissionDenied) as exc:
client.delete_glossary(glossary_name_outside)
assert exc.value.message.startswith(_VPCSC_PROHIBITED_MESSAGE)
@vpcsc_config.skip_unless_inside_vpcsc
def test_batch_translate_text_w_inside(client, parent_inside):
source_language_code = "en"
target_language_codes = ["es"]
input_configs = [{"gcs_source": {"input_uri": "gs://fake-bucket/*"}}]
output_config = {
"gcs_destination": {"output_uri_prefix": "gs://fake-bucket/output/"}
}
client.batch_translate_text( # no perms issue
parent_inside,
source_language_code,
target_language_codes,
input_configs,
output_config,
)
@vpcsc_config.skip_unless_inside_vpcsc
def test_batch_translate_text_w_outside(client, parent_outside):
source_language_code = "en"
target_language_codes = ["es"]
input_configs = [{"gcs_source": {"input_uri": "gs://fake-bucket/*"}}]
output_config = {
"gcs_destination": {"output_uri_prefix": "gs://fake-bucket/output/"}
}
with pytest.raises(exceptions.PermissionDenied) as exc:
client.batch_translate_text(
parent_outside,
source_language_code,
target_language_codes,
input_configs,
output_config,
)
assert exc.value.message.startswith(_VPCSC_PROHIBITED_MESSAGE)