code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/service_access_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	. "code.cloudfoundry.org/cli/command/v7"
    10  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    11  	"code.cloudfoundry.org/cli/util/configv3"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/ginkgo/extensions/table"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("service-access Command", func() {
    20  	var (
    21  		cmd             ServiceAccessCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v7fakes.FakeActor
    26  		binaryName      string
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    31  		fakeConfig = new(commandfakes.FakeConfig)
    32  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    33  		fakeActor = new(v7fakes.FakeActor)
    34  
    35  		cmd = ServiceAccessCommand{
    36  			BaseCommand: BaseCommand{
    37  				UI:          testUI,
    38  				Config:      fakeConfig,
    39  				SharedActor: fakeSharedActor,
    40  				Actor:       fakeActor,
    41  			},
    42  		}
    43  	})
    44  
    45  	When("logged in", func() {
    46  		BeforeEach(func() {
    47  			fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    48  			fakeConfig.TargetReturns("some-url")
    49  		})
    50  
    51  		DescribeTable("message text",
    52  			func(broker, serviceOffering, organization, expectedOutput string) {
    53  
    54  				setFlag(&cmd, "-o", organization)
    55  				setFlag(&cmd, "-e", serviceOffering)
    56  				setFlag(&cmd, "-b", broker)
    57  
    58  				executeErr := cmd.Execute(nil)
    59  
    60  				Expect(executeErr).NotTo(HaveOccurred())
    61  				Expect(testUI.Out).To(Say(expectedOutput))
    62  			},
    63  			Entry("when no flags are passed", "", "", "",
    64  				"Getting service access as some-user\\.\\.\\."),
    65  			Entry("when the broker flag is passed", "test-broker", "", "",
    66  				"Getting service access for broker test-broker as some-user\\.\\.\\."),
    67  			Entry("when the broker and service flags are passed", "test-broker", "test-service", "",
    68  				"Getting service access for broker test-broker and service offering test-service as some-user\\.\\.\\."),
    69  			Entry("when the broker and org flags are passed", "test-broker", "", "test-org",
    70  				"Getting service access for broker test-broker and organization test-org as some-user\\.\\.\\."),
    71  			Entry("when the broker, service and org flags are passed", "test-broker", "test-service", "test-org",
    72  				"Getting service access for broker test-broker and service offering test-service and organization test-org as some-user\\.\\.\\."),
    73  			Entry("when the service flag is passed", "", "test-service", "",
    74  				"Getting service access for service offering test-service as some-user\\.\\.\\."),
    75  			Entry("when the service and org flags are passed", "", "test-service", "test-org",
    76  				"Getting service access for service offering test-service and organization test-org as some-user\\.\\.\\."),
    77  			Entry("when the org flag is passed", "", "", "test-org",
    78  				"Getting service access for organization test-org as some-user\\.\\.\\."),
    79  		)
    80  
    81  		When("there are service plans", func() {
    82  			BeforeEach(func() {
    83  				fakeActor.GetServiceAccessReturns(
    84  					fakeServiceAccessResult(),
    85  					v7action.Warnings{"warning"},
    86  					nil,
    87  				)
    88  			})
    89  
    90  			It("displays broker, service plan and access", func() {
    91  				executeErr := cmd.Execute(nil)
    92  				Expect(executeErr).ToNot(HaveOccurred())
    93  				Expect(testUI.Err).To(Say("warning"))
    94  
    95  				const tableHeaders = `offering\s+plan\s+access\s+orgs`
    96  				Expect(testUI.Out).To(Say(`broker:\s+broker-one`))
    97  				Expect(testUI.Out).To(Say(tableHeaders))
    98  				Expect(testUI.Out).To(Say(`service-one\s+plan-one\s+all`))
    99  				Expect(testUI.Out).To(Say(`service-two\s+plan-two\s+none`))
   100  
   101  				Expect(testUI.Out).To(Say(`broker:\s+broker-two`))
   102  				Expect(testUI.Out).To(Say(tableHeaders))
   103  				Expect(testUI.Out).To(Say(`service-three\s+plan-three\s+limited\s+org-1,org-2`))
   104  				Expect(testUI.Out).To(Say(`service-four\s+plan-four\s+limited\s+org-1,org-3`))
   105  
   106  				Expect(testUI.Out).To(Say(`broker:\s+broker-three`))
   107  				Expect(testUI.Out).To(Say(`offering\s+plan\s+access\s+space`))
   108  				Expect(testUI.Out).To(Say(`service-five\s+plan-five\s+limited\s+space-1 in org org-1`))
   109  			})
   110  		})
   111  
   112  		When("there are no service plans", func() {
   113  			BeforeEach(func() {
   114  				fakeActor.GetServiceAccessReturns(
   115  					[]v7action.ServicePlanAccess{},
   116  					v7action.Warnings{"a warning"},
   117  					nil,
   118  				)
   119  			})
   120  
   121  			It("displays a message", func() {
   122  				executeErr := cmd.Execute(nil)
   123  				Expect(executeErr).ToNot(HaveOccurred())
   124  				Expect(testUI.Err).To(Say("a warning"))
   125  				Expect(testUI.Out).To(Say("No service plans found."))
   126  			})
   127  		})
   128  
   129  		When("resource flags are passed", func() {
   130  			BeforeEach(func() {
   131  				setFlag(&cmd, "-o", "test-organization")
   132  				setFlag(&cmd, "-e", "test-service")
   133  				setFlag(&cmd, "-b", "test-broker")
   134  			})
   135  
   136  			It("passes the right flags to the actor", func() {
   137  				executeErr := cmd.Execute(nil)
   138  				Expect(executeErr).NotTo(HaveOccurred())
   139  
   140  				Expect(fakeActor.GetServiceAccessCallCount()).To(Equal(1))
   141  
   142  				actualService, actualBroker, actualOrg := fakeActor.GetServiceAccessArgsForCall(0)
   143  				Expect(actualBroker).To(Equal("test-broker"))
   144  				Expect(actualService).To(Equal("test-service"))
   145  				Expect(actualOrg).To(Equal("test-organization"))
   146  			})
   147  
   148  		})
   149  
   150  		When("the actor errors", func() {
   151  			BeforeEach(func() {
   152  				fakeActor.GetServiceAccessReturns(nil, v7action.Warnings{"warning"}, errors.New("explode"))
   153  			})
   154  
   155  			It("displays warnings and propagates the error", func() {
   156  				executeErr := cmd.Execute(nil)
   157  				Expect(testUI.Err).To(Say("warning"))
   158  				Expect(executeErr).To(MatchError("explode"))
   159  			})
   160  		})
   161  	})
   162  
   163  	When("checking target fails", func() {
   164  		BeforeEach(func() {
   165  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
   166  		})
   167  
   168  		It("returns an error", func() {
   169  			executeErr := cmd.Execute(nil)
   170  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
   171  
   172  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   173  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   174  			Expect(checkTargetedOrg).To(BeFalse())
   175  			Expect(checkTargetedSpace).To(BeFalse())
   176  		})
   177  	})
   178  
   179  	When("getting user fails", func() {
   180  		BeforeEach(func() {
   181  			fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("fake get user error"))
   182  		})
   183  
   184  		It("returns an error", func() {
   185  			executeErr := cmd.Execute(nil)
   186  			Expect(executeErr).To(MatchError("fake get user error"))
   187  
   188  			Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1))
   189  		})
   190  	})
   191  })
   192  
   193  func fakeServiceAccessResult() []v7action.ServicePlanAccess {
   194  	return []v7action.ServicePlanAccess{
   195  		{
   196  			BrokerName:          "broker-one",
   197  			ServiceOfferingName: "service-one",
   198  			ServicePlanName:     "plan-one",
   199  			VisibilityType:      "public",
   200  		},
   201  		{
   202  			BrokerName:          "broker-one",
   203  			ServiceOfferingName: "service-two",
   204  			ServicePlanName:     "plan-two",
   205  			VisibilityType:      "admin",
   206  		},
   207  		{
   208  			BrokerName:          "broker-two",
   209  			ServiceOfferingName: "service-three",
   210  			ServicePlanName:     "plan-three",
   211  			VisibilityType:      "organization",
   212  			VisibilityDetails:   []string{"org-1", "org-2"},
   213  		},
   214  		{
   215  			BrokerName:          "broker-two",
   216  			ServiceOfferingName: "service-four",
   217  			ServicePlanName:     "plan-four",
   218  			VisibilityType:      "organization",
   219  			VisibilityDetails:   []string{"org-1", "org-3"},
   220  		},
   221  		{
   222  			BrokerName:          "broker-three",
   223  			ServiceOfferingName: "service-five",
   224  			ServicePlanName:     "plan-five",
   225  			VisibilityType:      "space",
   226  			VisibilityDetails:   []string{"space-1 in org org-1"},
   227  		},
   228  	}
   229  }