github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/integration/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 Context("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(Exit(0)) 18 Expect(session.Out).To(Say("NAME:")) 19 Expect(session.Out).To(Say("create-user - Create a new user")) 20 Expect(session.Out).To(Say("USAGE:")) 21 Expect(session.Out).To(Say("cf create-user USERNAME PASSWORD")) 22 Expect(session.Out).To(Say("cf create-user USERNAME --origin ORIGIN")) 23 Expect(session.Out).To(Say("EXAMPLES:")) 24 Expect(session.Out).To(Say(" cf create-user j.smith@example.com S3cr3t # internal user")) 25 Expect(session.Out).To(Say(" cf create-user j.smith@example.com --origin ldap # LDAP user")) 26 Expect(session.Out).To(Say(" cf create-user j.smith@example.com --origin provider-alias # SAML or OpenID Connect federated user")) 27 Expect(session.Out).To(Say("OPTIONS:")) 28 Expect(session.Out).To(Say("--origin Origin for mapping a user account to a user in an external identity provider")) 29 Expect(session.Out).To(Say("SEE ALSO:")) 30 Expect(session.Out).To(Say("passwd, set-org-role, set-space-role")) 31 }) 32 }) 33 }) 34 35 Context("when the environment is not setup correctly", func() { 36 Context("when no API endpoint is set", func() { 37 BeforeEach(func() { 38 helpers.UnsetAPI() 39 }) 40 41 It("fails with no API endpoint set message", func() { 42 session := helpers.CF("create-user", helpers.RandomUsername(), helpers.RandomPassword()) 43 Eventually(session).Should(Exit(1)) 44 Expect(session.Out).To(Say("FAILED")) 45 Expect(session.Err).To(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint.")) 46 }) 47 }) 48 49 Context("when not logged in", func() { 50 BeforeEach(func() { 51 helpers.LogoutCF() 52 }) 53 54 It("fails with not logged in message", func() { 55 session := helpers.CF("create-user", helpers.RandomUsername(), helpers.RandomPassword()) 56 Eventually(session).Should(Exit(1)) 57 Expect(session.Out).To(Say("FAILED")) 58 Expect(session.Err).To(Say("Not logged in. Use 'cf login' to log in.")) 59 }) 60 }) 61 }) 62 63 Context("when the environment is setup correctly", func() { 64 Context("when the logged in user is not authorized to create new users", func() { 65 var ( 66 newUser string 67 newPassword string 68 ) 69 70 BeforeEach(func() { 71 helpers.LoginCF() 72 noobUser := helpers.RandomUsername() 73 noobPassword := helpers.RandomPassword() 74 session := helpers.CF("create-user", noobUser, noobPassword) 75 Eventually(session).Should(Exit(0)) 76 session = helpers.CF("auth", noobUser, noobPassword) 77 Eventually(session).Should(Exit(0)) 78 newUser = helpers.RandomUsername() 79 newPassword = helpers.RandomPassword() 80 }) 81 82 It("fails with insufficient scope error", func() { 83 session := helpers.CF("create-user", newUser, newPassword) 84 Eventually(session).Should(Exit(1)) 85 Expect(session.Out).To(Say("Error creating user %s.", newUser)) 86 Expect(session.Err).To(Say("Insufficient scope for this resource")) 87 Expect(session.Out).To(Say("FAILED")) 88 }) 89 }) 90 91 Context("when the logged in user is authorized to create new users", func() { 92 BeforeEach(func() { 93 helpers.LoginCF() 94 }) 95 96 Context("when passed invalid username", func() { 97 DescribeTable("when passed funkyUsername", 98 func(funkyUsername string) { 99 session := helpers.CF("create-user", funkyUsername, helpers.RandomPassword()) 100 Eventually(session).Should(Exit(1)) 101 Expect(session.Out).To(Say("Error creating user %s.", funkyUsername)) 102 Expect(session.Err).To(Say("Username must match pattern: \\[\\\\p\\{L\\}\\+0\\-9\\+\\\\\\-_\\.@'!\\]\\+")) 103 Expect(session.Out).To(Say("FAILED")) 104 }, 105 106 Entry("fails when passed an emoji", "😀"), 107 Entry("fails when passed a backtick", "`"), 108 ) 109 110 Context("when the username is empty", func() { 111 It("fails with a username must be provided error", func() { 112 session := helpers.CF("create-user", "", helpers.RandomPassword()) 113 Eventually(session).Should(Exit(1)) 114 Expect(session.Out).To(Say("Error creating user .")) 115 Expect(session.Err).To(Say("A username must be provided.")) 116 Expect(session.Out).To(Say("FAILED")) 117 }) 118 }) 119 }) 120 121 Context("when the user passes in an origin flag", func() { 122 Context("when the origin is UAA", func() { 123 Context("when password is not present", func() { 124 It("errors and prints usage", func() { 125 newUser := helpers.RandomUsername() 126 session := helpers.CF("create-user", newUser, "--origin", "UAA") 127 Eventually(session).Should(Exit(1)) 128 Expect(session.Err).To(Say("Incorrect Usage: the required argument `PASSWORD` was not provided")) 129 Expect(session.Out).To(Say("FAILED")) 130 Expect(session.Out).To(Say("USAGE")) 131 }) 132 }) 133 }) 134 Context("when the origin is the empty string", func() { 135 Context("when password is not present", func() { 136 It("errors and prints usage", func() { 137 newUser := helpers.RandomUsername() 138 session := helpers.CF("create-user", newUser, "--origin", "") 139 Eventually(session).Should(Exit(1)) 140 Expect(session.Err).To(Say("Incorrect Usage: the required argument `PASSWORD` was not provided")) 141 Expect(session.Out).To(Say("FAILED")) 142 Expect(session.Out).To(Say("USAGE")) 143 }) 144 }) 145 }) 146 147 Context("when the origin is not UAA or empty", func() { 148 It("creates the new user in the specified origin", func() { 149 newUser := helpers.RandomUsername() 150 session := helpers.CF("create-user", newUser, "--origin", "ldap") 151 Eventually(session).Should(Exit(0)) 152 Expect(session.Out).To(Say("Creating user %s...", newUser)) 153 Expect(session.Out).To(Say("OK")) 154 Expect(session.Out).To(Say("TIP: Assign roles with 'cf set-org-role' and 'cf set-space-role'")) 155 }) 156 }) 157 158 Context("when argument for flag is not present", func() { 159 It("fails with incorrect usage error", func() { 160 session := helpers.CF("create-user", helpers.RandomUsername(), "--origin") 161 Eventually(session).Should(Exit(1)) 162 Expect(session.Err).To(Say("Incorrect Usage: expected argument for flag `--origin'")) 163 }) 164 }) 165 }) 166 167 Context("when password is not present", func() { 168 It("fails with incorrect usage error", func() { 169 session := helpers.CF("create-user", helpers.RandomUsername()) 170 Eventually(session).Should(Exit(1)) 171 Expect(session.Err).To(Say("Incorrect Usage: the required argument `PASSWORD` was not provided")) 172 Expect(session.Out).To(Say("FAILED")) 173 Expect(session.Out).To(Say("USAGE")) 174 }) 175 }) 176 177 Context("when the user already exists", func() { 178 var ( 179 newUser string 180 newPassword string 181 ) 182 183 BeforeEach(func() { 184 newUser = helpers.RandomUsername() 185 newPassword = helpers.RandomPassword() 186 session := helpers.CF("create-user", newUser, newPassword) 187 Eventually(session).Should(Exit(0)) 188 }) 189 190 It("fails with the user already exists message", func() { 191 session := helpers.CF("create-user", newUser, newPassword) 192 Eventually(session).Should(Exit(0)) 193 Expect(session.Err).To(Say("user %s already exists", newUser)) 194 Expect(session.Out).To(Say("OK")) 195 }) 196 }) 197 198 Context("when the user does not already exist", func() { 199 It("creates the new user", func() { 200 newUser := helpers.RandomUsername() 201 newPassword := helpers.RandomPassword() 202 session := helpers.CF("create-user", newUser, newPassword) 203 Eventually(session).Should(Exit(0)) 204 Expect(session.Out).To(Say("Creating user %s...", newUser)) 205 Expect(session.Out).To(Say("OK")) 206 Expect(session.Out).To(Say("TIP: Assign roles with 'cf set-org-role' and 'cf set-space-role'")) 207 }) 208 }) 209 }) 210 }) 211 })