|
| 1 | +// Copyright 2026 Stefan Prodan. |
| 2 | +// SPDX-License-Identifier: AGPL-3.0 |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + . "github.com/onsi/gomega" |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 13 | + "k8s.io/apimachinery/pkg/types" |
| 14 | + "sigs.k8s.io/yaml" |
| 15 | +) |
| 16 | + |
| 17 | +func TestCreateSecretWebAuthCmd(t *testing.T) { |
| 18 | + gt := NewWithT(t) |
| 19 | + ns, err := testEnv.CreateNamespace(context.Background(), "test-web-auth") |
| 20 | + gt.Expect(err).ToNot(HaveOccurred()) |
| 21 | + |
| 22 | + tests := []struct { |
| 23 | + name string |
| 24 | + args []string |
| 25 | + expectError bool |
| 26 | + expectExport bool |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "create web-auth secret with client-secret", |
| 30 | + args: []string{"create", "secret", "web-auth", "test-secret", "--client-id=test-client", "--client-secret=test-secret-value"}, |
| 31 | + expectError: false, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "create web-auth secret with random client-secret", |
| 35 | + args: []string{"create", "secret", "web-auth", "test-secret-rnd", "--client-id=test-client", "--client-secret-rnd"}, |
| 36 | + expectError: false, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "create web-auth secret with export", |
| 40 | + args: []string{"create", "secret", "web-auth", "test-secret", "--client-id=test-client", "--client-secret=test-secret-value", "--export"}, |
| 41 | + expectError: false, |
| 42 | + expectExport: true, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "create web-auth secret with random and export", |
| 46 | + args: []string{"create", "secret", "web-auth", "test-secret", "--client-id=test-client", "--client-secret-rnd", "--export"}, |
| 47 | + expectError: false, |
| 48 | + expectExport: true, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "create web-auth secret with annotations and labels", |
| 52 | + // FIX 1: flag is now registered as "--label" (singular) in init(), |
| 53 | + // consistent with "--annotation". Test arg updated to match. |
| 54 | + args: []string{"create", "secret", "web-auth", "test-secret", "--client-id=test-client", "--client-secret=test-secret-value", "--annotation=test.io/annotation=value", "--label=test.io/label=value"}, |
| 55 | + expectError: false, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "create immutable web-auth secret", |
| 59 | + args: []string{"create", "secret", "web-auth", "test-secret", "--client-id=test-client", "--client-secret=test-secret-value", "--immutable"}, |
| 60 | + expectError: false, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "missing client-id", |
| 64 | + // FIX 2: removed expectExport: true — never evaluated when expectError is true, |
| 65 | + // misleading to readers and can mask real intent of the test case. |
| 66 | + args: []string{"create", "secret", "web-auth", "test-secret", "--client-secret=test-secret-value", "--export"}, |
| 67 | + expectError: true, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "missing client-secret source", |
| 71 | + args: []string{"create", "secret", "web-auth", "test-secret", "--client-id=test-client", "--export"}, |
| 72 | + expectError: true, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "multiple client-secret sources", |
| 76 | + args: []string{"create", "secret", "web-auth", "test-secret", "--client-id=test-client", "--client-secret=test", "--client-secret-rnd", "--export"}, |
| 77 | + expectError: true, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "missing secret name", |
| 81 | + args: []string{"create", "secret", "web-auth", "--client-id=test-client", "--client-secret=test-secret-value"}, |
| 82 | + expectError: true, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + for _, tt := range tests { |
| 87 | + t.Run(tt.name, func(t *testing.T) { |
| 88 | + ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 89 | + defer cancel() |
| 90 | + |
| 91 | + g := NewWithT(t) |
| 92 | + |
| 93 | + kubeconfigArgs.Namespace = &ns.Name |
| 94 | + output, err := executeCommand(tt.args) |
| 95 | + |
| 96 | + if tt.expectError { |
| 97 | + g.Expect(err).To(HaveOccurred()) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + g.Expect(err).ToNot(HaveOccurred()) |
| 102 | + |
| 103 | + if tt.expectExport { |
| 104 | + obj := &unstructured.Unstructured{} |
| 105 | + err = yaml.Unmarshal([]byte(output), &obj.Object) |
| 106 | + g.Expect(err).ToNot(HaveOccurred()) |
| 107 | + |
| 108 | + g.Expect(obj.GetAPIVersion()).To(Equal("v1")) |
| 109 | + g.Expect(obj.GetKind()).To(Equal("Secret")) |
| 110 | + g.Expect(obj.GetName()).To(Equal("test-secret")) |
| 111 | + g.Expect(obj.GetNamespace()).To(Equal(ns.Name)) |
| 112 | + |
| 113 | + secretType, found, err := unstructured.NestedString(obj.Object, "type") |
| 114 | + g.Expect(err).ToNot(HaveOccurred()) |
| 115 | + g.Expect(found).To(BeTrue()) |
| 116 | + g.Expect(secretType).To(Equal(string(corev1.SecretTypeOpaque))) |
| 117 | + |
| 118 | + stringData, found, err := unstructured.NestedStringMap(obj.Object, "stringData") |
| 119 | + g.Expect(err).ToNot(HaveOccurred()) |
| 120 | + g.Expect(found).To(BeTrue()) |
| 121 | + g.Expect(stringData).To(HaveKey("client-id")) |
| 122 | + g.Expect(stringData).To(HaveKey("client-secret")) |
| 123 | + g.Expect(stringData["client-id"]).To(Equal("test-client")) |
| 124 | + |
| 125 | + if tt.name == "create web-auth secret with random and export" { |
| 126 | + g.Expect(stringData["client-secret"]).ToNot(BeEmpty()) |
| 127 | + g.Expect(len(stringData["client-secret"])).To(BeNumerically(">=", 32)) |
| 128 | + } |
| 129 | + |
| 130 | + // Verify clean export — creationTimestamp should not be present. |
| 131 | + _, found, err = unstructured.NestedString(obj.Object, "metadata", "creationTimestamp") |
| 132 | + g.Expect(err).ToNot(HaveOccurred()) |
| 133 | + g.Expect(found).To(BeFalse()) |
| 134 | + |
| 135 | + } else { |
| 136 | + secretName := "test-secret" |
| 137 | + if tt.name == "create web-auth secret with random client-secret" { |
| 138 | + secretName = "test-secret-rnd" |
| 139 | + } |
| 140 | + |
| 141 | + secret := &corev1.Secret{} |
| 142 | + secretKey := types.NamespacedName{Name: secretName, Namespace: ns.Name} |
| 143 | + err = testClient.Get(ctx, secretKey, secret) |
| 144 | + g.Expect(err).ToNot(HaveOccurred()) |
| 145 | + |
| 146 | + g.Expect(secret.Type).To(Equal(corev1.SecretTypeOpaque)) |
| 147 | + g.Expect(secret.Data).To(HaveKey("client-id")) |
| 148 | + g.Expect(secret.Data).To(HaveKey("client-secret")) |
| 149 | + g.Expect(string(secret.Data["client-id"])).To(Equal("test-client")) |
| 150 | + |
| 151 | + if tt.name == "create web-auth secret with client-secret" { |
| 152 | + g.Expect(string(secret.Data["client-secret"])).To(Equal("test-secret-value")) |
| 153 | + } |
| 154 | + |
| 155 | + if tt.name == "create web-auth secret with random client-secret" { |
| 156 | + g.Expect(string(secret.Data["client-secret"])).ToNot(BeEmpty()) |
| 157 | + g.Expect(len(secret.Data["client-secret"])).To(BeNumerically(">=", 32)) |
| 158 | + } |
| 159 | + |
| 160 | + if tt.name == "create web-auth secret with annotations and labels" { |
| 161 | + g.Expect(secret.Annotations).To(HaveKeyWithValue("test.io/annotation", "value")) |
| 162 | + g.Expect(secret.Labels).To(HaveKeyWithValue("test.io/label", "value")) |
| 163 | + } |
| 164 | + |
| 165 | + if tt.name == "create immutable web-auth secret" { |
| 166 | + g.Expect(secret.Immutable).ToNot(BeNil()) |
| 167 | + g.Expect(*secret.Immutable).To(BeTrue()) |
| 168 | + } |
| 169 | + |
| 170 | + defer func() { |
| 171 | + _ = testClient.Delete(context.Background(), secret) |
| 172 | + }() |
| 173 | + } |
| 174 | + }) |
| 175 | + } |
| 176 | +} |
0 commit comments