Skip to content

Commit

Permalink
Parameters added to the get_users method: page, per_page, tag_id, and…
Browse files Browse the repository at this point in the history
… tag_name.
  • Loading branch information
jkeyes committed Aug 10, 2013
1 parent 1897df0 commit d827dc3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
23 changes: 21 additions & 2 deletions intercom/intercom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
# License: http://jkeyes.mit-license.org/
#

import json
import os

from mock import Mock

DIRPATH = os.path.dirname(__file__)
FIXTURES = os.path.join(DIRPATH, 'fixtures')


def create_response(status, fixture=None):
def request(*args, **kwargs):
response = Mock()
Expand All @@ -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
32 changes: 32 additions & 0 deletions tests/unit/test_intercom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

0 comments on commit d827dc3

Please sign in to comment.