Skip to content

Commit

Permalink
Merge pull request #9 from aserto-dev/directory-client-tests
Browse files Browse the repository at this point in the history
move test files to _test package namespace
  • Loading branch information
ronenh committed Oct 16, 2023
2 parents d0a68c1 + 379f1c9 commit e7bd7b1
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 63 deletions.
63 changes: 32 additions & 31 deletions client/connection_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client //nolint: testpackage
package client_test

import (
"bytes"
Expand All @@ -19,6 +19,7 @@ import (
"testing"
"time"

"github.com/aserto-dev/go-aserto/client"
"github.com/aserto-dev/header"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
Expand All @@ -32,7 +33,7 @@ type dialRecorder struct {
address string
tlsConf *tls.Config
callerCreds credentials.PerRPCCredentials
connection *Connection
connection *client.Connection
dialOptions []grpc.DialOption
}

Expand All @@ -41,7 +42,7 @@ func (d *dialRecorder) DialContext(
address string,
tlsConf *tls.Config,
callerCreds credentials.PerRPCCredentials,
connection *Connection,
connection *client.Connection,
options []grpc.DialOption,
) (grpc.ClientConnInterface, error) {
d.context = ctx
Expand All @@ -56,10 +57,10 @@ func (d *dialRecorder) DialContext(

func TestWithAddr(t *testing.T) {
recorder := &dialRecorder{}
options, err := NewConnectionOptions(WithAddr("address"))
options, err := client.NewConnectionOptions(client.WithAddr("address"))
assert.NoError(t, err)

newConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck
client.InternalNewConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck

assert.Equal(t, "address", recorder.address)
}
Expand All @@ -71,9 +72,9 @@ func TestWithURL(t *testing.T) {
svcURL, err := url.Parse(URL)
assert.NoError(t, err)

options, err := NewConnectionOptions(WithURL(svcURL))
options, err := client.NewConnectionOptions(client.WithURL(svcURL))
assert.NoError(t, err)
newConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck
client.InternalNewConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck

assert.Equal(t, URL, recorder.address)
}
Expand All @@ -82,24 +83,24 @@ func TestAddrAndURL(t *testing.T) {
svcURL, err := url.Parse("https://server.com:123")
assert.NoError(t, err)

_, err = NewConnectionOptions(WithAddr("address"), WithURL(svcURL))
_, err = client.NewConnectionOptions(client.WithAddr("address"), client.WithURL(svcURL))
assert.Error(t, err)
}

func TestWithInsecure(t *testing.T) {
recorder := &dialRecorder{}
options, err := NewConnectionOptions(WithInsecure(true))
options, err := client.NewConnectionOptions(client.WithInsecure(true))
assert.NoError(t, err)
newConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck
client.InternalNewConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck

assert.True(t, recorder.tlsConf.InsecureSkipVerify)
}

func TestWithTokenAuth(t *testing.T) {
recorder := &dialRecorder{}
options, err := NewConnectionOptions(WithTokenAuth("<token>"))
options, err := client.NewConnectionOptions(client.WithTokenAuth("<token>"))
assert.NoError(t, err)
newConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck
client.InternalNewConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck

md, err := recorder.callerCreds.GetRequestMetadata(context.TODO())
assert.NoError(t, err)
Expand All @@ -111,9 +112,9 @@ func TestWithTokenAuth(t *testing.T) {

func TestWithBearerTokenAuth(t *testing.T) {
recorder := &dialRecorder{}
options, err := NewConnectionOptions(WithTokenAuth("bearer <token>"))
options, err := client.NewConnectionOptions(client.WithTokenAuth("bearer <token>"))
assert.NoError(t, err)
newConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck
client.InternalNewConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck

md, err := recorder.callerCreds.GetRequestMetadata(context.TODO())
assert.NoError(t, err)
Expand All @@ -125,9 +126,9 @@ func TestWithBearerTokenAuth(t *testing.T) {

func TestWithAPIKey(t *testing.T) {
recorder := &dialRecorder{}
options, err := NewConnectionOptions(WithAPIKeyAuth("<apikey>"))
options, err := client.NewConnectionOptions(client.WithAPIKeyAuth("<apikey>"))
assert.NoError(t, err)
newConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck
client.InternalNewConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck

md, err := recorder.callerCreds.GetRequestMetadata(context.TODO())
assert.NoError(t, err)
Expand All @@ -138,20 +139,20 @@ func TestWithAPIKey(t *testing.T) {
}

func TestTokenAndAPIKey(t *testing.T) {
_, err := NewConnectionOptions(WithAPIKeyAuth("<apikey>"), WithTokenAuth("<token>"))
_, err := client.NewConnectionOptions(client.WithAPIKeyAuth("<apikey>"), client.WithTokenAuth("<token>"))
assert.Error(t, err)
}

func TestWithTenantID(t *testing.T) {
recorder := &dialRecorder{}
options, err := NewConnectionOptions(WithTenantID("<tenantid>"))
options, err := client.NewConnectionOptions(client.WithTenantID("<tenantid>"))
assert.NoError(t, err)
newConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck
client.InternalNewConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck

assert.Equal(t, "<tenantid>", recorder.connection.TenantID)

ctx := context.TODO()
recorder.connection.unary( //nolint: errcheck, dupl
recorder.connection.InternalUnary( //nolint: errcheck, dupl
ctx,
"method",
"request",
Expand All @@ -173,7 +174,7 @@ func TestWithTenantID(t *testing.T) {
return nil
})

recorder.connection.stream( //nolint: errcheck
recorder.connection.InternalStream( //nolint: errcheck
ctx,
nil,
recorder.connection.Conn.(*grpc.ClientConn),
Expand Down Expand Up @@ -202,14 +203,14 @@ func TestWithTenantID(t *testing.T) {

func TestWithSessionID(t *testing.T) {
recorder := &dialRecorder{}
options, err := NewConnectionOptions(WithSessionID("<sessionid>"))
options, err := client.NewConnectionOptions(client.WithSessionID("<sessionid>"))
assert.NoError(t, err)
newConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck
client.InternalNewConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck

assert.Equal(t, "<sessionid>", recorder.connection.SessionID)

ctx := context.TODO()
recorder.connection.unary( //nolint: errcheck, dupl
recorder.connection.InternalUnary( //nolint: errcheck, dupl
ctx,
"method",
"request",
Expand All @@ -231,7 +232,7 @@ func TestWithSessionID(t *testing.T) {
return nil
})

recorder.connection.stream( //nolint: errcheck
recorder.connection.InternalStream( //nolint: errcheck
ctx,
nil,
recorder.connection.Conn.(*grpc.ClientConn),
Expand Down Expand Up @@ -276,9 +277,9 @@ func TestWithCACertPath(t *testing.T) {
assert.NoError(t, err, "Failed to save certificate")

recorder := &dialRecorder{}
options, err := NewConnectionOptions(WithCACertPath(caPath))
options, err := client.NewConnectionOptions(client.WithCACertPath(caPath))
assert.NoError(t, err)
newConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck
client.InternalNewConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck

inPool, err := subjectInCertPool(recorder.tlsConf.RootCAs, CertSubjectName)
if err != nil {
Expand All @@ -304,9 +305,9 @@ func TestWithCACertPathAndInsecure(t *testing.T) {
assert.NoError(t, err, "Failed to save certificate")

recorder := &dialRecorder{}
options, err := NewConnectionOptions(WithCACertPath(caPath), WithInsecure(true))
options, err := client.NewConnectionOptions(client.WithCACertPath(caPath), client.WithInsecure(true))
assert.NoError(t, err)
newConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck
client.InternalNewConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck

assert.Nil(t, recorder.tlsConf.RootCAs, "Aserto cert should be nil")
assert.True(t, recorder.tlsConf.InsecureSkipVerify)
Expand All @@ -316,9 +317,9 @@ func TestWithDialOptions(t *testing.T) {
recorder := &dialRecorder{}
creds := grpc.WithTransportCredentials(insecure.NewCredentials())

options, err := NewConnectionOptions(WithDialOptions(creds))
options, err := client.NewConnectionOptions(client.WithDialOptions(creds))
assert.NoError(t, err)
newConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck
client.InternalNewConnection(context.TODO(), recorder.DialContext, options) //nolint: errcheck
assert.Contains(t, recorder.dialOptions, creds)
}

Expand Down
7 changes: 4 additions & 3 deletions client/directory/internal/connections_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal //nolint: testpackage
package internal_test

import (
"context"
Expand All @@ -7,13 +7,14 @@ import (
asserts "github.com/stretchr/testify/assert"

"github.com/aserto-dev/go-aserto/client"
"github.com/aserto-dev/go-aserto/client/directory/internal"
)

func TestConnections(t *testing.T) {
ctx := context.Background()

counter := &ConnectCounter{}
conns := NewConnections()
counter := &internal.ConnectCounter{}
conns := internal.NewConnections()
conns.Connect = counter.Connect

cfg := &client.Config{Address: "localhost:8282"}
Expand Down
21 changes: 11 additions & 10 deletions client/directory/v2/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package directory //nolint: testpackage
package directory_test

import (
"context"
Expand All @@ -9,6 +9,7 @@ import (

"github.com/aserto-dev/go-aserto/client"
"github.com/aserto-dev/go-aserto/client/directory/internal"
"github.com/aserto-dev/go-aserto/client/directory/v2"
)

const (
Expand All @@ -21,7 +22,7 @@ func TestUnmarshalConfig(t *testing.T) {
t.Run("base only", func(t *testing.T) {
assert := asserts.New(t)

cfg := Config{}
cfg := directory.Config{}
if err := json.Unmarshal([]byte(base), &cfg); err != nil {
assert.FailNow("failed to unmarshal config", err)
}
Expand All @@ -36,7 +37,7 @@ func TestUnmarshalConfig(t *testing.T) {
t.Run("no base", func(t *testing.T) {
assert := asserts.New(t)

var cfg Config
var cfg directory.Config
if err := json.Unmarshal([]byte(noBase), &cfg); err != nil {
assert.FailNow("failed to unmarshal config", err)
}
Expand All @@ -53,7 +54,7 @@ func TestUnmarshalConfig(t *testing.T) {
t.Run("overrides", func(t *testing.T) {
assert := asserts.New(t)

var cfg Config
var cfg directory.Config
if err := json.Unmarshal([]byte(overrides), &cfg); err != nil {
assert.FailNow("failed to unmarshal config", err)
}
Expand All @@ -76,11 +77,11 @@ func TestConnect(t *testing.T) {

conns, counter := mockConns()

cfg := &Config{
cfg := &directory.Config{
Config: &client.Config{Address: "localhost:8282"},
}

dir, err := connect(ctx, conns, cfg)
dir, err := directory.InternalConnect(ctx, conns, cfg)
assert.NoError(err)
assert.NotNil(dir)
assert.NotNil(dir.Reader)
Expand All @@ -95,12 +96,12 @@ func TestConnect(t *testing.T) {

conns, counter := mockConns()

cfg := &Config{
cfg := &directory.Config{
Config: &client.Config{Address: "localhost:1234"},
Reader: &client.Config{Address: "localhost:4321"},
}

dir, err := connect(ctx, conns, cfg)
dir, err := directory.InternalConnect(ctx, conns, cfg)
assert.NoError(err)
assert.NotNil(dir)
assert.NotNil(dir.Reader)
Expand All @@ -115,11 +116,11 @@ func TestConnect(t *testing.T) {

conns, counter := mockConns()

cfg := &Config{
cfg := &directory.Config{
Reader: &client.Config{Address: "localhost:9292"},
}

dir, err := connect(ctx, conns, cfg)
dir, err := directory.InternalConnect(ctx, conns, cfg)
assert.NoError(err)
assert.NotNil(dir)
assert.NotNil(dir.Reader)
Expand Down
11 changes: 11 additions & 0 deletions client/directory/v2/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package directory

import (
"context"

"github.com/aserto-dev/go-aserto/client/directory/internal"
)

func InternalConnect(ctx context.Context, conns *internal.Connections, cfg *Config) (*Client, error) {
return connect(ctx, conns, cfg)
}
36 changes: 36 additions & 0 deletions client/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package client

import (
"context"

"google.golang.org/grpc"
)

func InternalNewConnection(ctx context.Context,
dialContext dialer,
options *ConnectionOptions,
) (*Connection, error) {
return newConnection(ctx, dialContext, options)
}

func (c *Connection) InternalUnary(
ctx context.Context,
method string,
req, reply interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
return c.unary(ctx, method, req, reply, cc, invoker, opts...)
}

func (c *Connection) InternalStream(
ctx context.Context,
desc *grpc.StreamDesc,
cc *grpc.ClientConn,
method string,
streamer grpc.Streamer,
opts ...grpc.CallOption,
) (grpc.ClientStream, error) {
return c.stream(ctx, desc, cc, method, streamer, opts...)
}
11 changes: 11 additions & 0 deletions middleware/grpc/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package grpc

import (
"context"

"github.com/aserto-dev/go-authorizer/aserto/authorizer/v2/api"
)

func (b *IdentityBuilder) InternalBuild(ctx context.Context, req interface{}) *api.IdentityContext {
return b.build(ctx, req)
}
Loading

0 comments on commit e7bd7b1

Please sign in to comment.