-
Notifications
You must be signed in to change notification settings - Fork 1
/
events_test.go
137 lines (113 loc) · 2.99 KB
/
events_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
package plunk
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
var (
testEvent = "test_event"
eventTestEmail = "test@example.com"
)
func TestTriggerEvent(t *testing.T) {
p, err := New(secretKey, opts)
assert.Nil(t, err)
payload := EventPayload{
Event: testEvent,
Email: eventTestEmail,
}
// Test valid payload
resp, err := p.TriggerEvent(payload)
assert.Nil(t, err)
assert.NotNil(t, resp)
_, err = p.DeleteEvent(resp.Event)
assert.Nil(t, err)
// Test missing event
payload = EventPayload{
Email: eventTestEmail,
}
resp, err = p.TriggerEvent(payload)
assert.Equal(t, ErrMissingEvent, err)
assert.Nil(t, resp)
// Test missing email
payload = EventPayload{
Event: testEvent,
}
resp, err = p.TriggerEvent(payload)
assert.Equal(t, ErrMissingEmail, err)
assert.Nil(t, resp)
// Test HTTP request error
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer server.Close()
p.BaseUrl = server.URL
payload = EventPayload{
Event: testEvent,
Email: eventTestEmail,
}
resp, err = p.TriggerEvent(payload)
assert.NotNil(t, err)
assert.Nil(t, resp)
// Test JSON decoding error
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "{invalid json}")
}))
defer server.Close()
p.BaseUrl = server.URL
payload = EventPayload{
Event: testEvent,
Email: eventTestEmail,
}
resp, err = p.TriggerEvent(payload)
assert.NotNil(t, err)
assert.Nil(t, resp)
}
func TestDeleteEvent(t *testing.T) {
p, err := New(secretKey, opts)
assert.Nil(t, err)
payload := EventPayload{
Event: testEvent,
Email: eventTestEmail,
}
resp, err := p.TriggerEvent(payload)
assert.Nil(t, err)
assert.NotNil(t, resp)
id := resp.Event
event, err := p.DeleteEvent(resp.Event)
assert.Nil(t, err)
assert.NotNil(t, event)
assert.Equal(t, id, event.ID)
// Test valid ID
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodDelete, r.Method)
assert.Equal(t, "/events", r.URL.Path)
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"id":"`+id+`"}`)
}))
defer server.Close()
// Test missing ID
event, err = p.DeleteEvent("")
assert.Equal(t, ErrMissingEventID, err)
assert.Nil(t, event)
// Test HTTP request error
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer server.Close()
p.BaseUrl = server.URL
event, err = p.DeleteEvent(id)
assert.NotNil(t, err)
assert.Nil(t, event)
// Test JSON decoding error
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "{invalid json}")
}))
defer server.Close()
p.BaseUrl = server.URL
event, err = p.DeleteEvent(id)
assert.NotNil(t, err)
assert.Nil(t, event)
}