Skip to content

Commit

Permalink
PR(TEST): Test Permissioned Schema Reject/Accept
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadlone committed Feb 25, 2024
1 parent bc7b72d commit d1d03c2
Show file tree
Hide file tree
Showing 19 changed files with 3,419 additions and 2 deletions.
15 changes: 13 additions & 2 deletions tests/integration/acp/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
## More Information on what each directory tests.
## More Information on ACP test directories.


1) `./add_policy`
1) `./defradb/tests/integration/acp/add_policy`
- This directory tests ONLY the `Adding of a Policy` through DefraDB.
- Does NOT assert the schema.
- Does NOT test DPI validation.

2) `./defradb/tests/integration/acp/schema/add_dpi`
- This directory tests the loading/adding of a schema that has `@policy(id, resource)`
specified (i.e. permissioned schema). The tests ensure that only a schema linking to
a valid DPI policy is accepted. Naturally these tests will also be `Adding a Policy`
through DefraDB like in (1) before actually adding the schema. If a schema has a
policy specified that doesn't exist (or wasn't added yet), that schema WILL/MUST
be rejected in these tests.
- The tests assert the schema after to ensure rejection/acceptance.
- Tests DPI validation.
214 changes: 214 additions & 0 deletions tests/integration/acp/schema/add_dpi/accept_basic_dpi_fmts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
// 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_schema_add_dpi

import (
"fmt"
"testing"

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

func TestACP_AddDPISchema_BasicYAML_SchemaAccepted(t *testing.T) {
policyIDOfValidDPI := "dfe202ffb4f0fe9b46157c313213a3839e08a6f0a7c3aba55e4724cb49ffde8a"

test := testUtils.TestCase{
Description: "Test acp, specify basic policy that was added in YAML format, accept schema",

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

Creator: actor1Signature,

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: policyIDOfValidDPI,
},

testUtils.SchemaUpdate{
Schema: fmt.Sprintf(`
type Users @policy(
id: "%s",
resource: "users"
) {
name: String
age: Int
}
`,
policyIDOfValidDPI,
),
},

testUtils.IntrospectionRequest{
Request: `
query {
__type (name: "Users") {
name
fields {
name
type {
name
kind
}
}
}
}
`,
ExpectedData: map[string]any{
"__type": map[string]any{
"name": "Users", // NOTE: "Users" MUST exist
"fields": schemaUtils.DefaultFields.Append(
schemaUtils.Field{
"name": "name",
"type": map[string]any{
"kind": "SCALAR",
"name": "String",
},
},
).Append(
schemaUtils.Field{
"name": "age",
"type": map[string]any{
"kind": "SCALAR",
"name": "Int",
},
},
).Tidy(),
},
},
},
},
}

testUtils.ExecuteTestCase(t, test)
}

func TestACP_AddDPISchema_BasicJSON_SchemaAccepted(t *testing.T) {
policyIDOfValidDPI := "dfe202ffb4f0fe9b46157c313213a3839e08a6f0a7c3aba55e4724cb49ffde8a"

test := testUtils.TestCase{
Description: "Test acp, specify basic policy that was added in JSON format, accept schema",

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

Creator: actor1Signature,

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: policyIDOfValidDPI,
},

testUtils.SchemaUpdate{
Schema: fmt.Sprintf(`
type Users @policy(
id: "%s",
resource: "users"
) {
name: String
age: Int
}
`,
policyIDOfValidDPI,
),
},

testUtils.IntrospectionRequest{
Request: `
query {
__type (name: "Users") {
name
fields {
name
type {
name
kind
}
}
}
}
`,
ExpectedData: map[string]any{
"__type": map[string]any{
"name": "Users", // NOTE: "Users" MUST exist
"fields": schemaUtils.DefaultFields.Append(
schemaUtils.Field{
"name": "name",
"type": map[string]any{
"kind": "SCALAR",
"name": "String",
},
},
).Append(
schemaUtils.Field{
"name": "age",
"type": map[string]any{
"kind": "SCALAR",
"name": "Int",
},
},
).Tidy(),
},
},
},
},
}

testUtils.ExecuteTestCase(t, test)
}
Loading

0 comments on commit d1d03c2

Please sign in to comment.