-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathticket_comment_test.go
129 lines (110 loc) · 3.45 KB
/
ticket_comment_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
package zendesk
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
)
func TestNewPublicTicketComment(t *testing.T) {
publicComment := NewPublicTicketComment("public comment", 12345)
// Both true and nil are public comments
if *publicComment.Public == false {
t.Fatalf("Returned comment is not marked as public. Comment public is %v", *publicComment.Public)
}
}
func TestNewPrivateTicketComment(t *testing.T) {
privateComment := NewPrivateTicketComment("private comment", 12345)
// Both true and nil are public comments
if *privateComment.Public != false {
t.Fatalf("Returned comment is not marked as private. Comment public is %v", *privateComment.Public)
}
}
func TestCreateTicketComment(t *testing.T) {
mockAPI := newMockAPI(http.MethodPut, "ticket.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()
publicComment := NewPublicTicketComment("public comment", 12345)
_, err := client.CreateTicketComment(ctx, 2, publicComment)
if err != nil {
t.Fatalf("Failed to create ticket comment: %s", err)
}
}
func TestListTicketComments(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "ticket_comments.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()
result, err := client.ListTicketComments(ctx, 2, nil)
if err != nil {
t.Fatalf("Failed to list ticket comments: %s", err)
}
expectedLength := 2
if len(result.TicketComments) != expectedLength {
t.Fatalf("Returned ticket comments does not have the expected length %d. Ticket comments length is %d", expectedLength, len(result.TicketComments))
}
expectedPaginationMeta := CursorPaginationMeta{
HasMore: true,
AfterCursor: "xxx",
BeforeCursor: "yyy",
}
if result.Meta != expectedPaginationMeta {
t.Fatalf(`Failed to return correct cursor options.
Expected: %+v
Received: %+v`, expectedPaginationMeta, result.Meta)
}
}
func TestMakeCommentPrivate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
apiReturn error
expectedErrStr string
}{
{
name: "successfully made private",
apiReturn: nil,
expectedErrStr: "",
},
{
name: "error making private",
apiReturn: errors.New(`{"error":"Couldn't authenticate you"}`),
expectedErrStr: `401: {"error":"Couldn't authenticate you"}`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
mockAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if test.apiReturn != nil {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(test.apiReturn.Error()))
} else {
w.WriteHeader(http.StatusOK)
_, _ = w.Write(nil)
}
}))
defer mockAPI.Close()
client := newTestClient(mockAPI)
err := client.MakeCommentPrivate(ctx, 2, 12841284)
if err == nil {
if test.expectedErrStr != "" {
t.Fatalf("Expected error %s, did not get one", test.expectedErrStr)
}
} else {
if test.expectedErrStr != err.Error() {
t.Fatalf("Got %s, wanted %s", err.Error(), test.expectedErrStr)
}
}
})
}
}
func TestRedactTicketComment(t *testing.T) {
mockAPI := newMockAPI(http.MethodPut, "redact_ticket_comment.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()
err := client.RedactTicketComment(ctx, 123, RedactTicketCommentRequest{
TicketID: 100,
HTMLBody: "<div class=\"zd-comment\" dir=\"auto\">My ID number is <redact>847564</redact>!</div>",
})
if err != nil {
t.Fatalf("Failed to redact ticket comment: %s", err)
}
}