github.com/sleungcy/cli@v7.1.0+incompatible/integration/v6/isolated/oauth_token_command_test.go (about) 1 package isolated 2 3 import ( 4 "regexp" 5 6 "code.cloudfoundry.org/cli/integration/helpers" 7 "code.cloudfoundry.org/cli/util/configv3" 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 . "github.com/onsi/gomega/gbytes" 11 . "github.com/onsi/gomega/gexec" 12 ) 13 14 var _ = Describe("oauth-token command", func() { 15 Context("help", func() { 16 It("displays the help information", func() { 17 session := helpers.CF("oauth-token", "--help") 18 19 Eventually(session).Should(Say("NAME:")) 20 Eventually(session).Should(Say("oauth-token - Retrieve and display the OAuth token for the current session")) 21 Eventually(session).Should(Say("USAGE:")) 22 Eventually(session).Should(Say("cf oauth-token")) 23 Eventually(session).Should(Say("SEE ALSO:")) 24 Eventually(session).Should(Say("curl")) 25 Eventually(session).Should(Exit(0)) 26 }) 27 }) 28 29 When("the environment is not setup correctly", func() { 30 It("fails with the appropriate errors", func() { 31 helpers.CheckEnvironmentTargetedCorrectly(false, false, ReadOnlyOrg, "oauth-token") 32 }) 33 }) 34 35 When("the environment is setup correctly and user is logged in with password grant", func() { 36 BeforeEach(func() { 37 helpers.SkipIfClientCredentialsTestMode() 38 helpers.LoginCF() 39 }) 40 41 When("the refresh token is invalid", func() { 42 BeforeEach(func() { 43 helpers.SetConfig(func(conf *configv3.Config) { 44 conf.ConfigFile.RefreshToken = "invalid-refresh-token" 45 }) 46 }) 47 48 It("displays an error and exits 1", func() { 49 session := helpers.CF("oauth-token") 50 51 Eventually(session).Should(Say("FAILED")) 52 Eventually(session.Err).Should(Say(`The token expired, was revoked, or the token ID is incorrect\. Please log back in to re-authenticate\.`)) 53 Eventually(session).Should(Exit(1)) 54 }) 55 }) 56 57 When("the oauth client ID and secret combination is invalid", func() { 58 BeforeEach(func() { 59 helpers.SetConfig(func(conf *configv3.Config) { 60 conf.ConfigFile.UAAOAuthClient = "non-existent-client" 61 conf.ConfigFile.UAAOAuthClientSecret = "some-secret" 62 }) 63 }) 64 65 It("displays an error and exits 1", func() { 66 session := helpers.CF("oauth-token") 67 68 Eventually(session).Should(Say("FAILED")) 69 Eventually(session.Err).Should(Say(`Credentials were rejected, please try again\.`)) 70 Eventually(session).Should(Exit(1)) 71 }) 72 }) 73 74 When("the refresh token and oauth creds are valid", func() { 75 BeforeEach(func() { 76 helpers.SkipIfClientCredentialsTestMode() 77 }) 78 79 It("refreshes the access token and displays it", func() { 80 existingAccessToken := helpers.GetConfig().ConfigFile.AccessToken 81 82 session := helpers.CF("oauth-token").Wait() 83 84 output := string(session.Out.Contents()) 85 Expect(output).ToNot(ContainSubstring(existingAccessToken)) 86 Expect(output).To(MatchRegexp("bearer .+")) 87 88 Expect(session.Err.Contents()).To(BeEmpty()) 89 Expect(session.ExitCode()).To(Equal(0)) 90 }) 91 }) 92 }) 93 94 When("the environment is setup correctly and user is logged in with client credentials grant", func() { 95 BeforeEach(func() { 96 helpers.LoginCFWithClientCredentials() 97 }) 98 99 When("the access token has not expired", func() { 100 It("displays the current access token without trying to re-authenticate", func() { 101 existingAccessToken := helpers.GetConfig().ConfigFile.AccessToken 102 session := helpers.CF("oauth-token") 103 104 Eventually(session).Should(Say(regexp.QuoteMeta(existingAccessToken))) 105 Eventually(session).Should(Exit(0)) 106 }) 107 }) 108 109 When("the access token has expired", func() { 110 BeforeEach(func() { 111 helpers.SetConfig(func(conf *configv3.Config) { 112 conf.ConfigFile.AccessToken = helpers.ExpiredAccessToken() 113 }) 114 }) 115 It("displays an error and exits 1", func() { 116 session := helpers.CF("oauth-token") 117 118 Eventually(session).Should(Say("FAILED")) 119 Eventually(session.Err).Should(Say(`Access token has expired\.`)) 120 Eventually(session).Should(Exit(1)) 121 }) 122 }) 123 124 When("the oauth client ID and secret combination is invalid", func() { 125 BeforeEach(func() { 126 helpers.SetConfig(func(conf *configv3.Config) { 127 conf.ConfigFile.UAAOAuthClient = "non-existent-client" 128 conf.ConfigFile.UAAOAuthClientSecret = "some-secret" 129 }) 130 }) 131 132 It("displays an error and exits 1", func() { 133 session := helpers.CF("oauth-token") 134 135 Eventually(session).Should(Say("FAILED")) 136 Eventually(session.Err).Should(Say(`Credentials were rejected, please try again\.`)) 137 Eventually(session).Should(Exit(1)) 138 }) 139 }) 140 141 When("the access token is invalid", func() { 142 BeforeEach(func() { 143 helpers.SetConfig(func(conf *configv3.Config) { 144 conf.ConfigFile.AccessToken = "invalid-access-token" 145 }) 146 }) 147 148 When("the client credentials have been manually added to the config", func() { 149 BeforeEach(func() { 150 clientID, clientSecret := helpers.SkipIfClientCredentialsNotSet() 151 152 helpers.SetConfig(func(conf *configv3.Config) { 153 conf.ConfigFile.UAAOAuthClient = clientID 154 conf.ConfigFile.UAAOAuthClientSecret = clientSecret 155 }) 156 }) 157 158 It("re-authenticates and displays the new access token", func() { 159 session := helpers.CF("oauth-token") 160 161 Eventually(session).Should(Say("bearer .+")) 162 Eventually(session).Should(Exit(0)) 163 }) 164 }) 165 166 When("the client credentials are not present in the config", func() { 167 It("displays an error and exits 1", func() { 168 session := helpers.CF("oauth-token") 169 170 Eventually(session).Should(Say("FAILED")) 171 Eventually(session.Err).Should(Say(`Access token is invalid\.`)) 172 Eventually(session).Should(Exit(1)) 173 }) 174 }) 175 176 }) 177 178 When("the oauth creds are valid", func() { 179 When("the client credentials have been manually added to the config", func() { 180 BeforeEach(func() { 181 clientID, clientSecret := helpers.SkipIfClientCredentialsNotSet() 182 183 helpers.SetConfig(func(conf *configv3.Config) { 184 conf.ConfigFile.UAAOAuthClient = clientID 185 conf.ConfigFile.UAAOAuthClientSecret = clientSecret 186 }) 187 }) 188 189 It("re-authenticates and displays the new access token", func() { 190 existingAccessToken := helpers.GetConfig().ConfigFile.AccessToken 191 192 session := helpers.CF("oauth-token").Wait() 193 194 output := string(session.Out.Contents()) 195 Expect(output).ToNot(ContainSubstring(existingAccessToken)) 196 Expect(output).To(MatchRegexp("bearer .+")) 197 198 Expect(session.Err.Contents()).To(BeEmpty()) 199 Expect(session.ExitCode()).To(Equal(0)) 200 }) 201 }) 202 }) 203 }) 204 })