Skip to content

Commit 605ca26

Browse files
committed
Merge pull request #1020 from tseaver/defend_against_invalid_project
Defend against non-string 'project' passed to 'JSONClient()'.
2 parents 054959e + f2d96f9 commit 605ca26

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

gcloud/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"""gcloud client base class for interacting with API."""
1616

17+
import six
1718

1819
from gcloud._helpers import _get_production_project
1920
from gcloud.connection import Connection
@@ -147,6 +148,8 @@ def __init__(self, project=None, credentials=None, http=None):
147148
if project is None:
148149
raise ValueError('Project was not passed and could not be '
149150
'determined from the environment.')
151+
if not isinstance(project, six.string_types):
152+
raise ValueError('Project must be a string.')
150153
self.project = project
151154

152155
super(JSONClient, self).__init__(credentials=credentials, http=http)

gcloud/storage/test_client.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ def _makeOne(self, *args, **kw):
2727
def test_ctor_connection_type(self):
2828
from gcloud.storage.connection import Connection
2929

30-
PROJECT = object()
30+
PROJECT = 'PROJECT'
3131
CREDENTIALS = _Credentials()
3232

3333
client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)
34+
self.assertEqual(client.project, PROJECT)
3435
self.assertTrue(isinstance(client.connection, Connection))
3536
self.assertTrue(client.connection.credentials is CREDENTIALS)
3637
self.assertTrue(client.current_batch is None)
@@ -39,7 +40,7 @@ def test_ctor_connection_type(self):
3940
def test__push_batch_and__pop_batch(self):
4041
from gcloud.storage.batch import Batch
4142

42-
PROJECT = object()
43+
PROJECT = 'PROJECT'
4344
CREDENTIALS = _Credentials()
4445

4546
client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)
@@ -58,29 +59,29 @@ def test__push_batch_and__pop_batch(self):
5859
self.assertEqual(list(client._batch_stack), [])
5960

6061
def test_connection_setter(self):
61-
PROJECT = object()
62+
PROJECT = 'PROJECT'
6263
CREDENTIALS = _Credentials()
6364
client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)
6465
client._connection = None # Unset the value from the constructor
6566
client.connection = connection = object()
6667
self.assertTrue(client._connection is connection)
6768

6869
def test_connection_setter_when_set(self):
69-
PROJECT = object()
70+
PROJECT = 'PROJECT'
7071
CREDENTIALS = _Credentials()
7172
client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)
7273
self.assertRaises(ValueError, setattr, client, 'connection', None)
7374

7475
def test_connection_getter_no_batch(self):
75-
PROJECT = object()
76+
PROJECT = 'PROJECT'
7677
CREDENTIALS = _Credentials()
7778
client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)
7879
self.assertTrue(client.connection is client._connection)
7980
self.assertTrue(client.current_batch is None)
8081

8182
def test_connection_getter_with_batch(self):
8283
from gcloud.storage.batch import Batch
83-
PROJECT = object()
84+
PROJECT = 'PROJECT'
8485
CREDENTIALS = _Credentials()
8586
client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)
8687
batch = Batch(client)
@@ -92,7 +93,7 @@ def test_connection_getter_with_batch(self):
9293
def test_bucket(self):
9394
from gcloud.storage.bucket import Bucket
9495

95-
PROJECT = object()
96+
PROJECT = 'PROJECT'
9697
CREDENTIALS = _Credentials()
9798
BUCKET_NAME = 'BUCKET_NAME'
9899

@@ -105,7 +106,7 @@ def test_bucket(self):
105106
def test_batch(self):
106107
from gcloud.storage.batch import Batch
107108

108-
PROJECT = object()
109+
PROJECT = 'PROJECT'
109110
CREDENTIALS = _Credentials()
110111

111112
client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)
@@ -116,7 +117,7 @@ def test_batch(self):
116117
def test_get_bucket_miss(self):
117118
from gcloud.exceptions import NotFound
118119

119-
PROJECT = object()
120+
PROJECT = 'PROJECT'
120121
CREDENTIALS = _Credentials()
121122
client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)
122123

@@ -139,7 +140,7 @@ def test_get_bucket_miss(self):
139140
def test_get_bucket_hit(self):
140141
from gcloud.storage.bucket import Bucket
141142

142-
PROJECT = object()
143+
PROJECT = 'PROJECT'
143144
CREDENTIALS = _Credentials()
144145
client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)
145146

@@ -163,7 +164,7 @@ def test_get_bucket_hit(self):
163164
self.assertEqual(http._called_with['uri'], URI)
164165

165166
def test_lookup_bucket_miss(self):
166-
PROJECT = object()
167+
PROJECT = 'PROJECT'
167168
CREDENTIALS = _Credentials()
168169
client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)
169170

@@ -187,7 +188,7 @@ def test_lookup_bucket_miss(self):
187188
def test_lookup_bucket_hit(self):
188189
from gcloud.storage.bucket import Bucket
189190

190-
PROJECT = object()
191+
PROJECT = 'PROJECT'
191192
CREDENTIALS = _Credentials()
192193
client = self._makeOne(project=PROJECT, credentials=CREDENTIALS)
193194

gcloud/test_client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def test_ctor_defaults(self):
134134
from gcloud._testing import _Monkey
135135
from gcloud import client
136136

137-
PROJECT = object()
137+
PROJECT = 'PROJECT'
138138
CREDENTIALS = object()
139139
FUNC_CALLS = []
140140

@@ -171,8 +171,14 @@ def mock_get_proj():
171171

172172
self.assertEqual(FUNC_CALLS, ['_get_production_project'])
173173

174+
def test_ctor_w_invalid_project(self):
175+
CREDENTIALS = object()
176+
HTTP = object()
177+
with self.assertRaises(ValueError):
178+
self._makeOne(project=object(), credentials=CREDENTIALS, http=HTTP)
179+
174180
def test_ctor_explicit(self):
175-
PROJECT = object()
181+
PROJECT = 'PROJECT'
176182
CREDENTIALS = object()
177183
HTTP = object()
178184

0 commit comments

Comments
 (0)