diff --git a/intercom/intercom.py b/intercom/intercom.py index 70361524..71aaa2e1 100644 --- a/intercom/intercom.py +++ b/intercom/intercom.py @@ -110,7 +110,7 @@ def _create_or_update_user(cls, method, **kwargs): return user_dict @classmethod - def get_users(cls): + def get_users(cls, page=None, per_page=None, tag_id=None, tag_name=None): """ Return a dict for the user represented by the specified email or user_id. @@ -121,7 +121,26 @@ def get_users(cls): 3 """ - user_dict = Intercom._call('GET', Intercom.api_endpoint + 'users') + + # collate parameters + params = {} + + if page: + params['page'] = page + + if per_page: + # if per_page is outside bounds use default + if per_page < 1 or per_page > 500: + per_page = 500 + params['per_page'] = per_page + + if tag_id: + params['tag_id'] = tag_id + + if tag_name: + params['tag_name'] = tag_name + + user_dict = Intercom._call('GET', Intercom.api_endpoint + 'users', params=params) return user_dict @classmethod diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py index 064767b9..b7d82942 100644 --- a/tests/unit/__init__.py +++ b/tests/unit/__init__.py @@ -5,6 +5,7 @@ # License: http://jkeyes.mit-license.org/ # +import json import os from mock import Mock @@ -12,6 +13,7 @@ DIRPATH = os.path.dirname(__file__) FIXTURES = os.path.join(DIRPATH, 'fixtures') + def create_response(status, fixture=None): def request(*args, **kwargs): response = Mock() @@ -21,3 +23,15 @@ def request(*args, **kwargs): response.content = open(fixture_path).read() return response return request + + +def local_response(): + def _call(*args, **kwargs): + response = Mock() + reply = {} + for name, value in kwargs.items(): + reply[name] = value + response.content = json.dumps(reply) + response.status_code = 200 + return response + return _call diff --git a/tests/unit/test_intercom.py b/tests/unit/test_intercom.py index 6b0d1030..da00b41d 100644 --- a/tests/unit/test_intercom.py +++ b/tests/unit/test_intercom.py @@ -7,8 +7,11 @@ from . import create_response +from . import local_response from mock import patch from nose.tools import raises +from nose.tools import eq_ +from nose.tools import ok_ from unittest import TestCase from intercom import ServerError @@ -79,3 +82,32 @@ def test_api_error(self): @patch('requests.request', create_response(500, 'invalid.json')) def test_api_error_when_json_is_invalid(self): Intercom.get_users() + + @patch('requests.request', local_response()) + def test_get_users_params(self): + resp = Intercom.get_users() + ok_('params' in resp) + ok_('page' not in resp['params']) + ok_('per_page' not in resp['params']) + ok_('tag_id' not in resp['params']) + ok_('tag_id' not in resp['params']) + + resp = Intercom.get_users(page=20) + ok_('params' in resp) + ok_('page' in resp['params']) + eq_(resp['params']['page'], 20) + + resp = Intercom.get_users(per_page=10) + ok_('params' in resp) + ok_('per_page' in resp['params']) + eq_(resp['params']['per_page'], 10) + + resp = Intercom.get_users(tag_id=100) + ok_('params' in resp) + ok_('tag_id' in resp['params']) + eq_(resp['params']['tag_id'], 100) + + resp = Intercom.get_users(tag_name="starter") + ok_('params' in resp) + ok_('tag_name' in resp['params']) + eq_(resp['params']['tag_name'], "starter")