From d8c73a70f57f3a47ff3abc413cbe7966fe995766 Mon Sep 17 00:00:00 2001 From: Omar Ryhan Date: Mon, 20 Sep 2021 22:21:45 +0200 Subject: [PATCH] Turn validation off by default --- aiogoogle/__version__.py | 2 +- aiogoogle/client.py | 4 ++-- aiogoogle/resource.py | 4 ++-- tests/ALL_APIS.py | 4 ++-- tests/test_units/test_validate.py | 34 +++++++++++++++++++++++-------- 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/aiogoogle/__version__.py b/aiogoogle/__version__.py index 5135ebf..d5ea21c 100644 --- a/aiogoogle/__version__.py +++ b/aiogoogle/__version__.py @@ -2,7 +2,7 @@ __about__ = "Async Google API client" __description__ = __about__ __url__ = "https://github.com/omarryhan/aiogoogle" -__version_info__ = ("2", "1", "1") +__version_info__ = ("3", "0", "0") __version__ = ".".join(__version_info__) __author__ = "Omar Ryhan" __author_email__ = "omarryhan@gmail.com" diff --git a/aiogoogle/client.py b/aiogoogle/client.py index 60b918d..741b72d 100644 --- a/aiogoogle/client.py +++ b/aiogoogle/client.py @@ -139,7 +139,7 @@ async def list_api(self, name, preferred=None, fields=None): ) return await self.as_anon(request) - async def discover(self, api_name, api_version=None, validate=True): + async def discover(self, api_name, api_version=None, validate=False): """ Donwloads a discovery document from Google's Discovery Service V1 and sets it a ``aiogoogle.resource.GoogleAPI`` @@ -157,7 +157,7 @@ async def discover(self, api_name, api_version=None, validate=True): api_version (str): API version to discover *e.g.: "v3" not "3" and not 3* - validate (bool): Set this to False to disallow input validation on calling methods + validate (bool): Set this to True to use this lib's built in parameter validation logic. Note that you shouldn't rely on this for critical user input validation. Returns: diff --git a/aiogoogle/resource.py b/aiogoogle/resource.py index d33e441..92efb6d 100644 --- a/aiogoogle/resource.py +++ b/aiogoogle/resource.py @@ -834,10 +834,10 @@ class GoogleAPI: discovery_document (dict): A discovery document - validate (bool): Whther or not to validate user input again the schema defined in the discovery document + validate (bool): Set this to True to use this lib's built in parameter validation logic. Note that you shouldn't rely on this for critical user input validation. """ - def __init__(self, discovery_document, validate=True): + def __init__(self, discovery_document, validate=False): self.discovery_document = self._add_extra_query_param_definitions( discovery_document ) diff --git a/tests/ALL_APIS.py b/tests/ALL_APIS.py index 67d1a0c..6b81bf3 100644 --- a/tests/ALL_APIS.py +++ b/tests/ALL_APIS.py @@ -42,6 +42,7 @@ ('compute', 'v1'), ('container', 'v1'), ('customsearch', 'v1'), + ('datacatalog', 'v1'), ('dataproc', 'v1'), ('datastore', 'v1'), ('deploymentmanager', 'v2'), @@ -62,7 +63,6 @@ ('firestore', 'v1'), ('fitness', 'v1'), ('games', 'v1'), - ('genomics', 'v1'), ('gmail', 'v1'), ('groupsmigration', 'v1'), ('groupssettings', 'v1'), @@ -94,7 +94,6 @@ ('pubsub', 'v1'), ('recommender', 'v1'), ('redis', 'v1'), - ('remotebuildexecution', 'v2'), ('reseller', 'v1'), ('run', 'v1'), ('runtimeconfig', 'v1'), @@ -113,6 +112,7 @@ ('sourcerepo', 'v1'), ('spanner', 'v1'), ('speech', 'v1'), + ('sqladmin', 'v1'), ('storage', 'v1'), ('storagetransfer', 'v1'), ('streetviewpublish', 'v1'), diff --git a/tests/test_units/test_validate.py b/tests/test_units/test_validate.py index 61da20b..b9ec47c 100644 --- a/tests/test_units/test_validate.py +++ b/tests/test_units/test_validate.py @@ -163,7 +163,7 @@ def test_validates_body_json_12(create_api): "response" ] # Testing arrays and objects with pytest.raises(ValidationError): - calendar.calendarList.list(json={"items": {}}) + calendar.calendarList.list(validate=True, json={"items": {}}) def test_validates_body_json_13(create_api): @@ -174,7 +174,7 @@ def test_validates_body_json_13(create_api): ] = calendar.calendarList.list._method_specs[ "response" ] # Testing arrays and objects - calendar.calendarList.list(json={"items": [{"accessRole": "a_valid_access_role"}]}) + calendar.calendarList.list(validate=True, json={"items": [{"accessRole": "a_valid_access_role"}]}) def test_validates_body_json_14(create_api): @@ -186,7 +186,7 @@ def test_validates_body_json_14(create_api): "response" ] # Testing arrays and objects with pytest.raises(ValidationError): - calendar.calendarList.list(json={"items": [{"accessRole": 1}]}) + calendar.calendarList.list(validate=True, json={"items": [{"accessRole": 1}]}) def test_validates_body_json_15(create_api): @@ -199,6 +199,7 @@ def test_validates_body_json_15(create_api): ] # Testing arrays and objects with pytest.warns(UserWarning, match="this_isnt_supposed_to_be_here"): calendar.calendarList.list( + validate=True, json={ "items": [ {"accessRole": "a_valid_access_role"}, @@ -218,6 +219,7 @@ def test_validates_body_json_16(create_api): ] # Testing arrays and objects with pytest.warns(UserWarning, match="this_isnt_supposed_to_be_here"): calendar.calendarList.list( + validate=True, json={ "items": [ {"accessRole": "a_valid_access_role"}, @@ -237,6 +239,7 @@ def test_validates_body_json_17(create_api): ] # Testing arrays and objects with pytest.warns(UserWarning, match="this_isnt_supposed_to_be_here"): calendar.calendarList.list( + validate=True, json={"items": [{"this_isnt_supposed_to_be_here": True}]} ) @@ -251,6 +254,7 @@ def test_validates_body_json_18(create_api): ] # Testing arrays and objects with pytest.warns(UserWarning, match="this_isnt_supposed_to_be_here"): calendar.calendarList.list( + validate=True, json={"items": [{"this_isnt_supposed_to_be_here": "asdasdasd"}]} ) @@ -266,6 +270,7 @@ def test_validates_body_json_19(create_api): with pytest.warns(UserWarning, match="this_isnt_supposed_to_be_here"): with pytest.raises(ValidationError): calendar.calendarList.list( + validate=True, json={ "items": [{"accessRole": 1, "this_isnt_supposed_to_be_here": 123}] } @@ -283,6 +288,7 @@ def test_validates_body_json_20(create_api): with pytest.warns(UserWarning, match="this_isnt_supposed_to_be_here"): with pytest.raises(ValidationError): calendar.calendarList.list( + validate=True, json={ "items": [ {"accessRole": 1, "this_isnt_supposed_to_be_here": "asdsda"} @@ -300,6 +306,7 @@ def test_validates_body_json_21(create_api): "response" ] # Testing arrays and objects calendar.calendarList.list( + validate=True, json={ "etag": "asd", "items": [ @@ -325,6 +332,7 @@ def test_validates_body_json_22(create_api): ] # Testing arrays and objects with pytest.raises(ValidationError): calendar.calendarList.list( + validate=True, json={ "etag": "asd", "items": [ @@ -350,6 +358,7 @@ def test_validates_body_json_23(create_api): ] # Testing arrays and objects with pytest.raises(ValidationError): calendar.calendarList.list( + validate=True, json={ "etag": "asd", "items": [ @@ -376,6 +385,7 @@ def test_validates_body_json_24(create_api): ] # Testing arrays and objects with pytest.raises(ValidationError): calendar.calendarList.list( + validate=True, json={ "etag": "asd", "items": [ @@ -416,32 +426,38 @@ def test_validates_repeated(create_api): sheets = create_api("sheets", "v4") sheets.spreadsheets.values.batchGet( spreadsheetId="IRRELEVANT", - ranges=['one', 'two'] + ranges=['one', 'two'], + validate=True ) sheets.spreadsheets.values.batchGet( spreadsheetId="IRRELEVANT", - ranges=('one', 'two') + ranges=('one', 'two'), + validate=True ) sheets.spreadsheets.values.batchGet( spreadsheetId="IRRELEVANT", - ranges=set(['one', 'two']) + ranges=set(['one', 'two']), + validate=True ) sheets.spreadsheets.values.batchGet( spreadsheetId="IRRELEVANT", - ranges='one' + ranges='one', + validate=True ) with pytest.raises(ValidationError): sheets.spreadsheets.values.batchGet( spreadsheetId="IRRELEVANT", - ranges=1 + ranges=1, + validate=True ) with pytest.raises(ValidationError): sheets.spreadsheets.values.batchGet( spreadsheetId="IRRELEVANT", - ranges=[132, 'valid'] # Only first item is invalid + ranges=[132, 'valid'], # Only first item is invalid + validate=True )