github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+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.Out).Should(Say("NAME:"))
    18  				Eventually(session.Out).Should(Say("create-user - Create a new user"))
    19  				Eventually(session.Out).Should(Say("USAGE:"))
    20  				Eventually(session.Out).Should(Say("cf create-user USERNAME PASSWORD"))
    21  				Eventually(session.Out).Should(Say("cf create-user USERNAME --origin ORIGIN"))
    22  				Eventually(session.Out).Should(Say("EXAMPLES:"))
    23  				Eventually(session.Out).Should(Say("   cf create-user j.smith@example.com S3cr3t                  # internal user"))
    24  				Eventually(session.Out).Should(Say("   cf create-user j.smith@example.com --origin ldap           # LDAP user"))
    25  				Eventually(session.Out).Should(Say("   cf create-user j.smith@example.com --origin provider-alias # SAML or OpenID Connect federated user"))
    26  				Eventually(session.Out).Should(Say("OPTIONS:"))
    27  				Eventually(session.Out).Should(Say("--origin      Origin for mapping a user account to a user in an external identity provider"))
    28  				Eventually(session.Out).Should(Say("SEE ALSO:"))
    29  				Eventually(session.Out).Should(Say("passwd, set-org-role, set-space-role"))
    30  				Eventually(session).Should(Exit(0))
    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.Out).Should(Say("FAILED"))
    44  				Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint."))
    45  				Eventually(session).Should(Exit(1))
    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.Out).Should(Say("FAILED"))
    57  				Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in."))
    58  				Eventually(session).Should(Exit(1))
    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.Out).Should(Say("Error creating user %s.", newUser))
    85  				Eventually(session.Err).Should(Say("Insufficient scope for this resource"))
    86  				Eventually(session.Out).Should(Say("FAILED"))
    87  				Eventually(session).Should(Exit(1))
    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.Out).Should(Say("Error creating user %s.", funkyUsername))
   101  						Eventually(session.Err).Should(Say("Username must match pattern: \\[\\\\p\\{L\\}\\+0\\-9\\+\\\\\\-_\\.@'!\\]\\+"))
   102  						Eventually(session.Out).Should(Say("FAILED"))
   103  						Eventually(session).Should(Exit(1))
   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.Out).Should(Say("Error creating user ."))
   114  						Eventually(session.Err).Should(Say("A username must be provided."))
   115  						Eventually(session.Out).Should(Say("FAILED"))
   116  						Eventually(session).Should(Exit(1))
   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.Err).Should(Say("Incorrect Usage: the required argument `PASSWORD` was not provided"))
   128  							Eventually(session.Out).Should(Say("FAILED"))
   129  							Eventually(session.Out).Should(Say("USAGE"))
   130  							Eventually(session).Should(Exit(1))
   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.Err).Should(Say("Incorrect Usage: the required argument `PASSWORD` was not provided"))
   140  							Eventually(session.Out).Should(Say("FAILED"))
   141  							Eventually(session.Out).Should(Say("USAGE"))
   142  							Eventually(session).Should(Exit(1))
   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.Out).Should(Say("Creating user %s...", newUser))
   152  						Eventually(session.Out).Should(Say("OK"))
   153  						Eventually(session.Out).Should(Say("TIP: Assign roles with 'cf set-org-role' and 'cf set-space-role'"))
   154  						Eventually(session).Should(Exit(0))
   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.Err).Should(Say("Incorrect Usage: expected argument for flag `--origin'"))
   162  						Eventually(session).Should(Exit(1))
   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.Err).Should(Say("Incorrect Usage: the required argument `PASSWORD` was not provided"))
   171  					Eventually(session.Out).Should(Say("FAILED"))
   172  					Eventually(session.Out).Should(Say("USAGE"))
   173  					Eventually(session).Should(Exit(1))
   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.Err).Should(Say("user %s already exists", newUser))
   193  					Eventually(session.Out).Should(Say("OK"))
   194  					Eventually(session).Should(Exit(0))
   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.Out).Should(Say("Creating user %s...", newUser))
   204  					Eventually(session.Out).Should(Say("OK"))
   205  					Eventually(session.Out).Should(Say("TIP: Assign roles with 'cf set-org-role' and 'cf set-space-role'"))
   206  					Eventually(session).Should(Exit(0))
   207  				})
   208  			})
   209  		})
   210  	})
   211  })