/
helpers_test.go
171 lines (142 loc) · 4.31 KB
/
helpers_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
// API test framework. This file contains setup code, and utility functions
// for the testing, but no actual tests.
package api
import (
"flag"
log "github.com/Sirupsen/logrus"
"net/http"
"net/http/httptest"
"strings"
"github.com/go-martini/martini"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
"testing"
)
var verboseMartini = flag.Bool("verbose", false, "Log all martini requests, and API internals")
// Define some sample structures for the DB
type User struct {
ID uint `gorm:"primary_key" json:"id"`
Name string `json:"name"`
Password string `json:"password"`
PrivateWidgets []PrivateWidget `json:"private_widgets"`
}
type PrivateWidget struct {
ID uint `gorm:"primary_key" json:"id"`
UserID uint `json:"user_id"`
Name string `json:"name"`
}
type Widget struct {
ID uint `gorm:"primary_key" json:"id"`
Name string `json:"name"`
}
type WidgetClone struct {
ID uint `gorm:"primary_key" json:"id"`
Name string `json:"name"`
}
type VerifiedWidget struct {
ID uint `gorm:"primary_key" json:"id"`
MustBeHelloWorld string `json:"must_be_hello_world"`
}
func (vw *VerifiedWidget) ValidateUpload() map[string]string {
if vw.MustBeHelloWorld == "Hello World!!" {
return nil
}
ve := make(map[string]string)
ve["must_be_hello_horld"] = "Is not equal to \"Hello World!!\""
return ve
}
var test_db *gorm.DB
func getTestDb() *gorm.DB {
if test_db != nil {
return test_db
}
db, err := gorm.Open("sqlite3", "./api-test.db")
if err != nil {
log.Panicf("Error opening sqlite3 database in test %v\n", err)
}
db.DropTable(&User{})
db.DropTable(&PrivateWidget{})
db.DropTable(&Widget{})
db.DropTable(&VerifiedWidget{})
db.CreateTable(&User{})
db.CreateTable(&PrivateWidget{})
db.CreateTable(&Widget{})
db.CreateTable(&VerifiedWidget{})
var private_widgets []PrivateWidget
db.Model(&User{}).Related(&private_widgets)
db.Create(&User{Name: "admin", Password: "password"})
db.Create(&Widget{ID: 1, Name: "Widget 1"})
db.Create(&Widget{ID: 2, Name: "Widget 2"})
db.Create(&Widget{ID: 3, Name: "Widget 3"})
test_db = &db
if *verboseMartini {
test_db = test_db.Debug()
}
return test_db
}
// getSilentMartini returns a ClassicMartini with logging disabled.
func getSilentMartini() *martini.ClassicMartini {
r := martini.NewRouter()
m := martini.New()
if *verboseMartini {
m.Use(martini.Logger())
}
m.Use(martini.Recovery())
m.Use(martini.Static("public"))
m.MapTo(r, (*martini.Routes)(nil))
m.Action(r.Handle)
return &martini.ClassicMartini{m, r}
}
var test_api API
// Returns a singleton instance of API, intialised with an empty DB containing a
// single user and a silent version of ClassicMartini
func getTestApi() API {
if test_api != nil {
return test_api
}
// Set Log level here. This should only be called once, and near the start
// of the test run
if !*verboseMartini {
log.SetLevel(log.ErrorLevel)
} else {
log.SetLevel(log.DebugLevel)
}
db := getTestDb()
a := New(Options{JwtKey: "RandomString", Db: db, Martini: getSilentMartini()})
a.AddDefaultRoutes(&PrivateWidget{}, RouteOptions{Authenticate: true})
a.AddDefaultRoutes(&Widget{})
a.AddDefaultRoutes(&VerifiedWidget{})
a.AddDefaultRoutes(&Widget{}, RouteOptions{UriModelName: "other_widgets"})
a.AddDefaultRoutes(&User{})
a.SetAuth(&User{}, "/auth")
test_api = a
return a
}
// Test a request to the api.
func testReq(t *testing.T, name string, method string, path string, body string, expectedCode int) string {
api := getTestApi()
payload := strings.NewReader(body)
req, err := http.NewRequest(method, path, payload)
if err != nil {
t.Errorf("Error creating request for %v: %v\n", path, err)
return ""
}
httpRecorder := httptest.NewRecorder()
api.Martini().ServeHTTP(httpRecorder, req)
if httpRecorder.Code == expectedCode {
t.Logf("SUCCESS - %v returned code %v and body %s\n", name, httpRecorder.Code, httpRecorder.Body.String())
} else {
t.Errorf("%v should have code %v. Got %v and body %s\n", name, expectedCode, httpRecorder.Code, httpRecorder.Body.String())
}
return httpRecorder.Body.String()
}
// ensurePanic is A deferrable function that fails the test with msg if there
// is no panic.
func ensurePanic(t *testing.T, msg string) {
p := recover()
if p == nil {
t.Errorf(msg)
} else {
t.Logf("SUCCESS - We tried '%s' and panicked appropriately", msg)
}
}