github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/oauth_token_test.go (about)

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