forked from go-catupiry/catu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mocks_test.go
221 lines (176 loc) · 4.18 KB
/
mocks_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package bolo_test
import (
"errors"
"fmt"
"net/http"
"strconv"
bolo "github.com/go-bolo/bolo"
"github.com/gookit/event"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
)
// Mocks:
// URLShortener is a plugin that shortens URLs.
type URLShortenerPlugin struct {
Name string
App bolo.App `json:"-"`
Controller bolo.HTTPController
}
// Init initializes the plugin.
func (p *URLShortenerPlugin) Init(app bolo.App) error {
p.Controller = &URLController{}
app.SetModel("url", &URLModel{})
app.GetEvents().On("bindRoutes", event.ListenerFunc(func(e event.Event) error {
return p.BindRoutes(app)
}), event.Normal)
return nil
}
// GetName returns the name of the plugin.
func (p *URLShortenerPlugin) GetName() string {
return p.Name
}
// SetName sets the name of the plugin.
func (p *URLShortenerPlugin) SetName(name string) error {
p.Name = name
return nil
}
func (p *URLShortenerPlugin) GetMigrations() []*bolo.Migration {
return []*bolo.Migration{}
}
func (p *URLShortenerPlugin) BindRoutes(app bolo.App) error {
ctl := p.Controller
router := app.SetRouterGroup("urls", "/urls")
router.GET("", ctl.Query)
router.GET("/:id", ctl.FindOne)
routerApi := app.SetRouterGroup("url-api", "/api/v1/urls")
app.SetResource("url-api", ctl, routerApi)
return nil
}
type JSONResponse struct {
bolo.BaseListReponse
URLs *[]*URLModel `json:"url"`
}
type CountJSONResponse struct {
bolo.BaseMetaResponse
}
type FindOneJSONResponse struct {
URL *URLModel `json:"url"`
}
type BodyRequest struct {
URL *URLModel `json:"url"`
}
type URLController struct{}
func (ctl *URLController) Query(c echo.Context) error {
data := struct {
Name string `json:"name"`
}{
Name: "oi",
}
if c.QueryParam("errorToReturn") != "" {
eCode := c.QueryParam("errorCode")
eMessage := c.QueryParam("errorMessage")
eCodeInt, _ := strconv.Atoi(eCode)
if eCodeInt == 0 {
eCodeInt = 500
}
return &bolo.HTTPError{
Code: eCodeInt,
Message: eMessage,
Internal: errors.New(eMessage),
}
}
return c.JSON(200, &data)
}
func (ctl *URLController) Create(c echo.Context) error {
var err error
ctx := c.(*bolo.RequestContext)
can := ctx.Can("create_url")
if !can {
return &bolo.HTTPError{
Code: http.StatusForbidden,
Message: "Forbidden",
}
}
var body BodyRequest
if err := c.Bind(&body); err != nil {
if er, ok := err.(*echo.HTTPError); ok {
return &bolo.HTTPError{
Code: er.Code,
Message: er.Message,
Internal: er.Internal,
}
}
return &bolo.HTTPError{
Code: http.StatusInternalServerError,
Message: "Invalid body data",
Internal: fmt.Errorf("urls.Create error on parse body: %w", err),
}
}
record := body.URL
record.ID = 0
if ctx.IsAuthenticated {
creatorID := ctx.AuthenticatedUser.GetID()
record.CreatorID = &creatorID
}
if err := c.Validate(record); err != nil {
if _, ok := err.(*echo.HTTPError); ok {
return err
}
return &bolo.HTTPError{
Code: http.StatusInternalServerError,
Message: "Error on validate data",
Internal: err,
}
}
err = record.Save(ctx)
if err != nil {
return err
}
resp := FindOneJSONResponse{
URL: record,
}
return c.JSON(http.StatusCreated, &resp)
}
func (ctl *URLController) FindOne(c echo.Context) error {
ctx := c.(*bolo.RequestContext)
id := ctx.Param("id")
record, err := FindOneURL(ctx, id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return &bolo.HTTPError{
Code: http.StatusNotFound,
Message: "Not found",
Internal: err,
}
}
return &bolo.HTTPError{
Code: http.StatusInternalServerError,
Message: "Error on FindOneURL",
Internal: err,
}
}
return c.JSON(http.StatusOK, &record)
}
func (ctl *URLController) Count(c echo.Context) error {
r := bolo.DefaultResponse{
Data: CountJSONResponse{
BaseMetaResponse: bolo.BaseMetaResponse{
Count: 90,
},
},
}
return c.JSON(http.StatusOK, &r.Data)
}
func (ctl *URLController) Update(c echo.Context) error {
r := bolo.DefaultResponse{
Data: FindOneJSONResponse{
URL: &URLModel{
ID: 13,
},
},
}
return c.JSON(http.StatusOK, &r)
}
func (ctl *URLController) Delete(c echo.Context) error {
return c.JSON(http.StatusNoContent, struct{}{})
}