github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/commands/passwd_test.go (about)

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