|
| 1 | +// Copyright 2025 Mia srl |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package custom_builtins |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/open-policy-agent/opa/ast" |
| 23 | + "github.com/open-policy-agent/opa/rego" |
| 24 | + "github.com/rond-authz/rond/custom_builtins/mocks" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +func prepareContext(t *testing.T, mongoClientMock *mocks.MongoClientMock) rego.BuiltinContext { |
| 29 | + t.Helper() |
| 30 | + |
| 31 | + ctx := context.Background() |
| 32 | + |
| 33 | + if mongoClientMock != nil { |
| 34 | + ctx = WithMongoClient(ctx, mongoClientMock) |
| 35 | + } |
| 36 | + |
| 37 | + return rego.BuiltinContext{ |
| 38 | + Context: ctx, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestMongoFindOneDefinition(t *testing.T) { |
| 43 | + defaultMock := &mocks.MongoClientMock{} |
| 44 | + |
| 45 | + t.Run("returns error if mongo client is not set", func(t *testing.T) { |
| 46 | + _, err := mongoFindOneDef( |
| 47 | + prepareContext(t, nil), |
| 48 | + nil, |
| 49 | + nil, |
| 50 | + ) |
| 51 | + require.ErrorContains(t, err, "mongo client not set") |
| 52 | + }) |
| 53 | + |
| 54 | + t.Run("returns error if mongo client in context is not valid", func(t *testing.T) { |
| 55 | + _, err := mongoFindOneDef( |
| 56 | + rego.BuiltinContext{ |
| 57 | + Context: context.WithValue(context.Background(), mongoClientCustomBuiltinContextKey{}, "not a valid client"), |
| 58 | + }, |
| 59 | + nil, |
| 60 | + nil, |
| 61 | + ) |
| 62 | + require.ErrorContains(t, err, "no MongoDB client found in context") |
| 63 | + }) |
| 64 | + |
| 65 | + t.Run("returns error if collection name is not a string", func(t *testing.T) { |
| 66 | + _, err := mongoFindOneDef( |
| 67 | + prepareContext(t, defaultMock), |
| 68 | + ast.BooleanTerm(false), |
| 69 | + nil, |
| 70 | + ) |
| 71 | + require.ErrorContains(t, err, "cannot unmarshal bool") |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("returns error if query is not an object", func(t *testing.T) { |
| 75 | + _, err := mongoFindOneDef(prepareContext( |
| 76 | + t, |
| 77 | + defaultMock), |
| 78 | + ast.StringTerm("coll"), |
| 79 | + ast.BooleanTerm(false), |
| 80 | + ) |
| 81 | + require.ErrorContains(t, err, "cannot unmarshal bool") |
| 82 | + }) |
| 83 | + |
| 84 | + t.Run("returns error if FindOne returns an error", func(t *testing.T) { |
| 85 | + assertionInvoked := false |
| 86 | + mock := &mocks.MongoClientMock{ |
| 87 | + FindOneError: errors.New("some error"), |
| 88 | + FindOneExpectation: func(collectionName string, query interface{}) { |
| 89 | + assertionInvoked = true |
| 90 | + require.Equal(t, "coll", collectionName) |
| 91 | + require.Equal(t, map[string]interface{}{}, query) |
| 92 | + }, |
| 93 | + } |
| 94 | + _, err := mongoFindOneDef( |
| 95 | + prepareContext(t, mock), |
| 96 | + ast.StringTerm("coll"), |
| 97 | + ast.MustParseTerm("{}"), |
| 98 | + ) |
| 99 | + require.ErrorContains(t, err, "some error") |
| 100 | + require.True(t, assertionInvoked) |
| 101 | + }) |
| 102 | +} |
0 commit comments