Skip to content

Commit

Permalink
OCM-7871 | fix: Mandatory fields should enter interactive mode if not…
Browse files Browse the repository at this point in the history
… provided

Signed-off-by: Maggie Chen <[email protected]>

add tests

Signed-off-by: Maggie Chen <[email protected]>
  • Loading branch information
chenz4027 committed May 29, 2024
1 parent cb97232 commit a8e7b20
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
54 changes: 54 additions & 0 deletions cmd/create/externalauthprovider/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package externalauthprovider

import (
"net/http"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
. "github.com/openshift-online/ocm-sdk-go/testing"

"github.com/openshift/rosa/pkg/test"
)

var _ = Describe("ExternalAuthProvider Create Tests", func() {
var testRuntime test.TestingRuntime

Context("Create break glass credential command", func() {
mockClusterReady := test.MockCluster(func(c *cmv1.ClusterBuilder) {
c.AWS(cmv1.NewAWS().SubnetIDs("subnet-0b761d44d3d9a4663", "subnet-0f87f640e56934cbc"))
c.Region(cmv1.NewCloudRegion().ID("us-east-1"))
c.State(cmv1.ClusterStateReady)
c.Hypershift(cmv1.NewHypershift().Enabled(true))
c.ExternalAuthConfig(cmv1.NewExternalAuthConfig().Enabled(true))
})
hypershiftClusterReady := test.FormatClusterList([]*cmv1.Cluster{mockClusterReady})

BeforeEach(func() {
testRuntime.InitRuntime()
// Reset flag to avoid any side effect on other tests
Cmd.Flags().Set("output", "")
})

It("KO: Should ask for interactive mode if params are not passed", func() {
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusNotFound, ""))
stdout, stderr, err := test.RunWithOutputCaptureAndArgv(runWithRuntime, testRuntime.RosaRuntime, Cmd, &[]string{})
Expect(err).To(HaveOccurred())
Expect(stderr).To(Equal(""))
Expect(stdout).To(Equal("INFO: Enabling interactive mode\n\x1b[0G\x1b[2K? " +
"Name: [? for help] \x1b[?25l\x1b[?25l\x1b7\x1b[999;999f\x1b[6n\x1b8\x1b[?25h\x1b[6n\x1b[?25h"))
})

It("KO: Should ask for interactive mode if all mandatory fields are not given", func() {
Cmd.Flags().Set("name", "test-name")
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, hypershiftClusterReady))
testRuntime.ApiServer.AppendHandlers(RespondWithJSON(http.StatusNotFound, ""))
stdout, stderr, err := test.RunWithOutputCaptureAndArgv(runWithRuntime, testRuntime.RosaRuntime, Cmd, &[]string{})
Expect(err).To(HaveOccurred())
Expect(stderr).To(Equal(""))
Expect(stdout).To(Equal("\x1b[0G\x1b[2K? Issuer audiences: [? for help]" +
" \x1b[?25l\x1b[?25l\x1b7\x1b[999;999f\x1b[6n\x1b8\x1b[?25h\x1b[6n\x1b[?25h"))
})
})
})
29 changes: 29 additions & 0 deletions cmd/create/externalauthprovider/externalauthprovider_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright (c) 2024 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package externalauthprovider

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestIdp(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "ExternalAuthProvider Create Suite")
}
10 changes: 5 additions & 5 deletions pkg/externalauthprovider/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func GetExternalAuthOptions(
}
}

if interactive.Enabled() && !cmd.Changed(nameFlag) {
if !cmd.Changed(nameFlag) {
result.name, err = interactive.GetString(interactive.Input{
Question: "Name",
Default: result.name,
Expand All @@ -214,7 +214,7 @@ func GetExternalAuthOptions(

}

if interactive.Enabled() && !cmd.Changed(issuerAudiencesFlag) {
if !cmd.Changed(issuerAudiencesFlag) {
issuerAudiencesInput, err := interactive.GetString(interactive.Input{
Question: "Issuer audiences",
Default: strings.Join(issuerAudiencesSlice, ","),
Expand All @@ -229,7 +229,7 @@ func GetExternalAuthOptions(
result.issuerAudiences = issuerAudiencesSlice
}

if interactive.Enabled() && !cmd.Changed(issuerUrlFlag) {
if !cmd.Changed(issuerUrlFlag) {
result.issuerUrl, err = interactive.GetString(interactive.Input{
Question: "The serving url of the token issuer",
Default: result.issuerUrl,
Expand All @@ -255,7 +255,7 @@ func GetExternalAuthOptions(
}
}

if interactive.Enabled() && !cmd.Changed(claimMappingUsernameClaimFlag) {
if !cmd.Changed(claimMappingUsernameClaimFlag) {
result.claimMappingUsernameClaim, err = interactive.GetString(interactive.Input{
Question: "Claim mapping username",
Default: defaultClaimMappingUsername,
Expand All @@ -267,7 +267,7 @@ func GetExternalAuthOptions(
}
}

if interactive.Enabled() && !cmd.Changed(claimMappingGroupsClaimFlag) {
if !cmd.Changed(claimMappingGroupsClaimFlag) {
result.claimMappingGroupsClaim, err = interactive.GetString(interactive.Input{
Question: "Claim mapping groups",
Default: defaultClaimMappingGroups,
Expand Down

0 comments on commit a8e7b20

Please sign in to comment.