github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/serviceaccess/service_access_test.go (about)

     1  package serviceaccess_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	testactor "github.com/cloudfoundry/cli/cf/actors/fakes"
     7  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     8  	"github.com/cloudfoundry/cli/cf/models"
     9  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    10  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    11  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    12  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    13  
    14  	. "github.com/cloudfoundry/cli/cf/commands/serviceaccess"
    15  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  )
    19  
    20  var _ = Describe("service-access command", func() {
    21  	var (
    22  		ui                  *testterm.FakeUI
    23  		actor               *testactor.FakeServiceActor
    24  		requirementsFactory *testreq.FakeReqFactory
    25  		serviceBroker1      models.ServiceBroker
    26  		serviceBroker2      models.ServiceBroker
    27  		tokenRefresher      *testapi.FakeAuthenticationRepository
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		ui = &testterm.FakeUI{}
    32  		actor = &testactor.FakeServiceActor{}
    33  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true}
    34  		tokenRefresher = &testapi.FakeAuthenticationRepository{}
    35  	})
    36  
    37  	runCommand := func(args ...string) bool {
    38  		cmd := NewServiceAccess(ui, testconfig.NewRepositoryWithDefaults(), actor, tokenRefresher)
    39  		return testcmd.RunCommand(cmd, args, requirementsFactory)
    40  	}
    41  
    42  	Describe("requirements", func() {
    43  		It("requires the user to be logged in", func() {
    44  			requirementsFactory.LoginSuccess = false
    45  			Expect(runCommand()).ToNot(HavePassedRequirements())
    46  		})
    47  		It("should fail with usage when provided any arguments", func() {
    48  			requirementsFactory.LoginSuccess = true
    49  			Expect(runCommand("blahblah")).To(BeFalse())
    50  			Expect(ui.FailedWithUsage).To(BeTrue())
    51  		})
    52  	})
    53  
    54  	Describe("when logged in", func() {
    55  		BeforeEach(func() {
    56  			serviceBroker1 = models.ServiceBroker{
    57  				Guid: "broker1",
    58  				Name: "brokername1",
    59  				Services: []models.ServiceOffering{
    60  					{
    61  						ServiceOfferingFields: models.ServiceOfferingFields{Label: "my-service-1"},
    62  						Plans: []models.ServicePlanFields{
    63  							{Name: "beep", Public: true},
    64  							{Name: "burp", Public: false},
    65  							{Name: "boop", Public: false, OrgNames: []string{"fwip", "brzzt"}},
    66  						},
    67  					},
    68  					{
    69  						ServiceOfferingFields: models.ServiceOfferingFields{Label: "my-service-2"},
    70  						Plans: []models.ServicePlanFields{
    71  							{Name: "petaloideous-noncelebration", Public: false},
    72  						},
    73  					},
    74  				},
    75  			}
    76  			serviceBroker2 = models.ServiceBroker{
    77  				Guid: "broker2",
    78  				Name: "brokername2",
    79  				Services: []models.ServiceOffering{
    80  					{ServiceOfferingFields: models.ServiceOfferingFields{Label: "my-service-3"}},
    81  				},
    82  			}
    83  
    84  			actor.FilterBrokersReturns([]models.ServiceBroker{
    85  				serviceBroker1,
    86  				serviceBroker2,
    87  			},
    88  				nil,
    89  			)
    90  		})
    91  
    92  		It("refreshes the auth token", func() {
    93  			runCommand()
    94  			Expect(tokenRefresher.RefreshTokenCalled).To(BeTrue())
    95  		})
    96  
    97  		Context("when refreshing the auth token fails", func() {
    98  			It("fails and returns the error", func() {
    99  				tokenRefresher.RefreshTokenError = errors.New("Refreshing went wrong")
   100  				runCommand()
   101  
   102  				Expect(ui.Outputs).To(ContainSubstrings(
   103  					[]string{"Refreshing went wrong"},
   104  					[]string{"FAILED"},
   105  				))
   106  			})
   107  		})
   108  
   109  		Context("When no flags are provided", func() {
   110  			It("tells the user it is obtaining the service access", func() {
   111  				runCommand()
   112  				Expect(ui.Outputs).To(ContainSubstrings(
   113  					[]string{"Getting service access as", "my-user"},
   114  				))
   115  			})
   116  
   117  			It("prints all of the brokers", func() {
   118  				runCommand()
   119  				Expect(ui.Outputs).To(ContainSubstrings(
   120  					[]string{"broker: brokername1"},
   121  					[]string{"service", "plan", "access", "orgs"},
   122  					[]string{"my-service-1", "beep", "all"},
   123  					[]string{"my-service-1", "burp", "none"},
   124  					[]string{"my-service-1", "boop", "limited", "fwip", "brzzt"},
   125  					[]string{"my-service-2", "petaloideous-noncelebration"},
   126  					[]string{"broker: brokername2"},
   127  					[]string{"service", "plan", "access", "orgs"},
   128  					[]string{"my-service-3"},
   129  				))
   130  			})
   131  		})
   132  
   133  		Context("When the broker flag is provided", func() {
   134  			It("tells the user it is obtaining the services access for a particular broker", func() {
   135  				runCommand("-b", "brokername1")
   136  				Expect(ui.Outputs).To(ContainSubstrings(
   137  					[]string{"Getting service access", "for broker brokername1 as", "my-user"},
   138  				))
   139  			})
   140  		})
   141  
   142  		Context("when the service flag is provided", func() {
   143  			It("tells the user it is obtaining the service access for a particular service", func() {
   144  				runCommand("-e", "my-service-1")
   145  				Expect(ui.Outputs).To(ContainSubstrings(
   146  					[]string{"Getting service access", "for service my-service-1 as", "my-user"},
   147  				))
   148  			})
   149  		})
   150  
   151  		Context("when the org flag is provided", func() {
   152  			It("tells the user it is obtaining the service access for a particular org", func() {
   153  				runCommand("-o", "fwip")
   154  				Expect(ui.Outputs).To(ContainSubstrings(
   155  					[]string{"Getting service access", "for organization fwip as", "my-user"},
   156  				))
   157  			})
   158  		})
   159  
   160  		Context("when the broker and service flag are both provided", func() {
   161  			It("tells the user it is obtaining the service access for a particular broker and service", func() {
   162  				runCommand("-b", "brokername1", "-e", "my-service-1")
   163  				Expect(ui.Outputs).To(ContainSubstrings(
   164  					[]string{"Getting service access", "for broker brokername1", "and service my-service-1", "as", "my-user"},
   165  				))
   166  			})
   167  		})
   168  
   169  		Context("when the broker and org name are both provided", func() {
   170  			It("tells the user it is obtaining the service access for a particular broker and org", func() {
   171  				runCommand("-b", "brokername1", "-o", "fwip")
   172  				Expect(ui.Outputs).To(ContainSubstrings(
   173  					[]string{"Getting service access", "for broker brokername1", "and organization fwip", "as", "my-user"},
   174  				))
   175  			})
   176  		})
   177  
   178  		Context("when the service and org name are both provided", func() {
   179  			It("tells the user it is obtaining the service access for a particular service and org", func() {
   180  				runCommand("-e", "my-service-1", "-o", "fwip")
   181  				Expect(ui.Outputs).To(ContainSubstrings(
   182  					[]string{"Getting service access", "for service my-service-1", "and organization fwip", "as", "my-user"},
   183  				))
   184  			})
   185  		})
   186  
   187  		Context("when all flags are provided", func() {
   188  			It("tells the user it is filtering on all options", func() {
   189  				runCommand("-b", "brokername1", "-e", "my-service-1", "-o", "fwip")
   190  				Expect(ui.Outputs).To(ContainSubstrings(
   191  					[]string{"Getting service access", "for broker brokername1", "and service my-service-1", "and organization fwip", "as", "my-user"},
   192  				))
   193  			})
   194  		})
   195  	})
   196  })