|
5 | 5 | from intercom import Intercom |
6 | 6 | from intercom import Admin |
7 | 7 | from intercom import Conversation |
8 | | -from intercom import User |
| 8 | +from intercom import Message |
| 9 | +from . import delete |
| 10 | +from . import get_or_create_user |
| 11 | +from . import get_timestamp |
9 | 12 |
|
10 | 13 | Intercom.app_id = os.environ.get('INTERCOM_APP_ID') |
11 | 14 | Intercom.app_api_key = os.environ.get('INTERCOM_APP_API_KEY') |
12 | 15 |
|
13 | 16 |
|
14 | 17 | class ConversationTest(unittest.TestCase): |
15 | | - email = "ada@example.com" |
16 | | - |
17 | 18 | @classmethod |
18 | 19 | def setup_class(cls): |
19 | 20 | # get admin |
20 | | - cls.admin = Admin.all()[0] |
| 21 | + cls.admin = Admin.all()[1] |
| 22 | + |
21 | 23 | # get user |
22 | | - cls.user = User.find(email=cls.email) |
23 | | - if not hasattr(cls.user, 'user_id'): |
24 | | - # Create a user |
25 | | - cls.user = User.create( |
26 | | - email=cls.email, |
27 | | - user_id="ada", |
28 | | - name="Ada Lovelace") |
29 | | - cls.user.companies = [ |
30 | | - {"company_id": 6, "name": "Intercom"}, |
31 | | - {"company_id": 9, "name": "Test Company"} |
32 | | - ] |
33 | | - cls.user.save() |
| 24 | + timestamp = get_timestamp() |
| 25 | + cls.user = get_or_create_user(timestamp) |
| 26 | + cls.email = cls.user.email |
| 27 | + |
| 28 | + # send user message |
| 29 | + message_data = { |
| 30 | + 'from': { |
| 31 | + 'type': "user", |
| 32 | + 'id': cls.user.id |
| 33 | + }, |
| 34 | + 'body': "Hey" |
| 35 | + } |
| 36 | + cls.user_message = Message.create(**message_data) |
| 37 | + |
| 38 | + conversations = Conversation.find_all() |
| 39 | + user_init_conv = conversations[0] |
| 40 | + # send admin reply |
| 41 | + cls.admin_conv = user_init_conv.reply( |
| 42 | + type='admin', admin_id=cls.admin.id, |
| 43 | + message_type='comment', body='There') |
| 44 | + |
| 45 | + @classmethod |
| 46 | + def teardown_class(cls): |
| 47 | + delete(cls.user) |
34 | 48 |
|
35 | 49 | def test_find_all_admin(self): |
36 | 50 | # FINDING CONVERSATIONS FOR AN ADMIN |
37 | 51 | # Iterate over all conversations (open and closed) assigned to an admin |
38 | | - |
39 | 52 | for convo in Conversation.find_all(type='admin', id=self.admin.id): |
40 | 53 | self.assertIsNotNone(convo.id) |
41 | | - self.__class__.convo_id = convo.id |
| 54 | + self.admin_conv.id = convo.id |
42 | 55 |
|
43 | 56 | def test_find_all_open_admin(self): |
44 | 57 | # Iterate over all open conversations assigned to an admin |
@@ -99,33 +112,29 @@ def test_conversation_parts(self): |
99 | 112 | # There is a body |
100 | 113 | self.assertIsNotNone(part.body) |
101 | 114 |
|
102 | | - # def test_reply(self): |
103 | | - # # REPLYING TO CONVERSATIONS |
104 | | - # convo_id = Conversation.find_all( |
105 | | - # type='admin', id=self.admin.id)[0].id |
106 | | - # conversation = Conversation.find(id=convo_id) |
107 | | - # num_parts = len(conversation.conversation_parts) |
108 | | - # # User (identified by email) replies with a comment |
109 | | - # conversation.reply( |
110 | | - # type='user', email=self.email, |
111 | | - # message_type='comment', body='foo') |
112 | | - # # Admin (identified by email) replies with a comment |
113 | | - # conversation.reply( |
114 | | - # type='admin', email=self.admin.email, |
115 | | - # message_type='comment', body='bar') |
116 | | - # conversation = Conversation.find(id=convo_id) |
117 | | - # self.assertEqual(num_parts + 2, len(conversation.conversation_parts)) |
| 115 | + def test_reply(self): |
| 116 | + # REPLYING TO CONVERSATIONS |
| 117 | + conversation = Conversation.find(id=self.admin_conv.id) |
| 118 | + num_parts = len(conversation.conversation_parts) |
| 119 | + # User (identified by email) replies with a comment |
| 120 | + conversation.reply( |
| 121 | + type='user', email=self.email, |
| 122 | + message_type='comment', body='foo') |
| 123 | + # Admin (identified by admin_id) replies with a comment |
| 124 | + conversation.reply( |
| 125 | + type='admin', admin_id=self.admin.id, |
| 126 | + message_type='comment', body='bar') |
| 127 | + conversation = Conversation.find(id=self.admin_conv.id) |
| 128 | + self.assertEqual(num_parts + 2, len(conversation.conversation_parts)) |
118 | 129 |
|
119 | 130 | def test_mark_read(self): |
120 | 131 | # MARKING A CONVERSATION AS READ |
121 | | - convo_id = Conversation.find_all(type='admin', id=self.admin.id)[0].id |
122 | | - conversation = Conversation.find(id=convo_id) |
| 132 | + conversation = Conversation.find(id=self.admin_conv.id) |
123 | 133 | conversation.read = False |
124 | 134 | conversation.save() |
125 | | - conversation = Conversation.find(id=convo_id) |
126 | | - # CANNOT MARK AS UNREAD VIA API |
127 | | - # self.assertFalse(conversation.read) |
| 135 | + conversation = Conversation.find(id=self.admin_conv.id) |
| 136 | + self.assertFalse(conversation.read) |
128 | 137 | conversation.read = True |
129 | 138 | conversation.save() |
130 | | - conversation = Conversation.find(id=convo_id) |
| 139 | + conversation = Conversation.find(id=self.admin_conv.id) |
131 | 140 | self.assertTrue(conversation.read) |
0 commit comments