github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/integration/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  
    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  		configPath = filepath.Join(homeDir, ".cf", "config.json")
    36  	})
    37  
    38  	Context("when the config file exists", func() {
    39  		BeforeEach(func() {
    40  			setupCF(ReadOnlyOrg, ReadOnlySpace)
    41  		})
    42  
    43  		Context("when the client id and secret keys are set in the config", func() {
    44  			BeforeEach(func() {
    45  				replaceConfig(
    46  					configPath, `"UAAOAuthClient": ".*"`, `"UAAOAuthClient": "cf2"`)
    47  				replaceConfig(
    48  					configPath, `"UAAOAuthClientSecret": ".*"`, `"UAAOAuthClientSecret": "secret2"`)
    49  			})
    50  
    51  			Context("oauth-token", func() {
    52  				It("uses the custom client id and secret", func() {
    53  					session := helpers.CF("oauth-token")
    54  					Eventually(session).Should(Exit(1))
    55  					Expect(session.Out).To(Say(
    56  						"Server error, status code: 401, error code: unauthorized"))
    57  				})
    58  			})
    59  
    60  			Context("auth", func() {
    61  				It("uses the custom client id and secret", func() {
    62  					username, password := helpers.GetCredentials()
    63  					session := helpers.CF("auth", username, password)
    64  					Eventually(session).Should(Exit(1))
    65  					Expect(session.Err).To(Say(
    66  						"Credentials were rejected, please try again."))
    67  				})
    68  			})
    69  
    70  			Context("login", func() {
    71  				It("uses the custom client id and secret", func() {
    72  					username, password := helpers.GetCredentials()
    73  					session := helpers.CF("login", "-u", username, "-p", password)
    74  					Eventually(session).Should(Exit(1))
    75  					Expect(session.Out).To(Say(
    76  						"Credentials were rejected, please try again."))
    77  				})
    78  			})
    79  		})
    80  
    81  		Context("when the client id in the config is empty", func() {
    82  			BeforeEach(func() {
    83  				replaceConfig(
    84  					configPath, `"UAAOAuthClient": ".*",`, `"UAAOAuthClient": "",`)
    85  			})
    86  
    87  			Context("v2 command", func() {
    88  				It("does not write default values for client id and secret", func() {
    89  					session := helpers.CF("oauth-token")
    90  					Eventually(session).Should(Exit(1))
    91  
    92  					configString := fileAsString(configPath)
    93  					Expect(configString).To(ContainSubstring(`"UAAOAuthClient": ""`))
    94  				})
    95  			})
    96  
    97  			Context("v3 command", func() {
    98  				It("writes default values for client id and secret", func() {
    99  					session := helpers.CF("tasks", "some-app")
   100  					Eventually(session).Should(Exit(1))
   101  
   102  					configString := fileAsString(configPath)
   103  					Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
   104  					Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
   105  				})
   106  			})
   107  		})
   108  
   109  		Context("when there are no client id and secret keys in the config", func() {
   110  			BeforeEach(func() {
   111  				replaceConfig(
   112  					configPath, `"UAAOAuthClient": ".*",`, "")
   113  				replaceConfig(
   114  					configPath, `"UAAOAuthClientSecret": ".*",`, "")
   115  			})
   116  
   117  			Context("v2 command", func() {
   118  				It("writes default values for client id and secret", func() {
   119  					session := helpers.CF("oauth-token")
   120  					Eventually(session).Should(Exit(0))
   121  
   122  					configString := fileAsString(configPath)
   123  					Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
   124  					Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
   125  				})
   126  			})
   127  
   128  			Context("v3 command", func() {
   129  				It("writes default values for client id and secret", func() {
   130  					session := helpers.CF("tasks")
   131  					Eventually(session).Should(Exit(1))
   132  
   133  					configString := fileAsString(configPath)
   134  					Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
   135  					Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
   136  				})
   137  			})
   138  		})
   139  	})
   140  
   141  	Context("when the config file does not exist", func() {
   142  		BeforeEach(func() {
   143  			err := os.Remove(configPath)
   144  			Expect(err).ToNot(HaveOccurred())
   145  		})
   146  
   147  		Context("v2 command", func() {
   148  			It("writes default values for client id and secret to the config", func() {
   149  				Expect(configPath).ToNot(BeAnExistingFile())
   150  
   151  				session := helpers.CF("help")
   152  				Eventually(session).Should(Exit(0))
   153  
   154  				configString := fileAsString(configPath)
   155  				Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
   156  				Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
   157  			})
   158  		})
   159  
   160  		Context("v3 command", func() {
   161  			It("writes default values for client id and secret to the config", func() {
   162  				Expect(configPath).ToNot(BeAnExistingFile())
   163  
   164  				session := helpers.CF("tasks")
   165  				Eventually(session).Should(Exit(1))
   166  
   167  				configString := fileAsString(configPath)
   168  				Expect(configString).To(ContainSubstring(`"UAAOAuthClient": "cf"`))
   169  				Expect(configString).To(ContainSubstring(`"UAAOAuthClientSecret": ""`))
   170  			})
   171  		})
   172  	})
   173  })