|
| 1 | +/* |
| 2 | +Copyright 2026 The Kube Bind 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 | + |
| 17 | +package options |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + |
| 24 | + kubebindv1alpha2 "github.com/kube-bind/kube-bind/sdk/apis/kubebind/v1alpha2" |
| 25 | +) |
| 26 | + |
| 27 | +func TestOIDCValidate(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + options *OIDC |
| 31 | + wantErr bool |
| 32 | + errMsg string |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "embedded OIDC with valid issuer URL ending in /oidc", |
| 36 | + options: &OIDC{ |
| 37 | + Type: string(kubebindv1alpha2.OIDCProviderTypeEmbedded), |
| 38 | + IssuerClientID: "test-client-id", |
| 39 | + IssuerClientSecret: "test-client-secret", |
| 40 | + IssuerURL: "http://localhost:8080/oidc", |
| 41 | + CallbackURL: "http://localhost:8080/callback", |
| 42 | + }, |
| 43 | + wantErr: false, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "embedded OIDC with invalid issuer URL not ending in /oidc", |
| 47 | + options: &OIDC{ |
| 48 | + Type: string(kubebindv1alpha2.OIDCProviderTypeEmbedded), |
| 49 | + IssuerClientID: "test-client-id", |
| 50 | + IssuerClientSecret: "test-client-secret", |
| 51 | + IssuerURL: "http://localhost:8080", |
| 52 | + CallbackURL: "http://localhost:8080/callback", |
| 53 | + }, |
| 54 | + wantErr: true, |
| 55 | + errMsg: "--oidc-issuer-url must end with '/oidc' when using embedded OIDC provider", |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "embedded OIDC with trailing slash /oidc/", |
| 59 | + options: &OIDC{ |
| 60 | + Type: string(kubebindv1alpha2.OIDCProviderTypeEmbedded), |
| 61 | + IssuerClientID: "test-client-id", |
| 62 | + IssuerClientSecret: "test-client-secret", |
| 63 | + IssuerURL: "http://localhost:8080/oidc/", |
| 64 | + CallbackURL: "http://localhost:8080/callback", |
| 65 | + }, |
| 66 | + wantErr: true, |
| 67 | + errMsg: "--oidc-issuer-url must end with '/oidc' when using embedded OIDC provider", |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "external OIDC does not require /oidc suffix", |
| 71 | + options: &OIDC{ |
| 72 | + Type: string(kubebindv1alpha2.OIDCProviderTypeExternal), |
| 73 | + IssuerClientID: "test-client-id", |
| 74 | + IssuerClientSecret: "test-client-secret", |
| 75 | + IssuerURL: "http://localhost:8080", |
| 76 | + CallbackURL: "http://localhost:8080/callback", |
| 77 | + AllowedGroups: []string{"admins"}, |
| 78 | + }, |
| 79 | + wantErr: false, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tt := range tests { |
| 84 | + t.Run(tt.name, func(t *testing.T) { |
| 85 | + err := tt.options.Validate() |
| 86 | + |
| 87 | + if !tt.wantErr { |
| 88 | + require.NoError(t, err) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + require.Error(t, err) |
| 93 | + if tt.errMsg != "" { |
| 94 | + require.EqualError(t, err, tt.errMsg) |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
0 commit comments