github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/servicekey/create_service_key_test.go (about)

     1  package servicekey_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  
     7  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     8  	"code.cloudfoundry.org/cli/cf/errors"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  	"code.cloudfoundry.org/cli/cf/requirements"
    11  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    12  
    13  	"code.cloudfoundry.org/cli/cf/api/apifakes"
    14  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    15  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    16  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    17  
    18  	"code.cloudfoundry.org/cli/cf/commandregistry"
    19  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    20  
    21  	. "github.com/onsi/ginkgo"
    22  	. "github.com/onsi/gomega"
    23  )
    24  
    25  var _ = Describe("create-service-key command", func() {
    26  	var (
    27  		ui                  *testterm.FakeUI
    28  		config              coreconfig.Repository
    29  		requirementsFactory *requirementsfakes.FakeFactory
    30  		serviceRepo         *apifakes.FakeServiceRepository
    31  		serviceKeyRepo      *apifakes.OldFakeServiceKeyRepo
    32  		deps                commandregistry.Dependency
    33  	)
    34  
    35  	updateCommandDependency := func(pluginCall bool) {
    36  		deps.UI = ui
    37  		deps.RepoLocator = deps.RepoLocator.SetServiceRepository(serviceRepo)
    38  		deps.RepoLocator = deps.RepoLocator.SetServiceKeyRepository(serviceKeyRepo)
    39  		deps.Config = config
    40  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("create-service-key").SetDependency(deps, pluginCall))
    41  	}
    42  
    43  	BeforeEach(func() {
    44  		ui = &testterm.FakeUI{}
    45  		config = testconfig.NewRepositoryWithDefaults()
    46  		serviceRepo = &apifakes.FakeServiceRepository{}
    47  		serviceInstance := models.ServiceInstance{}
    48  		serviceInstance.GUID = "fake-instance-guid"
    49  		serviceInstance.Name = "fake-service-instance"
    50  		serviceRepo.FindInstanceByNameReturns(serviceInstance, nil)
    51  		serviceKeyRepo = apifakes.NewFakeServiceKeyRepo()
    52  		requirementsFactory = new(requirementsfakes.FakeFactory)
    53  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    54  		requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{})
    55  		serviceInstanceReq := new(requirementsfakes.FakeServiceInstanceRequirement)
    56  		requirementsFactory.NewServiceInstanceRequirementReturns(serviceInstanceReq)
    57  		serviceInstanceReq.GetServiceInstanceReturns(serviceInstance)
    58  	})
    59  
    60  	var callCreateService = func(args []string) bool {
    61  		return testcmd.RunCLICommand("create-service-key", args, requirementsFactory, updateCommandDependency, false, ui)
    62  	}
    63  
    64  	Describe("requirements", func() {
    65  		It("fails when not logged in", func() {
    66  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    67  			Expect(callCreateService([]string{"fake-service-instance", "fake-service-key"})).To(BeFalse())
    68  		})
    69  
    70  		It("requires two arguments to run", func() {
    71  			Expect(callCreateService([]string{})).To(BeFalse())
    72  			Expect(callCreateService([]string{"fake-arg-one"})).To(BeFalse())
    73  			Expect(callCreateService([]string{"fake-arg-one", "fake-arg-two", "fake-arg-three"})).To(BeFalse())
    74  		})
    75  
    76  		It("fails when service instance is not found", func() {
    77  			serviceInstanceReq := new(requirementsfakes.FakeServiceInstanceRequirement)
    78  			serviceInstanceReq.ExecuteReturns(errors.New("no service instance"))
    79  			requirementsFactory.NewServiceInstanceRequirementReturns(serviceInstanceReq)
    80  			Expect(callCreateService([]string{"non-exist-service-instance", "fake-service-key"})).To(BeFalse())
    81  		})
    82  
    83  		It("fails when space is not targetted", func() {
    84  			requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "no targeted space"})
    85  			Expect(callCreateService([]string{"non-exist-service-instance", "fake-service-key"})).To(BeFalse())
    86  		})
    87  	})
    88  
    89  	Describe("requirements are satisfied", func() {
    90  		It("create service key successfully", func() {
    91  			callCreateService([]string{"fake-service-instance", "fake-service-key"})
    92  
    93  			Expect(ui.Outputs()).To(ContainSubstrings(
    94  				[]string{"Creating service key", "fake-service-key", "for service instance", "fake-service-instance", "as", "my-user"},
    95  				[]string{"OK"},
    96  			))
    97  			Expect(serviceKeyRepo.CreateServiceKeyMethod.InstanceGUID).To(Equal("fake-instance-guid"))
    98  			Expect(serviceKeyRepo.CreateServiceKeyMethod.KeyName).To(Equal("fake-service-key"))
    99  		})
   100  
   101  		It("create service key failed when the service key already exists", func() {
   102  			serviceKeyRepo.CreateServiceKeyMethod.Error = errors.NewModelAlreadyExistsError("Service key", "exist-service-key")
   103  
   104  			callCreateService([]string{"fake-service-instance", "exist-service-key"})
   105  
   106  			Expect(ui.Outputs()).To(ContainSubstrings(
   107  				[]string{"Creating service key", "exist-service-key", "for service instance", "fake-service-instance", "as", "my-user"},
   108  				[]string{"OK"},
   109  				[]string{"Service key exist-service-key already exists"}))
   110  		})
   111  
   112  		It("create service key failed when the service is unbindable", func() {
   113  			serviceKeyRepo.CreateServiceKeyMethod.Error = errors.NewUnbindableServiceError()
   114  			callCreateService([]string{"fake-service-instance", "exist-service-key"})
   115  
   116  			Expect(ui.Outputs()).To(ContainSubstrings(
   117  				[]string{"Creating service key", "exist-service-key", "for service instance", "fake-service-instance", "as", "my-user"},
   118  				[]string{"FAILED"},
   119  				[]string{"This service doesn't support creation of keys."}))
   120  		})
   121  	})
   122  
   123  	Context("when passing arbitrary params", func() {
   124  		Context("as a json string", func() {
   125  			It("successfully creates a service key and passes the params as a json string", func() {
   126  				callCreateService([]string{"fake-service-instance", "fake-service-key", "-c", `{"foo": "bar"}`})
   127  
   128  				Expect(ui.Outputs()).To(ContainSubstrings(
   129  					[]string{"Creating service key", "fake-service-key", "for service instance", "fake-service-instance", "as", "my-user"},
   130  					[]string{"OK"},
   131  				))
   132  				Expect(serviceKeyRepo.CreateServiceKeyMethod.InstanceGUID).To(Equal("fake-instance-guid"))
   133  				Expect(serviceKeyRepo.CreateServiceKeyMethod.KeyName).To(Equal("fake-service-key"))
   134  				Expect(serviceKeyRepo.CreateServiceKeyMethod.Params).To(Equal(map[string]interface{}{"foo": "bar"}))
   135  			})
   136  		})
   137  
   138  		Context("that are not valid json", func() {
   139  			It("returns an error to the UI", func() {
   140  				callCreateService([]string{"fake-service-instance", "fake-service-key", "-c", `bad-json`})
   141  
   142  				Expect(ui.Outputs()).To(ContainSubstrings(
   143  					[]string{"FAILED"},
   144  					[]string{"Invalid configuration provided for -c flag. Please provide a valid JSON object or path to a file containing a valid JSON object."},
   145  				))
   146  			})
   147  		})
   148  		Context("as a file that contains json", func() {
   149  			var jsonFile *os.File
   150  			var params string
   151  
   152  			BeforeEach(func() {
   153  				params = "{\"foo\": \"bar\"}"
   154  			})
   155  
   156  			AfterEach(func() {
   157  				if jsonFile != nil {
   158  					jsonFile.Close()
   159  					os.Remove(jsonFile.Name())
   160  				}
   161  			})
   162  
   163  			JustBeforeEach(func() {
   164  				var err error
   165  				jsonFile, err = ioutil.TempFile("", "")
   166  				Expect(err).ToNot(HaveOccurred())
   167  
   168  				err = ioutil.WriteFile(jsonFile.Name(), []byte(params), os.ModePerm)
   169  				Expect(err).NotTo(HaveOccurred())
   170  			})
   171  
   172  			It("successfully creates a service key and passes the params as a json", func() {
   173  				callCreateService([]string{"fake-service-instance", "fake-service-key", "-c", jsonFile.Name()})
   174  
   175  				Expect(ui.Outputs()).To(ContainSubstrings(
   176  					[]string{"Creating service key", "fake-service-key", "for service instance", "fake-service-instance", "as", "my-user"},
   177  					[]string{"OK"},
   178  				))
   179  				Expect(serviceKeyRepo.CreateServiceKeyMethod.Params).To(Equal(map[string]interface{}{"foo": "bar"}))
   180  			})
   181  
   182  			Context("that are not valid json", func() {
   183  				BeforeEach(func() {
   184  					params = "bad-json"
   185  				})
   186  
   187  				It("returns an error to the UI", func() {
   188  					callCreateService([]string{"fake-service-instance", "fake-service-key", "-c", jsonFile.Name()})
   189  
   190  					Expect(ui.Outputs()).To(ContainSubstrings(
   191  						[]string{"FAILED"},
   192  						[]string{"Invalid configuration provided for -c flag. Please provide a valid JSON object or path to a file containing a valid JSON object."},
   193  					))
   194  				})
   195  			})
   196  		})
   197  	})
   198  })