示例#1
0
 def it_can_save_a_user_with_a_none_email(self):
     user = User(email=None,
                 user_id="i-1224242",
                 companies=[{
                     'company_id': 6,
                     'name': 'Intercom'
                 }])
     body = {
         'custom_attributes': {},
         'email': None,
         'user_id': 'i-1224242',
         'companies': [{
             'company_id': 6,
             'name': 'Intercom'
         }]
     }
     with patch.object(Client, 'post', return_value=body) as mock_method:
         self.client.users.save(user)
         ok_(user.email is None)
         eq_(user.user_id, 'i-1224242')
         mock_method.assert_called_once_with(
             '/users', {
                 'email': None,
                 'user_id': "i-1224242",
                 'companies': [{
                     'company_id': 6,
                     'name': 'Intercom'
                 }],
                 'custom_attributes': {}
             })
示例#2
0
 def it_saves_a_user_with_companies(self):
     body = {
         'email': '*****@*****.**',
         'user_id': 'i-1224242',
         'companies': [{
             'company_id': 6,
             'name': 'Intercom'
         }]
     }
     with patch.object(Client, 'post', return_value=body) as mock_method:
         user = User(email="*****@*****.**",
                     user_id="i-1224242",
                     companies=[{
                         'company_id': 6,
                         'name': 'Intercom'
                     }])
         self.client.users.save(user)
         eq_(user.email, '*****@*****.**')
         eq_(len(user.companies), 1)
         mock_method.assert_called_once_with(
             '/users', {
                 'email': "*****@*****.**",
                 'user_id': "i-1224242",
                 'companies': [{
                     'company_id': 6,
                     'name': 'Intercom'
                 }],
                 'custom_attributes': {}
             })
示例#3
0
 def test_save_user(self):
     user = User(email='*****@*****.**', custom_data={'age': '42'})
     user.save()
     self.assertEqual(None, user.user_id)
     self.assertEqual('*****@*****.**', user.email)
     self.assertEqual('42', user.custom_data['age'])
     self.assertEqual(1, user.session_count)
示例#4
0
 def setUp(self):  # noqa
     self.client = Client()
     now = time.mktime(datetime.utcnow().timetuple())
     self.user = User(email="*****@*****.**",
                      user_id="12345",
                      created_at=now,
                      name="Jim Bob")
     self.created_time = now - 300
示例#5
0
 def it_allows_easy_setting_of_multiple_companies(self):
     user = User()
     companies = [
         {"name": "Intercom", "company_id": "6"},
         {"name": "Test", "company_id": "9"},
     ]
     user.companies = companies
     eq_(user.to_dict()["companies"], companies)
示例#6
0
def test_user_companies():
    user = User()
    user.companies = [{
        'id': 1,
        'name': 'Intercom',
        'created_at': datetime.fromtimestamp(1331764344)
    }]
    raises(AttributeError, lambda: user.companies)
    user.companies = {'foo': 'bar'}
示例#7
0
 def it_to_dict_itself(self):
     created_at = datetime.utcnow()
     user = User(
         email="*****@*****.**", user_id="12345",
         created_at=created_at, name="Jim Bob")
     as_dict = user.to_dict()
     eq_(as_dict["email"], "*****@*****.**")
     eq_(as_dict["user_id"], "12345")
     eq_(as_dict["created_at"], calendar.timegm(created_at.utctimetuple()))
     eq_(as_dict["name"], "Jim Bob")
示例#8
0
    def it_allows_easy_setting_of_custom_data(self):
        now = datetime.utcnow()
        now_ts = calendar.timegm(now.utctimetuple())

        user = User()
        user.custom_attributes["mad"] = 123
        user.custom_attributes["other"] = now_ts
        user.custom_attributes["thing"] = "yay"
        attrs = {"mad": 123, "other": now_ts, "thing": "yay"}
        eq_(user.to_dict()["custom_attributes"], attrs)
示例#9
0
    def it_rejects_nested_data_structures_in_custom_attributes(self):
        user = User()
        with assert_raises(ValueError):
            user.custom_attributes["thing"] = [1]

        with assert_raises(ValueError):
            user.custom_attributes["thing"] = {1: 2}

        with assert_raises(ValueError):
            user.custom_attributes = {1: {2: 3}}

        user = User.from_api(get_user())
        with assert_raises(ValueError):
            user.custom_attributes["thing"] = [1]
