Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harden pubsub client against keys omitted in back-end responses #1440

Merged
merged 3 commits into from
Feb 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gcloud/pubsub/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def list_topics(self, page_size=None, page_token=None):
resp = self.connection.api_request(method='GET', path=path,
query_params=params)
topics = [Topic.from_api_repr(resource, self)
for resource in resp['topics']]
for resource in resp.get('topics', ())]
return topics, resp.get('nextPageToken')

def list_subscriptions(self, page_size=None, page_token=None,
Expand Down Expand Up @@ -128,7 +128,7 @@ def list_subscriptions(self, page_size=None, page_token=None,
topics = {}
subscriptions = [Subscription.from_api_repr(resource, self,
topics=topics)
for resource in resp['subscriptions']]
for resource in resp.get('subscriptions', ())]
return subscriptions, resp.get('nextPageToken')

def topic(self, name, timestamp_messages=False):
Expand Down
42 changes: 42 additions & 0 deletions gcloud/pubsub/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ def test_list_topics_with_paging(self):
self.assertEqual(req['query_params'],
{'pageSize': SIZE, 'pageToken': TOKEN1})

def test_list_topics_missing_key(self):
PROJECT = 'PROJECT'
CREDS = _Credentials()

CLIENT_OBJ = self._makeOne(project=PROJECT, credentials=CREDS)

RETURNED = {}
# Replace the connection on the client with one of our own.
CLIENT_OBJ.connection = _Connection(RETURNED)

# Execute request.
topics, next_page_token = CLIENT_OBJ.list_topics()
# Test values are correct.
self.assertEqual(len(topics), 0)
self.assertEqual(next_page_token, None)
self.assertEqual(len(CLIENT_OBJ.connection._requested), 1)
req = CLIENT_OBJ.connection._requested[0]
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/projects/%s/topics' % PROJECT)
self.assertEqual(req['query_params'], {})

def test_list_subscriptions_no_paging(self):
from gcloud.pubsub.subscription import Subscription
PROJECT = 'PROJECT'
Expand Down Expand Up @@ -154,6 +175,27 @@ def test_list_subscriptions_with_paging(self):
self.assertEqual(req['query_params'],
{'pageSize': SIZE, 'pageToken': TOKEN1})

def test_list_subscriptions_w_missing_key(self):
PROJECT = 'PROJECT'
CREDS = _Credentials()

CLIENT_OBJ = self._makeOne(project=PROJECT, credentials=CREDS)

RETURNED = {}
# Replace the connection on the client with one of our own.
CLIENT_OBJ.connection = _Connection(RETURNED)

# Execute request.
subscriptions, next_page_token = CLIENT_OBJ.list_subscriptions()
# Test values are correct.
self.assertEqual(len(subscriptions), 0)
self.assertEqual(next_page_token, None)
self.assertEqual(len(CLIENT_OBJ.connection._requested), 1)
req = CLIENT_OBJ.connection._requested[0]
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/projects/%s/subscriptions' % PROJECT)
self.assertEqual(req['query_params'], {})

def test_list_subscriptions_with_topic_name(self):
from gcloud.pubsub.subscription import Subscription
PROJECT = 'PROJECT'
Expand Down
3 changes: 3 additions & 0 deletions system_tests/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ def test_list_subscriptions(self):
self.assertFalse(topic.exists())
topic.create()
self.to_delete.append(topic)
empty, _ = Config.CLIENT.list_subscriptions(
topic_name=DEFAULT_TOPIC_NAME)
self.assertEqual(len(empty), 0)
subscriptions_to_create = [
'new%d' % (1000 * time.time(),),
'newer%d' % (1000 * time.time(),),
Expand Down