Skip to content

Commit 2ca6acd

Browse files
committed
Adding conversation tests for README.md examples.
1 parent f3e2186 commit 2ca6acd

File tree

2 files changed

+132
-1
lines changed

2 files changed

+132
-1
lines changed

intercom/conversation.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from intercom.user import Resource
2+
from intercom.api_operations.find_all import FindAll
23
from intercom.api_operations.find import Find
34
from intercom.api_operations.load import Load
5+
from intercom.api_operations.save import Save
46

57

6-
class Conversation(Resource, Find, Load):
8+
class Conversation(Resource, FindAll, Find, Load, Save):
79
pass
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import os
4+
import unittest
5+
from intercom import Intercom
6+
from intercom.admin import Admin
7+
from intercom.conversation import Conversation
8+
from intercom.user import User
9+
10+
Intercom.app_id = os.environ.get('INTERCOM_APP_ID')
11+
Intercom.app_api_key = os.environ.get('INTERCOM_APP_API_KEY')
12+
13+
14+
class ConversationTest(unittest.TestCase):
15+
email = "ada@example.com"
16+
17+
@classmethod
18+
def setup_class(cls):
19+
# get admin
20+
cls.admin = Admin.all()[0]
21+
# 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()
34+
35+
def test_find_all_admin(self):
36+
# FINDING CONVERSATIONS FOR AN ADMIN
37+
# Iterate over all conversations (open and closed) assigned to an admin
38+
39+
for convo in Conversation.find_all(type='admin', id=self.admin.id):
40+
self.assertIsNotNone(convo.id)
41+
self.__class__.convo_id = convo.id
42+
43+
def test_find_all_open_admin(self):
44+
# Iterate over all open conversations assigned to an admin
45+
for convo in Conversation.find_all(
46+
type='admin', id=self.admin.id, open=True):
47+
self.assertIsNotNone(convo.id)
48+
49+
def test_find_all_closed_admin(self):
50+
# Iterate over closed conversations assigned to an admin
51+
for convo in Conversation.find_all(
52+
type='admin', id=self.admin.id, open=False):
53+
self.assertIsNotNone(convo.id)
54+
55+
def test_find_all_closed_before_admin(self):
56+
for convo in Conversation.find_all(
57+
type='admin', id=self.admin.id, open=False,
58+
before=1374844930):
59+
self.assertIsNotNone(convo.id)
60+
61+
def test_find_all_user(self):
62+
# FINDING CONVERSATIONS FOR A USER
63+
# Iterate over all conversations (read + unread, correct) with a
64+
# user based on the users email
65+
for convo in Conversation.find_all(email=self.email, type='user'):
66+
self.assertIsNotNone(convo.id)
67+
68+
def test_find_all_read(self):
69+
# Iterate over through all conversations (read + unread) with a
70+
# user based on the users email
71+
for convo in Conversation.find_all(
72+
email=self.email, type='user', unread=False):
73+
self.assertIsNotNone(convo.id)
74+
75+
def test_find_all_unread(self):
76+
# Iterate over all unread conversations with a user based on the
77+
# users email
78+
for convo in Conversation.find_all(
79+
email=self.email, type='user', unread=True):
80+
self.assertIsNotNone(convo.id)
81+
82+
def test_find_single_conversation(self):
83+
# FINDING A SINGLE CONVERSATION
84+
convo_id = Conversation.find_all(type='admin', id=self.admin.id)[0].id
85+
conversation = Conversation.find(id=convo_id)
86+
self.assertEqual(conversation.id, convo_id)
87+
88+
def test_conversation_parts(self):
89+
# INTERACTING WITH THE PARTS OF A CONVERSATION
90+
convo_id = Conversation.find_all(type='admin', id=self.admin.id)[0].id
91+
conversation = Conversation.find(id=convo_id)
92+
93+
# Getting the subject of a part (only applies to email-based
94+
# conversations)
95+
self.assertEqual(conversation.conversation_message.subject, "")
96+
# Get the part_type of the first part
97+
self.assertIsNotNone(conversation.conversation_parts[0].part_type)
98+
# Get the body of the second part
99+
self.assertIsNotNone(conversation.conversation_parts[1].body)
100+
101+
# def test_reply(self):
102+
# # REPLYING TO CONVERSATIONS
103+
# convo_id = Conversation.find_all(type='admin', id=self.admin.id)[0].id
104+
# conversation = Conversation.find(id=convo_id)
105+
# num_parts = len(conversation.conversation_parts)
106+
# # User (identified by email) replies with a comment
107+
# conversation.reply(
108+
# type='user', email=self.email,
109+
# message_type='comment', body='foo')
110+
# # Admin (identified by email) replies with a comment
111+
# conversation.reply(
112+
# type='admin', email=self.admin.email,
113+
# message_type='comment', body='bar')
114+
# conversation = Conversation.find(id=convo_id)
115+
# self.assertEqual(num_parts + 2, len(conversation.conversation_parts))
116+
117+
def test_mark_read(self):
118+
# MARKING A CONVERSATION AS READ
119+
convo_id = Conversation.find_all(type='admin', id=self.admin.id)[0].id
120+
conversation = Conversation.find(id=convo_id)
121+
conversation.read = False
122+
conversation.save()
123+
conversation = Conversation.find(id=convo_id)
124+
# CANNOT MARK AS UNREAD VIA API
125+
# self.assertFalse(conversation.read)
126+
conversation.read = True
127+
conversation.save()
128+
conversation = Conversation.find(id=convo_id)
129+
self.assertTrue(conversation.read)

0 commit comments

Comments
 (0)