-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauth_test.go
67 lines (57 loc) · 1.51 KB
/
auth_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
package filedrop_test
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/foxcpp/filedrop"
)
var authDB = map[string]bool{
"foo": true,
"bar": true,
"baz": false,
}
func authCallback(r *http.Request) bool {
return authDB[r.URL.Query().Get("authToken")]
}
func TestAccessDenied(t *testing.T) {
conf := filedrop.Default
conf.UploadAuth.Callback = authCallback
conf.DownloadAuth.Callback = authCallback
serv := initServ(conf)
ts := httptest.NewServer(serv)
defer cleanServ(serv)
defer ts.Close()
c := ts.Client()
if !t.Run("upload (fail)", func(t *testing.T) {
doPOSTFail(t, c, ts.URL+"/filedrop?authToken=baz", "text/plain", strings.NewReader(file))
}) {
t.FailNow()
}
// Access check should be done before existence check to deter scanning.
if !t.Run("download (fail)", func(t *testing.T) {
doGETFail(t, c, ts.URL+"/filedrop/AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA/meow.txt?authToken=baz")
}) {
t.FailNow()
}
}
func TestUploadAuth(t *testing.T) {
conf := filedrop.Default
conf.UploadAuth.Callback = authCallback
conf.DownloadAuth.Callback = authCallback
serv := initServ(conf)
ts := httptest.NewServer(serv)
defer cleanServ(serv)
defer ts.Close()
c := ts.Client()
if !t.Run("upload", func(t *testing.T) {
doPOST(t, c, ts.URL+"/filedrop?authToken=foo", "text/plain", strings.NewReader(file))
}) {
t.FailNow()
}
if !t.Run("download (fail)", func(t *testing.T) {
doGETFail(t, c, ts.URL+"/filedrop/AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA?authToken=baz")
}) {
t.FailNow()
}
}