This repository has been archived by the owner on May 30, 2019. It is now read-only.
forked from gobuffalo/suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suite.go
73 lines (62 loc) · 1.45 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
package suite
import (
"testing"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/httptest"
"github.com/gobuffalo/mw-csrf"
"github.com/gobuffalo/packr"
"github.com/pkg/errors"
"github.com/stretchr/testify/suite"
)
type Action struct {
*Model
Session *buffalo.Session
App *buffalo.App
csrf buffalo.MiddlewareFunc
}
// NewAction returns new Action for given buffalo.App
func NewAction(app *buffalo.App) *Action {
as := &Action{
App: app,
Model: NewModel(),
}
return as
}
func NewActionWithFixtures(app *buffalo.App, box packr.Box) (*Action, error) {
m, err := NewModelWithFixtures(box)
if err != nil {
return nil, errors.WithStack(err)
}
as := &Action{
App: app,
Model: m,
}
return as, nil
}
func Run(t *testing.T, s suite.TestingSuite) {
suite.Run(t, s)
}
func (as *Action) HTML(u string, args ...interface{}) *httptest.Request {
return httptest.New(as.App).HTML(u, args...)
}
func (as *Action) JSON(u string, args ...interface{}) *httptest.JSON {
return httptest.New(as.App).JSON(u, args...)
}
func (as *Action) SetupTest() {
as.App.SessionStore = NewSessionStore()
s, _ := as.App.SessionStore.New(nil, as.App.SessionName)
as.Session = &buffalo.Session{
Session: s,
}
as.Model.SetupTest()
as.csrf = csrf.New
csrf.New = func(next buffalo.Handler) buffalo.Handler {
return func(c buffalo.Context) error {
return next(c)
}
}
}
func (as *Action) TearDownTest() {
csrf.New = as.csrf
as.Model.TearDownTest()
}