github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/service_access_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"code.cloudfoundry.org/cli/actor/actionerror"
     9  	"code.cloudfoundry.org/cli/actor/v2action"
    10  	"code.cloudfoundry.org/cli/command/commandfakes"
    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/ginkgo/extensions/table"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  func createBroker(i int) v2action.ServiceBrokerSummary {
    22  	return v2action.ServiceBrokerSummary{
    23  		ServiceBroker: v2action.ServiceBroker{
    24  			Name: fmt.Sprintf("sb%d", i),
    25  		},
    26  		Services: []v2action.ServiceSummary{
    27  			{
    28  				Service: v2action.Service{Label: fmt.Sprintf("service%d-2", i)},
    29  				Plans: []v2action.ServicePlanSummary{
    30  					{
    31  						ServicePlan: v2action.ServicePlan{Name: "simple"},
    32  						VisibleTo:   []string{"org1", "org2"},
    33  					},
    34  					{
    35  						ServicePlan: v2action.ServicePlan{
    36  							Name:   "complex",
    37  							Public: true,
    38  						},
    39  					},
    40  				},
    41  			},
    42  			{
    43  				Service: v2action.Service{Label: fmt.Sprintf("service%d-1", i)},
    44  				Plans: []v2action.ServicePlanSummary{
    45  					{
    46  						ServicePlan: v2action.ServicePlan{Name: "simple"},
    47  					},
    48  					{
    49  						ServicePlan: v2action.ServicePlan{
    50  							Name:   "complex",
    51  							Public: true,
    52  						},
    53  						VisibleTo: []string{"org3", "org4"},
    54  					},
    55  				},
    56  			},
    57  		},
    58  	}
    59  }
    60  
    61  func rowMatcher(brokers []v2action.ServiceBrokerSummary, b int, s int, p int, access string) string {
    62  	row := fmt.Sprintf(
    63  		`\s+%s\s+%s\s+%s`,
    64  		brokers[b].Services[s].Label,
    65  		brokers[b].Services[s].Plans[p].Name,
    66  		access,
    67  	)
    68  
    69  	if len(brokers[b].Services[s].Plans[p].VisibleTo) > 0 && access != "all" {
    70  		row = fmt.Sprintf(`%s\s+%s`, row, strings.Join(brokers[b].Services[s].Plans[p].VisibleTo, ","))
    71  	}
    72  
    73  	return row
    74  }
    75  
    76  var _ = Describe("service-access Command", func() {
    77  	var (
    78  		cmd             ServiceAccessCommand
    79  		testUI          *ui.UI
    80  		fakeConfig      *commandfakes.FakeConfig
    81  		fakeSharedActor *commandfakes.FakeSharedActor
    82  		fakeActor       *v6fakes.FakeServiceAccessActor
    83  		binaryName      string
    84  		executeErr      error
    85  	)
    86  
    87  	BeforeEach(func() {
    88  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    89  		fakeConfig = new(commandfakes.FakeConfig)
    90  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    91  		fakeActor = new(v6fakes.FakeServiceAccessActor)
    92  
    93  		cmd = ServiceAccessCommand{
    94  			UI:          testUI,
    95  			Config:      fakeConfig,
    96  			SharedActor: fakeSharedActor,
    97  			Actor:       fakeActor,
    98  		}
    99  
   100  		binaryName = "faceman"
   101  		fakeConfig.BinaryNameReturns("faceman")
   102  	})
   103  
   104  	When("a cloud controller API endpoint is set", func() {
   105  		BeforeEach(func() {
   106  			fakeConfig.TargetReturns("some-url")
   107  		})
   108  
   109  		When("checking target fails", func() {
   110  			BeforeEach(func() {
   111  				fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
   112  			})
   113  
   114  			It("returns an error", func() {
   115  				executeErr = cmd.Execute(nil)
   116  				Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
   117  
   118  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   119  				checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   120  				Expect(checkTargetedOrg).To(BeFalse())
   121  				Expect(checkTargetedSpace).To(BeFalse())
   122  			})
   123  		})
   124  
   125  		When("the user is logged in", func() {
   126  			BeforeEach(func() {
   127  				fakeConfig.CurrentUserReturns(
   128  					configv3.User{Name: "some-user"},
   129  					nil)
   130  			})
   131  
   132  			DescribeTable("flavour text",
   133  				func(broker, service, organization, expectedOutput string) {
   134  					cmd.Broker = broker
   135  					cmd.Service = service
   136  					cmd.Organization = organization
   137  
   138  					executeErr = cmd.Execute(nil)
   139  
   140  					Expect(executeErr).NotTo(HaveOccurred())
   141  					Expect(testUI.Out).To(Say(expectedOutput))
   142  				},
   143  				Entry("when no flags are passed", "", "", "",
   144  					"Getting service access as some-user\\.\\.\\."),
   145  				Entry("when the broker flag is passed", "test-broker", "", "",
   146  					"Getting service access for broker test-broker as some-user\\.\\.\\."),
   147  				Entry("when the broker and service flags are passed", "test-broker", "test-service", "",
   148  					"Getting service access for broker test-broker and service test-service as some-user\\.\\.\\."),
   149  				Entry("when the broker and org flags are passed", "test-broker", "", "test-org",
   150  					"Getting service access for broker test-broker and organization test-org as some-user\\.\\.\\."),
   151  				Entry("when the broker, service and org flags are passed", "test-broker", "test-service", "test-org",
   152  					"Getting service access for broker test-broker and service test-service and organization test-org as some-user\\.\\.\\."),
   153  				Entry("when the service flag is passed", "", "test-service", "",
   154  					"Getting service access for service test-service as some-user\\.\\.\\."),
   155  				Entry("when the service and org flags are passed", "", "test-service", "test-org",
   156  					"Getting service access for service test-service and organization test-org as some-user\\.\\.\\."),
   157  				Entry("when the org flag is passed", "", "", "test-org",
   158  					"Getting service access for organization test-org as some-user\\.\\.\\."),
   159  			)
   160  
   161  			When("there are no broker summaries returned", func() {
   162  				BeforeEach(func() {
   163  					fakeActor.GetServiceBrokerSummariesReturns([]v2action.ServiceBrokerSummary{}, nil, nil)
   164  				})
   165  
   166  				It("displays only the header and nothing else", func() {
   167  					executeErr = cmd.Execute(nil)
   168  					Expect(executeErr).NotTo(HaveOccurred())
   169  					Eventually(testUI.Out).Should(Say("Getting service access as some-user\\.\\.\\."))
   170  					Consistently(testUI.Out).ShouldNot(Say("[^\\s]"))
   171  				})
   172  			})
   173  
   174  			When("flags are passed", func() {
   175  				BeforeEach(func() {
   176  					cmd.Broker = "test-broker"
   177  					cmd.Service = "test-service"
   178  					cmd.Organization = "test-organization"
   179  				})
   180  
   181  				JustBeforeEach(func() {
   182  					executeErr = cmd.Execute(nil)
   183  				})
   184  
   185  				It("fetches service broker summaries from the passed flags", func() {
   186  					Expect(executeErr).NotTo(HaveOccurred())
   187  
   188  					Expect(fakeActor.GetServiceBrokerSummariesCallCount()).To(Equal(1))
   189  
   190  					actualBroker, actualService, actualOrg := fakeActor.GetServiceBrokerSummariesArgsForCall(0)
   191  					Expect(actualBroker).To(Equal("test-broker"))
   192  					Expect(actualService).To(Equal("test-service"))
   193  					Expect(actualOrg).To(Equal("test-organization"))
   194  				})
   195  
   196  				Context("but fetching summaries fails", func() {
   197  					BeforeEach(func() {
   198  						fakeActor.GetServiceBrokerSummariesReturns(nil, v2action.Warnings{"warning"}, errors.New("explode"))
   199  					})
   200  
   201  					It("displays warnings and propagates the error", func() {
   202  						Expect(testUI.Err).To(Say("warning"))
   203  						Expect(executeErr).To(MatchError("explode"))
   204  					})
   205  				})
   206  			})
   207  
   208  			Describe("tabular output", func() {
   209  				var brokers []v2action.ServiceBrokerSummary
   210  				JustBeforeEach(func() {
   211  					executeErr = cmd.Execute(nil)
   212  				})
   213  
   214  				When("the summaries returned are unordered", func() {
   215  					BeforeEach(func() {
   216  						brokers = []v2action.ServiceBrokerSummary{createBroker(2), createBroker(1)}
   217  						fakeActor.GetServiceBrokerSummariesReturns(brokers, v2action.Warnings{"warning"}, nil)
   218  					})
   219  
   220  					It("sorts brokers and services before displaying broker, service plan and access with org", func() {
   221  						Expect(executeErr).ToNot(HaveOccurred())
   222  						Expect(testUI.Err).To(Say("warning"))
   223  
   224  						// The command sorts the slices in places, so the order here is not the same as the order they were
   225  						// generated in and passed to GetServiceBrokerSummariesReturns.
   226  						tableHeaders := `service\s+plan\s+access\s+orgs`
   227  						Expect(testUI.Out).To(Say(`broker:\s+%s`, brokers[0].Name))
   228  						Expect(testUI.Out).To(Say(tableHeaders))
   229  						Expect(testUI.Out).To(Say(rowMatcher(brokers, 0, 0, 0, "all")))
   230  						Expect(testUI.Out).To(Say(rowMatcher(brokers, 0, 0, 1, "none")))
   231  						Expect(testUI.Out).To(Say(rowMatcher(brokers, 0, 1, 0, "all")))
   232  						Expect(testUI.Out).To(Say(rowMatcher(brokers, 0, 1, 1, "limited")))
   233  						Expect(testUI.Out).To(Say(`broker:\s+%s`, brokers[1].Name))
   234  						Expect(testUI.Out).To(Say(tableHeaders))
   235  						Expect(testUI.Out).To(Say(rowMatcher(brokers, 1, 0, 0, "all")))
   236  						Expect(testUI.Out).To(Say(rowMatcher(brokers, 1, 0, 1, "none")))
   237  						Expect(testUI.Out).To(Say(rowMatcher(brokers, 1, 1, 0, "all")))
   238  						Expect(testUI.Out).To(Say(rowMatcher(brokers, 1, 1, 1, "limited")))
   239  					})
   240  				})
   241  			})
   242  		})
   243  	})
   244  })