Skip to content

Commit

Permalink
Raise ValueError if credentials are not from google-auth (#2828)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Wayne Parrott authored Dec 8, 2016
1 parent e371b6a commit 3d6d725
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions packages/google-cloud-vision/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
import base64
import unittest

from google.cloud._helpers import _to_bytes
from google.cloud._helpers import _bytes_to_unicode
import mock


IMAGE_CONTENT = _to_bytes('/9j/4QNURXhpZgAASUkq')
IMAGE_CONTENT = b'/9j/4QNURXhpZgAASUkq'
IMAGE_SOURCE = 'gs://some/image.jpg'
PROJECT = 'PROJECT'
B64_IMAGE_CONTENT = _bytes_to_unicode(base64.b64encode(IMAGE_CONTENT))
B64_IMAGE_CONTENT = base64.b64encode(IMAGE_CONTENT).decode('ascii')


def _make_credentials():
import google.auth.credentials
return mock.Mock(spec=google.auth.credentials.Credentials)


class TestClient(unittest.TestCase):
Expand All @@ -35,7 +39,7 @@ def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_ctor(self):
creds = object()
creds = _make_credentials()
client = self._make_one(project=PROJECT, credentials=creds)
self.assertEqual(client.project, PROJECT)

Expand All @@ -59,7 +63,7 @@ def test_face_annotation(self):
}
]
}
credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

Expand All @@ -75,7 +79,7 @@ def test_face_annotation(self):
def test_image_with_client_gcs_source(self):
from google.cloud.vision.image import Image

credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT,
credentials=credentials)
gcs_image = client.image(source_uri=IMAGE_SOURCE)
Expand All @@ -85,7 +89,7 @@ def test_image_with_client_gcs_source(self):
def test_image_with_client_raw_content(self):
from google.cloud.vision.image import Image

credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT,
credentials=credentials)
raw_image = client.image(content=IMAGE_CONTENT)
Expand All @@ -97,7 +101,7 @@ def test_image_with_client_filename(self):
from mock import patch
from google.cloud.vision.image import Image

credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT,
credentials=credentials)
with patch('google.cloud.vision.image.open',
Expand All @@ -118,7 +122,7 @@ def test_multiple_detection_from_content(self):
logos = copy.deepcopy(LOGO_DETECTION_RESPONSE['responses'][0])
returned['responses'][0]['logoAnnotations'] = logos['logoAnnotations']

credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(returned)

Expand Down Expand Up @@ -165,7 +169,7 @@ def test_face_detection_from_source(self):
from google.cloud.vision.face import Face
from unit_tests._fixtures import FACE_DETECTION_RESPONSE
RETURNED = FACE_DETECTION_RESPONSE
credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

Expand All @@ -182,7 +186,7 @@ def test_face_detection_from_content(self):
from google.cloud.vision.face import Face
from unit_tests._fixtures import FACE_DETECTION_RESPONSE
RETURNED = FACE_DETECTION_RESPONSE
credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

Expand All @@ -200,7 +204,7 @@ def test_face_detection_from_content_no_results(self):
RETURNED = {
'responses': [{}]
}
credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

Expand All @@ -219,7 +223,7 @@ def test_label_detection_from_source(self):
from unit_tests._fixtures import (
LABEL_DETECTION_RESPONSE as RETURNED)

credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

Expand All @@ -240,7 +244,7 @@ def test_label_detection_no_results(self):
RETURNED = {
'responses': [{}]
}
credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

Expand All @@ -254,7 +258,7 @@ def test_landmark_detection_from_source(self):
from unit_tests._fixtures import (
LANDMARK_DETECTION_RESPONSE as RETURNED)

credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

Expand All @@ -276,7 +280,7 @@ def test_landmark_detection_from_content(self):
from unit_tests._fixtures import (
LANDMARK_DETECTION_RESPONSE as RETURNED)

credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

Expand All @@ -293,7 +297,7 @@ def test_landmark_detection_no_results(self):
RETURNED = {
'responses': [{}]
}
credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

Expand All @@ -306,7 +310,7 @@ def test_logo_detection_from_source(self):
from google.cloud.vision.entity import EntityAnnotation
from unit_tests._fixtures import LOGO_DETECTION_RESPONSE
RETURNED = LOGO_DETECTION_RESPONSE
credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

Expand All @@ -323,7 +327,7 @@ def test_logo_detection_from_content(self):
from google.cloud.vision.entity import EntityAnnotation
from unit_tests._fixtures import LOGO_DETECTION_RESPONSE
RETURNED = LOGO_DETECTION_RESPONSE
credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

Expand All @@ -341,7 +345,7 @@ def test_text_detection_from_source(self):
from unit_tests._fixtures import (
TEXT_DETECTION_RESPONSE as RETURNED)

credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

Expand All @@ -364,7 +368,7 @@ def test_safe_search_detection_from_source(self):
from unit_tests._fixtures import SAFE_SEARCH_DETECTION_RESPONSE

RETURNED = SAFE_SEARCH_DETECTION_RESPONSE
credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

Expand All @@ -383,7 +387,7 @@ def test_safe_search_no_results(self):
RETURNED = {
'responses': [{}]
}
credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

Expand All @@ -397,7 +401,7 @@ def test_image_properties_detection_from_source(self):
from unit_tests._fixtures import IMAGE_PROPERTIES_RESPONSE

RETURNED = IMAGE_PROPERTIES_RESPONSE
credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

Expand All @@ -419,7 +423,7 @@ def test_image_properties_no_results(self):
RETURNED = {
'responses': [{}]
}
credentials = object()
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials)
client._connection = _Connection(RETURNED)

Expand Down

0 comments on commit 3d6d725

Please sign in to comment.