Skip to content

Commit

Permalink
Updated for fx-integration in Clortho
Browse files Browse the repository at this point in the history
  • Loading branch information
renaz6 authored and denopink committed Aug 11, 2022
1 parent ece5f22 commit 872eaf3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 81 deletions.
15 changes: 0 additions & 15 deletions basculehttp/bearerTokenFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,3 @@ func ProvideBearerTokenFactory(configKey string, optional bool) fx.Option {
),
)
}

// ProvideResolver is a helper function for wiring up a Clortho Resolver
func ProvideResolver(key string, optional bool) fx.Option {
return fx.Provide(
fx.Annotated{
Name: "key_resolver",
Target: func(in ...clortho.ResolverOption) (clortho.Resolver, error) {
if optional {
return nil, nil
}
return clortho.NewResolver(clortho.WithKeyIDTemplate(key))
},
},
)
}
131 changes: 65 additions & 66 deletions basculehttp/bearerTokenFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/stretchr/testify/require"
"github.com/xmidt-org/arrange"
"github.com/xmidt-org/bascule"
"github.com/xmidt-org/clortho"
"go.uber.org/fx"
)

Expand Down Expand Up @@ -235,72 +234,72 @@ good:
}
}

func TestProvideResolver(t *testing.T) {
type In struct {
fx.In
R clortho.Resolver `name:"key_resolver"`
}
// func TestProvideResolver(t *testing.T) {
// type In struct {
// fx.In
// R clortho.Resolver `name:"key_resolver"`
// }

const yaml = `
good:
factory:
uri: "http://test:1111/keys/{keyId}"
purpose: 0
updateInterval: 604800000000000
`
v := viper.New()
v.SetConfigType("yaml")
require.NoError(t, v.ReadConfig(strings.NewReader(yaml)))
// const yaml = `
// good:
// factory:
// uri: "http://test:1111/keys/{keyId}"
// purpose: 0
// updateInterval: 604800000000000
// `
// v := viper.New()
// v.SetConfigType("yaml")
// require.NoError(t, v.ReadConfig(strings.NewReader(yaml)))

goodResolver, err := clortho.NewResolver(clortho.WithKeyIDTemplate("good"))
require.Nil(t, err)
// goodResolver, err := clortho.NewResolver(clortho.WithKeyIDTemplate("good"))
// require.Nil(t, err)

tests := []struct {
description string
key string
optional bool
expectedResolver clortho.Resolver
expectedErr error
}{
{
description: "Success",
key: "good",
optional: false,
expectedResolver: goodResolver,
},
{
description: "Silent failure",
key: "bad",
optional: true,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
result := In{}
assert := assert.New(t)
require := require.New(t)
app := fx.New(
arrange.TestLogger(t),
arrange.ForViper(v),
ProvideResolver(tc.key, tc.optional),
fx.Invoke(
func(in In) {
result = in
},
),
)
err := app.Err()
// tests := []struct {
// description string
// key string
// optional bool
// expectedResolver clortho.Resolver
// expectedErr error
// }{
// {
// description: "Success",
// key: "good",
// optional: false,
// expectedResolver: goodResolver,
// },
// {
// description: "Silent failure",
// key: "bad",
// optional: true,
// },
// }
// for _, tc := range tests {
// t.Run(tc.description, func(t *testing.T) {
// result := In{}
// assert := assert.New(t)
// require := require.New(t)
// app := fx.New(
// arrange.TestLogger(t),
// arrange.ForViper(v),
// ProvideResolver(tc.key, tc.optional),
// fx.Invoke(
// func(in In) {
// result = in
// },
// ),
// )
// err := app.Err()

assert.Equal(tc.expectedResolver, result.R)
if tc.expectedErr == nil {
assert.NoError(err)
return
}
require.Error(err)
assert.True(strings.Contains(err.Error(), tc.expectedErr.Error()),
fmt.Errorf("error [%v] doesn't contain error [%v]",
err, tc.expectedErr),
)
})
}
}
// assert.Equal(tc.expectedResolver, result.R)
// if tc.expectedErr == nil {
// assert.NoError(err)
// return
// }
// require.Error(err)
// assert.True(strings.Contains(err.Error(), tc.expectedErr.Error()),
// fmt.Errorf("error [%v] doesn't contain error [%v]",
// err, tc.expectedErr),
// )
// })
// }
// }

0 comments on commit 872eaf3

Please sign in to comment.