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

     1  package servicekey_test
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/command_registry"
     5  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     6  	"github.com/cloudfoundry/cli/cf/errors"
     7  	"github.com/cloudfoundry/cli/cf/models"
     8  	"github.com/cloudfoundry/cli/generic"
     9  
    10  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
    11  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    12  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    13  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    14  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    15  
    16  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    17  
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("service-key command", func() {
    23  	var (
    24  		ui                  *testterm.FakeUI
    25  		config              core_config.Repository
    26  		requirementsFactory *testreq.FakeReqFactory
    27  		serviceRepo         *testapi.FakeServiceRepo
    28  		serviceKeyRepo      *testapi.FakeServiceKeyRepo
    29  		deps                command_registry.Dependency
    30  	)
    31  
    32  	updateCommandDependency := func(pluginCall bool) {
    33  		deps.Ui = ui
    34  		deps.RepoLocator = deps.RepoLocator.SetServiceRepository(serviceRepo)
    35  		deps.RepoLocator = deps.RepoLocator.SetServiceKeyRepository(serviceKeyRepo)
    36  		deps.Config = config
    37  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("service-key").SetDependency(deps, pluginCall))
    38  	}
    39  
    40  	BeforeEach(func() {
    41  		ui = &testterm.FakeUI{}
    42  		config = testconfig.NewRepositoryWithDefaults()
    43  		serviceRepo = &testapi.FakeServiceRepo{}
    44  		serviceInstance := models.ServiceInstance{}
    45  		serviceInstance.Guid = "fake-service-instance-guid"
    46  		serviceInstance.Name = "fake-service-instance"
    47  		serviceRepo.FindInstanceByNameMap = generic.NewMap()
    48  		serviceRepo.FindInstanceByNameMap.Set("fake-service-instance", serviceInstance)
    49  		serviceKeyRepo = testapi.NewFakeServiceKeyRepo()
    50  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true, ServiceInstanceNotFound: false}
    51  		requirementsFactory.ServiceInstance = serviceInstance
    52  	})
    53  
    54  	var callGetServiceKey = func(args []string) bool {
    55  		return testcmd.RunCliCommand("service-key", args, requirementsFactory, updateCommandDependency, false)
    56  	}
    57  
    58  	Describe("requirements", func() {
    59  		It("fails when not logged in", func() {
    60  			requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: false}
    61  			Expect(callGetServiceKey([]string{"fake-service-key-name"})).To(BeFalse())
    62  		})
    63  
    64  		It("requires two arguments to run", func() {
    65  			Expect(callGetServiceKey([]string{})).To(BeFalse())
    66  			Expect(callGetServiceKey([]string{"fake-arg-one"})).To(BeFalse())
    67  			Expect(callGetServiceKey([]string{"fake-arg-one", "fake-arg-two"})).To(BeTrue())
    68  			Expect(callGetServiceKey([]string{"fake-arg-one", "fake-arg-two", "fake-arg-three"})).To(BeFalse())
    69  		})
    70  
    71  		It("fails when service instance is not found", func() {
    72  			requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, ServiceInstanceNotFound: true}
    73  			Expect(callGetServiceKey([]string{"non-exist-service-instance"})).To(BeFalse())
    74  		})
    75  
    76  		It("fails when space is not targetted", func() {
    77  			requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: false}
    78  			Expect(callGetServiceKey([]string{"fake-service-instance", "fake-service-key-name"})).To(BeFalse())
    79  		})
    80  	})
    81  
    82  	Describe("requirements are satisfied", func() {
    83  		Context("gets service key successfully", func() {
    84  			BeforeEach(func() {
    85  				serviceKeyRepo.GetServiceKeyMethod.ServiceKey = models.ServiceKey{
    86  					Fields: models.ServiceKeyFields{
    87  						Name:                "fake-service-key",
    88  						Guid:                "fake-service-key-guid",
    89  						Url:                 "fake-service-key-url",
    90  						ServiceInstanceGuid: "fake-service-instance-guid",
    91  						ServiceInstanceUrl:  "fake-service-instance-url",
    92  					},
    93  					Credentials: map[string]interface{}{
    94  						"username": "fake-username",
    95  						"password": "fake-password",
    96  						"host":     "fake-host",
    97  						"port":     "3306",
    98  						"database": "fake-db-name",
    99  						"uri":      "mysql://fake-user:fake-password@fake-host:3306/fake-db-name",
   100  					},
   101  				}
   102  			})
   103  
   104  			It("gets service credential", func() {
   105  				callGetServiceKey([]string{"fake-service-instance", "fake-service-key"})
   106  				Expect(ui.Outputs).To(ContainSubstrings(
   107  					[]string{"Getting key", "fake-service-key", "for service instance", "fake-service-instance", "as", "my-user"},
   108  					[]string{"username", "fake-username"},
   109  					[]string{"password", "fake-password"},
   110  					[]string{"host", "fake-host"},
   111  					[]string{"port", "3306"},
   112  					[]string{"database", "fake-db-name"},
   113  					[]string{"uri", "mysql://fake-user:fake-password@fake-host:3306/fake-db-name"},
   114  				))
   115  				Expect(ui.Outputs[1]).To(BeEmpty())
   116  				Expect(serviceKeyRepo.GetServiceKeyMethod.InstanceGuid).To(Equal("fake-service-instance-guid"))
   117  			})
   118  
   119  			It("gets service guid when '--guid' flag is provided", func() {
   120  				callGetServiceKey([]string{"--guid", "fake-service-instance", "fake-service-key"})
   121  
   122  				Expect(ui.Outputs).To(ContainSubstrings([]string{"fake-service-key-guid"}))
   123  				Expect(ui.Outputs).ToNot(ContainSubstrings(
   124  					[]string{"Getting key", "fake-service-key", "for service instance", "fake-service-instance", "as", "my-user"},
   125  				))
   126  			})
   127  		})
   128  
   129  		Context("when service key does not exist", func() {
   130  			It("shows no service key is found", func() {
   131  				callGetServiceKey([]string{"fake-service-instance", "non-exist-service-key"})
   132  				Expect(ui.Outputs).To(ContainSubstrings(
   133  					[]string{"Getting key", "non-exist-service-key", "for service instance", "fake-service-instance", "as", "my-user"},
   134  					[]string{"No service key", "non-exist-service-key", "found for service instance", "fake-service-instance"},
   135  				))
   136  			})
   137  
   138  			It("returns the empty string as guid when '--guid' flag is provided", func() {
   139  				callGetServiceKey([]string{"--guid", "fake-service-instance", "non-exist-service-key"})
   140  
   141  				Expect(len(ui.Outputs)).To(Equal(1))
   142  				Expect(ui.Outputs[0]).To(BeEmpty())
   143  			})
   144  		})
   145  
   146  		Context("when api returned NotAuthorizedError", func() {
   147  			It("shows no service key is found", func() {
   148  				serviceKeyRepo.GetServiceKeyMethod.ServiceKey = models.ServiceKey{}
   149  				serviceKeyRepo.GetServiceKeyMethod.Error = &errors.NotAuthorizedError{}
   150  
   151  				callGetServiceKey([]string{"fake-service-instance", "fake-service-key"})
   152  				Expect(ui.Outputs).To(ContainSubstrings(
   153  					[]string{"Getting key", "fake-service-key", "for service instance", "fake-service-instance", "as", "my-user"},
   154  					[]string{"No service key", "fake-service-key", "found for service instance", "fake-service-instance"},
   155  				))
   156  			})
   157  		})
   158  	})
   159  })