-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_app.go
71 lines (57 loc) · 1.72 KB
/
test_app.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
package test_tools
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/go-martini/martini"
"github.com/hashtock/hashtock-go/conf"
"github.com/hashtock/hashtock-go/models"
"github.com/hashtock/hashtock-go/services"
"github.com/hashtock/hashtock-go/webapp"
)
type TestApp struct {
cfg *conf.Config
server *httptest.Server
test *testing.T
storage *models.MgoStorage
AdminUser *models.Profile
User *models.Profile
NoUser *models.Profile
}
func NewTestApp(t *testing.T) *TestApp {
var err error
services.EnforceAuth = func(loginUrl string, exceptions ...string) martini.Handler {
return func(req *http.Request, ctx martini.Context) { ctx.Next() }
}
app := new(TestApp)
cfg := new(conf.Config)
cfg.General = conf.GeneralConf{
AppAddress: "localhost:8123",
DB: "localhost",
DBName: fmt.Sprintf("test_db_%v", time.Now().UnixNano()),
ServeAddr: "localhost:8123",
SessionKey: "test_key",
SessionSecret: "test_secter",
Admin: []string{"admin@a.com"},
}
app.storage, err = models.InitMongoStorage(cfg.General.DB, cfg.General.DBName)
if err != nil {
t.Fatal(err)
}
handler := webapp.Handlers(cfg, app.storage)
app.server = httptest.NewServer(handler)
app.test = t
if app.AdminUser, err = app.CreateProfile("admin@a.com", true); err != nil {
t.Fatal(err)
}
if app.User, err = app.CreateProfile("user@b.com", false); err != nil {
t.Fatal(err)
}
return app
}
func (t *TestApp) Stop() {
t.server.Close()
t.storage.Collection("").Database.DropDatabase()
}