From 0e16340fd22b56f53064f72f6160bccecf1beee3 Mon Sep 17 00:00:00 2001 From: breel Date: Sat, 25 Jul 2020 22:16:02 -0600 Subject: [PATCH] Create public namespaces by adding public=true when registering --- view/auth.go | 18 ++++++++++++++++++ view/register.go | 2 +- view/register_test.go | 19 +++++++++++++++++++ view/who_test.go | 8 +++----- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/view/auth.go b/view/auth.go index 2504515..d1a549e 100644 --- a/view/auth.go +++ b/view/auth.go @@ -36,12 +36,30 @@ func Auth(g storage.Graph, w http.ResponseWriter, r *http.Request) error { } func auth(g storage.Graph, w http.ResponseWriter, r *http.Request) error { + if isPublic(g, r) { + return nil + } if !hasAuth(r) { return requestAuth(g, w, r) } return checkAuth(g, w, r) } +func isPublic(g storage.Graph, r *http.Request) bool { + namespace, err := getAuthNamespace(r) + if err != nil { + return false + } + ones, err := g.List(r.Context(), namespace, UserKey) + if err != nil { + return false + } + if len(ones) == 0 { + return false + } + return ones[0].Title == "" +} + func hasAuth(r *http.Request) bool { _, err := r.Cookie(AuthKey) return err == nil diff --git a/view/register.go b/view/register.go index fc262a2..eba5ec0 100644 --- a/view/register.go +++ b/view/register.go @@ -28,7 +28,7 @@ func registerPost(g storage.Graph, w http.ResponseWriter, r *http.Request) error return nil } password := r.FormValue("password") - if len(password) == 0 { + if len(password) == 0 && r.URL.Query().Get("public") == "" { http.Error(w, `{"error": "password required"}`, http.StatusBadRequest) return nil } diff --git a/view/register_test.go b/view/register_test.go index 48cdbdc..d77a7df 100644 --- a/view/register_test.go +++ b/view/register_test.go @@ -59,6 +59,25 @@ func TestRegister(t *testing.T) { } }) + t.Run("register: public", func(t *testing.T) { + ns := uuid.New().String() + + r := httptest.NewRequest(http.MethodPost, "/register?public=true&namespace="+ns, strings.NewReader("")) + r.Header.Set("Content-Type", "application/x-www-form-urlencoded") + 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.MethodTrace, "/who?namespace="+ns, nil) + w = httptest.NewRecorder() + handler.ServeHTTP(w, r) + if w.Code != http.StatusOK { + t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) + } + }) + t.Run("register", func(t *testing.T) { ns := uuid.New().String() diff --git a/view/who_test.go b/view/who_test.go index 1f0bded..578ddce 100644 --- a/view/who_test.go +++ b/view/who_test.go @@ -562,7 +562,8 @@ func TestWho(t *testing.T) { t.Fatalf("GET put != expected: want:\n%+v, got \n%+v", want, o) } - r = httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/who?namespace=col&id=%s&connection=%s", want.Name, want.Peers()[0]), nil) + forget := want.Peers()[0] + r = httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/who?namespace=col&id=%s&connection=%s", want.Name, forget), nil) w = httptest.NewRecorder() handler.ServeHTTP(w, r) if w.Code != http.StatusOK { @@ -579,11 +580,8 @@ func TestWho(t *testing.T) { if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { t.Fatal(err) } - if _, ok := o.Connections[want.Peers()[0]]; ok { - t.Fatal(want.Peers()[0], o.Connections) - } if len(o.Connections) != len(put.Connections)-1 { - t.Fatal(o.Connections) + t.Fatalf("should've deleted %q but got %+v", forget, o.Connections) } }) }