Files assert auth, namespace results, and faster port test

master
breel 2020-07-25 20:42:55 -06:00
parent 6ca0a90134
commit 09b2a94dd8
5 changed files with 49 additions and 19 deletions

View File

@ -40,9 +40,7 @@ paths:
requestBody:
description: "An entity where all fields are optional and modified is disallowed"
content:
application/json:
schema:
$ref: "#/components/schemas/one"
$ref: "#/components/schemas/oneContent"
responses:
200:
$ref: "#/components/schemas/200"
@ -57,9 +55,7 @@ paths:
requestBody:
description: "An entity where all fields are optional and modified is disallowed"
content:
application/json:
schema:
$ref: "#/components/schemas/one"
$ref: "#/components/schemas/oneContent"
responses:
200:
$ref: "#/components/schemas/200"
@ -74,9 +70,7 @@ paths:
requestBody:
description: "An entity where all fields are optional and modified is disallowed"
content:
application/json:
schema:
$ref: "#/components/schemas/one"
$ref: "#/components/schemas/oneContent"
responses:
200:
$ref: "#/components/schemas/200"
@ -133,6 +127,11 @@ components:
$ref: "#/components/schemas/one"
schemas:
oneContent:
application/json:
schema:
$ref: "#/components/schemas/one"
one:
$ref: "./swagger.yaml#/components/schemas/one"

View File

@ -17,7 +17,12 @@ import (
)
func files(_ storage.Graph, w http.ResponseWriter, r *http.Request) error {
r.URL.Path = strings.TrimPrefix(r.URL.Path, config.New().FilePrefix)
namespace, err := getNamespace(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return nil
}
r.URL.Path = strings.TrimPrefix(r.URL.Path, path.Join(config.New().FilePrefix, namespace))
if len(r.URL.Path) < 2 {
http.NotFound(w, r)
return nil

View File

@ -35,8 +35,29 @@ func TestFiles(t *testing.T) {
t.Logf("config: %+v", config.New())
handler := jsonHandler(storage.Graph{})
prefix := path.Join(config.New().FilePrefix, "col")
qparam := "namespace=col"
t.Run("bad qparam doesnt have namespace", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s/fake", prefix), nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusBadRequest {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
})
t.Run("bad prefix doesnt have namespace", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s/fake?%s", config.New().FilePrefix, qparam), nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusNotFound {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
})
t.Run("get fake file 404", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s/fake", config.New().FilePrefix), nil)
r := httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s/fake?%s", prefix, qparam), nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusNotFound {
@ -61,7 +82,7 @@ func TestFiles(t *testing.T) {
}
f.Write([]byte("hello, world"))
f.Close()
r := httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s?direct=true", path.Join(config.New().FilePrefix, path.Base(f.Name()))), nil)
r := httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s?direct=true&%s", path.Join(prefix, path.Base(f.Name())), qparam), nil)
w := httptest.NewRecorder()
t.Logf("URL = %q", r.URL.String())
handler.ServeHTTP(w, r)
@ -92,14 +113,14 @@ func TestFiles(t *testing.T) {
}))
defer s.Close()
r := httptest.NewRequest(http.MethodPost, fmt.Sprintf("%s?direct=true", path.Join(config.New().FilePrefix, name)), strings.NewReader(s.URL))
r := httptest.NewRequest(http.MethodPost, fmt.Sprintf("%s?direct=true&%s", path.Join(prefix, name), qparam), strings.NewReader(s.URL))
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
r = httptest.NewRequest(http.MethodGet, path.Join(config.New().FilePrefix, name), nil)
r = httptest.NewRequest(http.MethodGet, path.Join(prefix, name)+"?"+qparam, nil)
w = httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusOK {
@ -118,7 +139,7 @@ func TestFiles(t *testing.T) {
f.Write([]byte("hello, world"))
f.Close()
r := httptest.NewRequest(http.MethodPost, path.Join(config.New().FilePrefix, path.Base(f.Name())), strings.NewReader(`bad link teehee`))
r := httptest.NewRequest(http.MethodPost, path.Join(prefix, path.Base(f.Name()))+"?"+qparam, strings.NewReader(`bad link teehee`))
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code == http.StatusOK {
@ -139,7 +160,7 @@ func TestFiles(t *testing.T) {
w2.Write([]byte("hello, world"))
writer.Close()
r := httptest.NewRequest(http.MethodPost, path.Join(config.New().FilePrefix, name), b)
r := httptest.NewRequest(http.MethodPost, path.Join(prefix, name)+"?"+qparam, b)
r.Header.Set("Content-Type", writer.FormDataContentType())
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
@ -147,7 +168,7 @@ func TestFiles(t *testing.T) {
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
}
r = httptest.NewRequest(http.MethodGet, path.Join(config.New().FilePrefix, name), nil)
r = httptest.NewRequest(http.MethodGet, path.Join(prefix, name)+"?"+qparam, nil)
w = httptest.NewRecorder()
handler.ServeHTTP(w, r)
if w.Code != http.StatusOK {

View File

@ -6,6 +6,7 @@ import (
"encoding/json"
"io/ioutil"
"local/dndex/storage"
"local/dndex/storage/entity"
"net/http"
"net/http/httptest"
"os"
@ -29,7 +30,11 @@ func TestPort(t *testing.T) {
if err := g.Delete(context.TODO(), "col", map[string]string{}); err != nil {
t.Fatal(err)
}
fillDB(t, g)
for _, name := range []string{"A", "B", "C"} {
if err := g.Insert(context.TODO(), "col", entity.One{Name: name}); err != nil {
t.Fatal(err)
}
}
}
handler := jsonHandler(g)

View File

@ -431,7 +431,7 @@ func TestWho(t *testing.T) {
}
func fillDB(t *testing.T, g storage.Graph) []entity.One {
ones := make([]entity.One, 25)
ones := make([]entity.One, 13)
for i := range ones {
ones[i] = randomOne()
if i > 0 {