forked from intercom/intercom-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
notification_test.go
152 lines (138 loc) · 3.25 KB
/
notification_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
package intercom
import (
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
)
var errTest = errors.New("Test Error")
type alwaysErrorReader struct{}
func (r *alwaysErrorReader) Read(p []byte) (n int, err error) {
return 0, errTest
}
func TestParsingFromReader(t *testing.T) {
r, _ := os.Open("fixtures/notification.json")
n, _ := NewNotification(r)
if n.ID != "notif_ccd8a4d0-f965-11e3-a367-c779cae3e1b3" {
t.Errorf("Notification did not have ID")
}
if n.CreatedAt != 1392731331 {
t.Errorf("Notification did not have CreatedAt")
}
if n.DeliveryAttempts != 1 {
t.Errorf("Notification did not have DeliveryAttempts")
}
if n.Topic != "company.created" {
t.Errorf("Notification did not have Topic")
}
if n.FirstSentAt != 1392731392 {
t.Errorf("Notification did not have FirstSentAt")
}
if n.Company == nil {
t.Errorf("Notification did not have Company")
}
if n.Conversation != nil {
t.Errorf("Notification should not have Conversation")
}
if n.Event != nil {
t.Errorf("Notification should not have Event")
}
if n.Tag != nil {
t.Errorf("Notification should not have Tag")
}
if n.User != nil {
t.Errorf("Notification shoud not have User")
}
}
func TestParsingError(t *testing.T) {
r := &alwaysErrorReader{}
_, err := NewNotification(r)
if err == nil {
t.Errorf("Error not returned")
}
}
func TestParsingConverationFromReader(t *testing.T) {
topics := []string{
"conversation.user.created",
"conversation.user.replied",
"conversation.admin.replied",
"conversation.admin.single.created",
"conversation.admin.assigned",
"conversation.admin.noted",
"conversation.admin.closed",
"conversation.admin.opened",
}
for _, topic := range topics {
payload, _ := ioutil.ReadFile("fixtures/conversation.json")
r := strings.NewReader(fmt.Sprintf(`{
"topic": "%s",
"data": {
"item": %s
}
}`, topic, string(payload)))
n, _ := NewNotification(r)
if n.Conversation == nil {
t.Errorf("Notification did not have Conversation")
}
}
}
func TestParsingUserFromReader(t *testing.T) {
topics := []string{
"user.created",
"user.deleted",
"user.unsubscribed",
"user.email.updated",
}
for _, topic := range topics {
payload, _ := ioutil.ReadFile("fixtures/user.json")
r := strings.NewReader(fmt.Sprintf(`{
"topic": "%s",
"data": {
"item": %s
}
}`, topic, string(payload)))
n, _ := NewNotification(r)
if n.User == nil {
t.Errorf("Notification did not have User")
}
}
}
func TestParsingTagFromReader(t *testing.T) {
topics := []string{
"user.tag.created",
"user.tag.deleted",
}
for _, topic := range topics {
payload, _ := ioutil.ReadFile("fixtures/tag.json")
r := strings.NewReader(fmt.Sprintf(`{
"topic": "%s",
"data": {
"item": %s
}
}`, topic, string(payload)))
n, _ := NewNotification(r)
if n.Tag == nil {
t.Errorf("Notification did not have Tag")
}
}
}
func TestParsingEventFromReader(t *testing.T) {
topics := []string{
"event.created",
}
for _, topic := range topics {
payload, _ := ioutil.ReadFile("fixtures/event.json")
r := strings.NewReader(fmt.Sprintf(`{
"topic": "%s",
"data": {
"item": %s
}
}`, topic, string(payload)))
n, _ := NewNotification(r)
if n.Event == nil {
t.Errorf("Notification did not have Event")
}
}
}