github.com/sleungcy/cli@v7.1.0+incompatible/command/v7/passwd_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/command/commandfakes"
     8  	"code.cloudfoundry.org/cli/command/translatableerror"
     9  	v7 "code.cloudfoundry.org/cli/command/v7"
    10  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    11  	"code.cloudfoundry.org/cli/util/configv3"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("passwd Command", func() {
    17  	var (
    18  		cmd             v7.PasswdCommand
    19  		fakeUI          *commandfakes.FakeUI
    20  		fakeConfig      *commandfakes.FakeConfig
    21  		fakeSharedActor *commandfakes.FakeSharedActor
    22  		fakeActor       *v7fakes.FakeActor
    23  		binaryName      string
    24  		executeErr      error
    25  	)
    26  
    27  	BeforeEach(func() {
    28  		fakeUI = new(commandfakes.FakeUI)
    29  		fakeConfig = new(commandfakes.FakeConfig)
    30  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    31  		fakeActor = new(v7fakes.FakeActor)
    32  
    33  		binaryName = "faceman"
    34  		fakeConfig.BinaryNameReturns(binaryName)
    35  
    36  		cmd = v7.PasswdCommand{
    37  			BaseCommand: v7.BaseCommand{
    38  				UI:          fakeUI,
    39  				Config:      fakeConfig,
    40  				Actor:       fakeActor,
    41  				SharedActor: fakeSharedActor,
    42  			},
    43  		}
    44  
    45  		fakeConfig.CurrentUserReturns(configv3.User{Name: "steve", GUID: "steve-guid"}, nil)
    46  
    47  		fakeUI.DisplayPasswordPromptReturnsOnCall(0, "old1", nil)
    48  		fakeUI.DisplayPasswordPromptReturnsOnCall(1, "new1", nil)
    49  		fakeUI.DisplayPasswordPromptReturnsOnCall(2, "new1", nil)
    50  	})
    51  
    52  	JustBeforeEach(func() {
    53  		executeErr = cmd.Execute(nil)
    54  	})
    55  
    56  	When("changing password succeeds", func() {
    57  		It("displays flavor text", func() {
    58  			text, variables := fakeUI.DisplayTextWithFlavorArgsForCall(0)
    59  			Expect(text).To(Equal("Changing password for user {{.Username}}..."))
    60  			Expect(variables[0]).To(Equal(map[string]interface{}{
    61  				"Username": "steve",
    62  			}))
    63  		})
    64  
    65  		It("makes the correct request to UAA", func() {
    66  			Expect(fakeActor.UpdateUserPasswordCallCount()).To(Equal(1))
    67  
    68  			userGUID, oldPassword, newPassword := fakeActor.UpdateUserPasswordArgsForCall(0)
    69  			Expect(userGUID).To(Equal("steve-guid"))
    70  			Expect(oldPassword).To(Equal("old1"))
    71  			Expect(newPassword).To(Equal("new1"))
    72  		})
    73  
    74  		It("displays OK and unsets user information", func() {
    75  			Expect(fakeUI.DisplayOKCallCount()).To(Equal(1))
    76  			Expect(fakeConfig.UnsetUserInformationCallCount()).To(Equal(1))
    77  			Expect(fakeUI.DisplayTextArgsForCall(0)).To(Equal("Please log in again."))
    78  		})
    79  	})
    80  
    81  	When("checking target fails", func() {
    82  		BeforeEach(func() {
    83  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    84  		})
    85  
    86  		It("returns an error", func() {
    87  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    88  
    89  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    90  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    91  			Expect(checkTargetedOrg).To(BeFalse())
    92  			Expect(checkTargetedSpace).To(BeFalse())
    93  		})
    94  	})
    95  
    96  	When("the user is not logged in", func() {
    97  		var expectedErr error
    98  
    99  		BeforeEach(func() {
   100  			expectedErr = errors.New("some current user error")
   101  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
   102  		})
   103  
   104  		It("return an error", func() {
   105  			Expect(executeErr).To(Equal(expectedErr))
   106  		})
   107  	})
   108  
   109  	When("an error occurs while reading the current password", func() {
   110  		BeforeEach(func() {
   111  			fakeUI.DisplayPasswordPromptReturnsOnCall(0, "", errors.New("current-password-error"))
   112  			cmd.UI = fakeUI
   113  		})
   114  
   115  		It("returns the error", func() {
   116  			Expect(executeErr).To(MatchError("current-password-error"))
   117  		})
   118  	})
   119  
   120  	When("an error occurs while reading the new password", func() {
   121  		BeforeEach(func() {
   122  			fakeUI.DisplayPasswordPromptReturnsOnCall(1, "", errors.New("new-password-error"))
   123  			cmd.UI = fakeUI
   124  		})
   125  
   126  		It("returns the error", func() {
   127  			Expect(executeErr).To(MatchError("new-password-error"))
   128  		})
   129  	})
   130  
   131  	When("an error occurs while reading the verification password", func() {
   132  		BeforeEach(func() {
   133  			fakeUI := new(commandfakes.FakeUI)
   134  			fakeUI.DisplayPasswordPromptReturnsOnCall(2, "", errors.New("verify-password-error"))
   135  			cmd.UI = fakeUI
   136  		})
   137  
   138  		It("returns the error", func() {
   139  			Expect(executeErr).To(MatchError("verify-password-error"))
   140  		})
   141  	})
   142  
   143  	When("the verify password does not match the new password", func() {
   144  		BeforeEach(func() {
   145  			fakeUI.DisplayPasswordPromptReturnsOnCall(2, "WRONG", nil)
   146  		})
   147  
   148  		It("returns an error", func() {
   149  			Expect(executeErr).To(MatchError(translatableerror.PasswordVerificationFailedError{}))
   150  			Expect(fakeActor.UpdateUserPasswordCallCount()).To(Equal(0))
   151  		})
   152  	})
   153  
   154  	When("an error occurs while making the request to UAA", func() {
   155  		BeforeEach(func() {
   156  			fakeActor.UpdateUserPasswordReturns(errors.New("update-pw-error"))
   157  		})
   158  
   159  		It("returns the error", func() {
   160  			Expect(executeErr).To(MatchError("update-pw-error"))
   161  		})
   162  	})
   163  })