Skip to content
Next Next commit
add conformance tests into client unit tests
  • Loading branch information
IlyaFaer committed Mar 25, 2020
commit ade9985707020dfe95dccad55e22f1cf24916ce7
11 changes: 11 additions & 0 deletions tests/unit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@
# 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.

import io
import json
import os


def _read_local_json(json_file):
here = os.path.dirname(__file__)
json_path = os.path.abspath(os.path.join(here, json_file))
with io.open(json_path, "r", encoding="utf-8-sig") as fileobj:
return json.load(fileobj)
Comment thread
IlyaFaer marked this conversation as resolved.
9 changes: 1 addition & 8 deletions tests/unit/test__signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import binascii
import calendar
import datetime
import io
import json
import os
import time
import unittest

Expand All @@ -29,12 +27,7 @@
import six
from six.moves import urllib_parse


def _read_local_json(json_file):
here = os.path.dirname(__file__)
json_path = os.path.abspath(os.path.join(here, json_file))
with io.open(json_path, "r", encoding="utf-8-sig") as fileobj:
return json.load(fileobj)
from . import _read_local_json


_SERVICE_ACCOUNT_JSON = _read_local_json("url_signer_v4_test_account.json")
Expand Down
43 changes: 41 additions & 2 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@

import io
import json
import unittest

import mock
import pytest
import requests
import unittest
from six.moves import http_client

from google.oauth2.service_account import Credentials
from . import _read_local_json

_SERVICE_ACCOUNT_JSON = _read_local_json("url_signer_v4_test_account.json")
_CONFORMANCE_TESTS = _read_local_json("url_signer_v4_test_data.json")
_POST_POLICY_TESTS = [test for test in _CONFORMANCE_TESTS if "bucket_name" in test]
_DUMMY_CREDENTIALS = Credentials.from_service_account_info(_SERVICE_ACCOUNT_JSON)

Comment thread
IlyaFaer marked this conversation as resolved.

def _make_credentials():
import google.auth.credentials
Expand Down Expand Up @@ -1742,3 +1749,35 @@ def test_get_signed_policy_v4_with_access_token(self):
policy["fields"]["policy"],
b"eyJjb25kaXRpb25zIjogW3siYnVja2V0IjogImJ1Y2tldC1uYW1lIn0sIHsiYWNsIjogInByaXZhdGUifSwgWyJzdGFydHMtd2l0aCIsICIkQ29udGVudC1UeXBlIiwgInRleHQvcGxhaW4iXV0sICJleHBpcmF0aW9uIjogIjIwMjAtMDMtMTJUMDA6MDA6MDAifQ==",
)


@pytest.mark.parametrize("test_data", _POST_POLICY_TESTS)
def test_conformance_post_policy(test_data):
import datetime
from google.cloud.storage.client import Client

client = Client(credentials=_DUMMY_CREDENTIALS)

bucket_bound_hostname = None
if test_data.get("urlStyle") == "BUCKET_BOUND_HOSTNAME":
bucket_bound_hostname = test_data.get("bucketBoundHostname")

policy = client.generate_signed_post_policy_v4(
credentials=_DUMMY_CREDENTIALS,
bucket_name=test_data["bucket_name"],
blob_name=test_data["object_name"],
conditions=test_data["conditions"],
fields=test_data.get("fields"),
expiration=datetime.datetime(2020, 3, 25),
virtual_hosted_style=test_data.get("urlStyle") == "VIRTUAL_HOSTED_STYLE",
bucket_bound_hostname=bucket_bound_hostname,
scheme=test_data.get("scheme"),
)

assert (
policy["fields"]["x-goog-credential"]
== "test-iam-credentials@dummy-project-id.iam.gserviceaccount.com/20200325/auto/storage/goog4_request"
)
assert policy["url"] == test_data["expectedUrl"]
assert policy["fields"]["policy"].decode() == test_data["expectedPolicy"]
assert policy["fields"]["x-goog-signature"] == test_data["expectedSignature"]