-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreenlight_test.go
62 lines (51 loc) · 1.6 KB
/
greenlight_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
package signup
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestPostWebhook(t *testing.T) {
t.Run("POSTs a webhook to Greenlight with the signup payload", func(t *testing.T) {
apiKey := "test-api-key"
sessionStartDate, _ := time.Parse(time.RFC822, "14 Mar 22 17:00 UTC")
su := Signup{
NameFirst: "Bob",
NameLast: "Ross",
Email: "[email protected]",
Cell: "555-123-4567",
Referrer: "instagram",
ReferrerResponse: "",
StartDateTime: sessionStartDate,
Cohort: "is-mar-14-22-12pm",
SessionID: "X5TsABhN94yesyMEi",
UserLocation: "Louisiana",
AttendingLocation: "IN_PERSON",
}
mockGreenlightSvr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Sends API Key header
require.Equal(t, apiKey, r.Header.Get("X-Greenlight-Signup-Api-Key"))
// POSTs correct JSON body
var glReq Signup
d := json.NewDecoder(r.Body)
err := d.Decode(&glReq)
require.NoError(t, err)
require.Equal(t, su, glReq)
// Responds with a signup ID
resp := signupResp{
Status: "success",
SignupID: "a-new-signup-id-from-greenlight",
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(resp)
}))
glSvc := NewGreenlightService(mockGreenlightSvr.URL, apiKey)
err := glSvc.postWebhook(context.Background(), &su)
require.NoError(t, err)
require.Equal(t, "a-new-signup-id-from-greenlight", *su.id)
})
}