github.com/cloudfoundry/cli@v7.1.0+incompatible/actor/v2action/service_key_test.go (about)

     1  package v2action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  
     9  	"code.cloudfoundry.org/cli/actor/actionerror"
    10  	. "code.cloudfoundry.org/cli/actor/v2action"
    11  	"code.cloudfoundry.org/cli/actor/v2action/v2actionfakes"
    12  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
    13  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
    14  )
    15  
    16  var _ = Describe("Service Key", func() {
    17  	var (
    18  		actor                     *Actor
    19  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    24  		actor = NewActor(fakeCloudControllerClient, nil, nil)
    25  	})
    26  
    27  	Describe("CreateServiceKey", func() {
    28  		var (
    29  			executeErr error
    30  			warnings   Warnings
    31  			serviceKey ServiceKey
    32  		)
    33  
    34  		JustBeforeEach(func() {
    35  			serviceKey, warnings, executeErr = actor.CreateServiceKey("some-service-instance-name", "some-key-name", "some-space-guid", map[string]interface{}{"some-parameter": "some-value"})
    36  		})
    37  
    38  		It("looks for the service instance with the given name", func() {
    39  			Expect(fakeCloudControllerClient.GetSpaceServiceInstancesCallCount()).To(Equal(1))
    40  			spaceGuid, includedUserProvided, filters := fakeCloudControllerClient.GetSpaceServiceInstancesArgsForCall(0)
    41  			Expect(spaceGuid).To(Equal("some-space-guid"))
    42  			Expect(includedUserProvided).To(BeTrue())
    43  			Expect(filters).To(ConsistOf(
    44  				ccv2.Filter{Type: constant.NameFilter, Operator: constant.EqualOperator, Values: []string{"some-service-instance-name"}},
    45  			))
    46  		})
    47  
    48  		When("getting the service instance errors", func() {
    49  			BeforeEach(func() {
    50  				fakeCloudControllerClient.GetSpaceServiceInstancesReturns(
    51  					nil,
    52  					ccv2.Warnings{"warning-1"},
    53  					errors.New("some-error"),
    54  				)
    55  			})
    56  
    57  			It("returns the error", func() {
    58  				Expect(executeErr).To(MatchError(errors.New("some-error")))
    59  				Expect(warnings).To(ConsistOf("warning-1"))
    60  			})
    61  		})
    62  
    63  		When("getting the service instance succeeds", func() {
    64  			BeforeEach(func() {
    65  				fakeCloudControllerClient.GetSpaceServiceInstancesReturns(
    66  					[]ccv2.ServiceInstance{{GUID: "some-service-instance-guid"}},
    67  					ccv2.Warnings{"warning-1"},
    68  					nil,
    69  				)
    70  			})
    71  
    72  			It("creates the service key with the correct values", func() {
    73  				Expect(fakeCloudControllerClient.CreateServiceKeyCallCount()).To(Equal(1))
    74  				serviceInstanceGuid, keyName, parameters := fakeCloudControllerClient.CreateServiceKeyArgsForCall(0)
    75  				Expect(keyName).To(Equal("some-key-name"))
    76  				Expect(serviceInstanceGuid).To(Equal("some-service-instance-guid"))
    77  				Expect(parameters).To(Equal(map[string]interface{}{"some-parameter": "some-value"}))
    78  			})
    79  
    80  			When("creating the service key errors", func() {
    81  				BeforeEach(func() {
    82  					fakeCloudControllerClient.CreateServiceKeyReturns(
    83  						ccv2.ServiceKey{},
    84  						ccv2.Warnings{"warning-2"},
    85  						errors.New("some-error"),
    86  					)
    87  				})
    88  
    89  				It("returns the error", func() {
    90  					Expect(executeErr).To(MatchError(errors.New("some-error")))
    91  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
    92  				})
    93  			})
    94  
    95  			When("creating the service key succeeds", func() {
    96  				BeforeEach(func() {
    97  					fakeCloudControllerClient.CreateServiceKeyReturns(
    98  						ccv2.ServiceKey{GUID: "service-key-guid"},
    99  						ccv2.Warnings{"warning-2"},
   100  						errors.New("some-error"),
   101  					)
   102  				})
   103  
   104  				It("returns the warnings", func() {
   105  					Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   106  				})
   107  
   108  				It("returns the created service key", func() {
   109  					Expect(serviceKey.GUID).To(Equal("service-key-guid"))
   110  				})
   111  			})
   112  		})
   113  
   114  		When("getting the service instance doesn't return any service instances", func() {
   115  			BeforeEach(func() {
   116  				fakeCloudControllerClient.GetSpaceServiceInstancesReturns(
   117  					[]ccv2.ServiceInstance{},
   118  					ccv2.Warnings{"warning-1"},
   119  					nil,
   120  				)
   121  			})
   122  
   123  			It("returns a ServiceInstanceNotFoundError", func() {
   124  				Expect(executeErr).To(MatchError(actionerror.ServiceInstanceNotFoundError{Name: "some-service-instance-name"}))
   125  				Expect(warnings).To(ConsistOf("warning-1"))
   126  			})
   127  		})
   128  	})
   129  })