Support primary key ID and unique key Name where api uses name
This commit is contained in:
@@ -47,6 +47,7 @@ func auth(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) er
|
||||
|
||||
func isPublic(g storage.RateLimitedGraph, r *http.Request) bool {
|
||||
namespace, err := getAuthNamespace(r)
|
||||
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
@@ -103,8 +104,10 @@ func requestAuth(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
userKey := ones[0]
|
||||
|
||||
id := uuid.New().String()
|
||||
token := entity.One{
|
||||
Name: uuid.New().String(),
|
||||
ID: id,
|
||||
Name: id,
|
||||
Title: namespace,
|
||||
}
|
||||
if err := g.Insert(r.Context(), namespace, token); err != nil {
|
||||
|
||||
@@ -22,7 +22,7 @@ func TestAuth(t *testing.T) {
|
||||
g := storage.NewRateLimitedGraph()
|
||||
handler := jsonHandler(g)
|
||||
|
||||
if err := g.Insert(context.TODO(), "col."+AuthKey, entity.One{Name: UserKey, Title: "password"}); err != nil {
|
||||
if err := g.Insert(context.TODO(), "col."+AuthKey, entity.One{ID: UserKey, Name: UserKey, Title: "password"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -48,13 +48,13 @@ func TestAuth(t *testing.T) {
|
||||
t.Run("auth: expired provided", func(t *testing.T) {
|
||||
os.Setenv("AUTHLIFETIME", "1ms")
|
||||
defer os.Setenv("AUTHLIFETIME", "1h")
|
||||
one := entity.One{Name: uuid.New().String(), Title: "title"}
|
||||
one := entity.One{ID: uuid.New().String(), Name: uuid.New().String(), Title: "title"}
|
||||
if err := g.Insert(context.TODO(), "col", one); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
time.Sleep(time.Millisecond * 50)
|
||||
r := httptest.NewRequest(http.MethodGet, "/who?namespace=col", nil)
|
||||
r.Header.Set("Cookie", fmt.Sprintf("%s=%s", AuthKey, one.Name))
|
||||
r.Header.Set("Cookie", fmt.Sprintf("%s=%s", AuthKey, one.ID))
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, r)
|
||||
if w.Code != http.StatusSeeOther {
|
||||
@@ -82,7 +82,7 @@ func TestAuth(t *testing.T) {
|
||||
|
||||
t.Run("auth: provided", func(t *testing.T) {
|
||||
os.Setenv("AUTHLIFETIME", "1h")
|
||||
one := entity.One{Name: uuid.New().String(), Title: "title"}
|
||||
one := entity.One{ID: uuid.New().String(), Name: uuid.New().String(), Title: "title"}
|
||||
if err := g.Insert(context.TODO(), "col."+AuthKey, one); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func TestPort(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"A", "B", "C"} {
|
||||
if err := g.Insert(context.TODO(), "col", entity.One{Name: name}); err != nil {
|
||||
if err := g.Insert(context.TODO(), "col", entity.One{ID: "id-" + name, Name: name}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"local/dndex/storage"
|
||||
"local/dndex/storage/entity"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func register(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
|
||||
@@ -33,6 +35,7 @@ func registerPost(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Req
|
||||
return nil
|
||||
}
|
||||
one := entity.One{
|
||||
ID: uuid.New().String(),
|
||||
Name: UserKey,
|
||||
Title: password,
|
||||
}
|
||||
|
||||
@@ -170,7 +170,6 @@ func whoPut(namespace string, g storage.RateLimitedGraph, w http.ResponseWriter,
|
||||
return nil
|
||||
}
|
||||
|
||||
body = bytes.ReplaceAll(body, []byte(`"`+entity.JSONName+`"`), []byte(`"`+entity.Name+`"`))
|
||||
op := bson.M{}
|
||||
if err := json.Unmarshal(body, &op); err != nil {
|
||||
return err
|
||||
|
||||
@@ -58,7 +58,7 @@ func TestWho(t *testing.T) {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
o := entity.One{}
|
||||
var o entity.One
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -105,7 +105,7 @@ func TestWho(t *testing.T) {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
o := entity.One{}
|
||||
var o entity.One
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -140,7 +140,7 @@ func TestWho(t *testing.T) {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
o := entity.One{}
|
||||
var o entity.One
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -175,7 +175,7 @@ func TestWho(t *testing.T) {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
o := entity.One{}
|
||||
var o entity.One
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -207,7 +207,7 @@ func TestWho(t *testing.T) {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
o := entity.One{}
|
||||
var o entity.One
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -260,7 +260,7 @@ func TestWho(t *testing.T) {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
o := entity.One{}
|
||||
var o entity.One
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -278,7 +278,7 @@ func TestWho(t *testing.T) {
|
||||
handler, _, iwant, _, can := fresh(t)
|
||||
defer can()
|
||||
want := iwant
|
||||
r := httptest.NewRequest(http.MethodPost, "/who?namespace=col&id="+want.Name, strings.NewReader(`{"title":"this should fail to insert"}`))
|
||||
r := httptest.NewRequest(http.MethodPost, "/who?namespace=col&id="+want.Name, strings.NewReader(`{"title":"this should fail to insert", "_id":"trash"}`))
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, r)
|
||||
if w.Code != http.StatusConflict {
|
||||
@@ -291,6 +291,7 @@ func TestWho(t *testing.T) {
|
||||
defer can()
|
||||
iwant := want
|
||||
iwant.Name = ""
|
||||
iwant.ID = "NEWBIE" + iwant.ID
|
||||
b, err := json.Marshal(iwant)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -301,7 +302,7 @@ func TestWho(t *testing.T) {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
o := entity.One{}
|
||||
var o entity.One
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -319,6 +320,7 @@ func TestWho(t *testing.T) {
|
||||
handler, _, want, _, can := fresh(t)
|
||||
defer can()
|
||||
want.Name = "hello world #1 e ę"
|
||||
want.ID = "hello world #1 e ę" + want.ID
|
||||
want.Connections = nil
|
||||
b, err := json.Marshal(want)
|
||||
if err != nil {
|
||||
@@ -336,7 +338,7 @@ func TestWho(t *testing.T) {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
o := entity.One{}
|
||||
var o entity.One
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -369,6 +371,7 @@ func TestWho(t *testing.T) {
|
||||
defer can()
|
||||
|
||||
want.Name = "hello world #4"
|
||||
want.ID = "hello world #4" + want.ID
|
||||
want.Connections = nil
|
||||
b, err := json.Marshal(want)
|
||||
if err != nil {
|
||||
@@ -410,6 +413,7 @@ func TestWho(t *testing.T) {
|
||||
defer can()
|
||||
|
||||
want.Name = "hello world #1 e ę"
|
||||
want.ID = "hello world #1 e ę" + want.ID
|
||||
want.Connections = nil
|
||||
b, err := json.Marshal(want)
|
||||
if err != nil {
|
||||
@@ -499,6 +503,7 @@ func TestWho(t *testing.T) {
|
||||
from := ones[4]
|
||||
push := ones[10].Peer()
|
||||
push.Relationship = "spawn"
|
||||
push.ID = uuid.New().String()
|
||||
b, err := json.Marshal(push)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -509,7 +514,7 @@ func TestWho(t *testing.T) {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
got := entity.One{}
|
||||
var got entity.One
|
||||
if err := json.NewDecoder(w.Body).Decode(&got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -558,7 +563,7 @@ func TestWho(t *testing.T) {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
got := entity.One{}
|
||||
var got entity.One
|
||||
if err := json.NewDecoder(w.Body).Decode(&got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -675,7 +680,7 @@ func TestWho(t *testing.T) {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
o := entity.One{}
|
||||
var o entity.One
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -702,7 +707,7 @@ func TestWho(t *testing.T) {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
o := entity.One{}
|
||||
var o entity.One
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -744,7 +749,7 @@ func TestWho(t *testing.T) {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
o := entity.One{}
|
||||
var o entity.One
|
||||
if err := json.NewDecoder(w.Body).Decode(&o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -943,6 +948,7 @@ func fillDB(t *testing.T, g storage.RateLimitedGraph) []entity.One {
|
||||
|
||||
func randomOne() entity.One {
|
||||
return entity.One{
|
||||
ID: "iddd-" + uuid.New().String()[:5],
|
||||
Name: "name-" + uuid.New().String()[:5],
|
||||
Type: "type-" + uuid.New().String()[:5],
|
||||
Title: "titl-" + uuid.New().String()[:5],
|
||||
|
||||
Reference in New Issue
Block a user