示例#10
0
    def setUp(self):  # noqa
        self.client = Client()

        created_at = datetime.utcnow()
        params = {
            'email': '*****@*****.**',
            'user_id': 'i-1224242',
            'custom_attributes': {
                'mad': 123,
                'another': 432,
                'other': time.mktime(created_at.timetuple()),
                'thing': 'yay'
            }
        }
        self.user = User(**params)
示例#11
0
 def it_gets_sets_rw_keys(self):
     created_at = datetime.utcnow()
     payload = {
         'email': '*****@*****.**',
         'user_id': 'abc123',
         'name': 'Bob Smith',
         'last_seen_ip': '1.2.3.4',
         'last_seen_user_agent': 'ie6',
         'created_at': calendar.timegm(created_at.utctimetuple())
     }
     user = User(**payload)
     expected_keys = ['custom_attributes']
     expected_keys.extend(list(payload.keys()))
     eq_(sorted(expected_keys), sorted(user.to_dict().keys()))
     for key in list(payload.keys()):
         eq_(payload[key], user.to_dict()[key])
示例#12
0
def test_user_company():
    user = User()
    # use a Company object
    user.company = Company({
        'id': 1,
        'name': 'Intercom',
        'created_at': datetime.fromtimestamp(1331764344)
    })

    # use a dict object
    user.company = {
        'id': 1,
        'name': 'Intercom',
        'created_at': datetime.fromtimestamp(1331764344)
    }
    raises(AttributeError, lambda: user.company)
    user.company = ['foo']
示例#13
0
    def it_saves_a_user_always_sends_custom_attributes(self):

        body = {
            'email': '*****@*****.**',
            'user_id': 'i-1224242',
            'custom_attributes': {}
        }

        with patch.object(Client, 'post', return_value=body) as mock_method:
            user = User(email="*****@*****.**", user_id="i-1224242")
            self.client.users.save(user)
            eq_(user.email, '*****@*****.**')
            eq_(user.custom_attributes, {})
            mock_method.assert_called_once_with(
                '/users',
                {'email': "*****@*****.**", 'user_id': "i-1224242",
                 'custom_attributes': {}})
示例#14
0
 def it_can_save_after_increment(self):  # noqa
     user = User(
         email=None, user_id="i-1224242",
         companies=[{'company_id': 6, 'name': 'Intercom'}])
     body = {
         'custom_attributes': {},
         'email': "",
         'user_id': 'i-1224242',
         'companies': [{
             'company_id': 6,
             'name': 'Intercom'
         }]
     }
     with patch.object(Client, 'post', return_value=body) as mock_method:  # noqa
         user.increment('mad')
         eq_(user.to_dict()['custom_attributes']['mad'], 1)
         self.client.users.save(user)
示例#15
0
def test_user_properties():

    created_at = datetime.fromtimestamp(1331764344)
    last_request_at = datetime.fromtimestamp(1331764345)
    last_impression_at = datetime.fromtimestamp(1331764346)
    user = User()
    user.email = '*****@*****.**'
    user.user_id = 1234
    user.name = 'Somebody'
    user.last_seen_ip = '192.168.1.100'
    user.last_seen_user_agent = 'Mozilla/5.0'
    user.last_request_at = last_request_at
    user.last_impression_at = last_impression_at
    user.created_at = created_at
    user.unsubscribed_from_emails = True
    user.custom_data = {'name': 'Ace'}
    user.companies = [{'id': 1, 'name': 'Intercom', 'created_at': created_at}]
    try:
        # cannot set the relationship score
        user.relationship_score = 50
        raise AttributeError
    except AttributeError:
        pass

    eq_(user.email, '*****@*****.**')
    eq_(user.user_id, 1234)
    eq_(user.name, 'Somebody')
    eq_(user.last_seen_ip, '192.168.1.100')
    eq_(user.last_seen_user_agent, 'Mozilla/5.0')
    eq_(user.last_request_at, last_request_at)
    eq_(user.last_impression_at, last_impression_at)
    eq_(user.relationship_score, None)
    eq_(user.created_at, created_at)
    eq_(user.unsubscribed_from_emails, True)
    eq_(user.custom_data['name'], 'Ace')
    eq_(user.session_count, 0)
    raises(AttributeError, lambda: user.companies)
