@@ -9,6 +9,8 @@ package cmd_test
99import (
1010 "fmt"
1111 "os"
12+ "os/exec"
13+ "strings"
1214 "time"
1315
1416 . "github.com/onsi/ginkgo/v2"
@@ -62,7 +64,11 @@ var _ = Describe("API Key Integration Tests", func() {
6264 },
6365 }
6466
67+ GinkgoWriter .Printf ("Attempting to register API key for owner: %s at API: %s\n " , registerCmd .Opts .Owner , os .Getenv ("OMS_PORTAL_API" ))
6568 newKey , err := registerCmd .Register (portalClient )
69+ if err != nil {
70+ GinkgoWriter .Printf ("Registration failed: %v\n " , err )
71+ }
6672 Expect (err ).To (BeNil (), "API key registration should succeed" )
6773 Expect (newKey ).NotTo (BeNil (), "Register should return the created API key" )
6874
@@ -100,7 +106,12 @@ var _ = Describe("API Key Integration Tests", func() {
100106 },
101107 }
102108
109+ GinkgoWriter .Printf ("[DEBUG] Attempting to register API key for owner: %s, org: %s at API: %s\n " ,
110+ testOwner , testOrg , os .Getenv ("OMS_PORTAL_API" ))
103111 newKey , err := registerCmd .Register (portalClient )
112+ if err != nil {
113+ GinkgoWriter .Printf ("[ERROR] Registration failed: %v\n " , err )
114+ }
104115 Expect (err ).To (BeNil (), "API key registration should succeed" )
105116 Expect (newKey ).NotTo (BeNil (), "Register should return the created API key" )
106117
@@ -217,4 +228,107 @@ var _ = Describe("API Key Integration Tests", func() {
217228 Expect (err .Error ()).To (ContainSubstring ("invalid date format" ))
218229 })
219230 })
231+
232+ Describe ("Old API Key Detection and Warning" , func () {
233+ var (
234+ cliPath = "../../oms-cli"
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=fakeapikeywith22charsa" , // 22 characters
242+ "OMS_PORTAL_API=http://localhost:3000/api" ,
243+ )
244+
245+ output , err := cmd .CombinedOutput ()
246+ outputStr := string (output )
247+ if err != nil {
248+ GinkgoWriter .Printf ("Command error: %v, Output: %s\n " , err , outputStr )
249+ }
250+
251+ Expect (outputStr ).To (ContainSubstring ("OMS CLI version" ))
252+ })
253+ })
254+
255+ Context ("when using a new long-format API key" , func () {
256+ It ("should not show any warning" , func () {
257+ cmd := exec .Command (cliPath , "version" )
258+ cmd .Env = append (os .Environ (),
259+ "OMS_PORTAL_API_KEY=fake-api-key" ,
260+ "OMS_PORTAL_API=http://localhost:3000/api" ,
261+ )
262+
263+ output , err := cmd .CombinedOutput ()
264+ outputStr := string (output )
265+ if err != nil {
266+ GinkgoWriter .Printf ("Command error: %v, Output: %s\n " , err , outputStr )
267+ }
268+
269+ Expect (outputStr ).To (ContainSubstring ("OMS CLI version" ))
270+ Expect (outputStr ).NotTo (ContainSubstring ("old API key" ))
271+ Expect (outputStr ).NotTo (ContainSubstring ("Failed to upgrade" ))
272+ })
273+ })
274+
275+ Context ("when using a 22-character key with list api-keys command" , func () {
276+ It ("should attempt the upgrade and handle the error gracefully" , func () {
277+ cmd := exec .Command (cliPath , "list" , "api-keys" )
278+ cmd .Env = append (os .Environ (),
279+ "OMS_PORTAL_API_KEY=fakeapikeywith22charsa" , // 22 characters (old format)
280+ "OMS_PORTAL_API=http://localhost:3000/api" ,
281+ )
282+
283+ output , err := cmd .CombinedOutput ()
284+ outputStr := string (output )
285+
286+ Expect (err ).To (HaveOccurred ())
287+
288+ hasWarning := strings .Contains (outputStr , "old API key" ) ||
289+ strings .Contains (outputStr , "Failed to upgrade" ) ||
290+ strings .Contains (outputStr , "Unauthorized" )
291+
292+ Expect (hasWarning ).To (BeTrue (),
293+ "Should contain warning about old key or auth failure. Got: " + outputStr )
294+ })
295+ })
296+
297+ Context ("when checking key length detection" , func () {
298+ It ("should correctly identify 22-character old format" , func () {
299+ oldKey := "fakeapikeywith22charsa"
300+ Expect (len (oldKey )).To (Equal (22 ))
301+ })
302+
303+ It ("should correctly identify new long format" , func () {
304+ newKey := "4hBieJRj2pWeB9qKJ9wQGE3CrcldLnLwP8fz6qutMjkf1n1"
305+ Expect (len (newKey )).NotTo (Equal (22 ))
306+ Expect (len (newKey )).To (BeNumerically (">" , 22 ))
307+ })
308+ })
309+ })
310+
311+ Describe ("PreRun Hook Execution" , func () {
312+ var (
313+ cliPath = "../../oms-cli"
314+ )
315+
316+ Context ("when running any OMS command" , func () {
317+ It ("should execute the PreRun hook" , func () {
318+ cmd := exec .Command (cliPath , "version" )
319+ cmd .Env = append (os .Environ (),
320+ "OMS_PORTAL_API_KEY=valid-key-format-short" ,
321+ "OMS_PORTAL_API=http://localhost:3000/api" ,
322+ )
323+
324+ output , err := cmd .CombinedOutput ()
325+ outputStr := string (output )
326+ if err != nil {
327+ GinkgoWriter .Printf ("Command error: %v, Output: %s\n " , err , outputStr )
328+ }
329+
330+ Expect (outputStr ).To (ContainSubstring ("OMS CLI version" ))
331+ })
332+ })
333+ })
220334})
0 commit comments