github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v7/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 - Display the OAuth token for the current session and refresh the token if necessary")) 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 current access token is valid", func() { 36 BeforeEach(func() { 37 helpers.SkipIfClientCredentialsTestMode() 38 helpers.LoginCF() 39 }) 40 41 It("reads the access token from the config file", func() { 42 existingAccessToken := helpers.GetConfig().ConfigFile.AccessToken 43 44 session := helpers.CF("oauth-token").Wait() 45 46 output := string(session.Out.Contents()) 47 Expect(output).To(ContainSubstring(existingAccessToken)) 48 Expect(output).To(MatchRegexp("bearer .+")) 49 50 Expect(session.Err.Contents()).To(BeEmpty()) 51 Expect(session.ExitCode()).To(Equal(0)) 52 }) 53 }) 54 55 When("the current access token needs to be refreshed", func() { 56 BeforeEach(func() { 57 helpers.SkipIfClientCredentialsTestMode() 58 helpers.LoginCF() 59 helpers.SetConfig(func(conf *configv3.Config) { 60 conf.ConfigFile.AccessToken = "change-me-token" 61 }) 62 }) 63 64 It("refreshes the access token and displays it", func() { 65 existingAccessToken := helpers.GetConfig().ConfigFile.AccessToken 66 67 session := helpers.CF("oauth-token").Wait() 68 69 output := string(session.Out.Contents()) 70 Expect(output).ToNot(ContainSubstring(existingAccessToken)) 71 Expect(output).To(MatchRegexp("bearer .+")) 72 73 Expect(session.Err.Contents()).To(BeEmpty()) 74 Expect(session.ExitCode()).To(Equal(0)) 75 }) 76 77 When("the refresh token is invalid", func() { 78 BeforeEach(func() { 79 helpers.SetConfig(func(conf *configv3.Config) { 80 conf.ConfigFile.RefreshToken = "invalid-refresh-token" 81 }) 82 }) 83 84 It("displays an error and exits 1", func() { 85 session := helpers.CF("oauth-token") 86 87 Eventually(session).Should(Say("FAILED")) 88 Eventually(session.Err).Should(Say(`The token expired, was revoked, or the token ID is incorrect\. Please log back in to re-authenticate\.`)) 89 Eventually(session).Should(Exit(1)) 90 }) 91 }) 92 93 When("the oauth client ID and secret combination is invalid", func() { 94 BeforeEach(func() { 95 helpers.SetConfig(func(conf *configv3.Config) { 96 conf.ConfigFile.UAAOAuthClient = "non-existent-client" 97 conf.ConfigFile.UAAOAuthClientSecret = "some-secret" 98 }) 99 }) 100 101 It("displays an error and exits 1", func() { 102 session := helpers.CF("oauth-token") 103 104 Eventually(session).Should(Say("FAILED")) 105 Eventually(session.Err).Should(Say(`Credentials were rejected, please try again\.`)) 106 Eventually(session).Should(Exit(1)) 107 }) 108 }) 109 }) 110 111 Context("using client credentials", func() { 112 BeforeEach(func() { 113 helpers.LoginCFWithClientCredentials() 114 }) 115 116 When("the access token is valid", func() { 117 It("displays the current access token without trying to re-authenticate", func() { 118 existingAccessToken := helpers.GetConfig().ConfigFile.AccessToken 119 session := helpers.CF("oauth-token") 120 121 Eventually(session).Should(Say(regexp.QuoteMeta(existingAccessToken))) 122 Eventually(session).Should(Exit(0)) 123 }) 124 }) 125 126 When("the access token needs to be refreshed", func() { 127 BeforeEach(func() { 128 helpers.SetConfig(func(conf *configv3.Config) { 129 conf.ConfigFile.AccessToken = "invalid-access-token" 130 }) 131 }) 132 133 When("the provided client credentials are valid", func() { 134 BeforeEach(func() { 135 clientID, clientSecret := helpers.SkipIfClientCredentialsNotSet() 136 137 helpers.SetConfig(func(conf *configv3.Config) { 138 conf.ConfigFile.UAAOAuthClient = clientID 139 conf.ConfigFile.UAAOAuthClientSecret = clientSecret 140 }) 141 }) 142 143 It("re-authenticates and displays the new access token", func() { 144 session := helpers.CF("oauth-token") 145 146 Eventually(session).Should(Say("bearer .+")) 147 Eventually(session).Should(Exit(0)) 148 }) 149 }) 150 151 When("the oauth client ID and secret combination is invalid", func() { 152 BeforeEach(func() { 153 helpers.SetConfig(func(conf *configv3.Config) { 154 conf.ConfigFile.UAAOAuthClient = "non-existent-client" 155 conf.ConfigFile.UAAOAuthClientSecret = "some-secret" 156 }) 157 }) 158 159 It("displays an error and exits 1", func() { 160 session := helpers.CF("oauth-token") 161 162 Eventually(session).Should(Say("FAILED")) 163 Eventually(session.Err).Should(Say(`Credentials were rejected, please try again\.`)) 164 Eventually(session).Should(Exit(1)) 165 }) 166 }) 167 168 When("the client credentials are not present in the config", func() { 169 It("displays an error and exits 1", func() { 170 session := helpers.CF("oauth-token") 171 172 Eventually(session).Should(Say("FAILED")) 173 Eventually(session.Err).Should(Say(`Access token is invalid\.`)) 174 Eventually(session).Should(Exit(1)) 175 }) 176 }) 177 }) 178 }) 179 })