github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/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 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 . "github.com/onsi/gomega/gbytes" 14 . "github.com/onsi/gomega/gexec" 15 ) 16 17 func fileAsString(path string) string { 18 configBytes, err := ioutil.ReadFile(path) 19 Expect(err).ToNot(HaveOccurred()) 20 21 return string(configBytes) 22 } 23 24 func replaceConfig(path string, old string, new string) { 25 r := regexp.MustCompile(old) 26 newConfig := r.ReplaceAllString(fileAsString(path), new) 27 err := ioutil.WriteFile(path, []byte(newConfig), 0600) 28 Expect(err).ToNot(HaveOccurred()) 29 } 30 31 var _ = Describe("custom oauth client id", func() { 32 var configPath string 33 34 BeforeEach(func() { 35 configPath = filepath.Join(homeDir, ".cf", "config.json") 36 }) 37 38 Context("when the config file exists", func() { 39 BeforeEach(func() { 40 setupCF(ReadOnlyOrg, ReadOnlySpace) 41 }) 42 43 Context("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 session := helpers.CF("auth", username, password) 63 Eventually(session).Should(Exit(1)) 64 Expect(session.Err).To(Say( 65 "Credentials were rejected, please try again.")) 66 }) 67 }) 68 69 Context("login", func() { 70 It("uses the custom client id and secret", func() { 71 username, password := helpers.GetCredentials() 72 session := helpers.CF("login", "-u", username, "-p", password) 73 Eventually(session).Should(Exit(1)) 74 Expect(session.Out).To(Say( 75 "Credentials were rejected, please try again.")) 76 }) 77 }) 78 }) 79 80 Context("when the client id in the config is empty", func() { 81 BeforeEach(func() { 82 replaceConfig( 83 configPath, `"UAAOAuthClient": ".*",`, `"UAAOAuthClient": "",`) 84 }) 85 86 Context("v2 command", func() { 87 It("replaces the empty client id with the default values for client id and secret", func() { 88 session := helpers.CF("oauth-token") 89 Eventually(session).Should(Exit(0)) 90 91 configString := fileAsString(configPath) 92 Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`)) 93 Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`)) 94 }) 95 }) 96 97 Context("v3 command", func() { 98 It("writes default values for client id and secret", func() { 99 session := helpers.CF("tasks", "some-app") 100 Eventually(session).Should(Exit(1)) 101 102 configString := fileAsString(configPath) 103 Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`)) 104 Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`)) 105 }) 106 }) 107 }) 108 109 Context("when there are no client id and secret keys in the config", func() { 110 BeforeEach(func() { 111 replaceConfig( 112 configPath, `"UAAOAuthClient": ".*",`, "") 113 replaceConfig( 114 configPath, `"UAAOAuthClientSecret": ".*",`, "") 115 }) 116 117 Context("v2 command", func() { 118 It("writes default values for client id and secret", func() { 119 session := helpers.CF("oauth-token") 120 Eventually(session).Should(Exit(0)) 121 122 configString := fileAsString(configPath) 123 Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`)) 124 Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`)) 125 }) 126 }) 127 128 Context("v3 command", func() { 129 It("writes default values for client id and secret", func() { 130 session := helpers.CF("tasks") 131 Eventually(session).Should(Exit(1)) 132 133 configString := fileAsString(configPath) 134 Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`)) 135 Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`)) 136 }) 137 }) 138 }) 139 }) 140 141 Context("when the config file does not exist", func() { 142 BeforeEach(func() { 143 err := os.Remove(configPath) 144 Expect(err).ToNot(HaveOccurred()) 145 }) 146 147 Context("v2 command", func() { 148 It("writes default values for client id and secret to the config", func() { 149 Expect(configPath).ToNot(BeAnExistingFile()) 150 151 session := helpers.CF("help") 152 Eventually(session).Should(Exit(0)) 153 154 configString := fileAsString(configPath) 155 Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`)) 156 Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`)) 157 }) 158 }) 159 160 Context("v3 command", func() { 161 It("writes default values for client id and secret to the config", func() { 162 Expect(configPath).ToNot(BeAnExistingFile()) 163 164 session := helpers.CF("tasks") 165 Eventually(session).Should(Exit(1)) 166 167 configString := fileAsString(configPath) 168 Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`)) 169 Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`)) 170 }) 171 }) 172 }) 173 })