github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/oauth_token_test.go (about)

     1  package commands_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/authentication/authenticationfakes"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/requirements"
    10  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    11  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    12  	"code.cloudfoundry.org/cli/plugin/models"
    13  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    14  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    15  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    16  
    17  	"os"
    18  
    19  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    20  	. "github.com/onsi/ginkgo"
    21  	. "github.com/onsi/gomega"
    22  )
    23  
    24  var _ = Describe("OauthToken", func() {
    25  	var (
    26  		ui                  *testterm.FakeUI
    27  		authRepo            *authenticationfakes.FakeRepository
    28  		requirementsFactory *requirementsfakes.FakeFactory
    29  		configRepo          coreconfig.Repository
    30  		deps                commandregistry.Dependency
    31  	)
    32  
    33  	updateCommandDependency := func(pluginCall bool) {
    34  		deps.UI = ui
    35  		deps.RepoLocator = deps.RepoLocator.SetAuthenticationRepository(authRepo)
    36  		deps.Config = configRepo
    37  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("oauth-token").SetDependency(deps, pluginCall))
    38  	}
    39  
    40  	BeforeEach(func() {
    41  		ui = &testterm.FakeUI{}
    42  		fakeLogger := new(tracefakes.FakePrinter)
    43  		authRepo = new(authenticationfakes.FakeRepository)
    44  		configRepo = testconfig.NewRepositoryWithDefaults()
    45  		requirementsFactory = new(requirementsfakes.FakeFactory)
    46  		deps = commandregistry.NewDependency(os.Stdout, fakeLogger, "")
    47  	})
    48  
    49  	runCommand := func() bool {
    50  		return testcmd.RunCLICommand("oauth-token", []string{}, requirementsFactory, updateCommandDependency, false, ui)
    51  	}
    52  
    53  	Describe("requirements", func() {
    54  		It("fails when the user is not logged in", func() {
    55  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    56  			Expect(runCommand()).ToNot(HavePassedRequirements())
    57  		})
    58  	})
    59  
    60  	Describe("when logged in", func() {
    61  		BeforeEach(func() {
    62  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    63  		})
    64  
    65  		It("fails if oauth refresh fails", func() {
    66  			authRepo.RefreshAuthTokenReturns("", errors.New("Could not refresh"))
    67  			runCommand()
    68  
    69  			Expect(ui.Outputs()).To(ContainSubstrings(
    70  				[]string{"FAILED"},
    71  				[]string{"Could not refresh"},
    72  			))
    73  		})
    74  
    75  		It("returns to the user the oauth token after a refresh", func() {
    76  			authRepo.RefreshAuthTokenReturns("1234567890", nil)
    77  			runCommand()
    78  
    79  			Expect(ui.Outputs()).To(ContainSubstrings(
    80  				[]string{"1234567890"},
    81  			))
    82  		})
    83  
    84  		Context("when invoked by a plugin", func() {
    85  			var (
    86  				pluginModel plugin_models.GetOauthToken_Model
    87  			)
    88  
    89  			BeforeEach(func() {
    90  				pluginModel = plugin_models.GetOauthToken_Model{}
    91  				deps.PluginModels.OauthToken = &pluginModel
    92  			})
    93  
    94  			It("populates the plugin model upon execution", func() {
    95  				authRepo.RefreshAuthTokenReturns("911999111", nil)
    96  				testcmd.RunCLICommand("oauth-token", []string{}, requirementsFactory, updateCommandDependency, true, ui)
    97  				Expect(pluginModel.Token).To(Equal("911999111"))
    98  			})
    99  		})
   100  	})
   101  })