github.com/loafoe/cli@v7.1.0+incompatible/integration/v7/isolated/passwd_command_test.go (about) 1 package isolated 2 3 import ( 4 "fmt" 5 "io" 6 "strings" 7 8 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 9 "code.cloudfoundry.org/cli/integration/helpers" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 . "github.com/onsi/gomega/gbytes" 13 . "github.com/onsi/gomega/gexec" 14 ) 15 16 var _ = Describe("passwd command", func() { 17 Describe("help", func() { 18 When("--help flag is set", func() { 19 It("appears in cf help -a", func() { 20 session := helpers.CF("help", "-a") 21 Eventually(session).Should(Exit(0)) 22 Expect(session).To(HaveCommandInCategoryWithDescription("passwd", "GETTING STARTED", "Change user password")) 23 }) 24 25 It("displays command usage to output", func() { 26 session := helpers.CF("passwd", "--help") 27 28 Eventually(session).Should(Say(`NAME:`)) 29 Eventually(session).Should(Say("passwd - Change user password")) 30 Eventually(session).Should(Say("USAGE:")) 31 Eventually(session).Should(Say("cf passwd")) 32 Eventually(session).Should(Say("ALIAS:")) 33 Eventually(session).Should(Say("pw")) 34 35 Eventually(session).Should(Exit(0)) 36 }) 37 }) 38 }) 39 40 Describe("functionality", func() { 41 var ( 42 username string 43 currentPassword string 44 newPassword string 45 verifyPassword string 46 session *Session 47 passwdInput io.Reader 48 ) 49 50 BeforeEach(func() { 51 helpers.LoginCF() 52 username = helpers.NewUsername() 53 currentPassword = helpers.NewPassword() 54 newPassword = helpers.NewPassword() 55 verifyPassword = newPassword 56 session = helpers.CF("create-user", username, currentPassword) 57 Eventually(session).Should(Exit(0)) 58 helpers.LogoutCF() 59 60 env := map[string]string{ 61 "CF_USERNAME": username, 62 "CF_PASSWORD": currentPassword, 63 } 64 session = helpers.CFWithEnv(env, "auth") 65 Eventually(session).Should(Exit(0)) 66 }) 67 68 AfterEach(func() { 69 helpers.LoginCF() 70 helpers.DeleteUser(username) 71 }) 72 73 JustBeforeEach(func() { 74 passwdInput = strings.NewReader(fmt.Sprintf(`%s 75 %s 76 %s 77 `, currentPassword, newPassword, verifyPassword)) 78 session = helpers.CFWithStdin(passwdInput, "passwd") 79 }) 80 81 When("the password is successfully changed", func() { 82 It("succeeds", func() { 83 Eventually(session.Out).Should(Say("Current password:")) 84 Eventually(session.Out).Should(Say("New password:")) 85 Eventually(session.Out).Should(Say("Verify password:")) 86 Eventually(session.Out).Should(Say("Changing password for user " + username + "...")) 87 Eventually(session.Out).Should(Say("OK")) 88 Eventually(session.Out).Should(Say("Please log in again")) 89 Eventually(session).Should(Exit(0)) 90 91 helpers.LoginAs(username, newPassword) 92 }) 93 }) 94 95 When("the user's new passwords don't match", func() { 96 BeforeEach(func() { 97 verifyPassword = "passwood-dont-match" 98 }) 99 100 It("fails with an error message", func() { 101 Eventually(session.Out).Should(Say("Current password:")) 102 Eventually(session.Out).Should(Say("New password:")) 103 Eventually(session.Out).Should(Say("Verify password:")) 104 Eventually(session.Out).Should(Say("Changing password for user " + username + "...")) 105 Eventually(session.Err).Should(Say("Password verification does not match.")) 106 Eventually(session.Out).Should(Say("FAILED")) 107 Eventually(session).Should(Exit(1)) 108 109 helpers.LoginAs(username, currentPassword) 110 }) 111 }) 112 113 When("the user enters the wrong current password", func() { 114 BeforeEach(func() { 115 currentPassword = "definitely not the right password" 116 }) 117 118 It("fails with an error message", func() { 119 Eventually(session.Out).Should(Say("Current password:")) 120 Eventually(session.Out).Should(Say("New password:")) 121 Eventually(session.Out).Should(Say("Verify password:")) 122 Eventually(session.Out).Should(Say("Changing password for user " + username + "...")) 123 Eventually(session.Err).Should(Say("Old password is incorrect")) 124 Eventually(session.Out).Should(Say("FAILED")) 125 Eventually(session).Should(Exit(1)) 126 }) 127 }) 128 129 When("the user enters the same current and new password", func() { 130 BeforeEach(func() { 131 newPassword = currentPassword 132 verifyPassword = currentPassword 133 }) 134 135 It("fails with an error message", func() { 136 Eventually(session.Out).Should(Say("Current password:")) 137 Eventually(session.Out).Should(Say("New password:")) 138 Eventually(session.Out).Should(Say("Verify password:")) 139 Eventually(session.Out).Should(Say("Changing password for user " + username + "...")) 140 Eventually(session.Err).Should(Say("Your new password cannot be the same as the old password.")) 141 Eventually(session.Out).Should(Say("FAILED")) 142 Eventually(session).Should(Exit(1)) 143 144 helpers.LoginAs(username, currentPassword) 145 }) 146 }) 147 148 When("the user is not logged in", func() { 149 BeforeEach(func() { 150 helpers.LogoutCF() 151 }) 152 153 It("fails with an error message", func() { 154 Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' or 'cf login --sso' to log in.")) 155 Eventually(session.Out).Should(Say("FAILED")) 156 Eventually(session).Should(Exit(1)) 157 }) 158 }) 159 }) 160 })