From ed699c1226d27e34fbf4e73a607b7081b4a70338 Mon Sep 17 00:00:00 2001 From: Kiran Raju Date: Tue, 26 Jul 2022 16:06:44 -0500 Subject: [PATCH 1/3] TSDK-366 Hosted Authentication params added --- nylas/client/client.py | 16 +++++++++----- tests/test_client.py | 49 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/nylas/client/client.py b/nylas/client/client.py index 9bc2f458..ed4dc5d7 100644 --- a/nylas/client/client.py +++ b/nylas/client/client.py @@ -147,11 +147,13 @@ def access_token(self, value): self._access_token = value def authentication_url( - self, - redirect_uri, - login_hint="", - state="", - scopes=("email", "calendar", "contacts"), + self, + redirect_uri, + login_hint="", + state="", + scopes=("email", "calendar", "contacts"), + provider="", + redirect_on_error=None ): args = { "redirect_uri": redirect_uri, @@ -165,6 +167,10 @@ def authentication_url( if isinstance(scopes, str): scopes = [scopes] args["scopes"] = ",".join(scopes) + if provider and provider in ['icloud', 'gmail', 'office365', 'exchange', 'imap']: + args["provider"] = provider + if redirect_on_error and isinstance(redirect_on_error, bool): + args["redirect_on_error"] = "true" if redirect_on_error is True else "false" url = URLObject(self.authorize_url).add_query_params(args.items()) return str(url) diff --git a/tests/test_client.py b/tests/test_client.py index 9d3ba21c..424f3039 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -135,6 +135,7 @@ def test_client_authentication_url_scopes_none(api_client, api_url): ("redirect_uri", "/redirect"), ("response_type", "code"), ("client_id", "None"), + ("scopes", "email"), # no scopes parameter ] ) @@ -143,6 +144,54 @@ def test_client_authentication_url_scopes_none(api_client, api_url): assert urls_equal(expected, actual) +def test_client_authentication_url_optional_params(api_client, api_url): + expected = ( + URLObject(api_url) + .with_path("/oauth/authorize") + .set_query_params( + [ + ("login_hint", ""), + ("state", ""), + ("redirect_uri", "/redirect"), + ("response_type", "code"), + ("client_id", "None"), + ("scopes", "email"), + ("provider", "gmail"), + ("redirect_on_error", "false") + ] + ) + ) + actual = URLObject(api_client.authentication_url("/redirect", scopes="email", provider="gmail", + redirect_on_error=False)) + assert urls_equal(expected, actual) + + +def test_client_authentication_url_invalid_param_values(api_client, api_url): + expected = ( + URLObject(api_url) + .with_path("/oauth/authorize") + .set_query_params( + [ + ("login_hint", ""), + ("state", ""), + ("redirect_uri", "/redirect"), + ("response_type", "code"), + ("client_id", "None"), + ("scopes", "email"), + ] + ) + ) + actual = URLObject(api_client.authentication_url("/redirect", scopes="email", provider="Google")) + assert urls_equal(expected, actual) + + expected.set_query_param("provider", "gmail") + + actual2 = URLObject(api_client.authentication_url("/redirect", scopes="email", provider="gmail", + redirect_on_error="true")) + + assert urls_equal(expected, actual2) + + def test_client_token_for_code(mocked_responses, api_client, api_url): endpoint = re.compile(api_url + "/oauth/token") response_body = json.dumps({"access_token": "hooray"}) From cc048ec6680bd1dd335537e485a16c45ae0695b0 Mon Sep 17 00:00:00 2001 From: Kiran Raju Date: Tue, 26 Jul 2022 16:32:19 -0500 Subject: [PATCH 2/3] Fixing test cases --- nylas/client/client.py | 2 +- tests/test_client.py | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/nylas/client/client.py b/nylas/client/client.py index ed4dc5d7..65f96f7d 100644 --- a/nylas/client/client.py +++ b/nylas/client/client.py @@ -169,7 +169,7 @@ def authentication_url( args["scopes"] = ",".join(scopes) if provider and provider in ['icloud', 'gmail', 'office365', 'exchange', 'imap']: args["provider"] = provider - if redirect_on_error and isinstance(redirect_on_error, bool): + if redirect_on_error is not None and isinstance(redirect_on_error, bool): args["redirect_on_error"] = "true" if redirect_on_error is True else "false" url = URLObject(self.authorize_url).add_query_params(args.items()) diff --git a/tests/test_client.py b/tests/test_client.py index 424f3039..3a76e19c 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -135,7 +135,6 @@ def test_client_authentication_url_scopes_none(api_client, api_url): ("redirect_uri", "/redirect"), ("response_type", "code"), ("client_id", "None"), - ("scopes", "email"), # no scopes parameter ] ) @@ -184,12 +183,12 @@ def test_client_authentication_url_invalid_param_values(api_client, api_url): actual = URLObject(api_client.authentication_url("/redirect", scopes="email", provider="Google")) assert urls_equal(expected, actual) - expected.set_query_param("provider", "gmail") + expected2 = expected.set_query_param("provider", "gmail") actual2 = URLObject(api_client.authentication_url("/redirect", scopes="email", provider="gmail", redirect_on_error="true")) - assert urls_equal(expected, actual2) + assert urls_equal(expected2, actual2) def test_client_token_for_code(mocked_responses, api_client, api_url): From e4402d6d9315559ec610c1f92b0fcb0d49a1f873 Mon Sep 17 00:00:00 2001 From: Kiran Raju Date: Tue, 26 Jul 2022 16:35:54 -0500 Subject: [PATCH 3/3] Fix formatting issues --- nylas/client/client.py | 22 ++++++++++++++-------- tests/test_client.py | 20 ++++++++++++++------ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/nylas/client/client.py b/nylas/client/client.py index 65f96f7d..6f7ab174 100644 --- a/nylas/client/client.py +++ b/nylas/client/client.py @@ -147,13 +147,13 @@ def access_token(self, value): self._access_token = value def authentication_url( - self, - redirect_uri, - login_hint="", - state="", - scopes=("email", "calendar", "contacts"), - provider="", - redirect_on_error=None + self, + redirect_uri, + login_hint="", + state="", + scopes=("email", "calendar", "contacts"), + provider="", + redirect_on_error=None, ): args = { "redirect_uri": redirect_uri, @@ -167,7 +167,13 @@ def authentication_url( if isinstance(scopes, str): scopes = [scopes] args["scopes"] = ",".join(scopes) - if provider and provider in ['icloud', 'gmail', 'office365', 'exchange', 'imap']: + if provider and provider in [ + "icloud", + "gmail", + "office365", + "exchange", + "imap", + ]: args["provider"] = provider if redirect_on_error is not None and isinstance(redirect_on_error, bool): args["redirect_on_error"] = "true" if redirect_on_error is True else "false" diff --git a/tests/test_client.py b/tests/test_client.py index 3a76e19c..1eb6f4b8 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -156,12 +156,15 @@ def test_client_authentication_url_optional_params(api_client, api_url): ("client_id", "None"), ("scopes", "email"), ("provider", "gmail"), - ("redirect_on_error", "false") + ("redirect_on_error", "false"), ] ) ) - actual = URLObject(api_client.authentication_url("/redirect", scopes="email", provider="gmail", - redirect_on_error=False)) + actual = URLObject( + api_client.authentication_url( + "/redirect", scopes="email", provider="gmail", redirect_on_error=False + ) + ) assert urls_equal(expected, actual) @@ -180,13 +183,18 @@ def test_client_authentication_url_invalid_param_values(api_client, api_url): ] ) ) - actual = URLObject(api_client.authentication_url("/redirect", scopes="email", provider="Google")) + actual = URLObject( + api_client.authentication_url("/redirect", scopes="email", provider="Google") + ) assert urls_equal(expected, actual) expected2 = expected.set_query_param("provider", "gmail") - actual2 = URLObject(api_client.authentication_url("/redirect", scopes="email", provider="gmail", - redirect_on_error="true")) + actual2 = URLObject( + api_client.authentication_url( + "/redirect", scopes="email", provider="gmail", redirect_on_error="true" + ) + ) assert urls_equal(expected2, actual2)