Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR(TEST): Add tests where requests without identity can access
Browse files Browse the repository at this point in the history
shahzadlone committed Nov 27, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 55eadb0 commit d05b6e7
Showing 2 changed files with 412 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -248,3 +248,209 @@ func TestACP_OwnerGivesOnlyReadAccessToAllActors_GQL_AllActorsCanReadButNotUpdat

testUtils.ExecuteTestCase(t, test)
}

func TestACP_OwnerGivesOnlyReadAccessToAllActors_GQL_CanReadEvenWithoutIdentityButNotUpdateOrDelete(t *testing.T) {
expectedPolicyID := "fc56b7509c20ac8ce682b3b9b4fdaad868a9c70dda6ec16720298be64f16e9a4"

test := testUtils.TestCase{

Description: "Test acp, owner gives read access to all actors (gql), can read without an identity but can't update or delete",

SupportedMutationTypes: immutable.Some(
[]testUtils.MutationType{
// GQL mutation will return no error when wrong identity is used so test that separately.
testUtils.GQLRequestMutationType,
},
),

Actions: []any{
testUtils.AddPolicy{

Identity: testUtils.ClientIdentity(1),

Policy: `
name: Test Policy
description: A Policy
actor:
name: actor
resources:
users:
permissions:
read:
expr: owner + reader + writer
write:
expr: owner + writer
nothing:
expr: dummy
relations:
owner:
types:
- actor
reader:
types:
- actor
writer:
types:
- actor
admin:
manages:
- reader
types:
- actor
dummy:
types:
- actor
`,

ExpectedPolicyID: expectedPolicyID,
},

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

testUtils.CreateDoc{
Identity: testUtils.ClientIdentity(1),

CollectionID: 0,

Doc: `
{
"name": "Shahzad",
"age": 28
}
`,
},

testUtils.Request{
Identity: testUtils.NoIdentity(), // Can not read without an identity.

Request: `
query {
Users {
_docID
name
age
}
}
`,

Results: map[string]any{
"Users": []map[string]any{}, // Can't see the documents yet
},
},

testUtils.DeleteDoc{ // Since can't read without identity, can't delete either.
CollectionID: 0,

Identity: testUtils.NoIdentity(),

DocID: 0,

ExpectedError: "document not found or not authorized to access",
},

testUtils.UpdateDoc{ // Since can't read without identity, can't update either.
CollectionID: 0,

Identity: testUtils.NoIdentity(),

DocID: 0,

Doc: `
{
"name": "Shahzad Lone"
}
`,

ExpectedError: "document not found or not authorized to access",
},

testUtils.AddDocActorRelationship{
RequestorIdentity: testUtils.ClientIdentity(1),

TargetIdentity: testUtils.AllClientIdentities(),

CollectionID: 0,

DocID: 0,

Relation: "reader",

ExpectedExistence: false,
},

testUtils.Request{
Identity: testUtils.NoIdentity(), // Now any identity can read, even if there is no identity

Request: `
query {
Users {
_docID
name
age
}
}
`,

Results: map[string]any{
"Users": []map[string]any{
{
"_docID": "bae-9d443d0c-52f6-568b-8f74-e8ff0825697b",
"name": "Shahzad",
"age": int64(28),
},
},
},
},

testUtils.UpdateDoc{ // But doesn't mean they can update.
CollectionID: 0,

Identity: testUtils.NoIdentity(),

DocID: 0,

Doc: `
{
"name": "Shahzad Lone"
}
`,

ExpectedError: "document not found or not authorized to access",
},

testUtils.DeleteDoc{ // But doesn't mean they can delete.
CollectionID: 0,

Identity: testUtils.NoIdentity(),

DocID: 0,

ExpectedError: "document not found or not authorized to access",
},
},
}

testUtils.ExecuteTestCase(t, test)
}
Original file line number Diff line number Diff line change
@@ -248,3 +248,209 @@ func TestACP_OwnerGivesOnlyReadAccessToAllActors_AllActorsCanReadButNotUpdateOrD

testUtils.ExecuteTestCase(t, test)
}

func TestACP_OwnerGivesOnlyReadAccessToAllActors_CanReadEvenWithoutIdentityButNotUpdateOrDelete(t *testing.T) {
expectedPolicyID := "fc56b7509c20ac8ce682b3b9b4fdaad868a9c70dda6ec16720298be64f16e9a4"

test := testUtils.TestCase{

Description: "Test acp, owner gives read access to all actors, can read without an identity but can't update or delete",

SupportedMutationTypes: immutable.Some(
[]testUtils.MutationType{
testUtils.CollectionNamedMutationType,
testUtils.CollectionSaveMutationType,
},
),

Actions: []any{
testUtils.AddPolicy{

Identity: testUtils.ClientIdentity(1),

Policy: `
name: Test Policy
description: A Policy
actor:
name: actor
resources:
users:
permissions:
read:
expr: owner + reader + writer
write:
expr: owner + writer
nothing:
expr: dummy
relations:
owner:
types:
- actor
reader:
types:
- actor
writer:
types:
- actor
admin:
manages:
- reader
types:
- actor
dummy:
types:
- actor
`,

ExpectedPolicyID: expectedPolicyID,
},

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

testUtils.CreateDoc{
Identity: testUtils.ClientIdentity(1),

CollectionID: 0,

Doc: `
{
"name": "Shahzad",
"age": 28
}
`,
},

testUtils.Request{
Identity: testUtils.NoIdentity(), // Can not read without an identity.

Request: `
query {
Users {
_docID
name
age
}
}
`,

Results: map[string]any{
"Users": []map[string]any{}, // Can't see the documents yet
},
},

testUtils.DeleteDoc{ // Since can't read without identity, can't delete either.
CollectionID: 0,

Identity: testUtils.NoIdentity(),

DocID: 0,

ExpectedError: "document not found or not authorized to access",
},

testUtils.UpdateDoc{ // Since can't read without identity, can't update either.
CollectionID: 0,

Identity: testUtils.NoIdentity(),

DocID: 0,

Doc: `
{
"name": "Shahzad Lone"
}
`,

ExpectedError: "document not found or not authorized to access",
},

testUtils.AddDocActorRelationship{
RequestorIdentity: testUtils.ClientIdentity(1),

TargetIdentity: testUtils.AllClientIdentities(),

CollectionID: 0,

DocID: 0,

Relation: "reader",

ExpectedExistence: false,
},

testUtils.Request{
Identity: testUtils.NoIdentity(), // Now any identity can read, even if there is no identity

Request: `
query {
Users {
_docID
name
age
}
}
`,

Results: map[string]any{
"Users": []map[string]any{
{
"_docID": "bae-9d443d0c-52f6-568b-8f74-e8ff0825697b",
"name": "Shahzad",
"age": int64(28),
},
},
},
},

testUtils.UpdateDoc{ // But doesn't mean they can update.
CollectionID: 0,

Identity: testUtils.NoIdentity(),

DocID: 0,

Doc: `
{
"name": "Shahzad Lone"
}
`,

ExpectedError: "document not found or not authorized to access",
},

testUtils.DeleteDoc{ // But doesn't mean they can delete.
CollectionID: 0,

Identity: testUtils.NoIdentity(),

DocID: 0,

ExpectedError: "document not found or not authorized to access",
},
},
}

testUtils.ExecuteTestCase(t, test)
}

0 comments on commit d05b6e7

Please sign in to comment.