Skip to content

Commit

Permalink
Add missing wiring of service label through With* methods in options
Browse files Browse the repository at this point in the history
  • Loading branch information
josephschorr committed Dec 5, 2024
1 parent c50ba64 commit 6a29d88
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/cmd/server/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func (m MiddlewareOption) WithDatastoreMiddleware(middleware Middleware) Middlew
EnableRequestLog: m.EnableRequestLog,
EnableResponseLog: m.EnableResponseLog,
DisableGRPCHistogram: m.DisableGRPCHistogram,
MiddlewareServiceLabel: m.MiddlewareServiceLabel,
unaryDatastoreMiddleware: &unary,
streamDatastoreMiddleware: &stream,
}
Expand All @@ -244,6 +245,7 @@ func (m MiddlewareOption) WithDatastore(ds datastore.Datastore) MiddlewareOption
EnableRequestLog: m.EnableRequestLog,
EnableResponseLog: m.EnableResponseLog,
DisableGRPCHistogram: m.DisableGRPCHistogram,
MiddlewareServiceLabel: m.MiddlewareServiceLabel,
unaryDatastoreMiddleware: &unary,
streamDatastoreMiddleware: &stream,
}
Expand Down
94 changes: 94 additions & 0 deletions pkg/cmd/server/defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package server

import (
"context"
"fmt"
"testing"
"time"

"github.com/rs/zerolog"
"github.com/stretchr/testify/require"

"github.com/authzed/spicedb/internal/datastore/memdb"
"github.com/authzed/spicedb/internal/dispatch"
"github.com/authzed/spicedb/internal/middleware/pertoken"
)

func TestWithDatastore(t *testing.T) {
someLogger := zerolog.Nop()
someAuthFunc := func(ctx context.Context) (context.Context, error) {
return nil, fmt.Errorf("expected auth error")
}
var someDispatcher dispatch.Dispatcher

opts := MiddlewareOption{
someLogger,
someAuthFunc,
true,
someDispatcher,
true,
true,
false,
"service",
nil,
nil,
}

someDS, err := memdb.NewMemdbDatastore(0, time.Hour, time.Hour)
require.NoError(t, err)

withDS := opts.WithDatastore(someDS)
require.NotNil(t, withDS)
require.NotNil(t, withDS.unaryDatastoreMiddleware)
require.NotNil(t, withDS.streamDatastoreMiddleware)

require.Equal(t, opts.Logger, withDS.Logger)
require.Equal(t, opts.DispatcherForMiddleware, withDS.DispatcherForMiddleware)
require.Equal(t, opts.EnableRequestLog, withDS.EnableRequestLog)
require.Equal(t, opts.EnableResponseLog, withDS.EnableResponseLog)
require.Equal(t, opts.DisableGRPCHistogram, withDS.DisableGRPCHistogram)
require.Equal(t, opts.MiddlewareServiceLabel, withDS.MiddlewareServiceLabel)

_, authError := withDS.AuthFunc(context.Background())
require.Error(t, authError)
require.ErrorContains(t, authError, "expected auth error")
}

func TestWithDatastoreMiddleware(t *testing.T) {
someLogger := zerolog.Nop()
someAuthFunc := func(ctx context.Context) (context.Context, error) {
return nil, fmt.Errorf("expected auth error")
}
var someDispatcher dispatch.Dispatcher

opts := MiddlewareOption{
someLogger,
someAuthFunc,
true,
someDispatcher,
true,
true,
false,
"anotherservice",
nil,
nil,
}

someMiddleware := pertoken.NewMiddleware(nil)

withDS := opts.WithDatastoreMiddleware(someMiddleware)
require.NotNil(t, withDS)
require.NotNil(t, withDS.unaryDatastoreMiddleware)
require.NotNil(t, withDS.streamDatastoreMiddleware)

require.Equal(t, opts.Logger, withDS.Logger)
require.Equal(t, opts.DispatcherForMiddleware, withDS.DispatcherForMiddleware)
require.Equal(t, opts.EnableRequestLog, withDS.EnableRequestLog)
require.Equal(t, opts.EnableResponseLog, withDS.EnableResponseLog)
require.Equal(t, opts.DisableGRPCHistogram, withDS.DisableGRPCHistogram)
require.Equal(t, opts.MiddlewareServiceLabel, withDS.MiddlewareServiceLabel)

_, authError := withDS.AuthFunc(context.Background())
require.Error(t, authError)
require.ErrorContains(t, authError, "expected auth error")
}

0 comments on commit 6a29d88

Please sign in to comment.