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
3 changes: 0 additions & 3 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@
# Development dependencies.
#
nose==1.3.4
httpretty==0.8.6
mock==1.0.1
coverage==3.7.1
Sphinx==1.3.1
11 changes: 6 additions & 5 deletions intercom/traits/api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,17 @@ def to_datetime_value(value):


class Resource(object):
changed_attributes = []

def __init__(_self, **params): # noqa
# intercom includes a 'self' field in the JSON, to avoid the naming
# conflict we go with _self here
_self.changed_attributes = []
_self.from_dict(params)

if hasattr(_self, 'flat_store_attributes'):
for attr in _self.flat_store_attributes:
if not hasattr(_self, attr):
setattr(_self, attr, FlatStore())
_self.changed_attributes = []

def _flat_store_attribute(self, attribute):
if hasattr(self, 'flat_store_attributes'):
Expand All @@ -59,14 +58,16 @@ def from_api(cls, response):

def from_response(self, response):
self.from_dict(response)
self.changed_attributes = []
return self

def from_dict(self, dict):
for attribute, value in list(dict.items()):
if type_field(attribute):
continue
setattr(self, attribute, value)
if hasattr(self, 'id'):
# already exists in Intercom
self.changed_attributes = []

@property
def attributes(self):
Expand All @@ -77,7 +78,7 @@ def attributes(self):
return res

def submittable_attribute(self, name, value):
return name in self.changed_attributes or isinstance(value, FlatStore)
return name in self.changed_attributes or (isinstance(value, FlatStore) and name in self.flat_store_attributes) # noqa

def __getattribute__(self, attribute):
value = super(Resource, self).__getattribute__(attribute)
Expand All @@ -96,5 +97,5 @@ def __setattr__(self, attribute, value):
else:
value_to_set = value
if attribute != 'changed_attributes':
self.__dict__['changed_attributes'].append(attribute)
self.changed_attributes.append(attribute)
super(Resource, self).__setattr__(attribute, value_to_set)
56 changes: 25 additions & 31 deletions tests/unit/test_collection_proxy.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,48 @@
# -*- coding: utf-8 -*-

import httpretty
import json
import re
import unittest

from intercom import Intercom
from intercom import User
from mock import call
from mock import patch
from nose.tools import eq_
from nose.tools import istest
from tests.unit import page_of_users

get = httpretty.GET
r = re.compile


class CollectionProxyTest(unittest.TestCase):

@istest
@httpretty.activate
def it_stops_iterating_if_no_next_link(self):
body = json.dumps(page_of_users(include_next_link=False))
httpretty.register_uri(get, r(r"/users"), body=body)
emails = [user.email for user in User.all()]
eq_(emails, ['user1@example.com', 'user2@example.com', 'user3@example.com']) # noqa
body = page_of_users(include_next_link=False)
with patch.object(Intercom, 'get', return_value=body) as mock_method:
emails = [user.email for user in User.all()]
mock_method.assert_called_once_with('/users')
eq_(emails, ['user1@example.com', 'user2@example.com', 'user3@example.com']) # noqa

@istest
@httpretty.activate
def it_keeps_iterating_if_next_link(self):
page1 = json.dumps(page_of_users(include_next_link=True))
page2 = json.dumps(page_of_users(include_next_link=False))
httpretty.register_uri(
get, r(r"https://api.intercom.io/users$"), body=page1,
match_querystring=True)
httpretty.register_uri(
get, r(r"https://api.intercom.io/users\?per_page=50&page=2"),
body=page2, match_querystring=True)
emails = [user.email for user in User.all()]
eq_(emails, ['user1@example.com', 'user2@example.com', 'user3@example.com'] * 2) # noqa
page1 = page_of_users(include_next_link=True)
page2 = page_of_users(include_next_link=False)
side_effect = [page1, page2]
with patch.object(Intercom, 'get', side_effect=side_effect) as mock_method: # noqa
emails = [user.email for user in User.all()]
eq_([call('/users'), call('https://api.intercom.io/users?per_page=50&page=2')], # noqa
mock_method.mock_calls)
eq_(emails, ['user1@example.com', 'user2@example.com', 'user3@example.com'] * 2) # noqa

@istest
@httpretty.activate
def it_supports_indexed_array_access(self):
body = json.dumps(page_of_users(include_next_link=False))
httpretty.register_uri(get, r(r"/users$"), body=body)
eq_(User.all()[0].email, 'user1@example.com')
body = page_of_users(include_next_link=False)
with patch.object(Intercom, 'get', return_value=body) as mock_method:
eq_(User.all()[0].email, 'user1@example.com')
mock_method.assert_called_once_with('/users')

@istest
@httpretty.activate
def it_supports_querying(self):
body = json.dumps(page_of_users(include_next_link=False))
httpretty.register_uri(get, r(r"/users$"), body=body)
emails = [user.email for user in User.find_all(tag_name='Taggart J')]
eq_(emails, ['user1@example.com', 'user2@example.com', 'user3@example.com']) # noqa
body = page_of_users(include_next_link=False)
with patch.object(Intercom, 'get', return_value=body) as mock_method:
emails = [user.email for user in User.find_all(tag_name='Taggart J')] # noqa
eq_(emails, ['user1@example.com', 'user2@example.com', 'user3@example.com']) # noqa
mock_method.assert_called_once_with('/users', tag_name='Taggart J')
43 changes: 19 additions & 24 deletions tests/unit/test_company.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,45 @@
# -*- coding: utf-8 -*-

