forked from mindreframer/golang-devops-stuff
/
suite_test.go
135 lines (118 loc) · 3.35 KB
/
suite_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
// Copyright 2014 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package auth
import (
"code.google.com/p/go.crypto/bcrypt"
"github.com/tsuru/config"
"github.com/tsuru/tsuru/db"
ttesting "github.com/tsuru/tsuru/testing"
"io"
"io/ioutil"
"launchpad.net/gocheck"
"net/http"
"os"
"path/filepath"
"testing"
)
type hasKeyChecker struct{}
func (c *hasKeyChecker) Info() *gocheck.CheckerInfo {
return &gocheck.CheckerInfo{Name: "HasKey", Params: []string{"user", "key"}}
}
func (c *hasKeyChecker) Check(params []interface{}, names []string) (bool, string) {
if len(params) != 2 {
return false, "you should provide two parameters"
}
user, ok := params[0].(*User)
if !ok {
return false, "first parameter should be a user pointer"
}
content, ok := params[1].(string)
if !ok {
return false, "second parameter should be a string"
}
key := Key{Content: content}
return user.HasKey(key), ""
}
var HasKey gocheck.Checker = &hasKeyChecker{}
func Test(t *testing.T) { gocheck.TestingT(t) }
type S struct {
conn *db.Storage
hashed string
user *User
team *Team
server *ttesting.SMTPServer
gitRoot string
gitHost string
gitPort string
gitProt string
}
var _ = gocheck.Suite(&S{})
func (s *S) SetUpSuite(c *gocheck.C) {
config.Set("auth:token-expire-days", 2)
config.Set("auth:hash-cost", bcrypt.MinCost)
config.Set("admin-team", "admin")
config.Set("database:url", "127.0.0.1:27017")
config.Set("database:name", "tsuru_auth_test")
s.conn, _ = db.Conn()
s.user = &User{Email: "timeredbull@globo.com", Password: "123456"}
s.user.Create()
s.hashed = s.user.Password
team := &Team{Name: "cobrateam", Users: []string{s.user.Email}}
err := s.conn.Teams().Insert(team)
c.Assert(err, gocheck.IsNil)
s.team = team
s.gitHost, _ = config.GetString("git:host")
s.gitPort, _ = config.GetString("git:port")
s.gitProt, _ = config.GetString("git:protocol")
s.server, err = ttesting.NewSMTPServer()
c.Assert(err, gocheck.IsNil)
config.Set("smtp:server", s.server.Addr())
config.Set("smtp:user", "root")
config.Set("smtp:password", "123456")
}
func (s *S) TearDownSuite(c *gocheck.C) {
conn, err := db.Conn()
c.Assert(err, gocheck.IsNil)
defer conn.Close()
err = conn.Apps().Database.DropDatabase()
c.Assert(err, gocheck.IsNil)
s.server.Stop()
}
func (s *S) TearDownTest(c *gocheck.C) {
if s.user.Password != s.hashed {
s.user.Password = s.hashed
err := s.user.Update()
c.Assert(err, gocheck.IsNil)
}
config.Set("git:host", s.gitHost)
config.Set("git:port", s.gitPort)
config.Set("git:protocol", s.gitProt)
}
func (s *S) getTestData(path ...string) io.ReadCloser {
path = append([]string{}, ".", "testdata")
p := filepath.Join(path...)
f, _ := os.OpenFile(p, os.O_RDONLY, 0)
return f
}
type testHandler struct {
body [][]byte
method []string
url []string
content string
header []http.Header
}
func (h *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.method = append(h.method, r.Method)
h.url = append(h.url, r.URL.String())
b, _ := ioutil.ReadAll(r.Body)
h.body = append(h.body, b)
h.header = append(h.header, r.Header)
w.Write([]byte(h.content))
}
type testBadHandler struct {
content string
}
func (h *testBadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, h.content, http.StatusInternalServerError)
}