Skip to content

Commit

Permalink
Merge 1fffe1d into 0a6402a
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored Jun 9, 2022
2 parents 0a6402a + 1fffe1d commit 993d26b
Show file tree
Hide file tree
Showing 25 changed files with 157 additions and 249 deletions.
2 changes: 1 addition & 1 deletion cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func NewContainer(v *viper.Viper, userOptions ...fx.Option) *fx.App {
sharedauth.NewIntrospectionValidator(
oauth2introspect.NewIntrospecter(v.GetString(authBearerIntrospectUrlFlag)),
v.GetBool(authBearerAudiencesWildcardFlag),
v.GetStringSlice(authBearerAudienceFlag)...,
sharedauth.AudienceIn(v.GetStringSlice(authBearerAudienceFlag)...),
),
))
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ require (
github.com/go-redis/redis/v8 v8.11.4
github.com/go-redis/redismock/v8 v8.0.6
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/numary/go-libs v0.0.0-20220511113055-4f47b504fa62
github.com/numary/go-libs v0.0.0-20220609100906-5d494c9ab0ba
github.com/uptrace/opentelemetry-go-extra/otellogrus v0.1.12
github.com/xdg-go/scram v1.1.0
gopkg.in/segmentio/analytics-go.v3 v3.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,8 @@ github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxzi
github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/numary/go-libs v0.0.0-20220511113055-4f47b504fa62 h1:Wc4FFjF9veiiBTnuqpeSqnX09Ek39zxgta1RceZZaOw=
github.com/numary/go-libs v0.0.0-20220511113055-4f47b504fa62/go.mod h1:7StJNTZ3QbU2uBWpOeryPaHD6xMYUN8AWXWTjP67kv0=
github.com/numary/go-libs v0.0.0-20220609100906-5d494c9ab0ba h1:KVqCQm9byYaJ174bCaoTFLXMhyE0+HIRFIKkydQpD9E=
github.com/numary/go-libs v0.0.0-20220609100906-5d494c9ab0ba/go.mod h1:7StJNTZ3QbU2uBWpOeryPaHD6xMYUN8AWXWTjP67kv0=
github.com/numary/ledger v0.0.0-20210702172952-a5bd30e551d0/go.mod h1:u2K28z9TDYd6id1qeD2uv7JDlajuRZ0fvOnCeDZmDxk=
github.com/numary/ledger v0.0.0-20211227131550-dc7b78f85b5b/go.mod h1:uovuDsK7Gs7duqKQ9PgaFulJnPTDftGdR/n3rBRzNIs=
github.com/numary/machine v0.0.0-20210702091459-23a82555adbf/go.mod h1:WAFvefAGYNjdDmPtDoZ305F58QDtUJyB0QWN3vzSZao=
Expand Down
10 changes: 4 additions & 6 deletions pkg/api/controllers/account_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import (
"github.com/numary/ledger/pkg/ledger/query"
)

type AccountController struct {
BaseController
}
type AccountController struct{}

func NewAccountController() AccountController {
return AccountController{}
Expand Down Expand Up @@ -49,7 +47,7 @@ func (ctl *AccountController) GetAccounts(c *gin.Context) {
return
}

ctl.response(c, http.StatusOK, cursor)
respondWithCursor[core.Account](c, http.StatusOK, cursor)
}

func (ctl *AccountController) GetAccount(c *gin.Context) {
Expand All @@ -63,7 +61,7 @@ func (ctl *AccountController) GetAccount(c *gin.Context) {
return
}

ctl.response(c, http.StatusOK, acc)
respondWithData[core.Account](c, http.StatusOK, acc)
}

func (ctl *AccountController) PostAccountMetadata(c *gin.Context) {
Expand All @@ -86,5 +84,5 @@ func (ctl *AccountController) PostAccountMetadata(c *gin.Context) {
return
}

ctl.noContent(c)
respondWithNoContent(c)
}
69 changes: 25 additions & 44 deletions pkg/api/controllers/account_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ import (
"go.uber.org/fx"
)

type GetAccountsCursor struct {
PageSize int `json:"page_size,omitempty"`
Previous string `json:"previous,omitempty"`
Next string `json:"next,omitempty"`
Data []core.Account `json:"data"`
}

type getAccountsResponse struct {
Cursor *GetAccountsCursor `json:"cursor,omitempty"`
}

func TestGetAccounts(t *testing.T) {
internal.RunTest(t, fx.Invoke(func(lc fx.Lifecycle, api *api.API) {
lc.Append(fx.Hook{
Expand Down Expand Up @@ -69,69 +58,64 @@ func TestGetAccounts(t *testing.T) {
t.Run("all", func(t *testing.T) {
rsp = internal.GetAccounts(api, url.Values{})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getAccountsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
// 3 accounts: world, bob, alice
assert.Len(t, resp.Cursor.Data, 3)
assert.Equal(t, resp.Cursor.Data[0].Address, "world")
assert.Equal(t, resp.Cursor.Data[1].Address, "bob")
assert.Equal(t, resp.Cursor.Data[2].Address, "alice")
assert.Len(t, cursor.Data, 3)
assert.Equal(t, cursor.Data[0].Address, "world")
assert.Equal(t, cursor.Data[1].Address, "bob")
assert.Equal(t, cursor.Data[2].Address, "alice")
})

t.Run("meta roles", func(t *testing.T) {
rsp = internal.GetAccounts(api, url.Values{
"metadata[roles]": []string{"admin"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getAccountsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
// 1 accounts: bob
assert.Len(t, resp.Cursor.Data, 1)
assert.Equal(t, resp.Cursor.Data[0].Address, "bob")
assert.Len(t, cursor.Data, 1)
assert.Equal(t, cursor.Data[0].Address, "bob")
})

t.Run("meta accountId", func(t *testing.T) {
rsp = internal.GetAccounts(api, url.Values{
"metadata[accountId]": []string{"3"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getAccountsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
// 1 accounts: bob
assert.Len(t, resp.Cursor.Data, 1)
assert.Equal(t, resp.Cursor.Data[0].Address, "bob")
assert.Len(t, cursor.Data, 1)
assert.Equal(t, cursor.Data[0].Address, "bob")
})

t.Run("meta enabled", func(t *testing.T) {
rsp = internal.GetAccounts(api, url.Values{
"metadata[enabled]": []string{"true"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getAccountsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
// 1 accounts: bob
assert.Len(t, resp.Cursor.Data, 1)
assert.Equal(t, resp.Cursor.Data[0].Address, "bob")
assert.Len(t, cursor.Data, 1)
assert.Equal(t, cursor.Data[0].Address, "bob")
})

t.Run("meta nested", func(t *testing.T) {
rsp = internal.GetAccounts(api, url.Values{
"metadata[a.nested.key]": []string{"hello"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getAccountsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
// 1 accounts: bob
assert.Len(t, resp.Cursor.Data, 1)
assert.Equal(t, resp.Cursor.Data[0].Address, "bob")
assert.Len(t, cursor.Data, 1)
assert.Equal(t, cursor.Data[0].Address, "bob")
})

t.Run("meta unknown", func(t *testing.T) {
rsp = internal.GetAccounts(api, url.Values{
"metadata[unknown]": []string{"key"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
cursor := internal.DecodeCursorResponse(t, rsp.Body, core.Account{})
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
assert.Len(t, cursor.Data, 0)
})

Expand All @@ -140,23 +124,21 @@ func TestGetAccounts(t *testing.T) {
"after": []string{"bob"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getAccountsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
// 1 accounts: alice
assert.Len(t, resp.Cursor.Data, 1)
assert.Equal(t, resp.Cursor.Data[0].Address, "alice")
assert.Len(t, cursor.Data, 1)
assert.Equal(t, cursor.Data[0].Address, "alice")
})

t.Run("address", func(t *testing.T) {
rsp = internal.GetAccounts(api, url.Values{
"address": []string{"b.b"},
})
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := getAccountsResponse{}
assert.NoError(t, json.Unmarshal(rsp.Body.Bytes(), &resp))
cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body)
// 1 accounts: bob
assert.Len(t, resp.Cursor.Data, 1)
assert.Equal(t, resp.Cursor.Data[0].Address, "bob")
assert.Len(t, cursor.Data, 1)
assert.Equal(t, cursor.Data[0].Address, "bob")
})

return nil
Expand Down Expand Up @@ -188,8 +170,7 @@ func TestGetAccount(t *testing.T) {

rsp = internal.GetAccount(api, "alice")
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)
resp := core.Account{}
internal.DecodeSingleResponse(t, rsp.Body, &resp)
resp, _ := internal.DecodeSingleResponse[core.Account](t, rsp.Body)

assert.EqualValues(t, core.Account{
Address: "alice",
Expand Down
33 changes: 13 additions & 20 deletions pkg/api/controllers/base_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package controllers
import (
"context"
"net/http"
"reflect"

"github.com/gin-gonic/gin"
"github.com/numary/go-libs/sharedapi"
Expand All @@ -12,22 +11,20 @@ import (
"github.com/pkg/errors"
)

type BaseController struct{}
func respondWithNoContent(c *gin.Context) {
c.Status(http.StatusNoContent)
}

func (ctl *BaseController) response(c *gin.Context, status int, data interface{}) {
if data == nil {
c.Status(status)
}
if reflect.TypeOf(data) == reflect.TypeOf(sharedapi.Cursor{}) {
cursor := data.(sharedapi.Cursor)
c.JSON(status, sharedapi.BaseResponse{
Cursor: &cursor,
})
} else {
c.JSON(status, sharedapi.BaseResponse{
Data: data,
})
}
func respondWithCursor[T any](c *gin.Context, status int, data sharedapi.Cursor[T]) {
c.JSON(status, sharedapi.BaseResponse[T]{
Cursor: &data,
})
}

func respondWithData[T any](c *gin.Context, status int, data T) {
c.JSON(status, sharedapi.BaseResponse[T]{
Data: &data,
})
}

const (
Expand All @@ -41,10 +38,6 @@ const (
errorCodeKey = "_errorCode"
)

func (ctl *BaseController) noContent(c *gin.Context) {
c.Status(http.StatusNoContent)
}

func coreErrorToErrorCode(err error) (int, string) {
switch {
case ledger.IsConflictError(err):
Expand Down
21 changes: 8 additions & 13 deletions pkg/api/controllers/config_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type LedgerStorage struct {
}

type ConfigController struct {
BaseController
Version string
StorageDriver storage.Driver
}
Expand All @@ -44,20 +43,16 @@ func (ctl *ConfigController) GetInfo(c *gin.Context) {
if err != nil {
panic(err)
}
ctl.response(
c,
http.StatusOK,
ConfigInfo{
Server: "numary-ledger",
Version: ctl.Version,
Config: &Config{
LedgerStorage: &LedgerStorage{
Driver: ctl.StorageDriver.Name(),
Ledgers: ledgers,
},
c.JSON(http.StatusOK, ConfigInfo{
Server: "numary-ledger",
Version: ctl.Version,
Config: &Config{
LedgerStorage: &LedgerStorage{
Driver: ctl.StorageDriver.Name(),
Ledgers: ledgers,
},
},
)
})
}

//go:embed swagger.yaml
Expand Down
5 changes: 4 additions & 1 deletion pkg/api/controllers/config_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers_test

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

Expand All @@ -10,6 +11,7 @@ import (
"github.com/numary/ledger/pkg/api/internal"
"github.com/numary/ledger/pkg/storage"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/fx"
)

Expand All @@ -21,7 +23,8 @@ func TestGetInfo(t *testing.T) {
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)

info := controllers.ConfigInfo{}
internal.DecodeSingleResponse(t, rsp.Body, &info)
require.NoError(t, json.NewDecoder(rsp.Body).Decode(&info))

info.Config.LedgerStorage.Ledgers = []string{}
assert.EqualValues(t, controllers.ConfigInfo{
Server: "numary-ledger",
Expand Down
7 changes: 2 additions & 5 deletions pkg/api/controllers/ledger_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"github.com/numary/ledger/pkg/ledger"
)

type LedgerController struct {
BaseController
}
type LedgerController struct{}

func NewLedgerController() LedgerController {
return LedgerController{}
Expand All @@ -23,6 +21,5 @@ func (ctl *LedgerController) GetStats(c *gin.Context) {
ResponseError(c, err)
return
}

ctl.response(c, http.StatusOK, stats)
respondWithData[ledger.Stats](c, http.StatusOK, stats)
}
4 changes: 2 additions & 2 deletions pkg/api/controllers/ledger_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func TestGetStats(t *testing.T) {
rsp = internal.GetStats(h)
assert.Equal(t, http.StatusOK, rsp.Result().StatusCode)

stats := ledger.Stats{}
internal.DecodeSingleResponse(t, rsp.Body, &stats)
stats, _ := internal.DecodeSingleResponse[ledger.Stats](t, rsp.Body)

assert.EqualValues(t, ledger.Stats{
Transactions: 2,
Accounts: 3,
Expand Down
10 changes: 5 additions & 5 deletions pkg/api/controllers/mapping_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"github.com/numary/ledger/pkg/ledger"
)

type MappingController struct {
BaseController
}
type MappingController struct{}

func NewMappingController() MappingController {
return MappingController{}
Expand All @@ -31,7 +29,8 @@ func (ctl *MappingController) PutMapping(c *gin.Context) {
ResponseError(c, err)
return
}
ctl.response(c, http.StatusOK, mapping)

respondWithData[*core.Mapping](c, http.StatusOK, mapping)
}

func (ctl *MappingController) GetMapping(c *gin.Context) {
Expand All @@ -42,5 +41,6 @@ func (ctl *MappingController) GetMapping(c *gin.Context) {
ResponseError(c, err)
return
}
ctl.response(c, http.StatusOK, mapping)

respondWithData[*core.Mapping](c, http.StatusOK, mapping)
}
Loading

0 comments on commit 993d26b

Please sign in to comment.