github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/enable_service_access_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/command/commandfakes"
     9  	"code.cloudfoundry.org/cli/command/flag"
    10  	"code.cloudfoundry.org/cli/command/translatableerror"
    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("enable-service-access command", func() {
    21  	var (
    22  		cmd             EnableServiceAccessCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v6fakes.FakeEnableServiceAccessActor
    27  		binaryName      string
    28  		executeErr      error
    29  		extraArgs       []string
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v6fakes.FakeEnableServiceAccessActor)
    37  		extraArgs = nil
    38  
    39  		cmd = EnableServiceAccessCommand{
    40  			UI:          testUI,
    41  			Config:      fakeConfig,
    42  			SharedActor: fakeSharedActor,
    43  			Actor:       fakeActor,
    44  			RequiredArgs: flag.Service{
    45  				Service: "some-service",
    46  			},
    47  		}
    48  
    49  		binaryName = "faceman"
    50  		fakeConfig.BinaryNameReturns(binaryName)
    51  	})
    52  
    53  	JustBeforeEach(func() {
    54  		executeErr = cmd.Execute(extraArgs)
    55  	})
    56  
    57  	When("the user provides arguments", func() {
    58  		BeforeEach(func() {
    59  			extraArgs = []string{"some-extra-arg"}
    60  		})
    61  
    62  		It("fails with a TooManyArgumentsError", func() {
    63  			Expect(executeErr).To(MatchError(translatableerror.TooManyArgumentsError{
    64  				ExtraArgument: "some-extra-arg",
    65  			}))
    66  		})
    67  	})
    68  
    69  	When("a cloud controller API endpoint is set", func() {
    70  		BeforeEach(func() {
    71  			fakeConfig.TargetReturns("some-url")
    72  		})
    73  
    74  		When("checking target fails", func() {
    75  			BeforeEach(func() {
    76  				fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    77  			})
    78  
    79  			It("returns a not logged in error", func() {
    80  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    81  				Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    82  			})
    83  		})
    84  
    85  		When("the user is logged in", func() {
    86  			BeforeEach(func() {
    87  				fakeConfig.CurrentUserReturns(
    88  					configv3.User{Name: "admin"},
    89  					nil)
    90  			})
    91  
    92  			When("no flags are passed", func() {
    93  				BeforeEach(func() {
    94  					fakeActor.EnableServiceForAllOrgsReturns(nil, nil)
    95  				})
    96  
    97  				It("passes on the service", func() {
    98  					Expect(fakeActor.EnableServiceForAllOrgsCallCount()).To(Equal(1))
    99  					service, _ := fakeActor.EnableServiceForAllOrgsArgsForCall(0)
   100  					Expect(service).To(Equal("some-service"))
   101  				})
   102  
   103  				It("displays informative success message", func() {
   104  					Expect(testUI.Out).To(Say("Enabling access to all plans of service some-service for all orgs as admin..."))
   105  					Expect(testUI.Out).To(Say("OK"))
   106  				})
   107  
   108  				When("enabling access to the service fails", func() {
   109  					BeforeEach(func() {
   110  						fakeActor.EnableServiceForAllOrgsReturns(v2action.Warnings{"warning", "second-warning"}, errors.New("explode"))
   111  					})
   112  
   113  					It("returns the error", func() {
   114  						Expect(testUI.Out).To(Say("Enabling access to all plans of service some-service for all orgs as admin..."))
   115  						Expect(executeErr).To(MatchError("explode"))
   116  					})
   117  
   118  					It("returns the warnings", func() {
   119  						Expect(testUI.Err).To(Say("warning"))
   120  						Expect(testUI.Err).To(Say("second-warning"))
   121  					})
   122  				})
   123  			})
   124  
   125  			When("the -b flag is passed", func() {
   126  				BeforeEach(func() {
   127  					cmd.ServiceBroker = "some-broker"
   128  					fakeActor.EnableServiceForAllOrgsReturns(nil, nil)
   129  				})
   130  
   131  				It("passes on the service broker", func() {
   132  					Expect(fakeActor.EnableServiceForAllOrgsCallCount()).To(Equal(1))
   133  					service, serviceBroker := fakeActor.EnableServiceForAllOrgsArgsForCall(0)
   134  					Expect(service).To(Equal("some-service"))
   135  					Expect(serviceBroker).To(Equal("some-broker"))
   136  				})
   137  
   138  				It("displays an informative success message", func() {
   139  					Expect(testUI.Out).To(Say("Enabling access to all plans of service some-service from broker some-broker for all orgs as admin..."))
   140  					Expect(testUI.Out).To(Say("OK"))
   141  				})
   142  
   143  				When("enabling access to the service fails", func() {
   144  					BeforeEach(func() {
   145  						fakeActor.EnableServiceForAllOrgsReturns(v2action.Warnings{"warning", "second-warning"}, errors.New("explode"))
   146  					})
   147  
   148  					It("returns the error", func() {
   149  						Expect(testUI.Out).To(Say("Enabling access to all plans of service some-service from broker some-broker for all orgs as admin..."))
   150  						Expect(executeErr).To(MatchError("explode"))
   151  					})
   152  
   153  					It("returns the warnings", func() {
   154  						Expect(testUI.Err).To(Say("warning"))
   155  						Expect(testUI.Err).To(Say("second-warning"))
   156  					})
   157  				})
   158  			})
   159  
   160  			When("the -p flag is passed", func() {
   161  				BeforeEach(func() {
   162  					cmd.ServicePlan = "some-plan"
   163  					fakeActor.EnablePlanForAllOrgsReturns(nil, nil)
   164  				})
   165  
   166  				It("passes on the service plan", func() {
   167  					Expect(fakeActor.EnablePlanForAllOrgsCallCount()).To(Equal(1))
   168  					service, plan, _ := fakeActor.EnablePlanForAllOrgsArgsForCall(0)
   169  					Expect(service).To(Equal("some-service"))
   170  					Expect(plan).To(Equal("some-plan"))
   171  				})
   172  
   173  				It("displays an informative success message", func() {
   174  					Expect(testUI.Out).To(Say("Enabling access of plan some-plan for service some-service as admin..."))
   175  					Expect(testUI.Out).To(Say("OK"))
   176  				})
   177  
   178  				When("enabling access to the plan fails", func() {
   179  					BeforeEach(func() {
   180  						fakeActor.EnablePlanForAllOrgsReturns(v2action.Warnings{"warning", "second-warning"}, errors.New("explode"))
   181  					})
   182  
   183  					It("returns the error", func() {
   184  						Expect(testUI.Out).To(Say("Enabling access of plan some-plan for service some-service as admin..."))
   185  						Expect(executeErr).To(MatchError("explode"))
   186  					})
   187  
   188  					It("returns the warnings", func() {
   189  						Expect(testUI.Err).To(Say("warning"))
   190  						Expect(testUI.Err).To(Say("second-warning"))
   191  					})
   192  				})
   193  			})
   194  
   195  			When("the -o flag is passed", func() {
   196  				BeforeEach(func() {
   197  					cmd.Organization = "some-org"
   198  					fakeActor.EnableServiceForOrgReturns(nil, nil)
   199  				})
   200  
   201  				It("passes on the organization name", func() {
   202  					Expect(fakeActor.EnableServiceForOrgCallCount()).To(Equal(1))
   203  					service, org, _ := fakeActor.EnableServiceForOrgArgsForCall(0)
   204  					Expect(service).To(Equal("some-service"))
   205  					Expect(org).To(Equal("some-org"))
   206  				})
   207  
   208  				It("displays an informative success message", func() {
   209  					Expect(testUI.Out).To(Say("Enabling access to all plans of service some-service for the org some-org as admin..."))
   210  					Expect(testUI.Out).To(Say("OK"))
   211  				})
   212  
   213  				When("enabling access to the org fails", func() {
   214  					BeforeEach(func() {
   215  						fakeActor.EnableServiceForOrgReturns(v2action.Warnings{"warning", "second-warning"}, errors.New("explode"))
   216  					})
   217  
   218  					It("returns the error", func() {
   219  						Expect(testUI.Out).To(Say("Enabling access to all plans of service some-service for the org some-org as admin..."))
   220  						Expect(executeErr).To(MatchError("explode"))
   221  					})
   222  
   223  					It("returns the warnings", func() {
   224  						Expect(testUI.Err).To(Say("warning"))
   225  						Expect(testUI.Err).To(Say("second-warning"))
   226  					})
   227  				})
   228  			})
   229  
   230  			When("the -b and -p flags are passed", func() {
   231  				BeforeEach(func() {
   232  					cmd.ServicePlan = "some-plan"
   233  					cmd.ServiceBroker = "some-broker"
   234  					fakeActor.EnablePlanForAllOrgsReturns(nil, nil)
   235  				})
   236  
   237  				It("passes on the service plan and the service broker", func() {
   238  					Expect(fakeActor.EnablePlanForAllOrgsCallCount()).To(Equal(1))
   239  					service, plan, broker := fakeActor.EnablePlanForAllOrgsArgsForCall(0)
   240  					Expect(service).To(Equal("some-service"))
   241  					Expect(plan).To(Equal("some-plan"))
   242  					Expect(broker).To(Equal("some-broker"))
   243  				})
   244  
   245  				It("displays an informative success message", func() {
   246  					Expect(testUI.Out).To(Say("Enabling access to plan some-plan for service some-service from broker some-broker for all orgs as admin..."))
   247  					Expect(testUI.Out).To(Say("OK"))
   248  				})
   249  
   250  				When("enabling access to the plan fails", func() {
   251  					BeforeEach(func() {
   252  						fakeActor.EnablePlanForAllOrgsReturns(v2action.Warnings{"warning", "second-warning"}, errors.New("explode"))
   253  					})
   254  
   255  					It("returns the error", func() {
   256  						Expect(testUI.Out).To(Say("Enabling access to plan some-plan for service some-service from broker some-broker for all orgs as admin..."))
   257  						Expect(executeErr).To(MatchError("explode"))
   258  					})
   259  
   260  					It("returns the warnings", func() {
   261  						Expect(testUI.Err).To(Say("warning"))
   262  						Expect(testUI.Err).To(Say("second-warning"))
   263  					})
   264  				})
   265  			})
   266  
   267  			When("the -b and -o flags are passed", func() {
   268  				BeforeEach(func() {
   269  					cmd.Organization = "some-org"
   270  					cmd.ServiceBroker = "some-broker"
   271  					fakeActor.EnableServiceForOrgReturns(nil, nil)
   272  				})
   273  
   274  				It("passes on the service plan and the organization name", func() {
   275  					Expect(fakeActor.EnableServiceForOrgCallCount()).To(Equal(1))
   276  					service, org, broker := fakeActor.EnableServiceForOrgArgsForCall(0)
   277  					Expect(service).To(Equal("some-service"))
   278  					Expect(org).To(Equal("some-org"))
   279  					Expect(broker).To(Equal("some-broker"))
   280  				})
   281  
   282  				It("displays an informative success message", func() {
   283  					Expect(testUI.Out).To(Say("Enabling access to all plans of service some-service from broker some-broker for the org some-org as admin..."))
   284  					Expect(testUI.Out).To(Say("OK"))
   285  				})
   286  
   287  				When("enabling access to the plan fails", func() {
   288  					BeforeEach(func() {
   289  						fakeActor.EnableServiceForOrgReturns(v2action.Warnings{"warning", "second-warning"}, errors.New("explode"))
   290  					})
   291  
   292  					It("returns the error", func() {
   293  						Expect(testUI.Out).To(Say("Enabling access to all plans of service some-service from broker some-broker for the org some-org as admin..."))
   294  						Expect(executeErr).To(MatchError("explode"))
   295  					})
   296  
   297  					It("returns the warnings", func() {
   298  						Expect(testUI.Err).To(Say("warning"))
   299  						Expect(testUI.Err).To(Say("second-warning"))
   300  					})
   301  				})
   302  			})
   303  
   304  			When("the -p and -o flags are passed", func() {
   305  				BeforeEach(func() {
   306  					cmd.Organization = "some-org"
   307  					cmd.ServicePlan = "some-plan"
   308  					fakeActor.EnablePlanForOrgReturns(nil, nil)
   309  				})
   310  
   311  				It("passes on the plan and organization name", func() {
   312  					Expect(fakeActor.EnablePlanForOrgCallCount()).To(Equal(1))
   313  					service, plan, org, _ := fakeActor.EnablePlanForOrgArgsForCall(0)
   314  
   315  					Expect(service).To(Equal("some-service"))
   316  					Expect(plan).To(Equal("some-plan"))
   317  					Expect(org).To(Equal("some-org"))
   318  				})
   319  
   320  				It("displays an informative success message", func() {
   321  					Expect(testUI.Out).To(Say("Enabling access to plan some-plan of service some-service for org some-org as admin..."))
   322  					Expect(testUI.Out).To(Say("OK"))
   323  				})
   324  
   325  				When("enabling access to the plan in the org fails", func() {
   326  					BeforeEach(func() {
   327  						fakeActor.EnablePlanForOrgReturns(v2action.Warnings{"warning", "second-warning"}, errors.New("explode"))
   328  					})
   329  
   330  					It("returns the error", func() {
   331  						Expect(testUI.Out).To(Say("Enabling access to plan some-plan of service some-service for org some-org as admin..."))
   332  						Expect(executeErr).To(MatchError("explode"))
   333  					})
   334  
   335  					It("returns the warnings", func() {
   336  						Expect(testUI.Err).To(Say("warning"))
   337  						Expect(testUI.Err).To(Say("second-warning"))
   338  					})
   339  				})
   340  			})
   341  
   342  			When("the -b, -o and -p flags are passed", func() {
   343  				BeforeEach(func() {
   344  					cmd.Organization = "some-org"
   345  					cmd.ServicePlan = "some-plan"
   346  					cmd.ServiceBroker = "some-broker"
   347  					fakeActor.EnablePlanForOrgReturns(nil, nil)
   348  				})
   349  
   350  				It("passes on the service plan, organization name and service broker", func() {
   351  					Expect(fakeActor.EnablePlanForOrgCallCount()).To(Equal(1))
   352  					service, plan, org, broker := fakeActor.EnablePlanForOrgArgsForCall(0)
   353  
   354  					Expect(service).To(Equal("some-service"))
   355  					Expect(plan).To(Equal("some-plan"))
   356  					Expect(org).To(Equal("some-org"))
   357  					Expect(broker).To(Equal("some-broker"))
   358  				})
   359  
   360  				It("displays an informative success message", func() {
   361  					Expect(testUI.Out).To(Say("Enabling access to plan some-plan of service some-service from broker some-broker for org some-org as admin..."))
   362  					Expect(testUI.Out).To(Say("OK"))
   363  				})
   364  
   365  				When("enabling access to the plan in the org fails", func() {
   366  					BeforeEach(func() {
   367  						fakeActor.EnablePlanForOrgReturns(v2action.Warnings{"warning", "second-warning"}, errors.New("explode"))
   368  					})
   369  
   370  					It("returns the error", func() {
   371  						Expect(testUI.Out).To(Say("Enabling access to plan some-plan of service some-service from broker some-broker for org some-org as admin..."))
   372  						Expect(executeErr).To(MatchError("explode"))
   373  					})
   374  
   375  					It("returns the warnings", func() {
   376  						Expect(testUI.Err).To(Say("warning"))
   377  						Expect(testUI.Err).To(Say("second-warning"))
   378  					})
   379  				})
   380  			})
   381  		})
   382  	})
   383  })