github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/integration/shared/isolated/oauth_token_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/integration/helpers"
     5  	"code.cloudfoundry.org/cli/util/configv3"
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  	. "github.com/onsi/gomega/gbytes"
     9  	. "github.com/onsi/gomega/gexec"
    10  )
    11  
    12  var _ = Describe("oauth-token command", func() {
    13  	Context("help", func() {
    14  		It("displays the help information", func() {
    15  			session := helpers.CF("oauth-token", "--help")
    16  
    17  			Eventually(session).Should(Say("NAME:"))
    18  			Eventually(session).Should(Say("oauth-token - Retrieve and display the OAuth token for the current session"))
    19  			Eventually(session).Should(Say("USAGE:"))
    20  			Eventually(session).Should(Say("cf oauth-token"))
    21  			Eventually(session).Should(Say("SEE ALSO:"))
    22  			Eventually(session).Should(Say("curl"))
    23  			Eventually(session).Should(Exit(0))
    24  		})
    25  	})
    26  
    27  	When("the environment is not setup correctly", func() {
    28  		It("fails with the appropriate errors", func() {
    29  			helpers.CheckEnvironmentTargetedCorrectly(false, false, ReadOnlyOrg, "oauth-token")
    30  		})
    31  	})
    32  
    33  	When("the environment is setup correctly and user is logged in with password grant", func() {
    34  		BeforeEach(func() {
    35  			helpers.LoginCF()
    36  		})
    37  
    38  		When("the refresh token is invalid", func() {
    39  			BeforeEach(func() {
    40  				helpers.SetConfig(func(conf *configv3.Config) {
    41  					conf.ConfigFile.RefreshToken = "invalid-refresh-token"
    42  				})
    43  			})
    44  
    45  			It("displays an error and exits 1", func() {
    46  				session := helpers.CF("oauth-token")
    47  
    48  				Eventually(session).Should(Say("FAILED"))
    49  				Eventually(session.Err).Should(Say(`The token expired, was revoked, or the token ID is incorrect\. Please log back in to re-authenticate\.`))
    50  				Eventually(session).Should(Exit(1))
    51  			})
    52  		})
    53  
    54  		When("the oauth client ID and secret combination is invalid", func() {
    55  			BeforeEach(func() {
    56  				helpers.SetConfig(func(conf *configv3.Config) {
    57  					conf.ConfigFile.UAAOAuthClient = "non-existent-client"
    58  					conf.ConfigFile.UAAOAuthClientSecret = "some-secret"
    59  				})
    60  			})
    61  
    62  			It("displays an error and exits 1", func() {
    63  				session := helpers.CF("oauth-token")
    64  
    65  				Eventually(session).Should(Say("FAILED"))
    66  				Eventually(session.Err).Should(Say(`Credentials were rejected, please try again\.`))
    67  				Eventually(session).Should(Exit(1))
    68  			})
    69  		})
    70  
    71  		When("the refresh token and oauth creds are valid", func() {
    72  			It("refreshes the access token and displays it", func() {
    73  				session := helpers.CF("oauth-token")
    74  
    75  				Eventually(session).Should(Say("bearer .+"))
    76  				Eventually(session).Should(Exit(0))
    77  			})
    78  		})
    79  	})
    80  
    81  	When("the environment is setup correctly and user is logged in with client credentials grant", func() {
    82  		BeforeEach(func() {
    83  			helpers.LoginCFWithClientCredentials()
    84  		})
    85  
    86  		When("the oauth client ID and secret combination is invalid", func() {
    87  			BeforeEach(func() {
    88  				helpers.SetConfig(func(conf *configv3.Config) {
    89  					conf.ConfigFile.UAAOAuthClient = "non-existent-client"
    90  					conf.ConfigFile.UAAOAuthClientSecret = "some-secret"
    91  				})
    92  			})
    93  
    94  			It("displays an error and exits 1", func() {
    95  				session := helpers.CF("oauth-token")
    96  
    97  				Eventually(session).Should(Say("FAILED"))
    98  				Eventually(session.Err).Should(Say(`Credentials were rejected, please try again\.`))
    99  				Eventually(session).Should(Exit(1))
   100  			})
   101  		})
   102  
   103  		When("the access token is invalid", func() {
   104  			BeforeEach(func() {
   105  				helpers.SetConfig(func(conf *configv3.Config) {
   106  					conf.ConfigFile.AccessToken = "invalid-access-token"
   107  				})
   108  			})
   109  
   110  			When("the client credentials have been manually added to the config", func() {
   111  				BeforeEach(func() {
   112  					clientID, clientSecret := helpers.SkipIfClientCredentialsNotSet()
   113  
   114  					helpers.SetConfig(func(conf *configv3.Config) {
   115  						conf.ConfigFile.UAAOAuthClient = clientID
   116  						conf.ConfigFile.UAAOAuthClientSecret = clientSecret
   117  					})
   118  				})
   119  
   120  				It("re-authenticates and displays the new access token", func() {
   121  					session := helpers.CF("oauth-token")
   122  
   123  					Eventually(session).Should(Say("bearer .+"))
   124  					Eventually(session).Should(Exit(0))
   125  				})
   126  			})
   127  
   128  			When("the client credentials are not present in the config", func() {
   129  				It("displays an error and exits 1", func() {
   130  					session := helpers.CF("oauth-token")
   131  
   132  					Eventually(session).Should(Say("FAILED"))
   133  					Eventually(session.Err).Should(Say(`Credentials were rejected, please try again\.`))
   134  					Eventually(session).Should(Exit(1))
   135  				})
   136  			})
   137  
   138  		})
   139  
   140  		When("the oauth creds are valid", func() {
   141  			When("the client credentials have been manually added to the config", func() {
   142  				BeforeEach(func() {
   143  					clientID, clientSecret := helpers.SkipIfClientCredentialsNotSet()
   144  
   145  					helpers.SetConfig(func(conf *configv3.Config) {
   146  						conf.ConfigFile.UAAOAuthClient = clientID
   147  						conf.ConfigFile.UAAOAuthClientSecret = clientSecret
   148  					})
   149  				})
   150  				It("re-authenticates and displays the new access token", func() {
   151  					session := helpers.CF("oauth-token")
   152  
   153  					Eventually(session).Should(Say("bearer .+"))
   154  					Eventually(session).Should(Exit(0))
   155  				})
   156  			})
   157  		})
   158  	})
   159  })