Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: ensure problematic chars are not creatable over REST #709

Merged
merged 1 commit into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/relationtuple/transact_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (h *handler) createRelation(w http.ResponseWriter, r *http.Request, _ httpr

if err := h.d.RelationTupleManager().WriteRelationTuples(r.Context(), &rel); err != nil {
h.d.Logger().WithError(err).WithFields(rel.ToLoggerFields()).Errorf("got an error while creating the relation tuple")
h.d.Writer().WriteError(w, r, errors.WithStack(herodot.ErrInternalServerError.WithError(err.Error())))
h.d.Writer().WriteError(w, r, err)
return
}

Expand Down
63 changes: 61 additions & 2 deletions internal/relationtuple/transact_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,21 @@ func TestWriteHandlers(t *testing.T) {
r := httprouter.New()
wr := &x.WriteRouter{Router: r}
rr := &x.ReadRouter{Router: r}
nspace := &namespace.Namespace{Name: "write handler test"}
reg := driver.NewSqliteTestRegistry(t, false)
require.NoError(t, reg.Config().Set(config.KeyNamespaces, []*namespace.Namespace{nspace}))

var nspaces []*namespace.Namespace
addNamespace := func(t *testing.T) *namespace.Namespace {
n := &namespace.Namespace{
ID: int32(len(nspaces)),
Name: t.Name(),
}
nspaces = append(nspaces, n)

require.NoError(t, reg.Config().Set(config.KeyNamespaces, nspaces))

return n
}

h := relationtuple.NewHandler(reg)
h.RegisterWriteRoutes(wr)
h.RegisterReadRoutes(rr)
Expand All @@ -46,6 +58,8 @@ func TestWriteHandlers(t *testing.T) {
}

t.Run("case=creates tuple", func(t *testing.T) {
nspace := addNamespace(t)

rt := &relationtuple.InternalRelationTuple{
Namespace: nspace.Name,
Object: "obj",
Expand Down Expand Up @@ -86,10 +100,51 @@ func TestWriteHandlers(t *testing.T) {
resp := doCreate([]byte("foo"))
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
})

t.Run("case=special chars error on creation already", func(t *testing.T) {
nspace := addNamespace(t)

rts := []*relationtuple.InternalRelationTuple{
{
Namespace: nspace.Name,
Object: "group:B",
Relation: "member",
Subject: &relationtuple.SubjectSet{
Namespace: nspace.Name,
Object: "group:A",
Relation: "member",
},
},
{
Namespace: nspace.Name,
Object: "@all",
Relation: "member",
Subject: &relationtuple.SubjectID{ID: "this:will#be interpreted:as a@subject set"},
},
}

for _, rt := range rts {
payload, err := json.Marshal(rt)
require.NoError(t, err)

resp := doCreate(payload)
assert.GreaterOrEqual(t, resp.StatusCode, http.StatusBadRequest)
assert.Less(t, resp.StatusCode, http.StatusInternalServerError)
}

actual, next, err := reg.RelationTupleManager().GetRelationTuples(context.Background(), &relationtuple.RelationQuery{
Namespace: nspace.Name,
})
require.NoError(t, err)
assert.Equal(t, "", next)
assert.Len(t, actual, 0)
})
})

t.Run("method=delete", func(t *testing.T) {
t.Run("case=deletes a tuple", func(t *testing.T) {
nspace := addNamespace(t)

rt := &relationtuple.InternalRelationTuple{
Namespace: nspace.Name,
Object: "deleted obj",
Expand All @@ -113,6 +168,8 @@ func TestWriteHandlers(t *testing.T) {

t.Run("method=patch", func(t *testing.T) {
t.Run("case=create and delete", func(t *testing.T) {
nspace := addNamespace(t)

deltas := []*relationtuple.PatchDelta{
{
Action: relationtuple.ActionInsert,
Expand Down Expand Up @@ -152,6 +209,8 @@ func TestWriteHandlers(t *testing.T) {
})

t.Run("case=ignores rest on err", func(t *testing.T) {
nspace := addNamespace(t)

deltas := []*relationtuple.PatchDelta{
{
Action: relationtuple.ActionInsert,
Expand Down