forked from intercom/intercom-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversation_api_test.go
146 lines (135 loc) · 4.81 KB
/
conversation_api_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
package intercom
import (
"io/ioutil"
"testing"
)
func TestConversationFind(t *testing.T) {
http := TestConversationHTTPClient{t: t, expectedURI: "/conversations/147", fixtureFilename: "fixtures/conversation.json"}
api := ConversationAPI{httpClient: &http}
convo, _ := api.find("147")
if convo.ID != "147" {
t.Errorf("Conversation not retrieved, %s", convo.ID)
}
if convo.TagList == nil || convo.TagList.Tags[0].ID != "12345" {
t.Errorf("Conversation tags not retrieved, %s", convo.ID)
}
}
func TestConversationRead(t *testing.T) {
http := TestConversationHTTPClient{t: t, expectedURI: "/conversations/147", fixtureFilename: "fixtures/conversation.json"}
http.testFunc = func(t *testing.T, readRequest interface{}) {
req := readRequest.(conversationReadRequest)
if req.Read != true {
t.Errorf("read was not marked true")
}
}
api := ConversationAPI{httpClient: &http}
convo, err := api.read("147")
if err != nil {
t.Errorf("%v", err)
}
if convo.ID != "147" {
t.Errorf("Conversation not retrieved, %s", convo.ID)
}
}
func TestConversationReply(t *testing.T) {
http := TestConversationHTTPClient{t: t, expectedURI: "/conversations/147/reply", fixtureFilename: "fixtures/conversation.json"}
http.testFunc = func(t *testing.T, replyRequest interface{}) {
reply := replyRequest.(*Reply)
if reply.ReplyType != CONVERSATION_NOTE.String() {
t.Errorf("Reply was not note")
}
}
api := ConversationAPI{httpClient: &http}
convo, err := api.reply("147", &Reply{ReplyType: CONVERSATION_NOTE.String(), AdminID: "123"})
if err != nil {
t.Errorf("%v", err)
}
if convo.ID != "147" {
t.Errorf("Conversation not retrieved, %s", convo.ID)
}
}
func TestConversationReplyWithAttachment(t *testing.T) {
http := TestConversationHTTPClient{t: t, expectedURI: "/conversations/147/reply", fixtureFilename: "fixtures/conversation.json"}
http.testFunc = func(t *testing.T, replyRequest interface{}) {
reply := replyRequest.(*Reply)
if reply.ReplyType != CONVERSATION_COMMENT.String() {
t.Errorf("Reply was not comment")
}
}
api := ConversationAPI{httpClient: &http}
convo, err := api.reply("147", &Reply{ReplyType: CONVERSATION_COMMENT.String(), AdminID: "123", AttachmentURLs: []string{"http://www.example.com/attachment.jpg"}})
if err != nil {
t.Errorf("%v", err)
}
if convo.ID != "147" {
t.Errorf("Conversation not retrieved, %s", convo.ID)
}
}
func TestConversationListAll(t *testing.T) {
http := TestConversationHTTPClient{t: t, expectedURI: "/conversations", fixtureFilename: "fixtures/conversations.json"}
api := ConversationAPI{httpClient: &http}
convos, _ := api.list(conversationListParams{})
if convos.Conversations[0].ID != "147" {
t.Errorf("Conversation not retrieved")
}
if convos.Conversations[0].User.ID != "536e564f316c83104c000020" {
t.Errorf("Conversation user not retrieved")
}
if convos.Conversations[0].ConversationMessage.Author.ID != "25" {
t.Errorf("Conversation Message Author not retrieved")
}
if convos.Conversations[0].ConversationParts.Parts[0].CreatedAt != 1400857494 {
t.Errorf("Conversation Part CreatedAt not retrieved")
}
if convos.Conversations[0].TagList != nil {
t.Errorf("Conversation Tags should be nil")
}
}
func TestConversationListUserUnread(t *testing.T) {
http := TestConversationHTTPClient{t: t, expectedURI: "/conversations", fixtureFilename: "fixtures/conversations.json"}
http.testFunc = func(t *testing.T, queryParams interface{}) {
ps := queryParams.(conversationListParams)
if *ps.Unread != true {
t.Errorf("Expect unread parameter, got %v", *ps.Unread)
}
}
api := ConversationAPI{httpClient: &http}
api.list(conversationListParams{Unread: Bool(true)})
}
func TestConversationListAdminOpen(t *testing.T) {
http := TestConversationHTTPClient{t: t, expectedURI: "/conversations", fixtureFilename: "fixtures/conversations.json"}
http.testFunc = func(t *testing.T, queryParams interface{}) {
ps := queryParams.(conversationListParams)
if *ps.Open != true {
t.Errorf("Expect open parameter, got %v", *ps.Unread)
}
}
api := ConversationAPI{httpClient: &http}
api.list(conversationListParams{Open: Bool(true)})
}
type TestConversationHTTPClient struct {
TestHTTPClient
t *testing.T
testFunc func(t *testing.T, queryParams interface{})
fixtureFilename string
expectedURI string
lastQueryParams interface{}
}
func (t *TestConversationHTTPClient) Get(uri string, queryParams interface{}) ([]byte, error) {
if t.testFunc != nil {
t.testFunc(t.t, queryParams)
}
if t.expectedURI != uri {
t.t.Errorf("Wrong endpoint called")
}
return ioutil.ReadFile(t.fixtureFilename)
}
func (t *TestConversationHTTPClient) Post(uri string, dataObject interface{}) ([]byte, error) {
if t.testFunc != nil {
t.testFunc(t.t, dataObject)
}
if t.expectedURI != uri {
t.t.Errorf("Wrong endpoint called")
}
return ioutil.ReadFile(t.fixtureFilename)
}