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