code.cloudfoundry.org/cli@v7.1.0+incompatible/cf/commands/ssh_code_test.go (about)

     1  package commands_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	"code.cloudfoundry.org/cli/cf/commands"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig/coreconfigfakes"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    13  
    14  	"code.cloudfoundry.org/cli/cf/api/authentication/authenticationfakes"
    15  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    16  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    17  
    18  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  )
    22  
    23  var _ = Describe("OneTimeSSHCode", func() {
    24  	var (
    25  		ui           *testterm.FakeUI
    26  		configRepo   coreconfig.Repository
    27  		authRepo     *authenticationfakes.FakeRepository
    28  		endpointRepo *coreconfigfakes.FakeEndpointRepository
    29  
    30  		cmd         commandregistry.Command
    31  		deps        commandregistry.Dependency
    32  		factory     *requirementsfakes.FakeFactory
    33  		flagContext flags.FlagContext
    34  
    35  		endpointRequirement requirements.Requirement
    36  	)
    37  
    38  	BeforeEach(func() {
    39  		ui = &testterm.FakeUI{}
    40  
    41  		configRepo = testconfig.NewRepositoryWithDefaults()
    42  		configRepo.SetAPIEndpoint("fake-api-endpoint")
    43  		endpointRepo = new(coreconfigfakes.FakeEndpointRepository)
    44  		repoLocator := deps.RepoLocator.SetEndpointRepository(endpointRepo)
    45  		authRepo = new(authenticationfakes.FakeRepository)
    46  		repoLocator = repoLocator.SetAuthenticationRepository(authRepo)
    47  
    48  		deps = commandregistry.Dependency{
    49  			UI:          ui,
    50  			Config:      configRepo,
    51  			RepoLocator: repoLocator,
    52  		}
    53  
    54  		cmd = &commands.OneTimeSSHCode{}
    55  		cmd.SetDependency(deps, false)
    56  
    57  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    58  
    59  		factory = new(requirementsfakes.FakeFactory)
    60  
    61  		endpointRequirement = &passingRequirement{Name: "endpoint-requirement"}
    62  		factory.NewAPIEndpointRequirementReturns(endpointRequirement)
    63  	})
    64  
    65  	Describe("Requirements", func() {
    66  		It("returns an EndpointRequirement", func() {
    67  			actualRequirements, err := cmd.Requirements(factory, flagContext)
    68  			Expect(err).NotTo(HaveOccurred())
    69  			Expect(factory.NewAPIEndpointRequirementCallCount()).To(Equal(1))
    70  			Expect(actualRequirements).To(ContainElement(endpointRequirement))
    71  		})
    72  
    73  		Context("when not provided exactly zero args", func() {
    74  			BeforeEach(func() {
    75  				flagContext.Parse("domain-name")
    76  			})
    77  
    78  			It("fails with usage", func() {
    79  				var firstErr error
    80  
    81  				reqs, err := cmd.Requirements(factory, flagContext)
    82  				Expect(err).NotTo(HaveOccurred())
    83  
    84  				for _, req := range reqs {
    85  					err := req.Execute()
    86  					if err != nil {
    87  						firstErr = err
    88  						break
    89  					}
    90  				}
    91  
    92  				Expect(firstErr.Error()).To(ContainSubstring("Incorrect Usage. No argument required"))
    93  			})
    94  		})
    95  	})
    96  
    97  	Describe("Execute", func() {
    98  		var runCLIerr error
    99  
   100  		BeforeEach(func() {
   101  			cmd.Requirements(factory, flagContext)
   102  
   103  			endpointRepo.GetCCInfoReturns(
   104  				&coreconfig.CCInfo{},
   105  				"some-endpoint",
   106  				nil,
   107  			)
   108  		})
   109  
   110  		JustBeforeEach(func() {
   111  			runCLIerr = cmd.Execute(flagContext)
   112  		})
   113  
   114  		It("tries to update the endpoint", func() {
   115  			Expect(runCLIerr).NotTo(HaveOccurred())
   116  			Expect(endpointRepo.GetCCInfoCallCount()).To(Equal(1))
   117  			Expect(endpointRepo.GetCCInfoArgsForCall(0)).To(Equal("fake-api-endpoint"))
   118  		})
   119  
   120  		Context("when updating the endpoint succeeds", func() {
   121  			ccInfo := &coreconfig.CCInfo{
   122  				APIVersion:               "some-version",
   123  				AuthorizationEndpoint:    "auth/endpoint",
   124  				MinCLIVersion:            "min-cli-version",
   125  				MinRecommendedCLIVersion: "min-rec-cli-version",
   126  				SSHOAuthClient:           "some-client",
   127  				RoutingAPIEndpoint:       "routing/endpoint",
   128  			}
   129  			BeforeEach(func() {
   130  				endpointRepo.GetCCInfoReturns(
   131  					ccInfo,
   132  					"updated-endpoint",
   133  					nil,
   134  				)
   135  			})
   136  
   137  			It("tries to refresh the auth token", func() {
   138  				Expect(runCLIerr).NotTo(HaveOccurred())
   139  				Expect(authRepo.RefreshAuthTokenCallCount()).To(Equal(1))
   140  			})
   141  
   142  			Context("when refreshing the token fails with an error", func() {
   143  				BeforeEach(func() {
   144  					authRepo.RefreshAuthTokenReturns("", errors.New("auth-error"))
   145  				})
   146  
   147  				It("fails with error", func() {
   148  					Expect(runCLIerr).To(HaveOccurred())
   149  					Expect(runCLIerr.Error()).To(Equal("Error refreshing oauth token: auth-error"))
   150  				})
   151  			})
   152  
   153  			Context("when refreshing the token succeeds", func() {
   154  				BeforeEach(func() {
   155  					authRepo.RefreshAuthTokenReturns("auth-token", nil)
   156  				})
   157  
   158  				It("tries to get the ssh-code", func() {
   159  					Expect(runCLIerr).NotTo(HaveOccurred())
   160  					Expect(authRepo.AuthorizeCallCount()).To(Equal(1))
   161  					Expect(authRepo.AuthorizeArgsForCall(0)).To(Equal("auth-token"))
   162  				})
   163  
   164  				Context("when getting the ssh-code succeeds", func() {
   165  					BeforeEach(func() {
   166  						authRepo.AuthorizeReturns("some-code", nil)
   167  					})
   168  
   169  					It("displays the token", func() {
   170  						Expect(runCLIerr).NotTo(HaveOccurred())
   171  						Expect(ui.Outputs()).To(ContainSubstrings(
   172  							[]string{"some-code"},
   173  						))
   174  					})
   175  				})
   176  
   177  				Context("when getting the ssh-code fails", func() {
   178  					BeforeEach(func() {
   179  						authRepo.AuthorizeReturns("", errors.New("auth-err"))
   180  					})
   181  
   182  					It("fails with error", func() {
   183  						Expect(runCLIerr).To(HaveOccurred())
   184  						Expect(runCLIerr.Error()).To(Equal("Error getting SSH code: auth-err"))
   185  					})
   186  				})
   187  			})
   188  		})
   189  	})
   190  })