forked from go-catupiry/catu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp_test.go
194 lines (170 loc) · 3.85 KB
/
App_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package bolo_test
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
approvals "github.com/approvals/go-approval-tests"
"github.com/go-bolo/bolo"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
var testAppInstance bolo.App
func GetTestAppInstance() bolo.App {
if testAppInstance != nil {
return testAppInstance
}
app := bolo.Init(&bolo.AppOptions{})
err := app.Bootstrap()
if err != nil {
panic(err)
}
testAppInstance = app
return app
}
func TestNewApp(t *testing.T) {
tests := []struct {
name string
}{
{
name: "should return a valid default app with required data",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := bolo.NewApp(&bolo.AppOptions{})
approvals.VerifyJSONStruct(t, got)
})
}
}
func TestApp_Bootstrap(t *testing.T) {
tests := []struct {
name string
}{
{
name: "should return a valid default app with required data",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := bolo.NewApp(&bolo.AppOptions{})
err := got.Bootstrap()
assert.Nil(t, err)
approvals.VerifyJSONStruct(t, got)
})
}
}
func TestRequest_CRUD(t *testing.T) {
app := GetTestApp()
app.RegisterPlugin(&URLShortenerPlugin{Name: "example"})
err := app.Bootstrap()
assert.Nil(t, err)
err = app.GetDB().AutoMigrate(
&URLModel{},
)
app.SetRolePermission("unAuthenticated", "create_url", true)
assert.Nil(t, err)
c, _ := bolo.NewBotContext(app)
savedRecord1 := URLModel{
Title: "Google",
Path: "http://www.google.com",
}
savedRecord1.Save(c)
savedRecord2 := URLModel{
Title: "Bing",
Path: "http://www.bing.com",
}
savedRecord2.Save(c)
assert.Nil(t, err)
type fields struct {
Plugins map[string]bolo.Plugin
}
type args struct {
accept string
queryParams string
data io.Reader
url string
method string
}
tests := []struct {
name string
fields fields
args args
wantHas bool
expectedStatus int
expectedError *bolo.HTTPError
}{
{
name: "should run a action with success",
args: args{
method: http.MethodGet,
url: "/urls",
accept: "application/json",
},
expectedStatus: http.StatusOK,
},
{
name: "JSON findOne should return 404 with invalid id",
args: args{
method: http.MethodGet,
url: "/urls/1111111111",
accept: "application/json",
},
expectedStatus: http.StatusNotFound,
},
{
name: "JSON create should create a new record",
args: args{
method: http.MethodPost,
url: "/api/v1/urls",
accept: "application/json",
data: strings.NewReader(`{"url":{"title":"example","path":"http://www.example.com"}}`),
},
expectedStatus: http.StatusCreated,
},
{
name: "JSON get count",
args: args{
method: http.MethodGet,
url: "/api/v1/urls/count",
accept: "application/json",
},
expectedStatus: http.StatusOK,
},
{
name: "JSON should run update action",
args: args{
method: http.MethodPost,
url: "/api/v1/urls/1",
accept: "application/json",
},
expectedStatus: http.StatusOK,
},
{
name: "JSON should run delete action",
args: args{
method: http.MethodDelete,
url: "/api/v1/urls/1",
accept: "application/json",
},
expectedStatus: http.StatusNoContent,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := app.GetRouter()
req := httptest.NewRequest(tt.args.method, tt.args.url, tt.args.data)
req.Header.Set(echo.HeaderAccept, tt.args.accept)
// Body content type:
req.Header.Set(echo.HeaderContentType, "application/json")
rec := httptest.NewRecorder() // run the request:
e.ServeHTTP(rec, req)
assert.Equal(t, tt.expectedStatus, rec.Code)
switch tt.args.accept {
case "application/json":
approvals.VerifyJSONBytes(t, rec.Body.Bytes())
}
})
}
}