github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v7/isolated/create_user_command_test.go (about)

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