-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcmd_suite_test.go
More file actions
124 lines (99 loc) · 3.55 KB
/
cmd_suite_test.go
File metadata and controls
124 lines (99 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd_test
import (
"bytes"
"encoding/json"
"io"
"net/http"
"os"
"testing"
"github.com/codesphere-cloud/oms/cli/cmd"
"github.com/codesphere-cloud/oms/internal/env"
"github.com/codesphere-cloud/oms/internal/portal"
"github.com/stretchr/testify/mock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestCmd(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Cmd Suite")
}
var _ = Describe("RootCmd", func() {
var (
mockEnv *env.MockEnv
mockHttpClient *portal.MockHttpClient
)
BeforeEach(func() {
mockEnv = env.NewMockEnv(GinkgoT())
mockHttpClient = portal.NewMockHttpClient(GinkgoT())
})
AfterEach(func() {
mockEnv.AssertExpectations(GinkgoT())
mockHttpClient.AssertExpectations(GinkgoT())
})
Describe("PreRun hook with old API key", func() {
Context("when API key is 22 characters (old format)", func() {
It("attempts to upgrade the key via GetApiKeyId", func() {
oldKey := "fakeapikeywith22charsa" // 22 characters
keyId := "test-key-id-12345"
expectedNewKey := keyId + oldKey
Expect(os.Setenv("OMS_PORTAL_API_KEY", oldKey)).NotTo(HaveOccurred())
Expect(os.Setenv("OMS_PORTAL_API", "http://test-portal.com/api")).NotTo(HaveOccurred())
mockEnv.EXPECT().GetOmsPortalApi().Return("http://test-portal.com/api")
mockHttpClient.EXPECT().Do(mock.Anything).RunAndReturn(
func(req *http.Request) (*http.Response, error) {
Expect(req.Header.Get("X-API-Key")).To(Equal(oldKey))
Expect(req.URL.Path).To(ContainSubstring("/key"))
response := map[string]string{
"keyId": keyId,
}
body, _ := json.Marshal(response)
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(body)),
}, nil
})
portalClient := &portal.PortalClient{
Env: mockEnv,
HttpClient: mockHttpClient,
}
result, err := portalClient.GetApiKeyId(oldKey)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(keyId))
// Concatenate on client side
newApiKey := result + oldKey
Expect(newApiKey).To(Equal(expectedNewKey))
Expect(os.Unsetenv("OMS_PORTAL_API_KEY")).NotTo(HaveOccurred())
Expect(os.Unsetenv("OMS_PORTAL_API")).NotTo(HaveOccurred())
})
})
Context("when API key is not 22 characters (new format)", func() {
It("does not attempt to upgrade the key", func() {
newKey := "new-long-api-key-format-very-long-string"
Expect(os.Setenv("OMS_PORTAL_API_KEY", newKey)).NotTo(HaveOccurred())
Expect(os.Setenv("OMS_PORTAL_API", "http://test-portal.com/api")).NotTo(HaveOccurred())
Expect(len(newKey)).NotTo(Equal(22))
Expect(os.Unsetenv("OMS_PORTAL_API_KEY")).NotTo(HaveOccurred())
Expect(os.Unsetenv("OMS_PORTAL_API")).NotTo(HaveOccurred())
})
})
Context("when API key is empty", func() {
It("does not attempt to upgrade", func() {
Expect(os.Setenv("OMS_PORTAL_API_KEY", "")).NotTo(HaveOccurred())
Expect(os.Setenv("OMS_PORTAL_API", "http://test-portal.com/api")).NotTo(HaveOccurred())
Expect(len(os.Getenv("OMS_PORTAL_API_KEY"))).To(Equal(0))
Expect(os.Unsetenv("OMS_PORTAL_API_KEY")).NotTo(HaveOccurred())
Expect(os.Unsetenv("OMS_PORTAL_API")).NotTo(HaveOccurred())
})
})
})
Describe("GetRootCmd", func() {
It("returns a valid root command", func() {
rootCmd := cmd.GetRootCmd()
Expect(rootCmd).NotTo(BeNil())
Expect(rootCmd.Use).To(Equal("oms"))
Expect(rootCmd.Short).To(Equal("Codesphere Operations Management System (OMS)"))
})
})
})