This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
forked from jazibjohar/intercom-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversation_test.go
187 lines (169 loc) · 6.26 KB
/
conversation_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package intercom
import "testing"
func TestFindConversation(t *testing.T) {
conversationService := ConversationService{Repository: TestConversationAPI{t: t}}
convo, _ := conversationService.Find("123")
if convo.ID != "123" {
t.Errorf("Did not receive conversation")
}
}
func TestReadConversation(t *testing.T) {
conversationService := ConversationService{Repository: TestConversationAPI{t: t}}
convo, _ := conversationService.MarkRead("123")
if convo.ID != "123" {
t.Errorf("Did not receive conversation")
}
}
func TestReplyConversationComment(t *testing.T) {
testAPI := TestConversationAPI{t: t}
testAPI.testFunc = func(t *testing.T, reply interface{}) {
if reply.(*Reply).IntercomID != "abc123" {
t.Errorf("user id not supplied")
}
if reply.(*Reply).ReplyType != "comment" {
t.Errorf("part was not comment, was %s", reply.(*Reply).ReplyType)
}
}
conversationService := ConversationService{Repository: testAPI}
conversationService.Reply("123", &User{ID: "abc123"}, CONVERSATION_COMMENT, "Body")
}
func TestReplyConversationCommentWithAttachment(t *testing.T) {
testAPI := TestConversationAPI{t: t}
testAPI.testFunc = func(t *testing.T, reply interface{}) {
if reply.(*Reply).IntercomID != "abc123" {
t.Errorf("user id not supplied")
}
if reply.(*Reply).ReplyType != "comment" {
t.Errorf("part was not comment, was %s", reply.(*Reply).ReplyType)
}
}
conversationService := ConversationService{Repository: testAPI}
conversationService.ReplyWithAttachmentURLs("123", &User{ID: "abc123"}, CONVERSATION_COMMENT, "Body", []string{"http://www.example.com/attachment.jpg"})
}
func TestReplyConversationOpen(t *testing.T) {
testAPI := TestConversationAPI{t: t}
testAPI.testFunc = func(t *testing.T, reply interface{}) {
if reply.(*Reply).IntercomID != "abc123" {
t.Errorf("user id not supplied")
}
if reply.(*Reply).ReplyType != "open" {
t.Errorf("part was not open, was %s", reply.(*Reply).ReplyType)
}
}
conversationService := ConversationService{Repository: testAPI}
conversationService.Reply("123", &User{ID: "abc123"}, CONVERSATION_OPEN, "Body")
}
func TestReplyConversationNote(t *testing.T) {
testAPI := TestConversationAPI{t: t}
testAPI.testFunc = func(t *testing.T, reply interface{}) {
if reply.(*Reply).AdminID != "abc123" {
t.Errorf("admin id not supplied")
}
if reply.(*Reply).ReplyType != "note" {
t.Errorf("part was not note, was %s", reply.(*Reply).ReplyType)
}
}
conversationService := ConversationService{Repository: testAPI}
conversationService.Reply("123", &Admin{ID: "abc123"}, CONVERSATION_NOTE, "Body")
}
func TestAssignConversation(t *testing.T) {
testAPI := TestConversationAPI{t: t}
testAPI.testFunc = func(t *testing.T, reply interface{}) {
if reply.(*Reply).AssigneeID != "def789" {
t.Errorf("assignee_id not supplied")
}
if reply.(*Reply).AdminID != "abc123" {
t.Errorf("admin id was not supplied")
}
if reply.(*Reply).ReplyType != "assignment" {
t.Errorf("part was not assignment, was %s", reply.(*Reply).ReplyType)
}
}
conversationService := ConversationService{Repository: testAPI}
conversationService.Assign("123", &Admin{ID: "abc123"}, &Admin{ID: "def789"})
}
func TestListAllConversations(t *testing.T) {
conversationService := ConversationService{Repository: TestConversationAPI{t: t}}
list, _ := conversationService.ListAll(PageParams{})
if list.Conversations[0].ID != "123" {
t.Errorf("did not receive conversation")
}
}
func TestListUserConversationsUnread(t *testing.T) {
testAPI := TestConversationAPI{t: t}
testAPI.testFunc = func(t *testing.T, params interface{}) {
if *params.(conversationListParams).Unread != true {
t.Errorf("unread was %v, expected true", *params.(conversationListParams).Unread)
}
}
conversationService := ConversationService{Repository: testAPI}
user := User{}
list, _ := conversationService.ListByUser(&user, SHOW_UNREAD, PageParams{})
if list.Conversations[0].ID != "123" {
t.Errorf("did not receive conversation")
}
}
func TestListUserConversationsAll(t *testing.T) {
testAPI := TestConversationAPI{t: t}
testAPI.testFunc = func(t *testing.T, params interface{}) {
if params.(conversationListParams).Unread != nil {
t.Errorf("unread was not nil, was %v", *params.(conversationListParams).Unread)
}
}
conversationService := ConversationService{Repository: testAPI}
user := User{}
list, _ := conversationService.ListByUser(&user, SHOW_ALL, PageParams{})
if list.Conversations[0].ID != "123" {
t.Errorf("did not receive conversation")
}
}
func TestListAdminConversationsAll(t *testing.T) {
testAPI := TestConversationAPI{t: t}
testAPI.testFunc = func(t *testing.T, params interface{}) {
if params.(conversationListParams).Open != nil {
t.Errorf("open was not nil, was %v", *params.(conversationListParams).Open)
}
}
conversationService := ConversationService{Repository: testAPI}
admin := Admin{}
list, _ := conversationService.ListByAdmin(&admin, SHOW_ALL, PageParams{})
if list.Conversations[0].ID != "123" {
t.Errorf("did not receive conversation")
}
}
func TestListAdminConversationsOpen(t *testing.T) {
testAPI := TestConversationAPI{t: t}
testAPI.testFunc = func(t *testing.T, params interface{}) {
if *params.(conversationListParams).Open != true {
t.Errorf("open was not true, was %v", *params.(conversationListParams).Open)
}
}
conversationService := ConversationService{Repository: testAPI}
admin := Admin{}
list, _ := conversationService.ListByAdmin(&admin, SHOW_OPEN, PageParams{})
if list.Conversations[0].ID != "123" {
t.Errorf("did not receive conversation")
}
}
type TestConversationAPI struct {
testFunc func(t *testing.T, params interface{})
t *testing.T
}
func (t TestConversationAPI) list(params conversationListParams) (ConversationList, error) {
if t.testFunc != nil {
t.testFunc(t.t, params)
}
return ConversationList{Conversations: []Conversation{Conversation{ID: "123"}}, Pages: PageParams{Page: 1, PerPage: 20}}, nil
}
func (t TestConversationAPI) find(id string) (Conversation, error) {
return Conversation{ID: "123"}, nil
}
func (t TestConversationAPI) read(id string) (Conversation, error) {
return Conversation{ID: "123"}, nil
}
func (t TestConversationAPI) reply(id string, reply *Reply) (Conversation, error) {
if t.testFunc != nil {
t.testFunc(t.t, reply)
}
return Conversation{ID: "123"}, nil
}