github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/serviceauthtoken/update_service_auth_token_test.go (about)

     1  package serviceauthtoken_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     5  	"code.cloudfoundry.org/cli/cf/commandregistry"
     6  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     7  	"code.cloudfoundry.org/cli/cf/models"
     8  	"code.cloudfoundry.org/cli/cf/requirements"
     9  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    10  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    11  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    12  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    13  
    14  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("update-service-auth-token command", func() {
    20  	var (
    21  		ui                  *testterm.FakeUI
    22  		configRepo          coreconfig.Repository
    23  		authTokenRepo       *apifakes.OldFakeAuthTokenRepo
    24  		requirementsFactory *requirementsfakes.FakeFactory
    25  		deps                commandregistry.Dependency
    26  	)
    27  
    28  	updateCommandDependency := func(pluginCall bool) {
    29  		deps.UI = ui
    30  		deps.RepoLocator = deps.RepoLocator.SetServiceAuthTokenRepository(authTokenRepo)
    31  		deps.Config = configRepo
    32  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("update-service-auth-token").SetDependency(deps, pluginCall))
    33  	}
    34  
    35  	BeforeEach(func() {
    36  		ui = &testterm.FakeUI{Inputs: []string{"y"}}
    37  		authTokenRepo = new(apifakes.OldFakeAuthTokenRepo)
    38  		configRepo = testconfig.NewRepositoryWithDefaults()
    39  		requirementsFactory = new(requirementsfakes.FakeFactory)
    40  	})
    41  
    42  	runCommand := func(args ...string) bool {
    43  		return testcmd.RunCLICommand("update-service-auth-token", args, requirementsFactory, updateCommandDependency, false, ui)
    44  	}
    45  
    46  	Describe("requirements", func() {
    47  		It("fails with usage when not provided exactly three args", func() {
    48  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    49  			runCommand("some-token-label", "a-provider")
    50  			Expect(ui.Outputs()).To(ContainSubstrings(
    51  				[]string{"Incorrect Usage", "Requires", "arguments"},
    52  			))
    53  		})
    54  
    55  		It("fails when not logged in", func() {
    56  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    57  			Expect(runCommand("label", "provider", "token")).To(BeFalse())
    58  		})
    59  
    60  		It("requires CC API version 2.47 or lower", func() {
    61  			requirementsFactory.NewMaxAPIVersionRequirementReturns(requirements.Failing{Message: "max api 2.47"})
    62  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    63  			Expect(runCommand("one", "two", "three")).To(BeFalse())
    64  		})
    65  	})
    66  
    67  	Context("when logged in and the service auth token exists", func() {
    68  		BeforeEach(func() {
    69  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    70  			requirementsFactory.NewMaxAPIVersionRequirementReturns(requirements.Passing{})
    71  			foundAuthToken := models.ServiceAuthTokenFields{}
    72  			foundAuthToken.GUID = "found-auth-token-guid"
    73  			foundAuthToken.Label = "found label"
    74  			foundAuthToken.Provider = "found provider"
    75  			authTokenRepo.FindByLabelAndProviderServiceAuthTokenFields = foundAuthToken
    76  		})
    77  
    78  		It("updates the service auth token with the provided args", func() {
    79  			runCommand("a label", "a provider", "a value")
    80  
    81  			expectedAuthToken := models.ServiceAuthTokenFields{}
    82  			expectedAuthToken.GUID = "found-auth-token-guid"
    83  			expectedAuthToken.Label = "found label"
    84  			expectedAuthToken.Provider = "found provider"
    85  			expectedAuthToken.Token = "a value"
    86  
    87  			Expect(ui.Outputs()).To(ContainSubstrings(
    88  				[]string{"Updating service auth token as", "my-user"},
    89  				[]string{"OK"},
    90  			))
    91  
    92  			Expect(authTokenRepo.FindByLabelAndProviderLabel).To(Equal("a label"))
    93  			Expect(authTokenRepo.FindByLabelAndProviderProvider).To(Equal("a provider"))
    94  			Expect(authTokenRepo.UpdatedServiceAuthTokenFields).To(Equal(expectedAuthToken))
    95  			Expect(authTokenRepo.UpdatedServiceAuthTokenFields).To(Equal(expectedAuthToken))
    96  		})
    97  	})
    98  })