Skip to content

Commit

Permalink
Refactor AccessDecision, FilterRelationships, ValidatePolicy, and Ver…
Browse files Browse the repository at this point in the history
…ifyAccessRequest queries, update VerifyAccessRequest tests
  • Loading branch information
iverc committed Jan 15, 2025
1 parent 2803c6e commit e2fa8ab
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
4 changes: 2 additions & 2 deletions x/acp/keeper/query_access_decision.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"github.com/sourcenetwork/sourcehub/x/acp/types"
)

func (k Keeper) AccessDecision(goCtx context.Context, req *types.QueryAccessDecisionRequest) (*types.QueryAccessDecisionResponse, error) {
func (q Querier) AccessDecision(goCtx context.Context, req *types.QueryAccessDecisionRequest) (*types.QueryAccessDecisionResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
repository := k.GetAccessDecisionRepository(ctx)
repository := q.GetAccessDecisionRepository(ctx)

decision, err := repository.Get(goCtx, req.Id)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions x/acp/keeper/query_filter_relationships.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
"github.com/sourcenetwork/sourcehub/x/acp/types"
)

func (k Keeper) FilterRelationships(goCtx context.Context, req *types.QueryFilterRelationshipsRequest) (*types.QueryFilterRelationshipsResponse, error) {
func (q Querier) FilterRelationships(goCtx context.Context, req *types.QueryFilterRelationshipsRequest) (*types.QueryFilterRelationshipsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)

engine, err := k.GetACPEngine(ctx)
engine, err := q.GetACPEngine(ctx)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions x/acp/keeper/query_validate_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/sourcenetwork/sourcehub/x/acp/types"
)

func (k Keeper) ValidatePolicy(goCtx context.Context, req *types.QueryValidatePolicyRequest) (*types.QueryValidatePolicyResponse, error) {
func (q Querier) ValidatePolicy(goCtx context.Context, req *types.QueryValidatePolicyRequest) (*types.QueryValidatePolicyResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
engine, err := k.GetACPEngine(ctx)
engine, err := q.GetACPEngine(ctx)
if err != nil {
return nil, err
}
Expand Down
9 changes: 4 additions & 5 deletions x/acp/keeper/query_verify_access_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@ import (
"github.com/sourcenetwork/sourcehub/x/acp/types"
)

func (k Keeper) VerifyAccessRequest(goCtx context.Context, req *types.QueryVerifyAccessRequestRequest) (*types.QueryVerifyAccessRequestResponse, error) {
func (q Querier) VerifyAccessRequest(goCtx context.Context, req *types.QueryVerifyAccessRequestRequest) (*types.QueryVerifyAccessRequestResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)

engine, err := k.GetACPEngine(ctx)
engine, err := q.GetACPEngine(ctx)
if err != nil {
return nil, err
}

actorId := req.AccessRequest.Actor.Id
addr, err := sdk.AccAddressFromBech32(actorId)
if err == nil {
// this means the actor ID is a cosmos account
// so convert it to a did
acc := k.accountKeeper.GetAccount(ctx, addr)
// this means the actor ID is a cosmos account, so convert it to a did
acc := q.accountKeeper.GetAccount(ctx, addr)
if acc == nil {
return nil, errors.Wrap("verify access request: could not produce did for actor", errors.ErrorType_BAD_INPUT, errors.Pair("actorId", actorId))
}
Expand Down
15 changes: 9 additions & 6 deletions x/acp/keeper/query_verify_access_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ resources:
}

func TestVerifyAccessRequest_QueryingObjectsTheActorHasAccessToReturnsTrue(t *testing.T) {
ctx, keeper, pol, creator := setupTestVerifyAccessRequest(t)
ctx, k, pol, creator := setupTestVerifyAccessRequest(t)
querier := NewQuerier(k)

req := &types.QueryVerifyAccessRequestRequest{
PolicyId: pol.Id,
Expand All @@ -79,7 +80,7 @@ func TestVerifyAccessRequest_QueryingObjectsTheActorHasAccessToReturnsTrue(t *te
},
},
}
result, err := keeper.VerifyAccessRequest(ctx, req)
result, err := querier.VerifyAccessRequest(ctx, req)

want := &types.QueryVerifyAccessRequestResponse{
Valid: true,
Expand All @@ -89,7 +90,8 @@ func TestVerifyAccessRequest_QueryingObjectsTheActorHasAccessToReturnsTrue(t *te
}

func TestVerifyAccessRequest_QueryingOperationActorIsNotAuthorizedReturnNotValid(t *testing.T) {
ctx, keeper, pol, creator := setupTestVerifyAccessRequest(t)
ctx, k, pol, creator := setupTestVerifyAccessRequest(t)
querier := NewQuerier(k)

req := &types.QueryVerifyAccessRequestRequest{
PolicyId: pol.Id,
Expand All @@ -105,7 +107,7 @@ func TestVerifyAccessRequest_QueryingOperationActorIsNotAuthorizedReturnNotValid
},
},
}
result, err := keeper.VerifyAccessRequest(ctx, req)
result, err := querier.VerifyAccessRequest(ctx, req)

want := &types.QueryVerifyAccessRequestResponse{
Valid: false,
Expand All @@ -115,7 +117,8 @@ func TestVerifyAccessRequest_QueryingOperationActorIsNotAuthorizedReturnNotValid
}

func TestVerifyAccessRequest_QueryingObjectThatDoesNotExistReturnValidFalse(t *testing.T) {
ctx, keeper, pol, creator := setupTestVerifyAccessRequest(t)
ctx, k, pol, creator := setupTestVerifyAccessRequest(t)
querier := NewQuerier(k)

req := &types.QueryVerifyAccessRequestRequest{
PolicyId: pol.Id,
Expand All @@ -131,7 +134,7 @@ func TestVerifyAccessRequest_QueryingObjectThatDoesNotExistReturnValidFalse(t *t
},
},
}
result, err := keeper.VerifyAccessRequest(ctx, req)
result, err := querier.VerifyAccessRequest(ctx, req)

want := &types.QueryVerifyAccessRequestResponse{
Valid: false,
Expand Down

0 comments on commit e2fa8ab

Please sign in to comment.