github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+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/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  func generateBrokers(numberOfBrokers int) []v2action.ServiceBrokerSummary {
    21  	var brokers []v2action.ServiceBrokerSummary
    22  	for i := 0; i < numberOfBrokers; i++ {
    23  		brokers = append(brokers, v2action.ServiceBrokerSummary{
    24  			ServiceBroker: v2action.ServiceBroker{
    25  				Name: fmt.Sprintf("sb%d", i),
    26  			},
    27  			Services: []v2action.ServiceSummary{
    28  				{
    29  					Service: v2action.Service{Label: fmt.Sprintf("service%d-2", i)},
    30  					Plans: []v2action.ServicePlanSummary{
    31  						{
    32  							ServicePlan: v2action.ServicePlan{Name: "simple"},
    33  							VisibleTo:   []string{"org1", "org2"},
    34  						},
    35  						{
    36  							ServicePlan: v2action.ServicePlan{
    37  								Name:   "complex",
    38  								Public: true,
    39  							},
    40  						},
    41  					},
    42  				},
    43  				{
    44  					Service: v2action.Service{Label: fmt.Sprintf("service%d-1", i)},
    45  					Plans: []v2action.ServicePlanSummary{
    46  						{
    47  							ServicePlan: v2action.ServicePlan{Name: "simple"},
    48  						},
    49  						{
    50  							ServicePlan: v2action.ServicePlan{
    51  								Name:   "complex",
    52  								Public: true,
    53  							},
    54  							VisibleTo: []string{"org3", "org4"},
    55  						},
    56  					},
    57  				},
    58  			},
    59  		})
    60  	}
    61  
    62  	return brokers
    63  }
    64  
    65  func rowMatcher(brokers []v2action.ServiceBrokerSummary, b int, s int, p int, access string) string {
    66  	row := fmt.Sprintf(
    67  		`\s+%s\s+%s\s+%s`,
    68  		brokers[b].Services[s].Label,
    69  		brokers[b].Services[s].Plans[p].Name,
    70  		access,
    71  	)
    72  
    73  	if len(brokers[b].Services[s].Plans[p].VisibleTo) > 0 && access != "all" {
    74  		row = fmt.Sprintf(`%s\s+%s`, row, strings.Join(brokers[b].Services[s].Plans[p].VisibleTo, ","))
    75  	}
    76  
    77  	return row
    78  }
    79  
    80  var _ = Describe("service-access Command", func() {
    81  	var (
    82  		cmd             ServiceAccessCommand
    83  		testUI          *ui.UI
    84  		fakeConfig      *commandfakes.FakeConfig
    85  		fakeSharedActor *commandfakes.FakeSharedActor
    86  		fakeActor       *v6fakes.FakeServiceAccessActor
    87  		binaryName      string
    88  		executeErr      error
    89  	)
    90  
    91  	BeforeEach(func() {
    92  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    93  		fakeConfig = new(commandfakes.FakeConfig)
    94  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    95  		fakeActor = new(v6fakes.FakeServiceAccessActor)
    96  
    97  		cmd = ServiceAccessCommand{
    98  			UI:          testUI,
    99  			Config:      fakeConfig,
   100  			SharedActor: fakeSharedActor,
   101  			Actor:       fakeActor,
   102  		}
   103  
   104  		binaryName = "faceman"
   105  		fakeConfig.BinaryNameReturns("faceman")
   106  
   107  		fakeConfig.ExperimentalReturns(true)
   108  	})
   109  
   110  	JustBeforeEach(func() {
   111  		executeErr = cmd.Execute(nil)
   112  	})
   113  
   114  	When("a cloud controller API endpoint is set", func() {
   115  		BeforeEach(func() {
   116  			fakeConfig.TargetReturns("some-url")
   117  		})
   118  
   119  		When("checking target fails", func() {
   120  			BeforeEach(func() {
   121  				fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
   122  			})
   123  
   124  			It("returns an error", func() {
   125  				Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
   126  
   127  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   128  				checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   129  				Expect(checkTargetedOrg).To(BeFalse())
   130  				Expect(checkTargetedSpace).To(BeFalse())
   131  			})
   132  		})
   133  
   134  		When("the user is logged in", func() {
   135  			BeforeEach(func() {
   136  				fakeConfig.CurrentUserReturns(
   137  					configv3.User{Name: "some-user"},
   138  					nil)
   139  			})
   140  
   141  			It("displays flavor text", func() {
   142  				Expect(testUI.Out).To(Say("Getting service access as some-user..."))
   143  				Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1))
   144  			})
   145  
   146  			When("flags are passed", func() {
   147  				BeforeEach(func() {
   148  					cmd.Broker = "test-broker"
   149  					cmd.Service = "test-service"
   150  					cmd.Organization = "test-organization"
   151  				})
   152  
   153  				It("fetches service broker summaries from the passed flags", func() {
   154  					Expect(executeErr).NotTo(HaveOccurred())
   155  
   156  					Expect(fakeActor.GetServiceBrokerSummariesCallCount()).To(Equal(1))
   157  
   158  					actualBroker, actualService, actualOrg := fakeActor.GetServiceBrokerSummariesArgsForCall(0)
   159  					Expect(actualBroker).To(Equal("test-broker"))
   160  					Expect(actualService).To(Equal("test-service"))
   161  					Expect(actualOrg).To(Equal("test-organization"))
   162  				})
   163  
   164  				Context("but fetching summaries fails", func() {
   165  					BeforeEach(func() {
   166  						fakeActor.GetServiceBrokerSummariesReturns(nil, v2action.Warnings{"warning"}, errors.New("explode"))
   167  					})
   168  
   169  					It("displays warnings and propagates the error", func() {
   170  						Expect(testUI.Err).To(Say("warning"))
   171  						Expect(executeErr).To(MatchError("explode"))
   172  					})
   173  				})
   174  			})
   175  
   176  			Describe("table", func() {
   177  				var brokers []v2action.ServiceBrokerSummary
   178  
   179  				BeforeEach(func() {
   180  					brokers = generateBrokers(2)
   181  					fakeActor.GetServiceBrokerSummariesReturns(generateBrokers(2), v2action.Warnings{"warning"}, nil)
   182  				})
   183  
   184  				It("displays each service broker, service, plan and access with org in the correct position", func() {
   185  					Expect(executeErr).ToNot(HaveOccurred())
   186  
   187  					tableHeaders := `service\s+plan\s+access\s+orgs`
   188  					Expect(testUI.Out).To(Say(`broker:\s+%s`, brokers[0].Name))
   189  					Expect(testUI.Out).To(Say(tableHeaders))
   190  					Expect(testUI.Out).To(Say(rowMatcher(brokers, 0, 1, 1, "all")))
   191  					Expect(testUI.Out).To(Say(rowMatcher(brokers, 0, 1, 0, "none")))
   192  					Expect(testUI.Out).To(Say(rowMatcher(brokers, 0, 0, 1, "all")))
   193  					Expect(testUI.Out).To(Say(rowMatcher(brokers, 0, 0, 0, "limited")))
   194  					Expect(testUI.Out).To(Say(`broker:\s+%s`, brokers[1].Name))
   195  					Expect(testUI.Out).To(Say(tableHeaders))
   196  					Expect(testUI.Out).To(Say(rowMatcher(brokers, 1, 1, 1, "all")))
   197  					Expect(testUI.Out).To(Say(rowMatcher(brokers, 1, 1, 0, "none")))
   198  					Expect(testUI.Out).To(Say(rowMatcher(brokers, 1, 0, 1, "all")))
   199  					Expect(testUI.Out).To(Say(rowMatcher(brokers, 1, 0, 0, "limited")))
   200  				})
   201  			})
   202  		})
   203  	})
   204  })