From 09b2a94dd8d56cac918b90edc284971e55c34dce Mon Sep 17 00:00:00 2001 From: breel Date: Sat, 25 Jul 2020 20:42:55 -0600 Subject: [PATCH] Files assert auth, namespace results, and faster port test --- public/swagger/swagger-who.yaml | 17 ++++++++-------- view/files.go | 7 ++++++- view/files_test.go | 35 ++++++++++++++++++++++++++------- view/port_test.go | 7 ++++++- view/who_test.go | 2 +- 5 files changed, 49 insertions(+), 19 deletions(-) diff --git a/public/swagger/swagger-who.yaml b/public/swagger/swagger-who.yaml index 0d3eb3b..e08f735 100644 --- a/public/swagger/swagger-who.yaml +++ b/public/swagger/swagger-who.yaml @@ -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" diff --git a/view/files.go b/view/files.go index e1af43e..bd24f04 100644 --- a/view/files.go +++ b/view/files.go @@ -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 diff --git a/view/files_test.go b/view/files_test.go index 166d981..ecbb63c 100644 --- a/view/files_test.go +++ b/view/files_test.go @@ -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 { diff --git a/view/port_test.go b/view/port_test.go index 797e259..b5349f7 100644 --- a/view/port_test.go +++ b/view/port_test.go @@ -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) diff --git a/view/who_test.go b/view/who_test.go index f2b1e5e..39fccf1 100644 --- a/view/who_test.go +++ b/view/who_test.go @@ -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 {