|
| 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_nac_setup_then_start |
| 12 | + |
| 13 | +import ( |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/sourcenetwork/immutable" |
| 17 | + |
| 18 | + testUtils "github.com/sourcenetwork/defradb/tests/integration" |
| 19 | +) |
| 20 | + |
| 21 | +func TestNAC_GatesIndexCreatePreSetup_AllowIfAuthorizedElseError(t *testing.T) { |
| 22 | + test := testUtils.TestCase{ |
| 23 | + Description: "node acp correctly gates index create operation (setup before nac), allow if authorized, otherwise error", |
| 24 | + SupportedDatabaseTypes: immutable.Some( |
| 25 | + []testUtils.DatabaseType{ |
| 26 | + // This test only supports file type databases since the setup steps will be done before |
| 27 | + // the node is re-started with nac enabled (if it's in-memory it will loose setup state). |
| 28 | + testUtils.BadgerFileType, |
| 29 | + }, |
| 30 | + ), |
| 31 | + Actions: []any{ |
| 32 | + // Note: Since this is not an in-memory test, we can do the setup steps before nac is enabled. |
| 33 | + testUtils.SchemaUpdate{ |
| 34 | + Schema: ` |
| 35 | + type User { |
| 36 | + name: String |
| 37 | + } |
| 38 | + `, |
| 39 | + }, |
| 40 | + |
| 41 | + // Starting with NAC, so only authorized user(s) can perform operations from here on out. |
| 42 | + testUtils.Close{}, |
| 43 | + testUtils.Start{ |
| 44 | + Identity: testUtils.ClientIdentity(1), |
| 45 | + EnableNAC: true, |
| 46 | + }, |
| 47 | + |
| 48 | + // We haven't authorized non-identities. So, this should error. |
| 49 | + testUtils.CreateIndex{ |
| 50 | + Identity: testUtils.NoIdentity(), |
| 51 | + CollectionID: 0, |
| 52 | + FieldName: "name", |
| 53 | + ExpectedError: "not authorized to perform operation", |
| 54 | + }, |
| 55 | + |
| 56 | + // Wrong user/identity will also not be authorized. |
| 57 | + testUtils.CreateIndex{ |
| 58 | + Identity: testUtils.ClientIdentity(2), |
| 59 | + CollectionID: 0, |
| 60 | + FieldName: "name", |
| 61 | + ExpectedError: "not authorized to perform operation", |
| 62 | + }, |
| 63 | + |
| 64 | + // This should work as the identity is authorized. |
| 65 | + testUtils.CreateIndex{ |
| 66 | + Identity: testUtils.ClientIdentity(1), |
| 67 | + CollectionID: 0, |
| 68 | + FieldName: "name", |
| 69 | + }, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + testUtils.ExecuteTestCase(t, test) |
| 74 | +} |
0 commit comments