-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathinvitations_test.go
104 lines (86 loc) · 2.61 KB
/
invitations_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
package mackerel
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/kylelemons/godebug/pretty"
)
func TestFindInvitation(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/invitations" {
t.Error("request URL should be but: ", req.URL.Path)
}
if req.Method != "GET" {
t.Error("request method should be GET but: ", req.Method)
}
respJSON, _ := json.Marshal(map[string][]map[string]interface{}{
"invitations": {
{
"email": "[email protected]",
"authority": "viewer",
"expiresAt": 1560000000,
},
},
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
invitations, err := client.FindInvitations()
if err != nil {
t.Error("err should be nil but: ", err)
}
if invitations[0].Email != "[email protected]" {
t.Error("request sends json including email but: ", invitations[0].Email)
}
if invitations[0].Authority != "viewer" {
t.Error("request sends json including authority but: ", invitations[0].Authority)
}
if invitations[0].ExpiresAt != 1560000000 {
t.Error("request sends json including joinedAt but: ", invitations[0].ExpiresAt)
}
}
func TestCreateInvitation(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/invitations" {
t.Error("request URL should be but: ", req.URL.Path)
}
if req.Method != http.MethodPost {
t.Error("request method should be POST but: ", req.Method)
}
body, _ := io.ReadAll(req.Body)
var invitation Invitation
if err := json.Unmarshal(body, &invitation); err != nil {
t.Fatal("request body should be decoded as json", string(body))
}
respJSON, _ := json.Marshal(map[string]interface{}{
"email": "[email protected]",
"authority": "viewer",
"expiresAt": 1560000000,
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
param := &Invitation{
Email: "[email protected]",
Authority: "viewer",
}
got, err := client.CreateInvitation(param)
if err != nil {
t.Error("err should be nil but: ", err)
}
want := &Invitation{
Email: "[email protected]",
Authority: "viewer",
ExpiresAt: 1560000000,
}
if diff := pretty.Compare(got, want); diff != "" {
t.Errorf("fail to get correct data: diff (-got +want)\n%s", diff)
}
}