示例#16
0
    def test_properties(self):
        user = User()
        user.email = '*****@*****.**'
        user.user_id = 1234
        user.name = 'Joe'
        user.last_seen_ip = '192.168.1.100'
        user.last_seen_user_agent = 'Mozilla/5.0'
        user.created_at = datetime.fromtimestamp(1331764344)
        user.custom_data = {'name': 'Ace'}

        try:
            # cannot set the relationship score
            user.relationship_score = 50
        except AttributeError:
            pass

        self.assertEqual('*****@*****.**', user.email)
        self.assertEqual(1234, user.user_id)
        self.assertEqual('Joe', user.name)
        self.assertEqual('192.168.1.100', user.last_seen_ip)
        self.assertEqual('Mozilla/5.0', user.last_seen_user_agent)
        self.assertEqual(None, user.relationship_score)
        self.assertEqual(1331764344, time.mktime(user.created_at.timetuple()))
        self.assertEqual('Ace', user.custom_data['name'])
示例#17
0
 def it_deletes_a_user(self):
     user = User(id="1")
     with patch.object(Client, 'delete', return_value={}) as mock_method:
         user = self.client.users.delete(user)
         eq_(user.id, "1")
         mock_method.assert_called_once_with('/users/1', {})
示例#18
0
 def it_throws_an_attribute_error_on_trying_to_access_an_attribute_that_has_not_been_set(
         self):  # noqa
     with assert_raises(AttributeError):
         user = User()
         user.foo_property
示例#19
0
    def setUp(self):  # noqa
        self.client = Client()

        self.job = {
            "app_id":
            "app_id",
            "id":
            "super_awesome_job",
            "created_at":
            1446033421,
            "completed_at":
            1446048736,
            "closing_at":
            1446034321,
            "updated_at":
            1446048736,
            "name":
            "api_bulk_job",
            "state":
            "completed",
            "links": {
                "error":
                "https://api.intercom.io/jobs/super_awesome_job/error",
                "self": "https://api.intercom.io/jobs/super_awesome_job"
            },
            "tasks": [{
                "id": "super_awesome_task",
                "item_count": 2,
                "created_at": 1446033421,
                "started_at": 1446033709,
                "completed_at": 1446033709,
                "state": "completed"
            }]
        }

        self.bulk_request = {
            "items": [{
                "method": "post",
                "data_type": "user",
                "data": {
                    "user_id": 25,
                    "email": "*****@*****.**"
                }
            }, {
                "method": "delete",
                "data_type": "user",
                "data": {
                    "user_id": 26,
                    "email": "*****@*****.**"
                }
            }]
        }

        self.users_to_create = [{"user_id": 25, "email": "*****@*****.**"}]

        self.users_to_delete = [{"user_id": 26, "email": "*****@*****.**"}]

        created_at = datetime.utcnow()
        params = {
            'email': '*****@*****.**',
            'user_id': 'i-1224242',
            'custom_attributes': {
                'mad': 123,
                'another': 432,
                'other': time.mktime(created_at.timetuple()),
                'thing': 'yay'
            }
        }
        self.user = User(**params)
            'level': 'INFO',
            'propagate': False,
        },
    },
}

dictConfig(LOGGING_CONFIG)

logger = logging.getLogger(__name__)


@pytest.fixture(params=[
    ({
        'side_effect': ResourceNotFound
    }, {
        'return_value': User(email='*****@*****.**')
    }),
    ({
        'return_value': User(email='*****@*****.**')
    }, {}),
])
def mock_intercom(mocker, request):
    mock_events = mocker.patch('intercom.client.Client.events')
    mock_events.create = mocker.MagicMock(return_value=Event(
        event_name='testing'))

    mock_users = mocker.patch('intercom.client.Client.users')

    find_kwargs, create_kwargs = request.param
    mock_users.find = mocker.MagicMock(**find_kwargs)
    mock_users.create = mocker.MagicMock(**create_kwargs)