|
| 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 | +) |
| 18 | + |
| 19 | +func TestAAC_GatesDocCreate_AllowIfAuthorizedElseError(t *testing.T) { |
| 20 | + test := testUtils.TestCase{ |
| 21 | + Description: "admin acp correctly gates doc create operation, allow if authorized, otherwise error", |
| 22 | + Actions: []any{ |
| 23 | + // Starting with ACC, so only authorized user(s) can perform operations from here on out. |
| 24 | + testUtils.Close{}, |
| 25 | + testUtils.Start{ |
| 26 | + Identity: testUtils.ClientIdentity(1), |
| 27 | + EnableNAC: true, |
| 28 | + }, |
| 29 | + // Note: Doing setup steps after starting with aac enabled, otherwise the in-memory tests |
| 30 | + // will loose setup state when the restart happens (i.e. the restart that started aac). |
| 31 | + testUtils.SchemaUpdate{ |
| 32 | + Identity: testUtils.ClientIdentity(1), |
| 33 | + Schema: `type User { name: String }`, |
| 34 | + }, |
| 35 | + // Note: Setup is now done, the test code that follows is what we want to assert. |
| 36 | + |
| 37 | + // We haven't authorized non-identities. So, this should error. |
| 38 | + testUtils.CreateDoc{ |
| 39 | + Identity: testUtils.NoIdentity(), |
| 40 | + CollectionID: 0, |
| 41 | + Doc: `{ "name": "Shahzad" }`, |
| 42 | + ExpectedError: "not authorized to perform operation", |
| 43 | + }, |
| 44 | + testUtils.Request{ // Should not be created |
| 45 | + Identity: testUtils.ClientIdentity(1), |
| 46 | + Request: `query{ User { name } }`, |
| 47 | + Results: map[string]any{"User": []map[string]any{}}, |
| 48 | + }, |
| 49 | + |
| 50 | + // Wrong user/identity will also not be authorized. |
| 51 | + testUtils.CreateDoc{ |
| 52 | + Identity: testUtils.ClientIdentity(2), |
| 53 | + CollectionID: 0, |
| 54 | + Doc: `{ "name": "Shahzad" }`, |
| 55 | + ExpectedError: "not authorized to perform operation", |
| 56 | + }, |
| 57 | + testUtils.Request{ // Should not be created |
| 58 | + Identity: testUtils.ClientIdentity(1), |
| 59 | + Request: `query{ User { name } }`, |
| 60 | + Results: map[string]any{"User": []map[string]any{}}, |
| 61 | + }, |
| 62 | + |
| 63 | + // This should work as the identity is authorized. |
| 64 | + testUtils.CreateDoc{ |
| 65 | + Identity: testUtils.ClientIdentity(1), |
| 66 | + CollectionID: 0, |
| 67 | + Doc: `{ "name": "Shahzad" }`, |
| 68 | + }, |
| 69 | + testUtils.Request{ // Should now be created |
| 70 | + Identity: testUtils.ClientIdentity(1), |
| 71 | + Request: `query{ User { name } }`, |
| 72 | + Results: map[string]any{ |
| 73 | + "User": []map[string]any{{"name": "Shahzad"}}, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + testUtils.ExecuteTestCase(t, test) |
| 80 | +} |
0 commit comments