forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
111 lines (87 loc) · 3.6 KB
/
handler_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
package courier_test
import (
"io"
"net/http"
"testing"
"time"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/test"
"github.com/nyaruka/gocommon/httpx"
"github.com/stretchr/testify/assert"
)
func testConfig() *courier.Config {
config := courier.NewConfig()
config.DB = "postgres://courier_test:temba@localhost:5432/courier_test?sslmode=disable"
config.Redis = "redis://localhost:6379/0"
return config
}
func TestHandling(t *testing.T) {
defer httpx.SetRequestor(httpx.DefaultRequestor)
httpx.SetRequestor(httpx.NewMockRequestor(map[string][]*httpx.MockResponse{
"http://mock.com/send": {
httpx.NewMockResponse(200, nil, []byte(`SENT`)),
},
}))
assert := assert.New(t)
// create our backend and server
mb := test.NewMockBackend()
s := courier.NewServer(testConfig(), mb)
// start everything
s.Start()
defer s.Stop()
time.Sleep(100 * time.Millisecond)
// create and add a new outgoing message
brokenChannel := test.NewMockChannel("53e5aafa-8155-449d-9009-fcb30d54bd26", "XX", "2020", "US", map[string]interface{}{})
mockChannel := test.NewMockChannel("e4bb1578-29da-4fa5-a214-9da19dd24230", "MCK", "2020", "US", map[string]interface{}{})
mb.AddChannel(mockChannel)
msg := test.NewMockMsg(courier.MsgID(101), courier.NilMsgUUID, brokenChannel, "tel:+250788383383", "test message")
mb.PushOutgoingMsg(msg)
// sleep a second, sender should take care of it in that time
time.Sleep(time.Second)
// message should have failed because we don't have a registered handler
assert.Equal(1, len(mb.WrittenMsgStatuses()))
assert.Equal(msg.ID(), mb.WrittenMsgStatuses()[0].ID())
assert.Equal(courier.MsgFailed, mb.WrittenMsgStatuses()[0].Status())
assert.Equal(1, len(mb.WrittenChannelLogs()))
mb.Reset()
// change our channel to our dummy channel
msg = test.NewMockMsg(courier.MsgID(102), courier.NilMsgUUID, mockChannel, "tel:+250788383383", "test message 2")
// send it
mb.PushOutgoingMsg(msg)
time.Sleep(time.Second)
// message should be marked as wired
assert.Len(mb.WrittenMsgStatuses(), 1)
status := mb.WrittenMsgStatuses()[0]
assert.Equal(msg.ID(), status.ID())
assert.Equal(courier.MsgSent, status.Status())
assert.Len(mb.WrittenChannelLogs(), 1)
clog := mb.WrittenChannelLogs()[0]
assert.Equal([]*courier.ChannelError{courier.NewChannelError("seeds", "", "contains ********** seeds")}, clog.Errors())
assert.Len(clog.HTTPLogs(), 1)
hlog := clog.HTTPLogs()[0]
assert.Equal("http://mock.com/send", hlog.URL)
assert.Equal("GET /send HTTP/1.1\r\nHost: mock.com\r\nUser-Agent: Go-http-client/1.1\r\nAuthorization: Token **********\r\nAccept-Encoding: gzip\r\n\r\n", hlog.Request)
mb.Reset()
// send the message again, should be skipped but again marked as wired
mb.PushOutgoingMsg(msg)
time.Sleep(time.Second)
// message should be marked as wired
assert.Equal(1, len(mb.WrittenMsgStatuses()))
assert.Equal(msg.ID(), mb.WrittenMsgStatuses()[0].ID())
assert.Equal(courier.MsgWired, mb.WrittenMsgStatuses()[0].Status())
// try to receive a message instead
resp, err := http.Get("http://localhost:8080/c/mck/e4bb1578-29da-4fa5-a214-9da19dd24230/receive")
assert.NoError(err)
assert.Equal(400, resp.StatusCode)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
assert.Contains(string(body), "missing from or text")
req, _ := http.NewRequest("GET", "http://localhost:8080/c/mck/e4bb1578-29da-4fa5-a214-9da19dd24230/receive?from=2065551212&text=hello", nil)
req.Header.Set("Cookie", "secret")
resp, err = http.DefaultClient.Do(req)
assert.NoError(err)
assert.Equal(200, resp.StatusCode)
defer resp.Body.Close()
body, _ = io.ReadAll(resp.Body)
assert.Contains(string(body), "ok")
}