forked from gobuffalo/suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suite.go
88 lines (76 loc) · 1.9 KB
/
suite.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
package suite
import (
"io/fs"
"testing"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/httptest"
"github.com/gobuffalo/middleware/csrf"
"github.com/stretchr/testify/suite"
)
// Action suite
type Action struct {
*Model
Session *buffalo.Session
App *buffalo.App
csrf buffalo.MiddlewareFunc
}
// HTML creates an httptest.Request with HTML content type.
func (as *Action) HTML(u string, args ...interface{}) *httptest.Request {
return httptest.New(as.App).HTML(u, args...)
}
// JSON creates an httptest.JSON request
func (as *Action) JSON(u string, args ...interface{}) *httptest.JSON {
return httptest.New(as.App).JSON(u, args...)
}
// XML creates an httptest.XML request
func (as *Action) XML(u string, args ...interface{}) *httptest.XML {
return httptest.New(as.App).XML(u, args...)
}
// SetupTest sets the session store, CSRF and clears database
func (as *Action) SetupTest() {
as.App.SessionStore = newSessionStore()
s, _ := as.App.SessionStore.New(nil, as.App.SessionName)
as.Session = &buffalo.Session{
Session: s,
}
if as.Model != nil {
as.Model.SetupTest()
}
as.csrf = csrf.New
csrf.New = func(next buffalo.Handler) buffalo.Handler {
return func(c buffalo.Context) error {
return next(c)
}
}
}
// TearDownTest resets csrf
func (as *Action) TearDownTest() {
csrf.New = as.csrf
if as.Model != nil {
as.Model.TearDownTest()
}
}
// NewAction returns new Action for given buffalo.App
func NewAction(app *buffalo.App) *Action {
as := &Action{
App: app,
Model: NewModel(),
}
return as
}
// NewActionWithFixtures creates a new ActionSuite with passed box for fixtures.
func NewActionWithFixtures(app *buffalo.App, fsys fs.FS) (*Action, error) {
m, err := NewModelWithFixtures(fsys)
if err != nil {
return nil, err
}
as := &Action{
App: app,
Model: m,
}
return as, nil
}
//Run the passed suite
func Run(t *testing.T, s suite.TestingSuite) {
suite.Run(t, s)
}