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

     1  package commands_test
     2  
     3  import (
     4  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     5  	"github.com/cloudfoundry/cli/cf/command_registry"
     6  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     7  	"github.com/cloudfoundry/cli/cf/models"
     8  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     9  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    10  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    11  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  
    15  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    16  )
    17  
    18  var _ = Describe("password command", func() {
    19  	var (
    20  		pwDeps passwordDeps
    21  		ui     *testterm.FakeUI
    22  		deps   command_registry.Dependency
    23  	)
    24  
    25  	updateCommandDependency := func(pluginCall bool) {
    26  		deps.Ui = ui
    27  		deps.Config = pwDeps.Config
    28  		deps.RepoLocator = deps.RepoLocator.SetPasswordRepository(pwDeps.PwdRepo)
    29  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("passwd").SetDependency(deps, pluginCall))
    30  	}
    31  
    32  	callPassword := func(inputs []string, pwDeps passwordDeps) (*testterm.FakeUI, bool) {
    33  		ui = &testterm.FakeUI{Inputs: inputs}
    34  		passed := testcmd.RunCliCommand("passwd", []string{}, pwDeps.ReqFactory, updateCommandDependency, false)
    35  		return ui, passed
    36  	}
    37  
    38  	BeforeEach(func() {
    39  		pwDeps = getPasswordDeps()
    40  	})
    41  
    42  	It("does not pass requirements if you are not logged in", func() {
    43  		pwDeps.ReqFactory.LoginSuccess = false
    44  		_, passed := callPassword([]string{}, pwDeps)
    45  		Expect(passed).To(BeFalse())
    46  	})
    47  
    48  	Context("when logged in successfully", func() {
    49  		BeforeEach(func() {
    50  			pwDeps.ReqFactory.LoginSuccess = true
    51  			pwDeps.PwdRepo.UpdateUnauthorized = false
    52  		})
    53  
    54  		It("passes requirements", func() {
    55  			_, passed := callPassword([]string{"", "", ""}, pwDeps)
    56  			Expect(passed).To(BeTrue())
    57  		})
    58  
    59  		It("changes your password when given a new password", func() {
    60  			pwDeps.PwdRepo.UpdateUnauthorized = false
    61  			ui, _ := callPassword([]string{"old-password", "new-password", "new-password"}, pwDeps)
    62  
    63  			Expect(ui.PasswordPrompts).To(ContainSubstrings(
    64  				[]string{"Current Password"},
    65  				[]string{"New Password"},
    66  				[]string{"Verify Password"},
    67  			))
    68  
    69  			Expect(ui.Outputs).To(ContainSubstrings(
    70  				[]string{"Changing password..."},
    71  				[]string{"OK"},
    72  				[]string{"Please log in again"},
    73  			))
    74  
    75  			Expect(pwDeps.PwdRepo.UpdateNewPassword).To(Equal("new-password"))
    76  			Expect(pwDeps.PwdRepo.UpdateOldPassword).To(Equal("old-password"))
    77  
    78  			Expect(pwDeps.Config.AccessToken()).To(Equal(""))
    79  			Expect(pwDeps.Config.OrganizationFields()).To(Equal(models.OrganizationFields{}))
    80  			Expect(pwDeps.Config.SpaceFields()).To(Equal(models.SpaceFields{}))
    81  		})
    82  
    83  		It("fails when the password verification does not match", func() {
    84  			ui, _ := callPassword([]string{"old-password", "new-password", "new-password-with-error"}, pwDeps)
    85  
    86  			Expect(ui.PasswordPrompts).To(ContainSubstrings(
    87  				[]string{"Current Password"},
    88  				[]string{"New Password"},
    89  				[]string{"Verify Password"},
    90  			))
    91  
    92  			Expect(ui.Outputs).To(ContainSubstrings(
    93  				[]string{"FAILED"},
    94  				[]string{"Password verification does not match"},
    95  			))
    96  
    97  			Expect(pwDeps.PwdRepo.UpdateNewPassword).To(Equal(""))
    98  		})
    99  
   100  		It("fails when the current password does not match", func() {
   101  			pwDeps.PwdRepo.UpdateUnauthorized = true
   102  			ui, _ := callPassword([]string{"old-password", "new-password", "new-password"}, pwDeps)
   103  
   104  			Expect(ui.PasswordPrompts).To(ContainSubstrings(
   105  				[]string{"Current Password"},
   106  				[]string{"New Password"},
   107  				[]string{"Verify Password"},
   108  			))
   109  
   110  			Expect(ui.Outputs).To(ContainSubstrings(
   111  				[]string{"Changing password..."},
   112  				[]string{"FAILED"},
   113  				[]string{"Current password did not match"},
   114  			))
   115  
   116  			Expect(pwDeps.PwdRepo.UpdateNewPassword).To(Equal("new-password"))
   117  			Expect(pwDeps.PwdRepo.UpdateOldPassword).To(Equal("old-password"))
   118  		})
   119  	})
   120  })
   121  
   122  type passwordDeps struct {
   123  	ReqFactory *testreq.FakeReqFactory
   124  	PwdRepo    *testapi.FakePasswordRepo
   125  	Config     core_config.Repository
   126  }
   127  
   128  func getPasswordDeps() passwordDeps {
   129  	return passwordDeps{
   130  		ReqFactory: &testreq.FakeReqFactory{LoginSuccess: true},
   131  		PwdRepo:    &testapi.FakePasswordRepo{UpdateUnauthorized: true},
   132  		Config:     testconfig.NewRepository(),
   133  	}
   134  }