github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/update_user_provided_service_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror"
     7  	"github.com/LukasHeimann/cloudfoundrycli/v8/api/cloudcontroller/ccerror"
     8  
     9  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/flag"
    10  	"github.com/LukasHeimann/cloudfoundrycli/v8/resources"
    11  	"github.com/LukasHeimann/cloudfoundrycli/v8/types"
    12  
    13  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action"
    14  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes"
    15  	. "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7"
    16  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes"
    17  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3"
    18  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/ui"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  	. "github.com/onsi/gomega/gbytes"
    22  )
    23  
    24  var _ = Describe("update-user-provided-service Command", func() {
    25  	var (
    26  		input           *Buffer
    27  		testUI          *ui.UI
    28  		fakeConfig      *commandfakes.FakeConfig
    29  		fakeActor       *v7fakes.FakeActor
    30  		fakeSharedActor *commandfakes.FakeSharedActor
    31  		cmd             UpdateUserProvidedServiceCommand
    32  		executeErr      error
    33  	)
    34  
    35  	expectOKMessage := func(testUI *ui.UI, serviceName, orgName, spaceName, userName string) {
    36  		Expect(testUI.Out).To(SatisfyAll(
    37  			Say("Updating user provided service %s in org %s / space %s as %s...", serviceName, orgName, spaceName, userName),
    38  			Say("OK"),
    39  			Say("TIP: Use 'cf restage' for any bound apps to ensure your env variable changes take effect"),
    40  		))
    41  	}
    42  
    43  	BeforeEach(func() {
    44  		input = NewBuffer()
    45  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    46  		fakeConfig = new(commandfakes.FakeConfig)
    47  		fakeActor = new(v7fakes.FakeActor)
    48  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    49  
    50  		cmd = UpdateUserProvidedServiceCommand{
    51  			BaseCommand: BaseCommand{
    52  				UI:          testUI,
    53  				Config:      fakeConfig,
    54  				SharedActor: fakeSharedActor,
    55  				Actor:       fakeActor,
    56  			},
    57  		}
    58  	})
    59  
    60  	JustBeforeEach(func() {
    61  		executeErr = cmd.Execute(nil)
    62  	})
    63  
    64  	It("checks the user is logged in, and targeting an org and space", func() {
    65  		Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    66  		orgChecked, spaceChecked := fakeSharedActor.CheckTargetArgsForCall(0)
    67  		Expect(orgChecked).To(BeTrue())
    68  		Expect(spaceChecked).To(BeTrue())
    69  	})
    70  
    71  	When("checking the target returns an error", func() {
    72  		BeforeEach(func() {
    73  			fakeSharedActor.CheckTargetReturns(errors.New("explode"))
    74  		})
    75  
    76  		It("returns the error", func() {
    77  			Expect(executeErr).To(MatchError("explode"))
    78  		})
    79  	})
    80  
    81  	When("the user is logged in, and is targeting an org and space", func() {
    82  		const (
    83  			fakeServiceInstanceName = "fake-service-instance-name"
    84  			fakeOrgName             = "fake-org-name"
    85  			fakeSpaceName           = "fake-space-name"
    86  			fakeSpaceGUID           = "fake-space-guid"
    87  			fakeUserName            = "fake-user-name"
    88  		)
    89  
    90  		BeforeEach(func() {
    91  			fakeConfig.TargetedSpaceReturns(configv3.Space{
    92  				Name: fakeSpaceName,
    93  				GUID: fakeSpaceGUID,
    94  			})
    95  
    96  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    97  				Name: fakeOrgName,
    98  			})
    99  
   100  			fakeActor.GetCurrentUserReturns(configv3.User{Name: fakeUserName}, nil)
   101  
   102  			setPositionalFlags(&cmd, fakeServiceInstanceName)
   103  
   104  			fakeActor.UpdateUserProvidedServiceInstanceReturns(v7action.Warnings{"something obstreperous"}, nil)
   105  		})
   106  
   107  		When("no flags were specified", func() {
   108  			It("succeeds with a message", func() {
   109  				Expect(executeErr).NotTo(HaveOccurred())
   110  
   111  				Expect(testUI.Out).To(SatisfyAll(
   112  					Say("Updating user provided service %s in org %s / space %s as %s...", fakeServiceInstanceName, fakeOrgName, fakeSpaceName, fakeUserName),
   113  					Say("No flags specified. No changes were made"),
   114  					Say("OK"),
   115  				))
   116  			})
   117  		})
   118  
   119  		expectUpdate := func(update resources.ServiceInstance) {
   120  			It("succeeds with a message", func() {
   121  				Expect(executeErr).NotTo(HaveOccurred())
   122  
   123  				expectOKMessage(testUI, fakeServiceInstanceName, fakeOrgName, fakeSpaceName, fakeUserName)
   124  
   125  				Expect(testUI.Err).To(Say("something obstreperous"))
   126  
   127  				Expect(fakeActor.UpdateUserProvidedServiceInstanceCallCount()).To(Equal(1))
   128  				actualName, actualSpaceGUID, actualUpdates := fakeActor.UpdateUserProvidedServiceInstanceArgsForCall(0)
   129  				Expect(actualName).To(Equal(fakeServiceInstanceName))
   130  				Expect(actualSpaceGUID).To(Equal(fakeSpaceGUID))
   131  				Expect(actualUpdates).To(Equal(update))
   132  			})
   133  		}
   134  
   135  		When("updating syslog URL", func() {
   136  			BeforeEach(func() {
   137  				setFlag(&cmd, "-l", flag.OptionalString{IsSet: true, Value: "https://syslog.com"})
   138  			})
   139  
   140  			expectUpdate(resources.ServiceInstance{
   141  				SyslogDrainURL: types.NewOptionalString("https://syslog.com"),
   142  			})
   143  		})
   144  
   145  		When("updating route service URL", func() {
   146  			BeforeEach(func() {
   147  				setFlag(&cmd, "-r", flag.OptionalString{IsSet: true, Value: "https://route.com"})
   148  			})
   149  
   150  			expectUpdate(resources.ServiceInstance{
   151  				RouteServiceURL: types.NewOptionalString("https://route.com"),
   152  			})
   153  		})
   154  
   155  		When("updating tags", func() {
   156  			BeforeEach(func() {
   157  				setFlag(&cmd, "-t", flag.Tags{IsSet: true, Value: []string{"one", "two", "three"}})
   158  			})
   159  
   160  			expectUpdate(resources.ServiceInstance{
   161  				Tags: types.NewOptionalStringSlice("one", "two", "three"),
   162  			})
   163  		})
   164  
   165  		When("updating credentials", func() {
   166  			BeforeEach(func() {
   167  				setFlag(&cmd, "-p", flag.CredentialsOrJSON{
   168  					OptionalObject: types.OptionalObject{
   169  						IsSet: true,
   170  						Value: map[string]interface{}{"foo": "bar", "baz": false},
   171  					},
   172  				})
   173  			})
   174  
   175  			expectUpdate(resources.ServiceInstance{
   176  				Credentials: types.NewOptionalObject(map[string]interface{}{"foo": "bar", "baz": false}),
   177  			})
   178  		})
   179  
   180  		When("updating credentials interactively", func() {
   181  			BeforeEach(func() {
   182  				setFlag(&cmd, "-p", flag.CredentialsOrJSON{
   183  					UserPromptCredentials: []string{"pass phrase", "cred"},
   184  					OptionalObject: types.OptionalObject{
   185  						IsSet: true,
   186  						Value: nil,
   187  					},
   188  				})
   189  
   190  				_, err := input.Write([]byte("very secret passphrase\nsecret cred\n"))
   191  				Expect(err).NotTo(HaveOccurred())
   192  			})
   193  
   194  			It("prompts the user for credentials", func() {
   195  				Expect(testUI.Out).To(Say("pass phrase: "))
   196  				Expect(testUI.Out).To(Say("cred: "))
   197  			})
   198  
   199  			It("does not echo the credentials", func() {
   200  				Expect(testUI.Out).NotTo(Say("secret"))
   201  				Expect(testUI.Err).NotTo(Say("secret"))
   202  			})
   203  
   204  			expectUpdate(resources.ServiceInstance{
   205  				Credentials: types.NewOptionalObject(map[string]interface{}{
   206  					"pass phrase": "very secret passphrase",
   207  					"cred":        "secret cred",
   208  				}),
   209  			})
   210  		})
   211  
   212  		When("updating everything", func() {
   213  			BeforeEach(func() {
   214  				setFlag(&cmd, "-l", flag.OptionalString{IsSet: true, Value: "https://syslog.com"})
   215  				setFlag(&cmd, "-r", flag.OptionalString{IsSet: true, Value: "https://route.com"})
   216  				setFlag(&cmd, "-t", flag.Tags{IsSet: true, Value: []string{"one", "two", "three"}})
   217  				setFlag(&cmd, "-p", flag.CredentialsOrJSON{
   218  					OptionalObject: types.OptionalObject{
   219  						IsSet: true,
   220  						Value: map[string]interface{}{"foo": "bar", "baz": false},
   221  					},
   222  				})
   223  			})
   224  
   225  			expectUpdate(resources.ServiceInstance{
   226  				SyslogDrainURL:  types.NewOptionalString("https://syslog.com"),
   227  				RouteServiceURL: types.NewOptionalString("https://route.com"),
   228  				Tags:            types.NewOptionalStringSlice("one", "two", "three"),
   229  				Credentials:     types.NewOptionalObject(map[string]interface{}{"foo": "bar", "baz": false}),
   230  			})
   231  		})
   232  
   233  		When("the service instance is not found", func() {
   234  			BeforeEach(func() {
   235  				setFlag(&cmd, "-l", flag.OptionalString{IsSet: true, Value: "https://syslog.com"})
   236  
   237  				fakeActor.UpdateUserProvidedServiceInstanceReturns(
   238  					v7action.Warnings{"something obstreperous"},
   239  					ccerror.ServiceInstanceNotFoundError{},
   240  				)
   241  			})
   242  
   243  			It("should return the correct error", func() {
   244  				Expect(executeErr).To(MatchError(actionerror.ServiceInstanceNotFoundError{
   245  					Name: fakeServiceInstanceName,
   246  				}))
   247  			})
   248  		})
   249  
   250  		When("the update fails", func() {
   251  			BeforeEach(func() {
   252  				setFlag(&cmd, "-l", flag.OptionalString{IsSet: true, Value: "https://syslog.com"})
   253  
   254  				fakeActor.UpdateUserProvidedServiceInstanceReturns(
   255  					v7action.Warnings{"something obstreperous"},
   256  					errors.New("bang"),
   257  				)
   258  			})
   259  
   260  			It("should fail with warnings and not say OK", func() {
   261  				Expect(executeErr).To(MatchError("bang"))
   262  
   263  				Expect(testUI.Out).To(Say("Updating user provided service %s in org %s / space %s as %s...", fakeServiceInstanceName, fakeOrgName, fakeSpaceName, fakeUserName))
   264  				Expect(testUI.Out).NotTo(Say("OK"))
   265  
   266  				Expect(testUI.Err).To(Say("something obstreperous"))
   267  			})
   268  		})
   269  	})
   270  })