Skip to content

Commit

Permalink
feat: add API to list namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Oct 13, 2022
1 parent c2a0ab3 commit a8d8767
Show file tree
Hide file tree
Showing 35 changed files with 1,935 additions and 42 deletions.
2 changes: 2 additions & 0 deletions internal/driver/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"syscall"

"github.com/ory/keto/internal/namespace/namespacehandler"
"github.com/ory/keto/internal/schema"
rts "github.com/ory/keto/proto/ory/keto/relation_tuples/v1alpha2"

Expand Down Expand Up @@ -297,6 +298,7 @@ func (r *RegistryDefault) allHandlers() []Handler {
relationtuple.NewHandler(r),
check.NewHandler(r),
expand.NewHandler(r),
namespacehandler.New(r),
schema.NewHandler(r),
}
}
Expand Down
7 changes: 7 additions & 0 deletions internal/e2e/cli_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ type cliClient struct {
c *cmdx.CommandExecuter
}

func (g *cliClient) queryNamespaces(t require.TestingT) (res ketoapi.GetNamespacesResponse) {
if t, ok := t.(*testing.T); ok {
t.Skip("not implemented for the CLI")
}
return
}

var _ client = (*cliClient)(nil)

func (g *cliClient) oplCheckSyntax(t require.TestingT, _ []byte) []*ketoapi.ParseError {
Expand Down
2 changes: 2 additions & 0 deletions internal/e2e/full_suit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type (
expand(t require.TestingT, r *ketoapi.SubjectSet, depth int) *ketoapi.Tree[*ketoapi.RelationTuple]
oplCheckSyntax(t require.TestingT, content []byte) []*ketoapi.ParseError
waitUntilLive(t require.TestingT)
queryNamespaces(t require.TestingT) ketoapi.GetNamespacesResponse
}
)

Expand Down Expand Up @@ -88,6 +89,7 @@ func Test(t *testing.T) {
syntaxRemote: reg.Config(ctx).OPLSyntaxAPIListenOn(),
},
} {
cl := cl
t.Run(fmt.Sprintf("client=%T", cl), runCases(cl, namespaceTestMgr))

if tc, ok := cl.(transactClient); ok {
Expand Down
9 changes: 9 additions & 0 deletions internal/e2e/grpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ type grpcClient struct {
ctx context.Context
}

func (g *grpcClient) queryNamespaces(t require.TestingT) (apiResponse ketoapi.GetNamespacesResponse) {
client := rts.NewNamespacesServiceClient(g.readConn(t))
res, err := client.ListNamespaces(g.ctx, &rts.ListNamespacesRequest{})
require.NoError(t, err)
require.NoError(t, convert(res, &apiResponse))

return
}

var _ transactClient = (*grpcClient)(nil)

func (g *grpcClient) conn(t require.TestingT, remote string) *grpc.ClientConn {
Expand Down
11 changes: 11 additions & 0 deletions internal/e2e/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package e2e

import (
"context"
"encoding/json"
"testing"

"github.com/ory/keto/internal/x/dbx"
Expand Down Expand Up @@ -124,3 +125,13 @@ func startServer(ctx context.Context, t testing.TB, reg driver.Registry) func()
require.NoError(t, <-serverErr)
}
}

// convert the struct in `from` to the pointer in `to` using JSON mashal and unmarshal. from and toPtr must have the
// same json field tags.
func convert(from, toPtr any) error {
raw, err := json.Marshal(from)
if err != nil {
return err
}
return json.Unmarshal(raw, toPtr)
}
8 changes: 8 additions & 0 deletions internal/e2e/rest_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ type restClient struct {
readURL, writeURL, oplSyntaxURL string
}

func (rc *restClient) queryNamespaces(t require.TestingT) (res ketoapi.GetNamespacesResponse) {
body, code := rc.makeRequest(t, http.MethodGet, "/namespaces", "", rc.readURL)
assert.Equal(t, http.StatusOK, code, body)
require.NoError(t, json.Unmarshal([]byte(body), &res))

return
}

func (rc *restClient) oplCheckSyntax(t require.TestingT, content []byte) []*ketoapi.ParseError {
body, code := rc.makeRequest(t, http.MethodPost, schema.RouteBase, string(content), rc.oplSyntaxURL)
assert.Equal(t, http.StatusOK, code, body)
Expand Down
19 changes: 19 additions & 0 deletions internal/e2e/sdk_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type sdkClient struct {
wc httpclient.WriteApi
mc httpclient.MetadataApi
sc httpclient.SyntaxApi
nc httpclient.NamespacesApi

readRemote, writeRemote, syntaxRemote string
}
Expand Down Expand Up @@ -78,6 +79,16 @@ func (c *sdkClient) getWriteClient() httpclient.WriteApi {
return c.wc
}

func (c *sdkClient) getNamespacesClient() httpclient.NamespacesApi {
if c.nc == nil {
cfg := httpclient.NewConfiguration()
cfg.Host = c.readRemote
cfg.Scheme = "http"
c.nc = httpclient.NewAPIClient(cfg).NamespacesApi
}
return c.nc
}

func (c *sdkClient) getOPLSyntaxClient() httpclient.SyntaxApi {
if c.sc == nil {
cfg := httpclient.NewConfiguration()
Expand Down Expand Up @@ -295,3 +306,11 @@ func (c *sdkClient) waitUntilLive(t require.TestingT) {
}
require.Equal(t, "ok", resp.Status)
}

func (c *sdkClient) queryNamespaces(t require.TestingT) (response ketoapi.GetNamespacesResponse) {
res, _, err := c.getNamespacesClient().GetNamespaces(c.requestCtx()).Execute()
require.NoError(t, err)
require.NoError(t, convert(res, &response))

return
}
9 changes: 9 additions & 0 deletions internal/e2e/testcases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ func runCases(c client, m *namespaceTestManager) func(*testing.T) {
return func(t *testing.T) {
c.waitUntilLive(t)

t.Run("case=list namespaces", func(t *testing.T) {
first := namespace.Namespace{Name: "my namespace"}
second := namespace.Namespace{Name: "my other namespace"}
m.add(t, &first, &second)

resp := c.queryNamespaces(t)
assert.GreaterOrEqual(t, len(resp.Namespaces), 2)
})

t.Run("case=gets empty namespace", func(t *testing.T) {
n := &namespace.Namespace{Name: t.Name()}
m.add(t, n)
Expand Down
6 changes: 6 additions & 0 deletions internal/httpclient/.openapi-generator/FILES

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/httpclient/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions internal/httpclient/api/openapi.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a8d8767

Please sign in to comment.