diff --git a/libsaas/services/zendesk/service.py b/libsaas/services/zendesk/service.py index 29ad5a9..96e5222 100644 --- a/libsaas/services/zendesk/service.py +++ b/libsaas/services/zendesk/service.py @@ -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. @@ -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): diff --git a/test/test_zendesk.py b/test/test_zendesk.py index c4b3c00..6b935c9 100644 --- a/test/test_zendesk.py +++ b/test/test_zendesk.py @@ -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})