From 33d04e70ba4d80c0cb4e32c0771eac6fb46892e1 Mon Sep 17 00:00:00 2001 From: Bruno Gomes Date: Thu, 25 Jan 2024 15:44:14 -0300 Subject: [PATCH] test: object owner tests --- go.mod | 2 +- go.sum | 4 +-- x/acp/client/cli/query_object_owner.go | 45 +++++++++++++++++++++++++ x/acp/keeper/query_object_owner_test.go | 39 ++++++++++++++++++--- 4 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 x/acp/client/cli/query_object_owner.go diff --git a/go.mod b/go.mod index 0df4aa9..07953de 100644 --- a/go.mod +++ b/go.mod @@ -40,7 +40,7 @@ require ( github.com/hyperledger/aries-framework-go v0.3.2 github.com/multiformats/go-varint v0.0.7 github.com/sourcenetwork/raccoondb v0.2.0 - github.com/sourcenetwork/zanzi v0.2.1 + github.com/sourcenetwork/zanzi v0.3.0 github.com/spf13/cobra v1.8.0 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.17.0 diff --git a/go.sum b/go.sum index f48093c..c9aac9b 100644 --- a/go.sum +++ b/go.sum @@ -1091,8 +1091,8 @@ github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9yS github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= github.com/sourcenetwork/raccoondb v0.2.0 h1:lQ/r8IUm1IMaivXWhqndgpisLsI59c6M9jn6ujKYBzk= github.com/sourcenetwork/raccoondb v0.2.0/go.mod h1:A5ElVAhdf9yDjmpLrA3DLqYib09Fnuzm3sFUbY5r9BE= -github.com/sourcenetwork/zanzi v0.2.1 h1:G0nv0Wq4ohkp2cKs9YAvah3WfhOU61BweAlcyKqfahM= -github.com/sourcenetwork/zanzi v0.2.1/go.mod h1:qbnviR4096fLkuQs2Qmbux530Yuq5GMfWDnDIyzsQ+8= +github.com/sourcenetwork/zanzi v0.3.0 h1:Y9uyrpsT569QjzAxNOwWDxeWOkcntm+26qDLR7nGuo4= +github.com/sourcenetwork/zanzi v0.3.0/go.mod h1:eLQ94tdz96vfwHIZXL5ZoHbV9YHQeMyFeTc5hFSGDRU= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= diff --git a/x/acp/client/cli/query_object_owner.go b/x/acp/client/cli/query_object_owner.go new file mode 100644 index 0000000..61e4fb0 --- /dev/null +++ b/x/acp/client/cli/query_object_owner.go @@ -0,0 +1,45 @@ +package cli + +import ( + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/spf13/cobra" + + "github.com/sourcenetwork/sourcehub/x/acp/types" +) + +func CmdQueryOjectOwner() *cobra.Command { + cmd := &cobra.Command{ + Use: "object-owner policy-id resource object-id", + Short: "queries an object for its owner", + Args: cobra.ExactArgs(3), + RunE: func(cmd *cobra.Command, args []string) error { + polId := args[0] + resource := args[1] + objId := args[2] + + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + + queryClient := types.NewQueryClient(clientCtx) + + req := types.QueryObjectOwnerRequest{ + PolicyId: polId, + Object: types.NewObject(resource, objId), + } + + res, err := queryClient.ObjectOwner(cmd.Context(), &req) + if err != nil { + return err + } + + return clientCtx.PrintProto(res) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} diff --git a/x/acp/keeper/query_object_owner_test.go b/x/acp/keeper/query_object_owner_test.go index 01f1d6a..280733d 100644 --- a/x/acp/keeper/query_object_owner_test.go +++ b/x/acp/keeper/query_object_owner_test.go @@ -92,10 +92,41 @@ func (s *queryObjectOwnerSuite) TestQueryReturnsObjectOwner() { }) } -// Test Query Unregistered Object +func (s *queryObjectOwnerSuite) TestQueryingForUnregisteredObjectReturnsEmptyOwner() { + ctx, keeper, policyId := s.setup(s.T()) + + resp, err := keeper.ObjectOwner(ctx, &types.QueryObjectOwnerRequest{ + PolicyId: policyId, + Object: types.NewObject("file", "404"), + }) + + require.Nil(s.T(), err) + require.Equal(s.T(), resp, &types.QueryObjectOwnerResponse{ + IsRegistered: false, + OwnerId: "", + }) +} + +func (s *queryObjectOwnerSuite) TestQueryingPolicyThatDoesNotExistReturnError() { + ctx, keeper, _ := s.setup(s.T()) -// Test Query on invalid object (?) + resp, err := keeper.ObjectOwner(ctx, &types.QueryObjectOwnerRequest{ + PolicyId: "some-policy", + Object: s.obj, + }) -// Test Query missing policy + require.ErrorIs(s.T(), err, types.ErrPolicyNotFound) + require.Nil(s.T(), resp) +} + +func (s *queryObjectOwnerSuite) TestQueryingForObjectInNonExistingPolicyReturnsError() { + ctx, keeper, policyId := s.setup(s.T()) -// Test Qury missing resource + resp, err := keeper.ObjectOwner(ctx, &types.QueryObjectOwnerRequest{ + PolicyId: policyId, + Object: types.NewObject("missing-resource", "abc"), + }) + + require.Nil(s.T(), resp) + require.NotNil(s.T(), err) +}