Skip to content

Commit

Permalink
Add basic query policy ids tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iverc committed Jan 8, 2025
1 parent 4ced454 commit 41173d7
Showing 1 changed file with 186 additions and 0 deletions.
186 changes: 186 additions & 0 deletions x/acp/keeper/query_policy_ids_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package keeper

import (
"context"
"encoding/json"
"strconv"
"testing"

"gopkg.in/yaml.v3"

"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"
)

type queryPolicyIdsSuite struct {
suite.Suite
}

func (s *queryPolicyIdsSuite) setupPolicies(
t *testing.T,
ctx context.Context,
msgServer types.MsgServer,
creator string,
policyNames []string,
marshalingType coretypes.PolicyMarshalingType,
) []string {
policyIds := []string{}

for _, name := range policyNames {
policy := &coretypes.PolicyShort{
Name: name,
Description: "Test policy for " + name,
Meta: map[string]string{
"k1": "v1",
"k2": "v2",
},
Resources: map[string]*coretypes.ResourceShort{
"file": {
Doc: "A test resource",
Permissions: map[string]*coretypes.PermissionShort{
"manage": {
Doc: "Permission to manage resources",
Expr: "owner",
},
},
Relations: map[string]*coretypes.RelationShort{
"owner": {
Doc: "Owner relation",
Manages: []string{"reader"},
Types: []string{
"actor-resource->",
},
},
"reader": {
Doc: "Reader relation",
},
},
},
},
Actor: &coretypes.ActorResource{
Name: "actor-resource",
Doc: "Test actor resource",
},
Version: "1.0",
}

var policyString string
switch marshalingType {
case coretypes.PolicyMarshalingType_SHORT_YAML:
policyYAML, err := yaml.Marshal(policy)
require.NoError(t, err, "failed to marshal policy to YAML")
policyString = string(policyYAML)
case coretypes.PolicyMarshalingType_SHORT_JSON:
policyJSON, err := json.Marshal(policy)
require.NoError(t, err, "failed to marshal policy to JSON")
policyString = string(policyJSON)
default:
t.Fatalf("unsupported marshaling type: %v", marshalingType)
}

msg := types.MsgCreatePolicy{
Creator: creator,
Policy: policyString,
MarshalType: marshalingType,
CreationTime: timestamp,
}

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

policyIds = append(policyIds, resp.Policy.Id)
}

return policyIds
}

func TestPolicyIds(t *testing.T) {
suite.Run(t, &queryPolicyIdsSuite{})
}

func (s *queryPolicyIdsSuite) TestQueryPolicyIds_YAML() {
ctx, keeper, accKeep := setupKeeper(s.T())
msgServer := NewMsgServerImpl(keeper)
creator := accKeep.FirstAcc().GetAddress().String()
policyIds := s.setupPolicies(s.T(), ctx, msgServer, creator, []string{"P1", "P2", "P3"}, coretypes.PolicyMarshalingType_SHORT_YAML)

resp, err := keeper.PolicyIds(ctx, &types.QueryPolicyIdsRequest{})
require.NoError(s.T(), err)
require.NotNil(s.T(), resp)
require.ElementsMatch(s.T(), policyIds, resp.Ids)
}

func (s *queryPolicyIdsSuite) TestQueryPolicyIds_JSON() {
ctx, keeper, accKeep := setupKeeper(s.T())
msgServer := NewMsgServerImpl(keeper)
creator := accKeep.FirstAcc().GetAddress().String()
policyIds := s.setupPolicies(s.T(), ctx, msgServer, creator, []string{"P1", "P2", "P3"}, coretypes.PolicyMarshalingType_SHORT_JSON)

resp, err := keeper.PolicyIds(ctx, &types.QueryPolicyIdsRequest{})
require.NoError(s.T(), err)
require.NotNil(s.T(), resp)
require.ElementsMatch(s.T(), policyIds, resp.Ids)
}

func (s *queryPolicyIdsSuite) TestQueryPolicyIds_NoPoliciesRegistered() {
ctx, keeper, _ := setupKeeper(s.T())

resp, err := keeper.PolicyIds(ctx, &types.QueryPolicyIdsRequest{})

require.NoError(s.T(), err)
require.NotNil(s.T(), resp)
require.Empty(s.T(), resp.Ids)
}

func (s *queryPolicyIdsSuite) TestQueryPolicyIds_DuplicatePolicyNames() {
ctx, keeper, accKeep := setupKeeper(s.T())
msgServer := NewMsgServerImpl(keeper)
creator := accKeep.FirstAcc().GetAddress().String()

_ = s.setupPolicies(s.T(), ctx, msgServer, creator, []string{"P1", "P1"}, coretypes.PolicyMarshalingType_SHORT_YAML)

resp, err := keeper.PolicyIds(ctx, &types.QueryPolicyIdsRequest{})
require.NoError(s.T(), err)
require.NotNil(s.T(), resp)
require.Equal(s.T(), 2, len(resp.Ids))
}

func (s *queryPolicyIdsSuite) TestQueryPolicyIds_LargeNumberOfPolicies_JSON() {
ctx, keeper, accKeep := setupKeeper(s.T())
msgServer := NewMsgServerImpl(keeper)
creator := accKeep.FirstAcc().GetAddress().String()

names := []string{}
for i := 0; i < 10_000; i++ {
names = append(names, "Policy"+strconv.Itoa(i))
}

policyIds := s.setupPolicies(s.T(), ctx, msgServer, creator, names, coretypes.PolicyMarshalingType_SHORT_JSON)

resp, err := keeper.PolicyIds(ctx, &types.QueryPolicyIdsRequest{})
require.NoError(s.T(), err)
require.NotNil(s.T(), resp)
require.ElementsMatch(s.T(), policyIds, resp.Ids)
}

func (s *queryPolicyIdsSuite) TestQueryPolicyIds_LargeNumberOfPolicies_YAML() {
ctx, keeper, accKeep := setupKeeper(s.T())
msgServer := NewMsgServerImpl(keeper)
creator := accKeep.FirstAcc().GetAddress().String()

names := []string{}
for i := 0; i < 10_000; i++ {
names = append(names, "Policy"+strconv.Itoa(i))
}

policyIds := s.setupPolicies(s.T(), ctx, msgServer, creator, names, coretypes.PolicyMarshalingType_SHORT_YAML)

resp, err := keeper.PolicyIds(ctx, &types.QueryPolicyIdsRequest{})
require.NoError(s.T(), err)
require.NotNil(s.T(), resp)
require.ElementsMatch(s.T(), policyIds, resp.Ids)
}

0 comments on commit 41173d7

Please sign in to comment.