Skip to content

Commit

Permalink
Add more query policy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iverc committed Jan 8, 2025
1 parent b2272cb commit 4ced454
Showing 1 changed file with 75 additions and 4 deletions.
79 changes: 75 additions & 4 deletions x/acp/keeper/query_policy_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,94 @@
package keeper

import (
"context"
"testing"

"github.com/sourcenetwork/acp_core/pkg/errors"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

coretypes "github.com/sourcenetwork/acp_core/pkg/types"
"github.com/sourcenetwork/sourcehub/x/acp/types"
)

func TestQueryPolicy_UnknownPolicyReturnsPolicyNotFoundErr(t *testing.T) {
ctx, k, _ := setupKeeper(t)
type queryPolicySuite struct {
suite.Suite
}

func (s *queryPolicySuite) setupPolicy(t *testing.T) (context.Context, Keeper, string) {
policyStr := `
name: Source Policy
description: A valid policy
resources:
file:
relations:
owner:
types:
- actor
rm-root:
permissions:
read:
expr: owner
write:
expr: owner
`

ctx, keeper, accKeep := setupKeeper(t)
creator := accKeep.FirstAcc().GetAddress().String()

msg := types.MsgCreatePolicy{
Creator: creator,
Policy: policyStr,
MarshalType: coretypes.PolicyMarshalingType_SHORT_YAML,
CreationTime: timestamp,
}

msgServer := NewMsgServerImpl(keeper)
resp, err := msgServer.CreatePolicy(ctx, &msg)
require.NoError(t, err)

return ctx, keeper, resp.Policy.Id
}

func TestQueryPolicy(t *testing.T) {
suite.Run(t, &queryPolicySuite{})
}

func (s *queryPolicySuite) TestQueryPolicy_Success() {
ctx, keeper, policyID := s.setupPolicy(s.T())

req := types.QueryPolicyRequest{
Id: policyID,
}
resp, err := keeper.Policy(ctx, &req)

require.NoError(s.T(), err)
require.NotNil(s.T(), resp)
require.Equal(s.T(), "Source Policy", resp.Policy.Name)
require.Equal(s.T(), "A valid policy", resp.Policy.Description)
}

func (s *queryPolicySuite) TestQueryPolicy_UnknownPolicyReturnsPolicyNotFoundErr() {
ctx, k, _ := setupKeeper(s.T())

req := types.QueryPolicyRequest{
Id: "not found",
}

resp, err := k.Policy(ctx, &req)

require.Nil(t, resp)
require.ErrorIs(t, err, errors.ErrorType_NOT_FOUND)
require.Nil(s.T(), resp)
require.ErrorIs(s.T(), err, errors.ErrorType_NOT_FOUND)
require.Contains(s.T(), err.Error(), "policy not found")
}

func (s *queryPolicySuite) TestQueryPolicy_NilRequestReturnsInvalidRequestErr() {
ctx, k, _ := setupKeeper(s.T())

resp, err := k.Policy(ctx, nil)

require.Nil(s.T(), resp)
require.Error(s.T(), err)
require.Contains(s.T(), err.Error(), "invalid request")
}

0 comments on commit 4ced454

Please sign in to comment.