github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/integration/v7/selfcontained/login_command_test.go (about) 1 package selfcontained_test 2 3 import ( 4 "fmt" 5 "net/http" 6 "os" 7 "path/filepath" 8 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 "github.com/onsi/gomega/gbytes" 12 "github.com/onsi/gomega/gexec" 13 apiv1 "k8s.io/client-go/tools/clientcmd/api/v1" 14 15 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 16 "code.cloudfoundry.org/cli/integration/helpers" 17 "code.cloudfoundry.org/cli/integration/v7/selfcontained/fake" 18 "code.cloudfoundry.org/cli/resources" 19 "code.cloudfoundry.org/cli/util/configv3" 20 ) 21 22 var _ = Describe("LoginCommand", func() { 23 Describe("CF-on-k8s", func() { 24 var ( 25 stdin *gbytes.Buffer 26 kubeConfigPath string 27 kubeConfig apiv1.Config 28 session *gexec.Session 29 loginArgs []string 30 apiConfig fake.CFAPIConfig 31 ) 32 33 BeforeEach(func() { 34 loginArgs = []string{"login"} 35 helpers.SetConfig(func(config *configv3.Config) { 36 config.ConfigFile.Target = apiServer.URL() 37 config.ConfigFile.CFOnK8s.Enabled = true 38 }) 39 40 apiConfig = fake.CFAPIConfig{ 41 Routes: map[string]fake.Response{ 42 "GET /": {Code: http.StatusOK, Body: ccv3.Info{CFOnK8s: true}}, 43 "GET /v3/organizations": { 44 Code: http.StatusOK, Body: map[string]interface{}{ 45 "pagination": map[string]interface{}{}, 46 "resources": []resources.Organization{}, 47 }, 48 }, 49 "GET /whoami": { 50 Code: http.StatusOK, Body: map[string]interface{}{ 51 "name": "two", 52 "kind": "User", 53 }, 54 }, 55 }, 56 } 57 apiServer.SetConfiguration(apiConfig) 58 59 kubeConfig = apiv1.Config{ 60 Kind: "Config", 61 APIVersion: "v1", 62 AuthInfos: []apiv1.NamedAuthInfo{ 63 { 64 Name: "one", 65 AuthInfo: apiv1.AuthInfo{ 66 Token: "foo", 67 }, 68 }, 69 { 70 Name: "two", 71 AuthInfo: apiv1.AuthInfo{ 72 AuthProvider: &apiv1.AuthProviderConfig{ 73 Name: "oidc", 74 Config: map[string]string{ 75 "id-token": string(token), 76 "idp-issuer-url": "-", 77 "client-id": "-", 78 }, 79 }, 80 }, 81 }, 82 }, 83 Clusters: []apiv1.NamedCluster{ 84 { 85 Name: "my-cluster", 86 Cluster: apiv1.Cluster{ 87 Server: "https://example.org", 88 }, 89 }, 90 }, 91 Contexts: []apiv1.NamedContext{ 92 { 93 Name: "my-context", 94 Context: apiv1.Context{ 95 Cluster: "my-cluster", 96 AuthInfo: "my-auth-info", 97 Namespace: "my-namespace", 98 }, 99 }, 100 }, 101 CurrentContext: "my-context", 102 } 103 104 kubeConfigPath := filepath.Join(homeDir, ".kube", "config") 105 storeKubeConfig(kubeConfig, kubeConfigPath) 106 107 stdin = gbytes.NewBuffer() 108 _, wErr := fmt.Fprintf(stdin, "%d\n", 2) 109 Expect(wErr).ToNot(HaveOccurred()) 110 111 env = helpers.CFEnv{ 112 Stdin: stdin, 113 EnvVars: map[string]string{ 114 "KUBECONFIG": kubeConfigPath, 115 }, 116 } 117 }) 118 119 JustBeforeEach(func() { 120 session = helpers.CustomCF(env, loginArgs...) 121 }) 122 123 AfterEach(func() { 124 Expect(os.RemoveAll(kubeConfigPath)).To(Succeed()) 125 }) 126 127 It("prompts the user to select a user from the kube config file", func() { 128 Eventually(session).Should(gbytes.Say("1. one")) 129 Eventually(session).Should(gbytes.Say("2. two")) 130 Eventually(session).Should(gbytes.Say("Choose your Kubernetes authentication info")) 131 Eventually(session).Should(gbytes.Say("OK")) 132 Eventually(session).Should(gexec.Exit(0)) 133 }) 134 135 It("sets the user into the configuration", func() { 136 Eventually(session).Should(gexec.Exit(0)) 137 Expect(loadConfig().CFOnK8s.AuthInfo).To(Equal("two")) 138 }) 139 140 It("displays the logged in user", func() { 141 Eventually(session).Should(gbytes.Say("user:(\\s*)two")) 142 }) 143 144 When("the kubeconfig contains no user information", func() { 145 BeforeEach(func() { 146 kubeConfig.AuthInfos = []apiv1.NamedAuthInfo{} 147 storeKubeConfig(kubeConfig, filepath.Join(homeDir, ".kube", "config")) 148 }) 149 150 It("displays an error", func() { 151 Eventually(session.Err).Should(gbytes.Say("Unable to authenticate.")) 152 Eventually(session).Should(gbytes.Say("FAILED")) 153 Eventually(session).Should(gexec.Exit(1)) 154 }) 155 }) 156 157 When("providing -a flag without having targeted the api before", func() { 158 BeforeEach(func() { 159 helpers.SetConfig(func(config *configv3.Config) { 160 config.ConfigFile.Target = "" 161 config.ConfigFile.CFOnK8s = configv3.CFOnK8s{} 162 }) 163 164 loginArgs = append(loginArgs, "-a", apiServer.URL()) 165 }) 166 167 It("displays the logged in user", func() { 168 Eventually(session).Should(gbytes.Say("user:(\\s*)two")) 169 }) 170 }) 171 }) 172 })