Skip to content

Commit d753697

Browse files
test: add integration tests for old API key detection and upgrade handling
1 parent 50638f0 commit d753697

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

cli/cmd/api_key_integration_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ package cmd_test
99
import (
1010
"fmt"
1111
"os"
12+
"os/exec"
13+
"strings"
1214
"time"
1315

1416
. "github.com/onsi/ginkgo/v2"
@@ -217,4 +219,103 @@ var _ = Describe("API Key Integration Tests", func() {
217219
Expect(err.Error()).To(ContainSubstring("invalid date format"))
218220
})
219221
})
222+
223+
Describe("Old API Key Detection and Warning", func() {
224+
var (
225+
cliPath string
226+
)
227+
228+
BeforeEach(func() {
229+
cliPath = "./oms-cli"
230+
231+
_, err := os.Stat(cliPath)
232+
if err != nil {
233+
Skip("OMS CLI not found at " + cliPath + ", please build it first with 'make build-cli'")
234+
}
235+
})
236+
237+
Context("when using a 22-character old API key format", func() {
238+
It("should detect the old format and attempt to upgrade", func() {
239+
cmd := exec.Command(cliPath, "version")
240+
cmd.Env = append(os.Environ(),
241+
"OMS_PORTAL_API_KEY=U4jsSHoDsOFGyEkPrWpsE", // 22 characters
242+
"OMS_PORTAL_API=http://localhost:3000/api",
243+
)
244+
245+
output, _ := cmd.CombinedOutput()
246+
outputStr := string(output)
247+
248+
Expect(outputStr).To(ContainSubstring("OMS CLI version"))
249+
})
250+
})
251+
252+
Context("when using a new long-format API key", func() {
253+
It("should not show any warning", func() {
254+
cmd := exec.Command(cliPath, "version")
255+
cmd.Env = append(os.Environ(),
256+
"OMS_PORTAL_API_KEY=4hBieJRj2pWeB9qKJ9wQGE3CrcldLnLwP8fz6qutMjkf1n1",
257+
"OMS_PORTAL_API=http://localhost:3000/api",
258+
)
259+
260+
output, _ := cmd.CombinedOutput()
261+
outputStr := string(output)
262+
263+
Expect(outputStr).To(ContainSubstring("OMS CLI version"))
264+
Expect(outputStr).NotTo(ContainSubstring("old API key"))
265+
Expect(outputStr).NotTo(ContainSubstring("Failed to upgrade"))
266+
})
267+
})
268+
269+
Context("when using a 22-character key with list api-keys command", func() {
270+
It("should attempt the upgrade and handle the error gracefully", func() {
271+
cmd := exec.Command(cliPath, "list", "api-keys")
272+
cmd.Env = append(os.Environ(),
273+
"OMS_PORTAL_API_KEY=U4jsSHoDsOFGyEkPrWpsE", // 22 characters (old format)
274+
"OMS_PORTAL_API=http://localhost:3000/api",
275+
)
276+
277+
output, err := cmd.CombinedOutput()
278+
outputStr := string(output)
279+
280+
Expect(err).To(HaveOccurred())
281+
282+
hasWarning := strings.Contains(outputStr, "old API key") ||
283+
strings.Contains(outputStr, "Failed to upgrade") ||
284+
strings.Contains(outputStr, "Unauthorized")
285+
286+
Expect(hasWarning).To(BeTrue(),
287+
"Should contain warning about old key or auth failure. Got: "+outputStr)
288+
})
289+
})
290+
291+
Context("when checking key length detection", func() {
292+
It("should correctly identify 22-character old format", func() {
293+
oldKey := "U4jsSHoDsOFGyEkPrWpsE"
294+
Expect(len(oldKey)).To(Equal(22))
295+
})
296+
297+
It("should correctly identify new long format", func() {
298+
newKey := "4hBieJRj2pWeB9qKJ9wQGE3CrcldLnLwP8fz6qutMjkf1n1"
299+
Expect(len(newKey)).NotTo(Equal(22))
300+
Expect(len(newKey)).To(BeNumerically(">", 22))
301+
})
302+
})
303+
})
304+
305+
Describe("PreRun Hook Execution", func() {
306+
Context("when running any OMS command", func() {
307+
It("should execute the PreRun hook", func() {
308+
cmd := exec.Command("./oms-cli", "version")
309+
cmd.Env = append(os.Environ(),
310+
"OMS_PORTAL_API_KEY=valid-key-format-short",
311+
"OMS_PORTAL_API=http://localhost:3000/api",
312+
)
313+
314+
output, _ := cmd.CombinedOutput()
315+
outputStr := string(output)
316+
317+
Expect(outputStr).To(ContainSubstring("OMS CLI version"))
318+
})
319+
})
320+
})
220321
})

0 commit comments

Comments
 (0)