/cf/commands/serviceaccess/service_access_test.go

https://gitlab.com/JamesClonk/cli · Go · 219 lines · 196 code · 23 blank · 0 comment · 0 complexity · 13dddee295e0f457019ef73a9337ee24 MD5 · raw file

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