github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v7/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  	"code.cloudfoundry.org/cli/util/configv3"
    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  		helpers.SkipIfClientCredentialsTestMode()
    36  		configPath = filepath.Join(homeDir, ".cf", "config.json")
    37  	})
    38  
    39  	When("the config file exists", func() {
    40  		BeforeEach(func() {
    41  			helpers.LoginCF()
    42  			helpers.TargetOrgAndSpace(ReadOnlyOrg, ReadOnlySpace)
    43  		})
    44  
    45  		When("the client id and secret keys are set in the config", func() {
    46  			BeforeEach(func() {
    47  				replaceConfig(
    48  					configPath, `"UAAOAuthClient": ".*"`, `"UAAOAuthClient": "cf2"`)
    49  				replaceConfig(
    50  					configPath, `"UAAOAuthClientSecret": ".*"`, `"UAAOAuthClientSecret": "secret2"`)
    51  			})
    52  
    53  			Context("oauth-token", func() {
    54  				BeforeEach(func() {
    55  					helpers.SetConfig(func(conf *configv3.Config) {
    56  						conf.ConfigFile.AccessToken = "invalid-access-token"
    57  					})
    58  				})
    59  
    60  				It("uses the custom client id and secret", func() {
    61  					session := helpers.CF("oauth-token")
    62  					Eventually(session).Should(Exit(1))
    63  					Expect(session.Err).To(Say(`Credentials were rejected, please try again\.`))
    64  				})
    65  			})
    66  
    67  			Context("auth", func() {
    68  				It("returns an error indicating that client credentials in the config file is no longer supported", func() {
    69  					username, password := helpers.GetCredentials()
    70  					env := map[string]string{
    71  						"CF_USERNAME": username,
    72  						"CF_PASSWORD": password,
    73  					}
    74  					session := helpers.CFWithEnv(env, "auth")
    75  					Eventually(session).Should(Exit(1))
    76  					Expect(session.Err).To(Say(
    77  						"Error: Support for manually writing your client credentials to config.json has been removed. For similar functionality please use `cf auth --client-credentials`."))
    78  				})
    79  			})
    80  
    81  			Context("login", func() {
    82  				It("returns an error indicating that client credentials in the config file is no longer supported", func() {
    83  					username, password := helpers.GetCredentials()
    84  					session := helpers.CF("login", "-u", username, "-p", password)
    85  					Eventually(session).Should(Exit(1))
    86  					Expect(session.Err).To(Say(
    87  						"Error: Support for manually writing your client credentials to config.json has been removed. For similar functionality please use `cf auth --client-credentials`."))
    88  				})
    89  			})
    90  		})
    91  
    92  		When("the client id in the config is empty", func() {
    93  			BeforeEach(func() {
    94  				replaceConfig(
    95  					configPath, `"UAAOAuthClient": ".*",`, `"UAAOAuthClient": "",`)
    96  			})
    97  
    98  			Context("oauth-token command", func() {
    99  				It("replaces the empty client id with the default values for client id and secret", func() {
   100  					session := helpers.CF("oauth-token")
   101  					Eventually(session).Should(Exit(0))
   102  
   103  					configString := fileAsString(configPath)
   104  					Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
   105  					Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
   106  				})
   107  			})
   108  
   109  			Context("tasks command", func() {
   110  				It("writes default values for client id and secret", func() {
   111  					session := helpers.CF("tasks", "some-app")
   112  					Eventually(session).Should(Exit(1))
   113  
   114  					configString := fileAsString(configPath)
   115  					Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
   116  					Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
   117  				})
   118  			})
   119  		})
   120  
   121  		When("there are no client id and secret keys in the config", func() {
   122  			BeforeEach(func() {
   123  				replaceConfig(
   124  					configPath, `"UAAOAuthClient": ".*",`, "")
   125  				replaceConfig(
   126  					configPath, `"UAAOAuthClientSecret": ".*",`, "")
   127  			})
   128  
   129  			Context("oauth-token command", func() {
   130  				It("writes default values for client id and secret", func() {
   131  					session := helpers.CF("oauth-token")
   132  					Eventually(session).Should(Exit(0))
   133  
   134  					configString := fileAsString(configPath)
   135  					Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
   136  					Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
   137  				})
   138  			})
   139  
   140  			Context("tasks command", func() {
   141  				It("writes default values for client id and secret", func() {
   142  					session := helpers.CF("tasks")
   143  					Eventually(session).Should(Exit(1))
   144  
   145  					configString := fileAsString(configPath)
   146  					Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
   147  					Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
   148  				})
   149  			})
   150  		})
   151  	})
   152  
   153  	When("the config file does not exist", func() {
   154  		BeforeEach(func() {
   155  			err := os.Remove(configPath)
   156  			Expect(err).ToNot(HaveOccurred())
   157  		})
   158  
   159  		Context("help command", func() {
   160  			It("writes default values for client id and secret to the config", func() {
   161  				Expect(configPath).ToNot(BeAnExistingFile())
   162  
   163  				session := helpers.CF("help")
   164  				Eventually(session).Should(Exit(0))
   165  
   166  				configString := fileAsString(configPath)
   167  				Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
   168  				Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
   169  			})
   170  		})
   171  
   172  		Context("tasks command", func() {
   173  			It("writes default values for client id and secret to the config", func() {
   174  				Expect(configPath).ToNot(BeAnExistingFile())
   175  
   176  				session := helpers.CF("tasks")
   177  				Eventually(session).Should(Exit(1))
   178  
   179  				configString := fileAsString(configPath)
   180  				Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
   181  				Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
   182  			})
   183  		})
   184  	})
   185  })