mirror of
https://github.com/absmach/magistrala.git
synced 2026-08-07 07:14:46 +00:00
168e8b90cb
Property Based Tests / api-test (push) Has been cancelled
Continuous Delivery / lint-and-build (push) Has been cancelled
Deploy GitHub Pages / swagger-ui (push) Has been cancelled
CI Pipeline / Lint Proto (push) Has been cancelled
CI Pipeline / Detect Changes (push) Has been cancelled
Continuous Delivery / Build and Push Docker Images (push) Has been cancelled
CI Pipeline / lint-and-build (push) Has been cancelled
CI Pipeline / Test ${{ matrix.module }} (push) Has been cancelled
CI Pipeline / Upload Coverage (push) Has been cancelled
Signed-off-by: dusan <borovcanindusan1@gmail.com>
56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
// Copyright (c) Abstract Machines
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package atom_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/absmach/magistrala/pkg/atom"
|
|
"github.com/absmach/magistrala/pkg/errors"
|
|
"github.com/absmach/magistrala/pkg/policies"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestPolicyEvaluatorCheckPolicy(t *testing.T) {
|
|
client := &authzClient{res: atom.AuthzResponse{Allowed: true}}
|
|
evaluator := atom.NewPolicyEvaluator(client)
|
|
|
|
err := evaluator.CheckPolicy(context.Background(), policies.Policy{
|
|
Domain: "domain-1",
|
|
Subject: "domain-1_user-1",
|
|
Permission: policies.ViewPermission,
|
|
ObjectType: policies.RulesType,
|
|
Object: "rule-1",
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, atom.AuthzRequest{
|
|
SubjectID: "user-1",
|
|
Action: "read",
|
|
ResourceID: "rule-1",
|
|
ObjectKind: "resource",
|
|
ObjectID: "rule-1",
|
|
Context: map[string]any{
|
|
"domain_id": "domain-1",
|
|
"legacy_object_type": policies.RulesType,
|
|
"legacy_relation": "",
|
|
},
|
|
}, client.req)
|
|
}
|
|
|
|
func TestPolicyEvaluatorDenied(t *testing.T) {
|
|
client := &authzClient{res: atom.AuthzResponse{Allowed: false}}
|
|
evaluator := atom.NewPolicyEvaluator(client)
|
|
|
|
err := evaluator.CheckPolicy(context.Background(), policies.Policy{
|
|
Subject: "user-1",
|
|
Permission: policies.AdminPermission,
|
|
ObjectType: policies.PlatformType,
|
|
Object: policies.MagistralaObject,
|
|
})
|
|
|
|
assert.True(t, errors.Contains(err, errors.ErrAuthorization))
|
|
}
|