forked from cloudfoundry-community/rds-broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
226 lines (177 loc) · 5.49 KB
/
main_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"github.com/go-martini/martini"
"github.com/jinzhu/gorm"
"bytes"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
var createInstanceReq []byte = []byte(`{
"service_id":"the-service",
"plan_id":"44d24fc7-f7a4-4ac1-b7a0-de82836e89a3",
"organization_guid":"an-org",
"space_guid":"a-space"
}`)
var brokerDB *gorm.DB
func setup() *martini.ClassicMartini {
os.Setenv("AUTH_USER", "default")
os.Setenv("AUTH_PASS", "default")
var s Settings
var dbConfig DBConfig
s.DbConfig = &dbConfig
dbConfig.DbType = "sqlite3"
dbConfig.DbName = ":memory:"
s.EncryptionKey = "12345678901234567890123456789012"
s.Environment = "test"
brokerDB, _ = InternalDBInit(&dbConfig)
m := App(&s, brokerDB)
return m
}
/*
Mock Objects
*/
func doRequest(m *martini.ClassicMartini, url string, method string, auth bool, body io.Reader) (*httptest.ResponseRecorder, *martini.ClassicMartini) {
if m == nil {
m = setup()
}
res := httptest.NewRecorder()
req, _ := http.NewRequest(method, url, body)
if auth {
req.SetBasicAuth("default", "default")
}
m.ServeHTTP(res, req)
return res, m
}
/*
End Mock Objects
*/
func validJson(response []byte, url string, t *testing.T) {
var aJson map[string]interface{}
if json.Unmarshal(response, &aJson) != nil {
t.Error(url, "should return a valid json")
}
}
func TestCatalog(t *testing.T) {
url := "/v2/catalog"
res, _ := doRequest(nil, url, "GET", false, nil)
// Without auth
if res.Code != http.StatusUnauthorized {
t.Error(url, "without auth should return 401")
}
res, _ = doRequest(nil, url, "GET", true, nil)
// With auth
if res.Code != http.StatusOK {
t.Error(url, "with auth should return 200 and it returned", res.Code)
}
// Is it a valid JSON?
validJson(res.Body.Bytes(), url, t)
}
func TestCreateInstance(t *testing.T) {
url := "/v2/service_instances/the_instance"
res, _ := doRequest(nil, url, "PUT", true, bytes.NewBuffer(createInstanceReq))
if res.Code != http.StatusCreated {
t.Logf("Unable to create instance. Body is: " + res.Body.String())
t.Error(url, "with auth should return 201 and it returned", res.Code)
}
// Is it a valid JSON?
validJson(res.Body.Bytes(), url, t)
// Does it say "created"?
if !strings.Contains(string(res.Body.Bytes()), "created") {
t.Error(url, "should return the instance created message")
}
// Is it in the database and has a username and password?
i := Instance{}
brokerDB.Where("uuid = ?", "the_instance").First(&i)
if i.Id == 0 {
t.Error("The instance should be saved in the DB")
}
if i.Username == "" || i.Password == "" {
t.Error("The instance should have a username and password")
}
if i.PlanId == "" || i.OrgGuid == "" || i.SpaceGuid == "" {
t.Error("The instance should have metadata")
}
}
func TestBindInstance(t *testing.T) {
url := "/v2/service_instances/the_instance/service_bindings/the_binding"
res, m := doRequest(nil, url, "PUT", true, bytes.NewBuffer(createInstanceReq))
// Without the instance
if res.Code != http.StatusNotFound {
t.Error(url, "with auth should return 404 and it returned", res.Code)
}
// Create the instance and try again
doRequest(m, "/v2/service_instances/the_instance", "PUT", true, bytes.NewBuffer(createInstanceReq))
res, _ = doRequest(m, url, "PUT", true, nil)
if res.Code != http.StatusCreated {
t.Logf("Unable to create instance. Body is: " + res.Body.String())
t.Error(url, "with auth should return 201 and it returned", res.Code)
}
// Is it a valid JSON?
validJson(res.Body.Bytes(), url, t)
type credentials struct {
Uri string
Username string
Password string
Host string
DbName string
}
type response struct {
Credentials credentials
}
var r response
json.Unmarshal(res.Body.Bytes(), &r)
// Does it contain "uri"
if r.Credentials.Uri == "" {
t.Error(url, "should return credentials")
}
instance := Instance{}
brokerDB.Where("uuid = ?", "the_instance").First(&instance)
// Does it return an unencrypted password?
if instance.Password == r.Credentials.Password || r.Credentials.Password == "" {
t.Error(url, "should return an unencrypted password and it returned", r.Credentials.Password)
}
}
func TestUnbind(t *testing.T) {
url := "/v2/service_instances/the_instance/service_bindings/the_binding"
res, _ := doRequest(nil, url, "DELETE", true, nil)
if res.Code != http.StatusOK {
t.Error(url, "with auth should return 200 and it returned", res.Code)
}
// Is it a valid JSON?
validJson(res.Body.Bytes(), url, t)
// Is it an empty object?
if string(res.Body.Bytes()) != "{}" {
t.Error(url, "should return an empty JSON")
}
}
func TestDeleteInstance(t *testing.T) {
url := "/v2/service_instances/the_instance"
res, m := doRequest(nil, url, "DELETE", true, nil)
// With no instance
if res.Code != http.StatusNotFound {
t.Error(url, "with auth should return 404 and it returned", res.Code)
}
// Create the instance and try again
doRequest(m, "/v2/service_instances/the_instance", "PUT", true, bytes.NewBuffer(createInstanceReq))
i := Instance{}
brokerDB.Where("uuid = ?", "the_instance").First(&i)
if i.Id == 0 {
t.Error("The instance should be in the DB")
}
res, _ = doRequest(m, url, "DELETE", true, nil)
if res.Code != http.StatusOK {
t.Logf("Unable to create instance. Body is: " + res.Body.String())
t.Error(url, "with auth should return 200 and it returned", res.Code)
}
// Is it actually gone from the DB?
i = Instance{}
brokerDB.Where("uuid = ?", "the_instance").First(&i)
if i.Id > 0 {
t.Error("The instance shouldn't be in the DB")
}
}