Skip to content
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
12 changes: 12 additions & 0 deletions nylas/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ def authentication_url(
login_hint="",
state="",
scopes=("email", "calendar", "contacts"),
provider="",
redirect_on_error=None,
):
args = {
"redirect_uri": redirect_uri,
Expand All @@ -165,6 +167,16 @@ def authentication_url(
if isinstance(scopes, str):
scopes = [scopes]
args["scopes"] = ",".join(scopes)
if provider and provider in [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this!

"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"

url = URLObject(self.authorize_url).add_query_params(args.items())
return str(url)
Expand Down
56 changes: 56 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,62 @@ 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)

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(expected2, 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"})
Expand Down