|
| 1 | +import unittest |
| 2 | +from messagebird import Client, ErrorException |
| 3 | + |
| 4 | +try: |
| 5 | + from unittest.mock import Mock |
| 6 | +except ImportError: |
| 7 | + # mock was added to unittest in Python 3.3, but was an external library |
| 8 | + # before. |
| 9 | + from mock import Mock |
| 10 | + |
| 11 | + |
| 12 | +class TestContact(unittest.TestCase): |
| 13 | + |
| 14 | + def test_contact(self): |
| 15 | + http_client = Mock() |
| 16 | + http_client.request.return_value = '{"id": "contact-id","href": "https://rest.messagebird.com/contacts/contact-id","msisdn": 31612345678,"firstName": "Foo","lastName": "Bar","customDetails": {"custom1": "First","custom2": "Second","custom3": "Third","custom4": "Fourth"},"groups": {"totalCount": 3,"href": "https://rest.messagebird.com/contacts/contact-id/groups"},"messages": {"totalCount": 5,"href": "https://rest.messagebird.com/contacts/contact-id/messages"},"createdDatetime": "2018-07-13T10:34:08+00:00","updatedDatetime": "2018-07-13T10:44:08+00:00"}' |
| 17 | + |
| 18 | + contact = Client('', http_client).contact('contact-id') |
| 19 | + |
| 20 | + http_client.request.assert_called_once_with('contacts/contact-id', 'GET', None) |
| 21 | + |
| 22 | + self.assertEqual(31612345678, contact.msisdn) |
| 23 | + self.assertEqual('First', contact.customDetails.custom1) |
| 24 | + self.assertEqual(3, contact.groups.totalCount) |
| 25 | + self.assertEqual('https://rest.messagebird.com/contacts/contact-id/messages', contact.messages.href) |
| 26 | + |
| 27 | + def test_contact_create(self): |
| 28 | + http_client = Mock() |
| 29 | + http_client.request.return_value = '{}' |
| 30 | + |
| 31 | + Client('', http_client).contact_create(31612345678, {'firstName': 'Foo', 'custom3': 'Third'}) |
| 32 | + |
| 33 | + http_client.request.assert_called_once_with('contacts', 'POST', {'msisdn': 31612345678, 'firstName': 'Foo', 'custom3': 'Third'}) |
| 34 | + |
| 35 | + def test_contact_delete(self): |
| 36 | + http_client = Mock() |
| 37 | + http_client.request.return_value = '' |
| 38 | + |
| 39 | + Client('', http_client).contact_delete('contact-id') |
| 40 | + |
| 41 | + http_client.request.assert_called_once_with('contacts/contact-id', 'DELETE', None) |
| 42 | + |
| 43 | + def test_contact_delete_invalid(self): |
| 44 | + http_client = Mock() |
| 45 | + http_client.request.return_value = '{"errors": [{"code": 20,"description": "contact not found","parameter": null}]}' |
| 46 | + |
| 47 | + with self.assertRaises(ErrorException): |
| 48 | + Client('', http_client).contact_delete('non-existent-contact-id') |
| 49 | + |
| 50 | + http_client.request.assert_called_once_with('contacts/non-existent-contact-id', 'DELETE', None) |
| 51 | + |
| 52 | + def test_contact_update(self): |
| 53 | + http_client = Mock() |
| 54 | + http_client.request.return_value = '' |
| 55 | + |
| 56 | + Client('', http_client).contact_update('contact-id', {'msisdn': 31687654321, 'custom4': 'fourth'}) |
| 57 | + |
| 58 | + http_client.request.assert_called_once_with('contacts/contact-id', 'PATCH', {'msisdn': 31687654321, 'custom4': 'fourth'}) |
| 59 | + |
| 60 | + def test_contact_list(self): |
| 61 | + http_client = Mock() |
| 62 | + http_client.request.return_value = '{"offset": 0,"limit": 20,"count": 2,"totalCount": 2,"links": {"first": "https://rest.messagebird.com/contacts?offset=0","previous": null,"next": null,"last": "https://rest.messagebird.com/contacts?offset=0"},"items": [{"id": "first-id","href": "https://rest.messagebird.com/contacts/first-id","msisdn": 31612345678,"firstName": "Foo","lastName": "Bar","customDetails": {"custom1": null,"custom2": null,"custom3": null,"custom4": null},"groups": {"totalCount": 0,"href": "https://rest.messagebird.com/contacts/first-id/groups"},"messages": {"totalCount": 0,"href": "https://rest.messagebird.com/contacts/first-id/messages"},"createdDatetime": "2018-07-13T10:34:08+00:00","updatedDatetime": "2018-07-13T10:34:08+00:00"},{"id": "second-id","href": "https://rest.messagebird.com/contacts/second-id","msisdn": 49612345678,"firstName": "Hello","lastName": "World","customDetails": {"custom1": null,"custom2": null,"custom3": null,"custom4": null},"groups": {"totalCount": 0,"href": "https://rest.messagebird.com/contacts/second-id/groups"},"messages": {"totalCount": 0,"href": "https://rest.messagebird.com/contacts/second-id/messages"},"createdDatetime": "2018-07-13T10:33:52+00:00","updatedDatetime": null}]}' |
| 63 | + |
| 64 | + contact_list = Client('', http_client).contact_list(10, 20) |
| 65 | + |
| 66 | + http_client.request.assert_called_once_with('contacts?limit=10&offset=20', 'GET', None) |
| 67 | + |
| 68 | + self.assertEqual(2, contact_list.totalCount) |
| 69 | + self.assertEqual('https://rest.messagebird.com/contacts?offset=0', contact_list.links.first) |
| 70 | + self.assertEqual('https://rest.messagebird.com/contacts/first-id/groups', contact_list.items[0].groups.href) |
0 commit comments