forked from unee-t/invite
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinvite_test.go
108 lines (96 loc) · 2.06 KB
/
invite_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
package invite
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
_ "github.com/go-sql-driver/mysql"
)
type checkFunc func(*httptest.ResponseRecorder) error
func hasStatus(want int) checkFunc {
return func(rec *httptest.ResponseRecorder) error {
if rec.Code != want {
return fmt.Errorf("expected status %d, found %d", want, rec.Code)
}
return nil
}
}
var h handler
func TestMain(m *testing.M) {
h, _ = New(context.Background())
defer h.DB.Close()
os.Exit(m.Run())
}
func Test_handler_lookupRoleID(t *testing.T) {
type args struct {
roleName string
}
tests := []struct {
name string
args args
wantIDRoleType int
wantErr bool
}{{
"Check Owner/Landlord is 2",
args{roleName: "Owner/Landlord"},
2,
false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotIDRoleType, err := h.lookupRoleID(tt.args.roleName)
if (err != nil) != tt.wantErr {
t.Errorf("handler.lookupRoleID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotIDRoleType != tt.wantIDRoleType {
t.Errorf("handler.lookupRoleID() = %v, want %v", gotIDRoleType, tt.wantIDRoleType)
}
})
}
}
func TestPush(t *testing.T) {
check := func(fns ...checkFunc) []checkFunc { return fns }
payload := func(u []Invite) io.Reader {
requestByte, _ := json.Marshal(u)
return bytes.NewReader(requestByte)
}
tests := [...]struct {
name string
verb string
path string
payload io.Reader
h func(w http.ResponseWriter, r *http.Request)
checks []checkFunc
}{
{
"Empty payload",
"POST",
"/create",
payload([]Invite{}),
h.handlePush,
check(hasStatus(400)),
},
}
for _, tt := range tests {
r, err := http.NewRequest(tt.verb, tt.path, tt.payload)
if err != nil {
t.Fatal(err)
}
t.Run(tt.name, func(t *testing.T) {
h := http.HandlerFunc(tt.h)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, r)
for _, check := range tt.checks {
if err := check(rec); err != nil {
t.Error(err)
}
}
})
}
}