github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/integration/shared/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  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gbytes"
    13  	. "github.com/onsi/gomega/gexec"
    14  )
    15  
    16  func fileAsString(path string) string {
    17  	configBytes, err := ioutil.ReadFile(path)
    18  	Expect(err).ToNot(HaveOccurred())
    19  
    20  	return string(configBytes)
    21  }
    22  
    23  func replaceConfig(path string, old string, new string) {
    24  	r := regexp.MustCompile(old)
    25  	newConfig := r.ReplaceAllString(fileAsString(path), new)
    26  	err := ioutil.WriteFile(path, []byte(newConfig), 0600)
    27  	Expect(err).ToNot(HaveOccurred())
    28  }
    29  
    30  var _ = Describe("custom oauth client id", func() {
    31  	var configPath string
    32  
    33  	BeforeEach(func() {
    34  		helpers.SkipIfClientCredentialsTestMode()
    35  		configPath = filepath.Join(homeDir, ".cf", "config.json")
    36  	})
    37  
    38  	When("the config file exists", func() {
    39  		BeforeEach(func() {
    40  			helpers.LoginCF()
    41  			helpers.TargetOrgAndSpace(ReadOnlyOrg, ReadOnlySpace)
    42  		})
    43  
    44  		When("the client id and secret keys are set in the config", func() {
    45  			BeforeEach(func() {
    46  				replaceConfig(
    47  					configPath, `"UAAOAuthClient": ".*"`, `"UAAOAuthClient": "cf2"`)
    48  				replaceConfig(
    49  					configPath, `"UAAOAuthClientSecret": ".*"`, `"UAAOAuthClientSecret": "secret2"`)
    50  			})
    51  
    52  			Context("oauth-token", func() {
    53  				It("uses the custom client id and secret", func() {
    54  					session := helpers.CF("oauth-token")
    55  					Eventually(session).Should(Exit(1))
    56  					Expect(session.Err).To(Say(`Credentials were rejected, please try again\.`))
    57  				})
    58  			})
    59  
    60  			Context("auth", func() {
    61  				It("uses the custom client id and secret", func() {
    62  					username, password := helpers.GetCredentials()
    63  					env := map[string]string{
    64  						"CF_USERNAME": username,
    65  						"CF_PASSWORD": password,
    66  					}
    67  					session := helpers.CFWithEnv(env, "auth")
    68  					Eventually(session).Should(Exit(1))
    69  					Expect(session.Err).To(Say(
    70  						"Credentials were rejected, please try again."))
    71  				})
    72  			})
    73  
    74  			Context("login", func() {
    75  				It("uses the custom client id and secret", func() {
    76  					username, password := helpers.GetCredentials()
    77  					session := helpers.CF("login", "-u", username, "-p", password)
    78  					Eventually(session).Should(Exit(1))
    79  					Expect(session.Err).To(Say(
    80  						"Credentials were rejected, please try again."))
    81  				})
    82  			})
    83  		})
    84  
    85  		When("the client id in the config is empty", func() {
    86  			BeforeEach(func() {
    87  				replaceConfig(
    88  					configPath, `"UAAOAuthClient": ".*",`, `"UAAOAuthClient": "",`)
    89  			})
    90  
    91  			Context("v6 command", func() {
    92  				It("replaces the empty client id with the default values for client id and secret", func() {
    93  					session := helpers.CF("oauth-token")
    94  					Eventually(session).Should(Exit(0))
    95  
    96  					configString := fileAsString(configPath)
    97  					Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
    98  					Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
    99  				})
   100  			})
   101  
   102  			Context("v7 command", func() {
   103  				It("writes default values for client id and secret", func() {
   104  					session := helpers.CF("tasks", "some-app")
   105  					Eventually(session).Should(Exit(1))
   106  
   107  					configString := fileAsString(configPath)
   108  					Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
   109  					Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
   110  				})
   111  			})
   112  		})
   113  
   114  		When("there are no client id and secret keys in the config", func() {
   115  			BeforeEach(func() {
   116  				replaceConfig(
   117  					configPath, `"UAAOAuthClient": ".*",`, "")
   118  				replaceConfig(
   119  					configPath, `"UAAOAuthClientSecret": ".*",`, "")
   120  			})
   121  
   122  			Context("v6 command", func() {
   123  				It("writes default values for client id and secret", func() {
   124  					session := helpers.CF("oauth-token")
   125  					Eventually(session).Should(Exit(0))
   126  
   127  					configString := fileAsString(configPath)
   128  					Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
   129  					Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
   130  				})
   131  			})
   132  
   133  			Context("v7 command", func() {
   134  				It("writes default values for client id and secret", func() {
   135  					session := helpers.CF("tasks")
   136  					Eventually(session).Should(Exit(1))
   137  
   138  					configString := fileAsString(configPath)
   139  					Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
   140  					Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
   141  				})
   142  			})
   143  		})
   144  	})
   145  
   146  	When("the config file does not exist", func() {
   147  		BeforeEach(func() {
   148  			err := os.Remove(configPath)
   149  			Expect(err).ToNot(HaveOccurred())
   150  		})
   151  
   152  		Context("v6 command", func() {
   153  			It("writes default values for client id and secret to the config", func() {
   154  				Expect(configPath).ToNot(BeAnExistingFile())
   155  
   156  				session := helpers.CF("help")
   157  				Eventually(session).Should(Exit(0))
   158  
   159  				configString := fileAsString(configPath)
   160  				Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
   161  				Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
   162  			})
   163  		})
   164  
   165  		Context("v7 command", func() {
   166  			It("writes default values for client id and secret to the config", func() {
   167  				Expect(configPath).ToNot(BeAnExistingFile())
   168  
   169  				session := helpers.CF("tasks")
   170  				Eventually(session).Should(Exit(1))
   171  
   172  				configString := fileAsString(configPath)
   173  				Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
   174  				Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
   175  			})
   176  		})
   177  	})
   178  })