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
6 changes: 3 additions & 3 deletions nylas/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,19 +817,19 @@ def _request(self, method, url, cls=None, headers=None, **kwargs):

def _add_auth_header(self, auth_method):
authorization = None
if auth_method is AuthMethod.BEARER:
if auth_method == AuthMethod.BEARER:
authorization = (
"Bearer {token}".format(token=self.access_token)
if self.access_token
else None
)
elif auth_method is AuthMethod.BASIC_CLIENT_ID_AND_SECRET:
elif auth_method == AuthMethod.BASIC_CLIENT_ID_AND_SECRET:
if self.client_id and self.client_secret:
credential = "{client_id}:{client_secret}".format(
client_id=self.client_id, client_secret=self.client_secret
)
authorization = "Basic {credential}".format(
credential=b64encode(credential.encode("utf8"))
credential=b64encode(credential.encode("utf8")).decode("utf8")
)
else:
if self.client_secret:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import responses
from nylas.client import APIClient
from nylas.client.restful_models import Contact
from nylas.utils import AuthMethod


def urls_equal(url1, url2):
Expand Down Expand Up @@ -345,3 +346,22 @@ def test_count(mocked_responses, api_client, api_url):

contact_count = api_client.contacts.count()
assert contact_count == 721


def test_add_auth_header_bearer(api_client):
api_client.access_token = "access_token"
auth_header = api_client._add_auth_header(AuthMethod.BEARER)
assert auth_header == {"Authorization": "Bearer access_token"}


def test_add_auth_header_basic_client_id_and_secret(api_client):
api_client.client_id = "client_id"
api_client.client_secret = "client_secret"
auth_header = api_client._add_auth_header(AuthMethod.BASIC_CLIENT_ID_AND_SECRET)
assert auth_header == {"Authorization": "Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ="}


def test_add_auth_header_basic(api_client):
api_client.client_secret = "client_secret"
auth_header = api_client._add_auth_header(AuthMethod.BASIC)
assert auth_header == {"Authorization": "Basic Y2xpZW50X3NlY3JldDo="}