github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+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).NotTo(Say("Enabling access of plan some-plan for service some-service as admin\\.\\.\\."))
   106  					Expect(testUI.Out).NotTo(Say("Enabling access to all plans of service some-service for the org some-org as admin..."))
   107  					Expect(testUI.Out).NotTo(Say("Enabling access to plan some-plan of service some-service for org some-org as admin..."))
   108  					Expect(testUI.Out).To(Say("OK"))
   109  				})
   110  
   111  				When("enabling access to the service fails", func() {
   112  					BeforeEach(func() {
   113  						fakeActor.EnableServiceForAllOrgsReturns(v2action.Warnings{"warning", "second-warning"}, errors.New("explode"))
   114  					})
   115  
   116  					It("returns the error", func() {
   117  						Expect(testUI.Out).To(Say("Enabling access to all plans of service some-service for all orgs as admin\\.\\.\\."))
   118  						Expect(executeErr).To(MatchError("explode"))
   119  					})
   120  
   121  					It("returns the warnings", func() {
   122  						Expect(testUI.Err).To(Say("warning"))
   123  						Expect(testUI.Err).To(Say("second-warning"))
   124  					})
   125  				})
   126  			})
   127  
   128  			When("the -p flag is passed", func() {
   129  				BeforeEach(func() {
   130  					cmd.ServicePlan = "some-plan"
   131  					fakeActor.EnablePlanForAllOrgsReturns(nil, nil)
   132  				})
   133  
   134  				It("passes on the service plan", func() {
   135  					Expect(fakeActor.EnablePlanForAllOrgsCallCount()).To(Equal(1))
   136  					service, plan := fakeActor.EnablePlanForAllOrgsArgsForCall(0)
   137  					Expect(service).To(Equal("some-service"))
   138  					Expect(plan).To(Equal("some-plan"))
   139  				})
   140  
   141  				It("displays an informative success message", func() {
   142  					Expect(testUI.Out).To(Say("Enabling access of plan some-plan for service some-service as admin\\.\\.\\."))
   143  					Expect(testUI.Out).NotTo(Say("Enabling access to all plans of service some-service for all orgs as admin\\.\\.\\."))
   144  					Expect(testUI.Out).NotTo(Say("Enabling access to all plans of service some-service for the org some-org as admin..."))
   145  					Expect(testUI.Out).NotTo(Say("Enabling access to plan some-plan of service some-service for org some-org as admin..."))
   146  					Expect(testUI.Out).To(Say("OK"))
   147  				})
   148  
   149  				When("enabling access to the plan fails", func() {
   150  					BeforeEach(func() {
   151  						fakeActor.EnablePlanForAllOrgsReturns(v2action.Warnings{"warning", "second-warning"}, errors.New("explode"))
   152  					})
   153  
   154  					It("returns the error", func() {
   155  						Expect(testUI.Out).To(Say("Enabling access of plan some-plan for service some-service as admin\\.\\.\\."))
   156  						Expect(executeErr).To(MatchError("explode"))
   157  					})
   158  
   159  					It("returns the warnings", func() {
   160  						Expect(testUI.Err).To(Say("warning"))
   161  						Expect(testUI.Err).To(Say("second-warning"))
   162  					})
   163  				})
   164  			})
   165  
   166  			When("the -o flag is passed", func() {
   167  				BeforeEach(func() {
   168  					cmd.Organization = "some-org"
   169  					fakeActor.EnableServiceForOrgReturns(nil, nil)
   170  				})
   171  
   172  				It("passes on the organization name", func() {
   173  					Expect(fakeActor.EnableServiceForOrgCallCount()).To(Equal(1))
   174  					service, org := fakeActor.EnableServiceForOrgArgsForCall(0)
   175  					Expect(service).To(Equal("some-service"))
   176  					Expect(org).To(Equal("some-org"))
   177  				})
   178  
   179  				It("displays an informative success message", func() {
   180  					Expect(testUI.Out).To(Say("Enabling access to all plans of service some-service for the org some-org as admin..."))
   181  					Expect(testUI.Out).NotTo(Say("Enabling access of plan some-plan for service some-service as admin\\.\\.\\."))
   182  					Expect(testUI.Out).NotTo(Say("Enabling access to all plans of service some-service for all orgs as admin\\.\\.\\."))
   183  					Expect(testUI.Out).NotTo(Say("Enabling access to plan some-plan of service some-service for org some-org as admin..."))
   184  					Expect(testUI.Out).To(Say("OK"))
   185  				})
   186  
   187  				When("enabling access to the org fails", func() {
   188  					BeforeEach(func() {
   189  						fakeActor.EnableServiceForOrgReturns(v2action.Warnings{"warning", "second-warning"}, errors.New("explode"))
   190  					})
   191  
   192  					It("returns the error", func() {
   193  						Expect(testUI.Out).To(Say("Enabling access to all plans of service some-service for the org some-org as admin..."))
   194  						Expect(executeErr).To(MatchError("explode"))
   195  					})
   196  
   197  					It("returns the warnings", func() {
   198  						Expect(testUI.Err).To(Say("warning"))
   199  						Expect(testUI.Err).To(Say("second-warning"))
   200  					})
   201  				})
   202  			})
   203  
   204  			When("the -p and -o flags are passed", func() {
   205  				BeforeEach(func() {
   206  					cmd.Organization = "some-org"
   207  					cmd.ServicePlan = "some-plan"
   208  					fakeActor.EnablePlanForOrgReturns(nil, nil)
   209  				})
   210  
   211  				It("passes on the plan and organization name", func() {
   212  					Expect(fakeActor.EnablePlanForOrgCallCount()).To(Equal(1))
   213  					service, plan, org := fakeActor.EnablePlanForOrgArgsForCall(0)
   214  
   215  					Expect(service).To(Equal("some-service"))
   216  					Expect(plan).To(Equal("some-plan"))
   217  					Expect(org).To(Equal("some-org"))
   218  				})
   219  
   220  				It("displays an informative success message", func() {
   221  					Expect(testUI.Out).To(Say("Enabling access to plan some-plan of service some-service for org some-org as admin..."))
   222  					Expect(testUI.Out).NotTo(Say("Enabling access of plan some-plan for service some-service as admin\\.\\.\\."))
   223  					Expect(testUI.Out).NotTo(Say("Enabling access to all plans of service some-service for all orgs as admin\\.\\.\\."))
   224  					Expect(testUI.Out).NotTo(Say("Enabling access to all plans of service some-service for the org some-org as admin..."))
   225  					Expect(testUI.Out).To(Say("OK"))
   226  				})
   227  
   228  				When("enabling access to the plan in the org fails", func() {
   229  					BeforeEach(func() {
   230  						fakeActor.EnablePlanForOrgReturns(v2action.Warnings{"warning", "second-warning"}, errors.New("explode"))
   231  					})
   232  
   233  					It("returns the error", func() {
   234  						Expect(testUI.Out).To(Say("Enabling access to plan some-plan of service some-service for org some-org as admin..."))
   235  						Expect(executeErr).To(MatchError("explode"))
   236  					})
   237  
   238  					It("returns the warnings", func() {
   239  						Expect(testUI.Err).To(Say("warning"))
   240  						Expect(testUI.Err).To(Say("second-warning"))
   241  					})
   242  				})
   243  			})
   244  		})
   245  	})
   246  })