forked from sourcenetwork/defradb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR(WIP): Add policy testing framework working
- Loading branch information
1 parent
49b97af
commit 5c9133c
Showing
11 changed files
with
209 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2024 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package tests | ||
|
||
import ( | ||
"github.com/sourcenetwork/immutable" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// AddPolicy will attempt to add the given policy using defraDB's ACP module. | ||
type AddPolicy struct { | ||
// NodeID may hold the ID (index) of the node we want to add policy to. | ||
// | ||
// If a value is not provided the policy will be added in all nodes. | ||
NodeID immutable.Option[int] | ||
|
||
// The raw policy string. | ||
Policy string | ||
|
||
// The policy creator, i.e. actor creating the policy. | ||
Creator string | ||
|
||
// If true then assumes YAML format for the policy, otherwise assumes JSON format. | ||
IsYAML bool | ||
|
||
// The expected policyID generated based on the Policy loaded in to acp module. | ||
ExpectedPolicyID string | ||
|
||
// Any error expected from the action. Optional. | ||
// | ||
// String can be a partial, and the test will pass if an error is returned that | ||
// contains this string. | ||
ExpectedError string | ||
} | ||
|
||
// addPolicyACP will attempt to add the given policy using defraDB's ACP module. | ||
func addPolicyACP( | ||
s *state, | ||
action AddPolicy, | ||
) { | ||
if s.dbt == badgerIMType || s.dbt == defraIMType { | ||
s.t.Skip("ACP test (adding policy) is not supported on in-memory database type(s)") | ||
} | ||
|
||
// If we expect an error, then ExpectedPolicyID should be empty. | ||
if action.ExpectedError != "" && action.ExpectedPolicyID != "" { | ||
require.Fail(s.t, "Expected error should not have an expected policyID with it.", s.testCase.Description) | ||
} | ||
|
||
// for nodeID, node := range getNodes(action.NodeID, s.nodes) { | ||
for _, node := range getNodes(action.NodeID, s.nodes) { | ||
if !node.ACPModule().HasValue() { | ||
require.Fail(s.t, "Failed to add policy because ACP module was not found", s.testCase.Description) | ||
} | ||
|
||
policyID, err := node.ACPModule().Value().AddPolicy( | ||
s.ctx, | ||
action.Policy, | ||
action.Creator, | ||
action.IsYAML, | ||
) | ||
|
||
if err == nil { | ||
require.Equal(s.t, action.ExpectedError, "") | ||
require.Equal(s.t, action.ExpectedPolicyID, policyID) | ||
// s.policyIDs[nodeID] = append(s.policyIDs[nodeID], policyID) | ||
} | ||
|
||
expectedErrorRaised := AssertError(s.t, s.testCase.Description, err, action.ExpectedError) | ||
assertExpectedErrorRaised(s.t, s.testCase.Description, action.ExpectedError, expectedErrorRaised) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2024 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package test_acp | ||
|
||
import ( | ||
"testing" | ||
|
||
testUtils "github.com/sourcenetwork/defradb/tests/integration" | ||
) | ||
|
||
func TestACP_CreateAndRead(t *testing.T) { | ||
test := testUtils.TestCase{ | ||
Description: "Simple acp create and read", | ||
Actions: []any{ | ||
testUtils.AddPolicy{ | ||
Policy: ` | ||
description: a test policy which marks a collection in a database as a resource | ||
resources: | ||
users: | ||
permissions: | ||
read: | ||
expr: owner + reader | ||
write: | ||
expr: owner | ||
relations: | ||
owner: | ||
types: | ||
- actor | ||
reader: | ||
types: | ||
- actor | ||
admin: | ||
manages: | ||
- reader | ||
types: | ||
- actor | ||
actor: | ||
name: actor | ||
`, | ||
IsYAML: true, | ||
Creator: "cosmos1zzg43wdrhmmk89z3pmejwete2kkd4a3vn7w969", | ||
ExpectedPolicyID: "53980e762616fcffbe76307995895e862f87ef3f21d509325d1dc772a770b001", | ||
ExpectedError: "", | ||
}, | ||
|
||
testUtils.SchemaUpdate{ | ||
Schema: ` | ||
type Users { | ||
name: String | ||
age: Int | ||
} | ||
`, | ||
}, | ||
|
||
testUtils.CreateDoc{ | ||
Doc: `{ | ||
"name": "John", | ||
"age": 27 | ||
}`, | ||
}, | ||
|
||
testUtils.Request{ | ||
Request: ` | ||
query { | ||
Users { | ||
_docID | ||
name | ||
age | ||
} | ||
} | ||
`, | ||
Results: []map[string]any{ | ||
{ | ||
"_docID": "bae-88b63198-7d38-5714-a9ff-21ba46374fd1", | ||
"name": "John", | ||
"age": int64(27), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
testUtils.ExecuteTestCase(t, test) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters