Skip to content

Commit

Permalink
PR(TEST): Add Policy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadlone committed Feb 8, 2024
1 parent 1d78bae commit de645a8
Show file tree
Hide file tree
Showing 10 changed files with 1,193 additions and 0 deletions.
100 changes: 100 additions & 0 deletions tests/integration/acp/policy/basic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// 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_policy

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
)

func TestACPAddPolicy_BasicYAML_ValidPolicyID(t *testing.T) {
test := testUtils.TestCase{
Description: "Test acp, adding basic policy in YAML format",
Actions: []any{
testUtils.AddPolicy{
IsYAML: true,

Creator: "cosmos1zzg43wdrhmmk89z3pmejwete2kkd4a3vn7w969",

Policy: `
description: a basic policy that satisfies minimum DPI requirements
actor:
name: actor
resources:
users:
permissions:
read:
expr: owner
write:
expr: owner
relations:
owner:
types:
- actor
`,

ExpectedPolicyID: "dfe202ffb4f0fe9b46157c313213a3839e08a6f0a7c3aba55e4724cb49ffde8a",
},
},
}

testUtils.ExecuteTestCase(t, test)
}

func TestACPAddPolicy_BasicJSON_ValidPolicyID(t *testing.T) {
test := testUtils.TestCase{
Description: "Test acp, adding basic policy in JSON format",
Actions: []any{
testUtils.AddPolicy{
IsYAML: false,

Creator: "cosmos1zzg43wdrhmmk89z3pmejwete2kkd4a3vn7w969",

Policy: `
{
"description": "a basic policy that satisfies minimum DPI requirements",
"resources": {
"users": {
"permissions": {
"read": {
"expr": "owner"
},
"write": {
"expr": "owner"
}
},
"relations": {
"owner": {
"types": [
"actor"
]
}
}
}
},
"actor": {
"name": "actor"
}
}
`,

ExpectedPolicyID: "dfe202ffb4f0fe9b46157c313213a3839e08a6f0a7c3aba55e4724cb49ffde8a",
},
},
}

testUtils.ExecuteTestCase(t, test)
}
14 changes: 14 additions & 0 deletions tests/integration/acp/policy/fixture.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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_policy

var actor1Signature = "cosmos1zzg43wdrhmmk89z3pmejwete2kkd4a3vn7w969"
var actor2Signature = "cosmos1x25hhksxhu86r45hqwk28dd70qzux3262hdrll"
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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_policy

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
)

func TestACPAddPolicy_ExtraPermissionsAndExtraRelations_ValidPolicyID(t *testing.T) {
test := testUtils.TestCase{

Description: "Test acp, add policy, extra permissions and relations, still valid",

Actions: []any{
testUtils.AddPolicy{
IsYAML: true,

Creator: actor1Signature,

Policy: `
description: a policy
actor:
name: actor
resources:
users:
permissions:
write:
expr: owner
read:
expr: owner + reader
extra:
expr: joker
relations:
owner:
types:
- actor
reader:
types:
- actor
joker:
types:
- actor
`,

ExpectedPolicyID: "ecfeeebd1b65e6a21b2f1b57006176bcbc6a37ef238f27c7034953f46fe04674",
},
},
}

testUtils.ExecuteTestCase(t, test)
}
99 changes: 99 additions & 0 deletions tests/integration/acp/policy/with_extra_perms_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// 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_policy

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
)

func TestACPAddPolicy_ExtraPermissions_ValidPolicyID(t *testing.T) {
test := testUtils.TestCase{

Description: "Test acp, add policy, extra permissions, still valid",

Actions: []any{
testUtils.AddPolicy{
IsYAML: true,

Creator: actor1Signature,

Policy: `
description: a policy
resources:
users:
permissions:
read:
expr: owner
write:
expr: owner
extra:
expr: owner
relations:
owner:
types:
- actor
actor:
name: actor
`,

ExpectedPolicyID: "9d518bb2d5aceb2c8f9b12b909eecd50276c1bd0250069875f265166e6030bb5",
},
},
}

testUtils.ExecuteTestCase(t, test)
}

func TestACPAddPolicy_ExtraDuplicatePermissions_Error(t *testing.T) {
test := testUtils.TestCase{

Description: "Test acp, add policy, extra duplicate permissions, return error",

Actions: []any{
testUtils.AddPolicy{
IsYAML: true,

Creator: actor1Signature,

Policy: `
description: a policy
resources:
users:
permissions:
read:
expr: owner
write:
expr: owner
write:
expr: owner
relations:
owner:
types:
- actor
actor:
name: actor
`,

ExpectedError: "key \"write\" already set in map",
},
},
}

testUtils.ExecuteTestCase(t, test)
}
111 changes: 111 additions & 0 deletions tests/integration/acp/policy/with_extra_relations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// 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_policy

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
)

func TestACPAddPolicy_ExtraRelations_ValidPolicyID(t *testing.T) {
test := testUtils.TestCase{

Description: "Test acp, add policy, extra relations, still valid",

Actions: []any{
testUtils.AddPolicy{
IsYAML: true,

Creator: actor1Signature,

Policy: `
description: a policy
actor:
name: actor
resources:
users:
permissions:
write:
expr: owner
read:
expr: owner + reader
relations:
owner:
types:
- actor
reader:
types:
- actor
joker:
types:
- actor
`,

ExpectedPolicyID: "450c47aa47b7b07820f99e5cb38170dc108a2f12b137946e6b47d0c0a73b607f",
},
},
}

testUtils.ExecuteTestCase(t, test)
}

func TestACPAddPolicy_ExtraDuplicateRelations_Error(t *testing.T) {
test := testUtils.TestCase{

Description: "Test acp, add policy, extra duplicate relations permissions, return error",

Actions: []any{
testUtils.AddPolicy{
IsYAML: true,

Creator: actor1Signature,

Policy: `
description: a policy
actor:
name: actor
resources:
users:
permissions:
write:
expr: owner
read:
expr: owner + reader
relations:
owner:
types:
- actor
reader:
types:
- actor
joker:
types:
- actor
joker:
types:
- actor
`,

ExpectedError: "key \"joker\" already set in map",
},
},
}

testUtils.ExecuteTestCase(t, test)
}
Loading

0 comments on commit de645a8

Please sign in to comment.