|
| 1 | +import unittest |
| 2 | +from messagebird import Client |
| 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 TestGroup(unittest.TestCase): |
| 13 | + |
| 14 | + def test_group(self): |
| 15 | + http_client = Mock() |
| 16 | + http_client.request.return_value = '{"id": "group-id","href": "https://rest.messagebird.com/groups/group-id","name": "Friends","contacts": {"totalCount": 3,"href": "https://rest.messagebird.com/groups/group-id"},"createdDatetime": "2018-07-25T12:16:10+00:00","updatedDatetime": "2018-07-25T12:16:23+00:00"}' |
| 17 | + |
| 18 | + group = Client('', http_client).group('group-id') |
| 19 | + |
| 20 | + http_client.request.assert_called_once_with('groups/group-id', 'GET', None) |
| 21 | + |
| 22 | + self.assertEqual('Friends', group.name) |
| 23 | + self.assertEqual(3, group.contacts.totalCount) |
| 24 | + |
| 25 | + def test_group_create(self): |
| 26 | + http_client = Mock() |
| 27 | + http_client.request.return_value = '{}' |
| 28 | + |
| 29 | + Client('', http_client).group_create('Family', {'foo': 'bar'}) |
| 30 | + |
| 31 | + http_client.request.assert_called_once_with('groups', 'POST', {'name': 'Family', 'foo': 'bar'}) |
| 32 | + |
| 33 | + def test_group_delete(self): |
| 34 | + http_client = Mock() |
| 35 | + http_client.request.return_value = '' |
| 36 | + |
| 37 | + Client('', http_client).group_delete('group-id') |
| 38 | + |
| 39 | + http_client.request.assert_called_once_with('groups/group-id', 'DELETE', None) |
| 40 | + |
| 41 | + def test_list(self): |
| 42 | + http_client = Mock() |
| 43 | + http_client.request.return_value = '{"offset": 0,"limit": 25,"count": 2,"totalCount": 2,"links": {"first": "https://rest.messagebird.com/groups?offset=0&limit=10","previous": null,"next": null,"last": "https://rest.messagebird.com/groups?offset=0&limit=10"},"items": [{"id": "first-id","href": "https://rest.messagebird.com/groups/first-id","name": "First","contacts": {"totalCount": 3,"href": "https://rest.messagebird.com/groups/first-id/contacts"},"createdDatetime": "2018-07-25T11:47:42+00:00","updatedDatetime": "2018-07-25T14:03:09+00:00"},{"id": "second-id","href": "https://rest.messagebird.com/groups/second-id","name": "Second","contacts": {"totalCount": 4,"href": "https://rest.messagebird.com/groups/second-id/contacts"},"createdDatetime": "2018-07-25T11:47:39+00:00","updatedDatetime": "2018-07-25T14:03:09+00:00"}]}' |
| 44 | + |
| 45 | + group_list = Client('', http_client).group_list(limit=25, offset=0) |
| 46 | + |
| 47 | + http_client.request.assert_called_once_with('groups?limit=25&offset=0', 'GET', None) |
| 48 | + |
| 49 | + self.assertEqual(2, group_list.totalCount) |
| 50 | + self.assertEqual('https://rest.messagebird.com/groups?offset=0&limit=10', group_list.links.first) |
| 51 | + self.assertEqual('Second', group_list.items[1].name) |
| 52 | + |
| 53 | + def test_group_update(self): |
| 54 | + http_client = Mock() |
| 55 | + http_client.request.return_value = '' |
| 56 | + |
| 57 | + Client('', http_client).group_update('group-id', 'friends') |
| 58 | + |
| 59 | + http_client.request.assert_called_once_with('groups/group-id', 'PATCH', {'name': 'friends'}) |
| 60 | + |
| 61 | + def test_group_add_contacts(self): |
| 62 | + http_client = Mock() |
| 63 | + http_client.request.return_value = '' |
| 64 | + |
| 65 | + Client('', http_client).group_add_contacts('group-id', ['contact-id', 'other-contact-id']) |
| 66 | + |
| 67 | + http_client.request.assert_called_once_with('groups/group-id?ids[]=contact-id&ids[]=other-contact-id', 'PUT', None) |
| 68 | + |
| 69 | + def test_group_remove_contact(self): |
| 70 | + http_client = Mock() |
| 71 | + http_client.request.return_value = '' |
| 72 | + |
| 73 | + Client('', http_client).group_remove_contact('group-id', 'contact-id') |
| 74 | + |
| 75 | + http_client.request.assert_called_once_with('groups/group-id/contacts/contact-id', 'DELETE', None) |
0 commit comments