|
| 1 | +// |
| 2 | +// Copyright 2026 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package usercontext |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/chainloop-dev/chainloop/app/controlplane/internal/usercontext/entities" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | +) |
| 26 | + |
| 27 | +var passHandler = func(_ context.Context, _ interface{}) (interface{}, error) { return "ok", nil } |
| 28 | + |
| 29 | +func TestWithSuspensionMiddleware(t *testing.T) { |
| 30 | + suspendedOrg := &entities.Org{ID: "org-1", Name: "test", Suspended: true} |
| 31 | + activeOrg := &entities.Org{ID: "org-1", Name: "test", Suspended: false} |
| 32 | + |
| 33 | + tests := []struct { |
| 34 | + name string |
| 35 | + org *entities.Org |
| 36 | + wantErr bool |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "no org context passes through", |
| 40 | + org: nil, |
| 41 | + wantErr: false, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "active org passes through", |
| 45 | + org: activeOrg, |
| 46 | + wantErr: false, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "suspended org is blocked", |
| 50 | + org: suspendedOrg, |
| 51 | + wantErr: true, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for _, tt := range tests { |
| 56 | + t.Run(tt.name, func(t *testing.T) { |
| 57 | + ctx := context.Background() |
| 58 | + if tt.org != nil { |
| 59 | + ctx = entities.WithCurrentOrg(ctx, tt.org) |
| 60 | + } |
| 61 | + |
| 62 | + m := WithSuspensionMiddleware() |
| 63 | + result, err := m(passHandler)(ctx, nil) |
| 64 | + |
| 65 | + if tt.wantErr { |
| 66 | + require.Error(t, err) |
| 67 | + assert.Contains(t, err.Error(), "suspended") |
| 68 | + assert.Nil(t, result) |
| 69 | + } else { |
| 70 | + require.NoError(t, err) |
| 71 | + assert.Equal(t, "ok", result) |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
0 commit comments