github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/command/v6/create_service_key_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/flag"
    11  	. "code.cloudfoundry.org/cli/command/v6"
    12  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("create-service-key Command", func() {
    21  	var (
    22  		cmd             CreateServiceKeyCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v6fakes.FakeCreateServiceKeyActor
    27  		binaryName      string
    28  		executeErr      error
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    33  		fakeConfig = new(commandfakes.FakeConfig)
    34  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  		fakeActor = new(v6fakes.FakeCreateServiceKeyActor)
    36  
    37  		cmd = CreateServiceKeyCommand{
    38  			UI:          testUI,
    39  			Config:      fakeConfig,
    40  			SharedActor: fakeSharedActor,
    41  			Actor:       fakeActor,
    42  			RequiredArgs: flag.ServiceInstanceKey{
    43  				ServiceInstance: "my-service",
    44  				ServiceKey:      "my-key",
    45  			},
    46  		}
    47  
    48  		binaryName = "faceman"
    49  		fakeConfig.BinaryNameReturns("faceman")
    50  	})
    51  
    52  	JustBeforeEach(func() {
    53  		executeErr = cmd.Execute(nil)
    54  	})
    55  
    56  	When("a cloud controller API endpoint is set", func() {
    57  		BeforeEach(func() {
    58  			fakeConfig.TargetReturns("some-url")
    59  		})
    60  
    61  		When("checking target fails", func() {
    62  			BeforeEach(func() {
    63  				fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    64  			})
    65  
    66  			It("returns an error", func() {
    67  				Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    68  
    69  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    70  				checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    71  				Expect(checkTargetedOrg).To(BeTrue())
    72  				Expect(checkTargetedSpace).To(BeTrue())
    73  			})
    74  		})
    75  
    76  		When("the user is logged in, and an org and space are targeted", func() {
    77  			BeforeEach(func() {
    78  				fakeConfig.CurrentUserReturns(
    79  					configv3.User{Name: "some-user"},
    80  					nil)
    81  				fakeConfig.HasTargetedOrganizationReturns(true)
    82  				fakeConfig.HasTargetedSpaceReturns(true)
    83  				fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    84  					GUID: "some-org-guid",
    85  					Name: "some-org",
    86  				})
    87  				fakeConfig.TargetedSpaceReturns(configv3.Space{
    88  					GUID: "some-space-guid",
    89  					Name: "some-space",
    90  				})
    91  			})
    92  
    93  			When("Sevice instance name and key name are passed", func() {
    94  				When("warnings are being returned", func() {
    95  					BeforeEach(func() {
    96  						fakeActor.CreateServiceKeyReturns(v2action.ServiceKey{}, v2action.Warnings{"some-warning", "another-warning"}, nil)
    97  					})
    98  
    99  					It("displays the warning", func() {
   100  						Expect(testUI.Err).To(Say("some-warning"))
   101  						Expect(testUI.Err).To(Say("another-warning"))
   102  					})
   103  				})
   104  
   105  				When("the service create is successful", func() {
   106  					It("displays flavor text and creates the service key", func() {
   107  						Expect(fakeActor.CreateServiceKeyCallCount()).To(Equal(1))
   108  						service, key, spaceGUID, _ := fakeActor.CreateServiceKeyArgsForCall(0)
   109  						Expect(service).To(Equal("my-service"))
   110  						Expect(key).To(Equal("my-key"))
   111  						Expect(spaceGUID).To(Equal("some-space-guid"))
   112  
   113  						Expect(executeErr).NotTo(HaveOccurred())
   114  						Expect(testUI.Out).To(Say("Creating service key my-key for service instance my-service as some-user..."))
   115  						Expect(testUI.Out).To(Say("OK"))
   116  					})
   117  				})
   118  
   119  				When("the service key creation fails", func() {
   120  					BeforeEach(func() {
   121  						fakeActor.CreateServiceKeyReturns(v2action.ServiceKey{}, nil, errors.New("explode"))
   122  					})
   123  
   124  					It("returns the error", func() {
   125  						Expect(executeErr).To(MatchError("explode"))
   126  					})
   127  				})
   128  
   129  				When("the service key already exists", func() {
   130  					BeforeEach(func() {
   131  						fakeActor.CreateServiceKeyReturns(v2action.ServiceKey{}, nil, ccerror.ServiceKeyTakenError{})
   132  					})
   133  
   134  					It("displays OK and the warning", func() {
   135  						Expect(testUI.Out).To(Say("OK"))
   136  						Expect(testUI.Err).To(Say("Service key my-key already exists"))
   137  					})
   138  
   139  					It("does not return an error", func() {
   140  						Expect(executeErr).NotTo(HaveOccurred())
   141  					})
   142  				})
   143  			})
   144  
   145  			When("passed JSON parameters", func() {
   146  				var params map[string]interface{}
   147  
   148  				BeforeEach(func() {
   149  					params = map[string]interface{}{"foo": "bar"}
   150  					cmd.ParametersAsJSON = params
   151  				})
   152  
   153  				It("behaves as usual, passing on the params", func() {
   154  					Expect(testUI.Out).To(Say("Creating service key my-key for service instance my-service as some-user..."))
   155  
   156  					Expect(fakeActor.CreateServiceKeyCallCount()).To(Equal(1))
   157  					_, _, _, expectedParams := fakeActor.CreateServiceKeyArgsForCall(0)
   158  					Expect(expectedParams).To(Equal(params))
   159  				})
   160  			})
   161  		})
   162  	})
   163  })