|
| 1 | +// Copyright 2025 Palantir Technologies, Inc. |
| 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 handler |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/palantir/policy-bot/policy" |
| 22 | + "github.com/palantir/policy-bot/policy/common" |
| 23 | + "github.com/palantir/policy-bot/pull" |
| 24 | + "github.com/palantir/policy-bot/pull/pulltest" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | +) |
| 28 | + |
| 29 | +type staticEvaluator common.Result |
| 30 | + |
| 31 | +func (eval *staticEvaluator) Trigger() common.Trigger { |
| 32 | + return common.TriggerStatic |
| 33 | +} |
| 34 | + |
| 35 | +func (eval *staticEvaluator) Evaluate(ctx context.Context, prctx pull.Context) common.Result { |
| 36 | + return common.Result(*eval) |
| 37 | +} |
| 38 | + |
| 39 | +func TestEvalContext_EvaluatePolicy_PendingAsFailure(t *testing.T) { |
| 40 | + ctx := context.Background() |
| 41 | + |
| 42 | + tests := map[string]struct { |
| 43 | + serverPendingAsFailure bool |
| 44 | + policyPendingAsFailure *bool |
| 45 | + resultStatus common.EvaluationStatus |
| 46 | + expectedState string |
| 47 | + }{ |
| 48 | + "default_pending_returns_pending": { |
| 49 | + serverPendingAsFailure: false, |
| 50 | + policyPendingAsFailure: nil, |
| 51 | + resultStatus: common.StatusPending, |
| 52 | + expectedState: "pending", |
| 53 | + }, |
| 54 | + "server_option_enabled_returns_failure": { |
| 55 | + serverPendingAsFailure: true, |
| 56 | + policyPendingAsFailure: nil, |
| 57 | + resultStatus: common.StatusPending, |
| 58 | + expectedState: "failure", |
| 59 | + }, |
| 60 | + "policy_option_enabled_returns_failure": { |
| 61 | + serverPendingAsFailure: false, |
| 62 | + policyPendingAsFailure: ptr(true), |
| 63 | + resultStatus: common.StatusPending, |
| 64 | + expectedState: "failure", |
| 65 | + }, |
| 66 | + "policy_option_overrides_server_to_disable": { |
| 67 | + serverPendingAsFailure: true, |
| 68 | + policyPendingAsFailure: ptr(false), |
| 69 | + resultStatus: common.StatusPending, |
| 70 | + expectedState: "pending", |
| 71 | + }, |
| 72 | + "approved_returns_success_regardless": { |
| 73 | + serverPendingAsFailure: true, |
| 74 | + policyPendingAsFailure: nil, |
| 75 | + resultStatus: common.StatusApproved, |
| 76 | + expectedState: "success", |
| 77 | + }, |
| 78 | + "disapproved_returns_failure_regardless": { |
| 79 | + serverPendingAsFailure: false, |
| 80 | + policyPendingAsFailure: nil, |
| 81 | + resultStatus: common.StatusDisapproved, |
| 82 | + expectedState: "failure", |
| 83 | + }, |
| 84 | + "skipped_returns_error_regardless": { |
| 85 | + serverPendingAsFailure: true, |
| 86 | + policyPendingAsFailure: nil, |
| 87 | + resultStatus: common.StatusSkipped, |
| 88 | + expectedState: "error", |
| 89 | + }, |
| 90 | + "both_server_and_policy_enabled": { |
| 91 | + serverPendingAsFailure: true, |
| 92 | + policyPendingAsFailure: ptr(true), |
| 93 | + resultStatus: common.StatusPending, |
| 94 | + expectedState: "failure", |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + for name, test := range tests { |
| 99 | + t.Run(name, func(t *testing.T) { |
| 100 | + prctx := &pulltest.Context{ |
| 101 | + OwnerValue: "testowner", |
| 102 | + RepoValue: "testrepo", |
| 103 | + NumberValue: 1, |
| 104 | + HeadSHAValue: "abc123", |
| 105 | + BranchBaseName: "main", |
| 106 | + } |
| 107 | + |
| 108 | + policyConfig := &policy.Config{} |
| 109 | + policyConfig.PendingAsFailure = test.policyPendingAsFailure |
| 110 | + |
| 111 | + ec := &EvalContext{ |
| 112 | + Options: &PullEvaluationOptions{ |
| 113 | + StatusCheckContext: "policy-bot", |
| 114 | + PendingAsFailure: test.serverPendingAsFailure, |
| 115 | + }, |
| 116 | + PublicURL: "http://localhost:8080", |
| 117 | + PullContext: prctx, |
| 118 | + Config: FetchedConfig{ |
| 119 | + Config: policyConfig, |
| 120 | + Source: "test", |
| 121 | + Path: ".policy.yml", |
| 122 | + }, |
| 123 | + SkipPostStatus: true, |
| 124 | + } |
| 125 | + |
| 126 | + evaluator := &staticEvaluator{ |
| 127 | + Status: test.resultStatus, |
| 128 | + StatusDescription: "test description", |
| 129 | + } |
| 130 | + |
| 131 | + _, err := ec.EvaluatePolicy(ctx, evaluator) |
| 132 | + require.NoError(t, err) |
| 133 | + |
| 134 | + require.NotNil(t, ec.Status, "status should be set") |
| 135 | + assert.Equal(t, test.expectedState, *ec.Status.State, "expected state %q, got %q", test.expectedState, *ec.Status.State) |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestEvalContext_EvaluatePolicy_PendingAsFailure_NilConfig(t *testing.T) { |
| 141 | + ctx := context.Background() |
| 142 | + |
| 143 | + prctx := &pulltest.Context{ |
| 144 | + OwnerValue: "testowner", |
| 145 | + RepoValue: "testrepo", |
| 146 | + NumberValue: 1, |
| 147 | + HeadSHAValue: "abc123", |
| 148 | + BranchBaseName: "main", |
| 149 | + } |
| 150 | + |
| 151 | + // Test with nil Config in FetchedConfig |
| 152 | + ec := &EvalContext{ |
| 153 | + Options: &PullEvaluationOptions{ |
| 154 | + StatusCheckContext: "policy-bot", |
| 155 | + PendingAsFailure: true, |
| 156 | + }, |
| 157 | + PublicURL: "http://localhost:8080", |
| 158 | + PullContext: prctx, |
| 159 | + Config: FetchedConfig{ |
| 160 | + Config: nil, // nil config |
| 161 | + Source: "test", |
| 162 | + Path: ".policy.yml", |
| 163 | + }, |
| 164 | + SkipPostStatus: true, |
| 165 | + } |
| 166 | + |
| 167 | + evaluator := &staticEvaluator{ |
| 168 | + Status: common.StatusPending, |
| 169 | + StatusDescription: "test description", |
| 170 | + } |
| 171 | + |
| 172 | + _, err := ec.EvaluatePolicy(ctx, evaluator) |
| 173 | + require.NoError(t, err) |
| 174 | + |
| 175 | + require.NotNil(t, ec.Status, "status should be set") |
| 176 | + assert.Equal(t, "failure", *ec.Status.State, "server option should be used when policy config is nil") |
| 177 | +} |
0 commit comments