Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support for zendesk token auth #52

Closed
Closed
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
15 changes: 12 additions & 3 deletions libsaas/services/zendesk/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class Zendesk(base.Resource):
"""
"""
def __init__(self, subdomain, username, password):
def __init__(self, subdomain, username, password=None, token=None):
"""
Create a Zendesk service.

Expand All @@ -23,14 +23,23 @@ def __init__(self, subdomain, username, password):
:var username: The email of the authenticated agent.
:vartype username: str

:var password: The password of the authenticated agent.
:var password: The password of the authenticated agent, leave this as
`None` when using token auth.
:vartype password: str

:var token: Only used with Zendesk token auth, leave password as
`None` when using this token auth.
:vartype token: str
"""
tmpl = '{0}.zendesk.com/api/v2'
self.apiroot = http.quote_any(tmpl.format(port.to_u(subdomain)))
self.apiroot = 'https://' + self.apiroot

self.add_filter(auth.BasicAuth(username, password))
if token is None:
self.add_filter(auth.BasicAuth(username, password))
else:
self.add_filter(auth.BasicAuth(username + '/token', token))

self.add_filter(self.use_json)

def get_url(self):
Expand Down
10 changes: 10 additions & 0 deletions test/test_zendesk.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ def expect(self, method=None, uri=None, params=None, headers=None):
if headers:
self.assertEqual(self.executor.request.headers, headers)

def test_basic_auth(self):
service = zendesk.Zendesk('mydomain', 'myuser', 'mypass')
self.assertEqual(service.filters[0].username, 'myuser')
self.assertEqual(service.filters[0].password, 'mypass')

def test_token_auth(self):
service = zendesk.Zendesk('mydomain', 'myuser', token='mytoken')
self.assertEqual(service.filters[0].username, 'myuser/token')
self.assertEqual(service.filters[0].password, 'mytoken')

def test_tickets(self):
self.service.tickets().get(page=3)
self.expect('GET', '/tickets.json', {'page': 3})
Expand Down