github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/integration/shared/isolated/oauth_client_test.go (about) 1 package isolated 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "regexp" 8 9 "code.cloudfoundry.org/cli/integration/helpers" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 . "github.com/onsi/gomega/gbytes" 13 . "github.com/onsi/gomega/gexec" 14 ) 15 16 func fileAsString(path string) string { 17 configBytes, err := ioutil.ReadFile(path) 18 Expect(err).ToNot(HaveOccurred()) 19 20 return string(configBytes) 21 } 22 23 func replaceConfig(path string, old string, new string) { 24 r := regexp.MustCompile(old) 25 newConfig := r.ReplaceAllString(fileAsString(path), new) 26 err := ioutil.WriteFile(path, []byte(newConfig), 0600) 27 Expect(err).ToNot(HaveOccurred()) 28 } 29 30 var _ = Describe("custom oauth client id", func() { 31 var configPath string 32 33 BeforeEach(func() { 34 configPath = filepath.Join(homeDir, ".cf", "config.json") 35 }) 36 37 When("the config file exists", func() { 38 BeforeEach(func() { 39 helpers.LoginCF() 40 helpers.TargetOrgAndSpace(ReadOnlyOrg, ReadOnlySpace) 41 }) 42 43 When("the client id and secret keys are set in the config", func() { 44 BeforeEach(func() { 45 replaceConfig( 46 configPath, `"UAAOAuthClient": ".*"`, `"UAAOAuthClient": "cf2"`) 47 replaceConfig( 48 configPath, `"UAAOAuthClientSecret": ".*"`, `"UAAOAuthClientSecret": "secret2"`) 49 }) 50 51 Context("oauth-token", func() { 52 It("uses the custom client id and secret", func() { 53 session := helpers.CF("oauth-token") 54 Eventually(session).Should(Exit(1)) 55 Expect(session.Err).To(Say(`Credentials were rejected, please try again\.`)) 56 }) 57 }) 58 59 Context("auth", func() { 60 It("uses the custom client id and secret", func() { 61 username, password := helpers.GetCredentials() 62 env := map[string]string{ 63 "CF_USERNAME": username, 64 "CF_PASSWORD": password, 65 } 66 session := helpers.CFWithEnv(env, "auth") 67 Eventually(session).Should(Exit(1)) 68 Expect(session.Err).To(Say( 69 "Credentials were rejected, please try again.")) 70 }) 71 }) 72 73 Context("login", func() { 74 It("uses the custom client id and secret", func() { 75 username, password := helpers.GetCredentials() 76 session := helpers.CF("login", "-u", username, "-p", password) 77 Eventually(session).Should(Exit(1)) 78 Expect(session).To(Say( 79 "Credentials were rejected, please try again.")) 80 }) 81 }) 82 }) 83 84 When("the client id in the config is empty", func() { 85 BeforeEach(func() { 86 replaceConfig( 87 configPath, `"UAAOAuthClient": ".*",`, `"UAAOAuthClient": "",`) 88 }) 89 90 Context("v6 command", func() { 91 It("replaces the empty client id with the default values for client id and secret", func() { 92 session := helpers.CF("oauth-token") 93 Eventually(session).Should(Exit(0)) 94 95 configString := fileAsString(configPath) 96 Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`)) 97 Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`)) 98 }) 99 }) 100 101 Context("v7 command", func() { 102 It("writes default values for client id and secret", func() { 103 session := helpers.CF("tasks", "some-app") 104 Eventually(session).Should(Exit(1)) 105 106 configString := fileAsString(configPath) 107 Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`)) 108 Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`)) 109 }) 110 }) 111 }) 112 113 When("there are no client id and secret keys in the config", func() { 114 BeforeEach(func() { 115 replaceConfig( 116 configPath, `"UAAOAuthClient": ".*",`, "") 117 replaceConfig( 118 configPath, `"UAAOAuthClientSecret": ".*",`, "") 119 }) 120 121 Context("v6 command", func() { 122 It("writes default values for client id and secret", func() { 123 session := helpers.CF("oauth-token") 124 Eventually(session).Should(Exit(0)) 125 126 configString := fileAsString(configPath) 127 Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`)) 128 Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`)) 129 }) 130 }) 131 132 Context("v7 command", func() { 133 It("writes default values for client id and secret", func() { 134 session := helpers.CF("tasks") 135 Eventually(session).Should(Exit(1)) 136 137 configString := fileAsString(configPath) 138 Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`)) 139 Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`)) 140 }) 141 }) 142 }) 143 }) 144 145 When("the config file does not exist", func() { 146 BeforeEach(func() { 147 err := os.Remove(configPath) 148 Expect(err).ToNot(HaveOccurred()) 149 }) 150 151 Context("v6 command", func() { 152 It("writes default values for client id and secret to the config", func() { 153 Expect(configPath).ToNot(BeAnExistingFile()) 154 155 session := helpers.CF("help") 156 Eventually(session).Should(Exit(0)) 157 158 configString := fileAsString(configPath) 159 Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`)) 160 Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`)) 161 }) 162 }) 163 164 Context("v7 command", func() { 165 It("writes default values for client id and secret to the config", func() { 166 Expect(configPath).ToNot(BeAnExistingFile()) 167 168 session := helpers.CF("tasks") 169 Eventually(session).Should(Exit(1)) 170 171 configString := fileAsString(configPath) 172 Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`)) 173 Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`)) 174 }) 175 }) 176 }) 177 })