-
Notifications
You must be signed in to change notification settings - Fork 0
/
twilio_test.go
148 lines (117 loc) · 4.09 KB
/
twilio_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
package signup
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// Magic Test Numbers
// https://www.twilio.com/docs/iam/test-credentials#magic-input
func setEnv() {
os.Setenv("TWILIO_ACCOUNT_SID", "testAccountSID")
os.Setenv("TWILIO_AUTH_TOKEN", "testAuthToken")
os.Setenv("TWILIO_PHONE_NUMBER", "+15005550006")
os.Setenv("TWILIO_CONVERSATIONS_SID", "CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
}
func TestSendSMSInConversation(t *testing.T) {
t.Skip("TODO")
t.Run("sends an SMS message through the Conversations API", func(t *testing.T) {
accountSID := os.Getenv("TWILIO_ACCOUNT_SID")
authToken := os.Getenv("TWILIO_AUTH_TOKEN")
fromPhoneNum := os.Getenv("TWILIO_PHONE_NUMBER")
conversationSid := os.Getenv("TWILIO_CONVERSATIONS_SID")
mockTwilioAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(128)
require.NoError(t, err)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, http.StatusBadRequest)
fmt.Fprint(w, bytes.NewBufferString(`{
"code": 50407,
"message": "Invalid messaging binding address",
"more_info": "https://www.twilio.com/docs/errors/50407",
"status": 400
}`))
}))
tSvc := NewTwilioService(twilioServiceOptions{
accountSID: accountSID,
authToken: authToken,
fromPhoneNum: fromPhoneNum,
apiBase: mockTwilioAPI.URL,
})
messageBody := "Welcome to Op Spark! Click this link for more info: https://opsk.org/bh213v34fa"
err := tSvc.sendSMSInConversation(messageBody, conversationSid)
require.NoErrorf(t, err, "twilio service: sendSMS: %v")
})
}
func TestFindConversationsByNumber(t *testing.T) {
t.Skip("TODO")
t.Run("finds all the conversation for the given phone number", func(t *testing.T) {
setEnv()
tSvc := NewTwilioService(twilioServiceOptions{
accountSID: os.Getenv("TWILIO_ACCOUNT_SID"),
authToken: os.Getenv("TWILIO_AUTH_TOKEN"),
fromPhoneNum: os.Getenv("TWILIO_PHONE_NUMBER"),
})
_, err := tSvc.findConversationsByNumber("+15005550006")
require.NoError(t, err)
fmt.Println(tSvc.apiBase)
})
}
func TestTwilioRun(t *testing.T) {
t.Skip("TODO")
t.Run("creates a new conversation when sending new messages", func(t *testing.T) {
tSvc := NewTwilioService(twilioServiceOptions{
accountSID: os.Getenv("TWILIO_ACCOUNT_SID"),
authToken: os.Getenv("TWILIO_AUTH_TOKEN"),
fromPhoneNum: os.Getenv("TWILIO_PHONE_NUMBER"),
conversationsSid: os.Getenv("TWILIO_CONVERSATIONS_SID"),
})
su := Signup{
NameFirst: "Rick",
NameLast: "Sanchez",
Cell: "+15005550006",
Cohort: "is-oct-17-22-5-30pm",
StartDateTime: mustMakeTime(t, time.RFC3339, "2022-10-17T22:30:00.000Z"),
}
err := tSvc.run(context.Background(), su)
require.NoError(t, err)
})
}
func TestInvalidNumErr(t *testing.T) {
// test that the error is returned when the number is invalid
t.Run("returns an error when the number is invalid", func(t *testing.T) {
signup := Signup{
NameFirst: "Henri",
NameLast: "Testaroni",
Email: "henri@email.com",
Cell: "555-555-5555",
Referrer: "instagram",
ReferrerResponse: "",
}
service := &MockSignupService{
RegisterFunc: func(ctx context.Context, su Signup) (Signup, error) {
// return invalid number error
return su, ErrInvalidNumber{err: fmt.Errorf("invalid number: %s", signup.Cell)}
},
}
server := &signupServer{service}
req := httptest.NewRequest(http.MethodPost, "/", signupToJson(t, signup))
req.Header.Set("Content-Type", "application/json")
res := httptest.NewRecorder()
server.HandleSignUp(res, req)
// check that the response is a 400
require.Equal(t, http.StatusBadRequest, res.Code)
// check that the response body is the expected error
var errResp errResp
err := json.Unmarshal([]byte(res.Body.Bytes()), &errResp)
require.NoError(t, err)
want := fmt.Sprintln(`{"message":"Invalid Phone Number","field":"phone"}`)
require.Equal(t, want, res.Body.String())
})
}