import httpretty
import json
import re
import intercom
import unittest

from intercom import Company
from intercom import Intercom
from mock import call
from mock import patch
from nose.tools import assert_raises
from nose.tools import eq_
from nose.tools import istest

get = httpretty.GET
r = re.compile


class CompanyTest(unittest.TestCase):

@istest
@httpretty.activate
def it_raises_error_if_no_response_on_find(self):
httpretty.register_uri(
get, r(r'/companies$'), body=None, status=200)
with assert_raises(intercom.HttpError):
Company.find(company_id='4')
with patch.object(Intercom, 'get', return_value=None) as mock_method:
with assert_raises(intercom.HttpError):
Company.find(company_id='4')
mock_method.assert_called_once_with('/companies', company_id='4')

@istest
@httpretty.activate
def it_raises_error_if_no_response_on_find_all(self):
httpretty.register_uri(
get, r(r'/companies$'), body=None, status=200)
with assert_raises(intercom.HttpError):
[x for x in Company.all()]
with patch.object(Intercom, 'get', return_value=None) as mock_method:
with assert_raises(intercom.HttpError):
[x for x in Company.all()]
mock_method.assert_called_once_with('/companies')

@istest
@httpretty.activate
def it_raises_error_on_load(self):
data = {
'type': 'user',
'id': 'aaaaaaaaaaaaaaaaaaaaaaaa',
'company_id': '4',
'name': 'MyCo'
}
httpretty.register_uri(
get, r(r'/companies$'), body=json.dumps(data), status=200)
company = Company.find(company_id='4')
httpretty.register_uri(
get, r(r'/companies/aaaaaaaaaaaaaaaaaaaaaaaa$'), body=None, status=200) # noqa
with assert_raises(intercom.HttpError):
company.load()
side_effect = [data, None]
with patch.object(Intercom, 'get', side_effect=side_effect) as mock_method: # noqa
company = Company.find(company_id='4')
with assert_raises(intercom.HttpError):
company.load()
eq_([call('/companies', company_id='4'), call('/companies/aaaaaaaaaaaaaaaaaaaaaaaa')], # noqa
mock_method.mock_calls)
2 changes: 0 additions & 2 deletions tests/unit/test_event.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-

import httpretty
import time
import unittest

Expand Down Expand Up @@ -41,7 +40,6 @@ def it_creates_an_event_with_metadata(self):
mock_method.assert_called_once_with('/events/', **data)

@istest
@httpretty.activate
def it_creates_an_event_without_metadata(self):
data = {
'event_name': 'sale of item',
Expand Down
29 changes: 12 additions & 17 deletions tests/unit/test_message.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
# -*- coding: utf-8 -*-

import httpretty
import json
import re
import time
import unittest

from datetime import datetime
from intercom import Intercom
from intercom import User
from intercom import Message
from mock import patch
from nose.tools import eq_
from nose.tools import istest

post = httpretty.POST
r = re.compile


class MessageTest(unittest.TestCase):

Expand All @@ -28,7 +24,6 @@ def setUp(self): # noqa
self.created_time = now - 300

@istest
@httpretty.activate
def it_creates_a_user_message_with_string_keys(self):
data = {
'from': {
Expand All @@ -37,13 +32,12 @@ def it_creates_a_user_message_with_string_keys(self):
},
'body': 'halp'
}
httpretty.register_uri(
post, r(r'/messages/$'), body=json.dumps(data), status=200)
message = Message.create(**data)
eq_('halp', message.body)
with patch.object(Intercom, 'post', return_value=data) as mock_method:
message = Message.create(**data)
mock_method.assert_called_once_with('/messages/', **data)
eq_('halp', message.body)

@istest
@httpretty.activate
def it_creates_an_admin_message(self):
data = {
'from': {
Expand All @@ -57,8 +51,9 @@ def it_creates_an_admin_message(self):
'body': 'halp',
'message_type': 'inapp'
}
httpretty.register_uri(
post, r(r'/messages/$'), body=json.dumps(data), status=200)
message = Message.create(**data)
eq_('halp', message.body)
eq_('inapp', message.message_type)

with patch.object(Intercom, 'post', return_value=data) as mock_method:
message = Message.create(**data)
mock_method.assert_called_once_with('/messages/', **data)
eq_('halp', message.body)
eq_('inapp', message.message_type)
18 changes: 6 additions & 12 deletions tests/unit/test_note.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
# -*- coding: utf-8 -*-

import httpretty
import json
import re
import unittest

from intercom import Intercom
from intercom import Note
from mock import patch
from nose.tools import eq_
from nose.tools import istest

post = httpretty.POST
r = re.compile


class NoteTest(unittest.TestCase):

@istest
@httpretty.activate
def it_creates_a_note(self):
data = {
'body': '<p>Note to leave on user</p>',
'created_at': 1234567890
}
httpretty.register_uri(
post, r(r'/notes/$'), body=json.dumps(data))
note = Note.create(body="Note to leave on user")
eq_(note.body, "<p>Note to leave on user</p>")
with patch.object(Intercom, 'post', return_value=data) as mock_method:
note = Note.create(body="Note to leave on user")
mock_method.assert_called_once_with('/notes/', body="Note to leave on user") # noqa
eq_(note.body, "<p>Note to leave on user</p>")

@istest
@httpretty.activate
def it_sets_gets_allowed_keys(self):
params = {
'body': 'Note body',
Expand Down
Loading