Skip to content

Commit

Permalink
test: object owner tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lodek committed Jan 25, 2024
1 parent 655921c commit 33d04e7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
45 changes: 45 additions & 0 deletions x/acp/client/cli/query_object_owner.go
Original file line number Diff line number Diff line change
@@ -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
}
39 changes: 35 additions & 4 deletions x/acp/keeper/query_object_owner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 33d04e7

Please sign in to comment.