github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/commands/serviceauthtoken/create_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/util/testhelpers/commands"
    11  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    12  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    13  
    14  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("create-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("create-service-auth-token").SetDependency(deps, pluginCall))
    33  	}
    34  
    35  	BeforeEach(func() {
    36  		ui = &testterm.FakeUI{}
    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("create-service-auth-token", args, requirementsFactory, updateCommandDependency, false, ui)
    44  	}
    45  
    46  	Describe("requirements", func() {
    47  		It("fails with usage when not invoked with exactly three args", func() {
    48  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    49  			runCommand("whoops", "i-accidentally-an-arg")
    50  
    51  			Expect(ui.Outputs()).To(ContainSubstrings(
    52  				[]string{"Incorrect Usage", "Requires", "arguments"},
    53  			))
    54  		})
    55  
    56  		It("fails when not logged in", func() {
    57  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    58  			Expect(runCommand("just", "enough", "args")).To(BeFalse())
    59  		})
    60  
    61  		It("requires CC API version 2.47 or lower", func() {
    62  			requirementsFactory.NewMaxAPIVersionRequirementReturns(requirements.Failing{Message: "max api 2.47"})
    63  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    64  			Expect(runCommand("one", "two", "three")).To(BeFalse())
    65  		})
    66  	})
    67  
    68  	Context("when logged in", func() {
    69  		BeforeEach(func() {
    70  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    71  			requirementsFactory.NewMaxAPIVersionRequirementReturns(requirements.Passing{})
    72  		})
    73  
    74  		It("creates a service auth token, obviously", func() {
    75  			runCommand("a label", "a provider", "a value")
    76  
    77  			Expect(ui.Outputs()).To(ContainSubstrings(
    78  				[]string{"Creating service auth token as", "my-user"},
    79  				[]string{"OK"},
    80  			))
    81  
    82  			authToken := models.ServiceAuthTokenFields{}
    83  			authToken.Label = "a label"
    84  			authToken.Provider = "a provider"
    85  			authToken.Token = "a value"
    86  			Expect(authTokenRepo.CreatedServiceAuthTokenFields).To(Equal(authToken))
    87  		})
    88  	})
    89  })