github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/servicekey/create_service_key_test.go (about)

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