-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
233 lines (225 loc) · 6.19 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
227
228
229
230
231
232
233
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/aidan-plenert-macdonald/grest/api"
)
type TestResponse func(t *testing.T, rec *httptest.ResponseRecorder)
var NoTest TestResponse = func(t *testing.T, rec *httptest.ResponseRecorder) {}
func TestGets(t *testing.T) {
start := time.Now()
api := api.NewApi("jdbc:postgres://localhost:5432/postgres")
server := api.GetServer("./openapi.yml")
if time.Since(start) > 2*time.Second {
t.Error("Slow start up time", time.Since(start))
}
type GetTest struct {
req *http.Request
status int
username string
password string
recTest TestResponse
}
tests := []GetTest{
{
httptest.NewRequest(http.MethodGet, "/_data/", nil),
http.StatusOK, "test", "test", NoTest,
},
{
httptest.NewRequest(http.MethodGet, "/_data/postgres", nil),
http.StatusOK, "test", "test", NoTest,
},
{
httptest.NewRequest(http.MethodGet, "/_data/postgres/public", nil),
http.StatusOK, "test", "test", NoTest,
},
{
httptest.NewRequest(http.MethodGet, "/_data/postgres/public", nil),
http.StatusUnauthorized, "test", "wrongpassword", NoTest,
},
{
httptest.NewRequest(http.MethodGet, "/_data/postgres/public", nil),
http.StatusUnauthorized, "nonexistant", "test", NoTest,
},
{
httptest.NewRequest(http.MethodDelete, "/_data/postgres/public/testtable", nil),
http.StatusOK, "test", "test", NoTest,
},
{
httptest.NewRequest(
http.MethodPut, "/_data/postgres/public/testtable",
strings.NewReader(
`{"col": "real"}`,
),
),
http.StatusOK, "test", "test", NoTest,
},
{
httptest.NewRequest(
http.MethodPost, "/_data/postgres/public/testtable",
strings.NewReader(
`{"col": 1}`,
),
),
http.StatusOK, "test", "test", NoTest,
},
{
httptest.NewRequest(http.MethodGet, "/_data/postgres/public/testtable", nil),
http.StatusOK, "test", "test", NoTest,
},
{
httptest.NewRequest(http.MethodDelete, "/_data/postgres/public/testtable", nil),
http.StatusOK, "test", "test", NoTest,
},
{
httptest.NewRequest(http.MethodGet, "/_data/postgres/public/testtable", nil),
http.StatusNotFound, "test", "test", NoTest,
},
// Can only create role as admin
{
httptest.NewRequest(
http.MethodPut, "/_roles/",
strings.NewReader(
`{"username": "newuser", "password": "pass"}`,
),
),
http.StatusForbidden, "test", "test", NoTest,
},
{
httptest.NewRequest(
http.MethodPut, "/_roles/",
strings.NewReader(
`{"username": "newuser", "password": "pass"}`,
),
),
http.StatusOK, "postgres", "test", NoTest,
},
// Create table as newuser
{
httptest.NewRequest(
http.MethodPut, "/_data/postgres/public/testtable",
strings.NewReader(
`{"col": "real"}`,
),
),
http.StatusOK, "newuser", "pass", NoTest,
},
{
httptest.NewRequest(
http.MethodPost, "/_data/postgres/public/testtable",
strings.NewReader(
`{"col": 1}`,
),
),
http.StatusOK, "newuser", "pass", NoTest,
},
// Test that permissions for the new table are working
{
httptest.NewRequest(http.MethodGet, "/_data/postgres/public/testtable", nil),
http.StatusForbidden, "test", "test", NoTest,
},
{
httptest.NewRequest(http.MethodGet, "/_data/postgres/public/testtable", nil),
http.StatusOK, "newuser", "pass", NoTest,
},
{
httptest.NewRequest(http.MethodGet, "/_data/postgres/public/testtable", nil),
http.StatusOK, "postgres", "test", NoTest,
},
// Add permissions
{
httptest.NewRequest(http.MethodPut, "/_roles/test/testtable/select", nil),
http.StatusOK, "postgres", "test", NoTest,
},
{
httptest.NewRequest(http.MethodGet, "/_data/postgres/public/testtable", nil),
http.StatusOK, "test", "test", NoTest,
},
// Test checking for roles
{
httptest.NewRequest(http.MethodGet, "/_roles/", nil),
http.StatusOK, "test", "test",
func(t *testing.T, rec *httptest.ResponseRecorder) {
target := []map[string]string{}
json.NewDecoder(rec.Body).Decode(&target)
for _, role := range target {
if role["subj"] != "test" {
t.Error("Role subject should always be 'test' not", role["subj"])
}
}
},
},
{
httptest.NewRequest(http.MethodGet, "/_roles/", nil),
http.StatusOK, "postgres", "test",
func(t *testing.T, rec *httptest.ResponseRecorder) {
target := []map[string]string{}
json.NewDecoder(rec.Body).Decode(&target)
if len(target) == 0 {
t.Error("Should have recieved more roles back")
}
},
},
{
httptest.NewRequest(http.MethodGet, "/_roles/newuser", nil),
http.StatusOK, "postgres", "test",
func(t *testing.T, rec *httptest.ResponseRecorder) {
target := []map[string]string{}
json.NewDecoder(rec.Body).Decode(&target)
if len(target) == 0 {
t.Error("Should have recieved more roles back")
}
for _, role := range target {
if role["subj"] != "newuser" {
t.Error("Role subject should always be 'newuser' not", role["subj"])
}
}
},
},
{
httptest.NewRequest(http.MethodDelete, "/_roles/newuser", nil),
http.StatusForbidden, "test", "test", NoTest,
},
{
httptest.NewRequest(http.MethodDelete, "/_roles/newuser", nil),
http.StatusOK, "postgres", "test", NoTest,
},
{
httptest.NewRequest(http.MethodDelete, "/_roles/newuser", nil),
http.StatusNotFound, "postgres", "test", NoTest,
},
{
httptest.NewRequest(http.MethodDelete, "/_roles/newuser", nil),
// Dangerous!! Unauthed user could discover roles by querying until they get a Forbidden
http.StatusNotFound, "test", "test", NoTest,
},
}
for _, test := range tests {
t.Run(
fmt.Sprintf("%s %s", test.req.Method, test.req.RequestURI),
func(t *testing.T) {
start := time.Now()
test.req.SetBasicAuth(test.username, test.password)
test.req.Header.Set("Content-Type", "application/json")
fmt.Println(test.req)
rec := httptest.NewRecorder()
server.ServeHTTP(rec, test.req)
if test.status != rec.Code {
t.Errorf(
"HTTP Code mismatch %d != %d",
test.status, rec.Code,
)
}
if time.Since(start) > 2*time.Second {
t.Error("Test ran for too long", time.Since(start))
}
test.recTest(t, rec)
},
)
}
}