github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/integration/v6/isolated/create_user_command_test.go (about) 1 package isolated 2 3 import ( 4 "code.cloudfoundry.org/cli/integration/helpers" 5 . "github.com/onsi/ginkgo" 6 . "github.com/onsi/ginkgo/extensions/table" 7 . "github.com/onsi/gomega" 8 . "github.com/onsi/gomega/gbytes" 9 . "github.com/onsi/gomega/gexec" 10 ) 11 12 var _ = Describe("create-user command", func() { 13 Describe("help", func() { 14 When("--help flag is set", func() { 15 It("Displays command usage to output", func() { 16 session := helpers.CF("create-user", "--help") 17 Eventually(session).Should(Say("NAME:")) 18 Eventually(session).Should(Say("create-user - Create a new user")) 19 Eventually(session).Should(Say("USAGE:")) 20 Eventually(session).Should(Say("cf create-user USERNAME PASSWORD")) 21 Eventually(session).Should(Say("cf create-user USERNAME --origin ORIGIN")) 22 Eventually(session).Should(Say("EXAMPLES:")) 23 Eventually(session).Should(Say(" cf create-user j.smith@example.com S3cr3t # internal user")) 24 Eventually(session).Should(Say(" cf create-user j.smith@example.com --origin ldap # LDAP user")) 25 Eventually(session).Should(Say(" cf create-user j.smith@example.com --origin provider-alias # SAML or OpenID Connect federated user")) 26 Eventually(session).Should(Say("OPTIONS:")) 27 Eventually(session).Should(Say("--origin Origin for mapping a user account to a user in an external identity provider")) 28 Eventually(session).Should(Say("SEE ALSO:")) 29 Eventually(session).Should(Say("passwd, set-org-role, set-space-role")) 30 Eventually(session).Should(Exit(0)) 31 }) 32 }) 33 }) 34 35 When("the environment is not setup correctly", func() { 36 It("fails with the appropriate errors", func() { 37 helpers.CheckEnvironmentTargetedCorrectly(false, false, ReadOnlyOrg, "create-user", "username", "password") 38 }) 39 }) 40 41 When("the environment is setup correctly", func() { 42 When("the logged in user is not authorized to create new users", func() { 43 var ( 44 newUser string 45 newPassword string 46 ) 47 48 BeforeEach(func() { 49 helpers.LoginCF() 50 noobUser := helpers.NewUsername() 51 noobPassword := helpers.NewPassword() 52 session := helpers.CF("create-user", noobUser, noobPassword) 53 Eventually(session).Should(Exit(0)) 54 helpers.LogoutCF() 55 56 env := map[string]string{ 57 "CF_USERNAME": noobUser, 58 "CF_PASSWORD": noobPassword, 59 } 60 session = helpers.CFWithEnv(env, "auth") 61 Eventually(session).Should(Exit(0)) 62 63 newUser = helpers.NewUsername() 64 newPassword = helpers.NewPassword() 65 }) 66 67 It("fails with insufficient scope error", func() { 68 session := helpers.CF("create-user", newUser, newPassword) 69 Eventually(session).Should(Say(`Creating user %s\.\.\.`, newUser)) 70 Eventually(session).Should(Say(`Error creating user %s\.`, newUser)) 71 Eventually(session.Err).Should(Say(`You are not authorized to perform the requested action\.`)) 72 Eventually(session).Should(Say("FAILED")) 73 Eventually(session).Should(Exit(1)) 74 }) 75 }) 76 77 When("the logged in user is authorized to create new users", func() { 78 BeforeEach(func() { 79 helpers.LoginCF() 80 }) 81 82 When("passed invalid username", func() { 83 DescribeTable("when passed funkyUsername", 84 func(funkyUsername string) { 85 session := helpers.CF("create-user", funkyUsername, helpers.NewPassword()) 86 Eventually(session).Should(Say("Error creating user %s.", funkyUsername)) 87 Eventually(session.Err).Should(Say("Username must match pattern: \\[\\\\p\\{L\\}\\+0\\-9\\+\\\\\\-_\\.@'!\\]\\+")) 88 Eventually(session).Should(Say("FAILED")) 89 Eventually(session).Should(Exit(1)) 90 }, 91 92 Entry("fails when passed an emoji", "😀"), 93 Entry("fails when passed a backtick", "`"), 94 ) 95 96 When("the username is empty", func() { 97 It("fails with a username must be provided error", func() { 98 session := helpers.CF("create-user", "", helpers.NewPassword()) 99 Eventually(session).Should(Say("Error creating user .")) 100 Eventually(session.Err).Should(Say("A username must be provided.")) 101 Eventually(session).Should(Say("FAILED")) 102 Eventually(session).Should(Exit(1)) 103 }) 104 }) 105 }) 106 107 When("the user passes in an origin flag", func() { 108 When("the origin is UAA", func() { 109 When("password is not present", func() { 110 It("errors and prints usage", func() { 111 newUser := helpers.NewUsername() 112 session := helpers.CF("create-user", newUser, "--origin", "UAA") 113 Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `PASSWORD` was not provided")) 114 Eventually(session).Should(Say("FAILED")) 115 Eventually(session).Should(Say("USAGE")) 116 Eventually(session).Should(Exit(1)) 117 }) 118 }) 119 }) 120 When("the origin is the empty string", func() { 121 When("password is not present", func() { 122 It("errors and prints usage", func() { 123 newUser := helpers.NewUsername() 124 session := helpers.CF("create-user", newUser, "--origin", "") 125 Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `PASSWORD` was not provided")) 126 Eventually(session).Should(Say("FAILED")) 127 Eventually(session).Should(Say("USAGE")) 128 Eventually(session).Should(Exit(1)) 129 }) 130 }) 131 }) 132 133 When("the origin is not UAA or empty", func() { 134 It("creates the new user in the specified origin", func() { 135 newUser := helpers.NewUsername() 136 session := helpers.CF("create-user", newUser, "--origin", "ldap") 137 Eventually(session).Should(Say("Creating user %s...", newUser)) 138 Eventually(session).Should(Say("OK")) 139 Eventually(session).Should(Say("TIP: Assign roles with 'cf set-org-role' and 'cf set-space-role'")) 140 Eventually(session).Should(Exit(0)) 141 }) 142 }) 143 144 When("argument for flag is not present", func() { 145 It("fails with incorrect usage error", func() { 146 session := helpers.CF("create-user", helpers.NewUsername(), "--origin") 147 Eventually(session.Err).Should(Say("Incorrect Usage: expected argument for flag `--origin'")) 148 Eventually(session).Should(Exit(1)) 149 }) 150 }) 151 }) 152 153 When("password is not present", func() { 154 It("fails with incorrect usage error", func() { 155 session := helpers.CF("create-user", helpers.NewUsername()) 156 Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `PASSWORD` was not provided")) 157 Eventually(session).Should(Say("FAILED")) 158 Eventually(session).Should(Say("USAGE")) 159 Eventually(session).Should(Exit(1)) 160 }) 161 }) 162 163 When("the user already exists", func() { 164 var ( 165 newUser string 166 newPassword string 167 ) 168 169 BeforeEach(func() { 170 newUser = helpers.NewUsername() 171 newPassword = helpers.NewPassword() 172 session := helpers.CF("create-user", newUser, newPassword) 173 Eventually(session).Should(Exit(0)) 174 }) 175 176 It("fails with the user already exists message", func() { 177 session := helpers.CF("create-user", newUser, newPassword) 178 Eventually(session.Err).Should(Say("user %s already exists", newUser)) 179 Eventually(session).Should(Say("OK")) 180 Eventually(session).Should(Exit(0)) 181 }) 182 }) 183 184 When("the user does not already exist", func() { 185 It("creates the new user", func() { 186 newUser := helpers.NewUsername() 187 newPassword := helpers.NewPassword() 188 session := helpers.CF("create-user", newUser, newPassword) 189 Eventually(session).Should(Say("Creating user %s...", newUser)) 190 Eventually(session).Should(Say("OK")) 191 Eventually(session).Should(Say("TIP: Assign roles with 'cf set-org-role' and 'cf set-space-role'")) 192 Eventually(session).Should(Exit(0)) 193 }) 194 }) 195 }) 196 }) 197 })