|
| 1 | +// Copyright 2025 Democratized Data Foundation |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.txt. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0, included in the file |
| 9 | +// licenses/APL.txt. |
| 10 | + |
| 11 | +package test_acp_aac |
| 12 | + |
| 13 | +import ( |
| 14 | + "testing" |
| 15 | + |
| 16 | + testUtils "github.com/sourcenetwork/defradb/tests/integration" |
| 17 | + "github.com/sourcenetwork/immutable" |
| 18 | +) |
| 19 | + |
| 20 | +func TestAAC_GatesPatchCollection_AllowIfAuthorizedElseError(t *testing.T) { |
| 21 | + test := testUtils.TestCase{ |
| 22 | + Description: "admin acp correctly gates patch collection operation, allow if authorized, otherwise error", |
| 23 | + Actions: []any{ |
| 24 | + // Setup that we can do before aac is enabled. |
| 25 | + testUtils.SchemaUpdate{ |
| 26 | + Schema: ` |
| 27 | + type Users {} |
| 28 | + `, |
| 29 | + }, |
| 30 | + testUtils.SchemaPatch{ |
| 31 | + Patch: ` |
| 32 | + [ |
| 33 | + { "op": "add", "path": "/Users/Fields/-", "value": {"Name": "name", "Kind": "String"} } |
| 34 | + ] |
| 35 | + `, |
| 36 | + SetAsDefaultVersion: immutable.Some(false), |
| 37 | + }, |
| 38 | + |
| 39 | + // Starting with ACC, so only authorized user(s) can perform operations. |
| 40 | + testUtils.Close{}, |
| 41 | + testUtils.Start{ |
| 42 | + Identity: testUtils.ClientIdentity(1), |
| 43 | + EnableAAC: true, |
| 44 | + }, |
| 45 | + |
| 46 | + // We haven't authorized non-identities. So, this should error. |
| 47 | + testUtils.PatchCollection{ |
| 48 | + Patch: ` |
| 49 | + [ |
| 50 | + { |
| 51 | + "op": "copy", |
| 52 | + "from": "/bafkreia2jn5ecrhtvy4fravk6pm3wqiny46m7mqymvjkgat7xiqupgqoai/Name", |
| 53 | + "path": "/bafkreialnju2rez4t3quvpobf3463eai3lo64vdrdhdmunz7yy7sv3f5ce/Name" |
| 54 | + } |
| 55 | + ] |
| 56 | + `, |
| 57 | + ExpectedError: "not authorized to perform operation", |
| 58 | + }, |
| 59 | + |
| 60 | + // Wrong user/identity will also not be authorized. |
| 61 | + testUtils.PatchCollection{ |
| 62 | + Identity: testUtils.ClientIdentity(2), |
| 63 | + Patch: ` |
| 64 | + [ |
| 65 | + { |
| 66 | + "op": "copy", |
| 67 | + "from": "/bafkreia2jn5ecrhtvy4fravk6pm3wqiny46m7mqymvjkgat7xiqupgqoai/Name", |
| 68 | + "path": "/bafkreialnju2rez4t3quvpobf3463eai3lo64vdrdhdmunz7yy7sv3f5ce/Name" |
| 69 | + } |
| 70 | + ] |
| 71 | + `, |
| 72 | + ExpectedError: "not authorized to perform operation", |
| 73 | + }, |
| 74 | + |
| 75 | + // This should work as the identity is authorized. |
| 76 | + testUtils.PatchCollection{ |
| 77 | + Identity: testUtils.ClientIdentity(1), |
| 78 | + Patch: ` |
| 79 | + [ |
| 80 | + { |
| 81 | + "op": "copy", |
| 82 | + "from": "/bafkreia2jn5ecrhtvy4fravk6pm3wqiny46m7mqymvjkgat7xiqupgqoai/Name", |
| 83 | + "path": "/bafkreialnju2rez4t3quvpobf3463eai3lo64vdrdhdmunz7yy7sv3f5ce/Name" |
| 84 | + } |
| 85 | + ] |
| 86 | + `, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + testUtils.ExecuteTestCase(t, test) |
| 92 | +} |
0 commit comments