github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/integration/v6/isolated/create_space_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/gomega"
     7  	. "github.com/onsi/gomega/gbytes"
     8  	. "github.com/onsi/gomega/gexec"
     9  )
    10  
    11  func expectHelpText(session *Session) {
    12  	Eventually(session).Should(Say(`NAME:`))
    13  	Eventually(session).Should(Say(`create-space - Create a space\n`))
    14  	Eventually(session).Should(Say(`\n`))
    15  
    16  	Eventually(session).Should(Say(`USAGE:`))
    17  	Eventually(session).Should(Say(`cf create-space SPACE \[-o ORG\] \[-q SPACE_QUOTA\]\n`))
    18  	Eventually(session).Should(Say(`\n`))
    19  
    20  	Eventually(session).Should(Say(`OPTIONS:`))
    21  	Eventually(session).Should(Say(`-o\s+Organization`))
    22  	Eventually(session).Should(Say(`-q\s+Quota to assign to the newly created space`))
    23  	Eventually(session).Should(Say(`\n`))
    24  
    25  	Eventually(session).Should(Say(`SEE ALSO:`))
    26  	Eventually(session).Should(Say(`set-space-isolation-segment, space-quotas, spaces, target`))
    27  }
    28  
    29  func expectSuccessTextAndExitCode(session *Session, user, orgName, spaceName string) {
    30  	Eventually(session).Should(Say(`Creating space %s in org %s as %s\.\.\.`, spaceName, orgName, user))
    31  	Eventually(session).Should(Say(`OK\n`))
    32  	Eventually(session).Should(Say(`Assigning role SpaceManager to user %s in org %s / space %s as %s\.\.\.`, user, orgName, spaceName, user))
    33  	Eventually(session).Should(Say(`OK\n`))
    34  	Eventually(session).Should(Say(`Assigning role SpaceDeveloper to user %s in org %s / space %s as %s\.\.\.`, user, orgName, spaceName, user))
    35  	Eventually(session).Should(Say(`OK\n\n`))
    36  	Eventually(session).Should(Say(`TIP: Use 'cf target -o "%s" -s "%s"' to target new space`, orgName, spaceName))
    37  	Eventually(session).Should(Exit(0))
    38  }
    39  
    40  var _ = Describe("create-space", func() {
    41  	var spaceName string
    42  
    43  	When("invoked with --help", func() {
    44  		It("displays the help information", func() {
    45  			session := helpers.CF("create-space", "--help")
    46  			expectHelpText(session)
    47  			Eventually(session).Should(Exit(0))
    48  		})
    49  	})
    50  
    51  	When("invoked with no arguments", func() {
    52  		It("shows an error and the help text", func() {
    53  			session := helpers.CF("create-space")
    54  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `SPACE` was not provided"))
    55  			expectHelpText(session)
    56  			Eventually(session).Should(Exit(1))
    57  		})
    58  	})
    59  
    60  	When("the environment is not set up correctly", func() {
    61  		It("fails with the appropriate errors", func() {
    62  			helpers.CheckEnvironmentTargetedCorrectly(false, false, "", "create-space", "some-space")
    63  		})
    64  	})
    65  
    66  	BeforeEach(func() {
    67  		spaceName = helpers.NewSpaceName()
    68  	})
    69  
    70  	When("logged in as a client", func() {
    71  		var client, orgName string
    72  
    73  		BeforeEach(func() {
    74  			client = helpers.LoginCFWithClientCredentials()
    75  			orgName = helpers.CreateAndTargetOrg()
    76  		})
    77  
    78  		It("successfully creates a space and assigns roles to the client", func() {
    79  			session := helpers.CF("create-space", spaceName)
    80  			expectSuccessTextAndExitCode(session, client, orgName, spaceName)
    81  
    82  			session = helpers.CF("space", spaceName, "--guid")
    83  			Eventually(session).Should(Exit(0))
    84  			session = helpers.CF("space-users", orgName, spaceName)
    85  
    86  			Eventually(session).Should(Say("SPACE MANAGER"))
    87  			Eventually(session).Should(Say(`\s+%s \(client\)`, client))
    88  			Eventually(session).Should(Say("SPACE DEVELOPER"))
    89  			Eventually(session).Should(Say(`\s+%s \(client\)`, client))
    90  		})
    91  	})
    92  
    93  	When("logged in as a user", func() {
    94  		var user, orgName string
    95  
    96  		BeforeEach(func() {
    97  			user = helpers.LoginCF()
    98  			orgName = helpers.CreateAndTargetOrg()
    99  		})
   100  
   101  		When("the space already exists", func() {
   102  			BeforeEach(func() {
   103  				session := helpers.CF("create-space", spaceName)
   104  				Eventually(session).Should(Exit(0))
   105  			})
   106  
   107  			It("warns the user that the space already exists", func() {
   108  				session := helpers.CF("create-space", spaceName)
   109  				Eventually(session).Should(Say(`Creating space %s in org %s as %s\.\.\.`, spaceName, orgName, user))
   110  				Eventually(session.Err).Should(Say(`Space %s already exists`, spaceName))
   111  				Eventually(session).Should(Say(`OK\n`))
   112  				Eventually(session).Should(Exit(0))
   113  			})
   114  		})
   115  
   116  		When("the space does not exist yet", func() {
   117  			When("a quota is not specified", func() {
   118  				It("creates the space in the targeted org", func() {
   119  					session := helpers.CF("create-space", spaceName)
   120  					expectSuccessTextAndExitCode(session, user, orgName, spaceName)
   121  
   122  					session = helpers.CF("space", spaceName)
   123  					Eventually(session).Should(Say(`name:\s+%s`, spaceName))
   124  					Eventually(session).Should(Say(`org:\s+%s`, orgName))
   125  					Eventually(session).Should(Exit(0))
   126  				})
   127  
   128  				It("makes the user a space manager and space developer", func() {
   129  					session := helpers.CF("create-space", spaceName)
   130  					expectSuccessTextAndExitCode(session, user, orgName, spaceName)
   131  
   132  					session = helpers.CF("space-users", orgName, spaceName)
   133  					Eventually(session).Should(Say(`SPACE MANAGER\n\s+%s`, user))
   134  					Eventually(session).Should(Say(`SPACE DEVELOPER\n\s+%s`, user))
   135  					Eventually(session).Should(Exit(0))
   136  				})
   137  			})
   138  
   139  			When("quota is specified", func() {
   140  				var (
   141  					quotaName string
   142  					session   *Session
   143  				)
   144  
   145  				When("the quota exists", func() {
   146  					BeforeEach(func() {
   147  						quotaName = helpers.QuotaName()
   148  						quotaSession := helpers.CF("create-space-quota", quotaName)
   149  						Eventually(quotaSession).Should(Exit(0))
   150  					})
   151  
   152  					It("creates the space with the provided quota", func() {
   153  						session = helpers.CF("create-space", spaceName, "-q", quotaName)
   154  						expectSuccessTextAndExitCode(session, user, orgName, spaceName)
   155  						session = helpers.CF("space", spaceName)
   156  						Eventually(session).Should(Say(`name:\s+%s`, spaceName))
   157  						Eventually(session).Should(Say(`space quota:\s+%s`, quotaName))
   158  						Eventually(session).Should(Exit(0))
   159  					})
   160  				})
   161  
   162  				When("the quota does not exist", func() {
   163  					BeforeEach(func() {
   164  						quotaName = "no-such-quota"
   165  						session = helpers.CF("create-space", spaceName, "-q", quotaName)
   166  					})
   167  
   168  					It("fails with an error", func() {
   169  						Eventually(session).Should(Say(`Creating space %s in org %s as %s\.\.\.`, spaceName, orgName, user))
   170  						Eventually(session.Err).Should(Say(`Quota no-such-quota not found`))
   171  						Eventually(session).Should(Say(`FAILED\n`))
   172  						Eventually(session).Should(Exit(1))
   173  					})
   174  
   175  					It("does not create the space", func() {
   176  						Eventually(helpers.CF("space", spaceName)).Should(Exit(1))
   177  					})
   178  				})
   179  			})
   180  
   181  			When("org is specified", func() {
   182  				var session *Session
   183  
   184  				When("the org exists", func() {
   185  					BeforeEach(func() {
   186  						orgName = helpers.NewOrgName()
   187  						orgSession := helpers.CF("create-org", orgName)
   188  						Eventually(orgSession).Should(Exit(0))
   189  					})
   190  
   191  					It("creates the space in the specified org", func() {
   192  						session = helpers.CF("create-space", spaceName, "-o", orgName)
   193  						expectSuccessTextAndExitCode(session, user, orgName, spaceName)
   194  
   195  						helpers.TargetOrg(orgName)
   196  						session = helpers.CF("space", spaceName)
   197  						Eventually(session).Should(Say(`name:\s+%s`, spaceName))
   198  						Eventually(session).Should(Say(`org:\s+%s`, orgName))
   199  						Eventually(session).Should(Exit(0))
   200  					})
   201  				})
   202  
   203  				When("the org does not exist", func() {
   204  					BeforeEach(func() {
   205  						orgName = "no-such-org"
   206  					})
   207  
   208  					It("fails with an error and does not create the space", func() {
   209  						session = helpers.CF("create-space", spaceName, "-o", orgName)
   210  						Eventually(session).Should(Say(`Creating space %s in org %s as %s\.\.\.`, spaceName, orgName, user))
   211  						Eventually(session.Err).Should(Say(`Organization '%s' not found`, orgName))
   212  						Eventually(session).Should(Say(`FAILED\n`))
   213  						Eventually(session).Should(Exit(1))
   214  
   215  						Eventually(helpers.CF("space", spaceName)).Should(Exit(1))
   216  					})
   217  				})
   218  			})
   219  
   220  			When("the user is not authorized to create a space", func() {
   221  				var user string
   222  
   223  				BeforeEach(func() {
   224  					user = helpers.SwitchToOrgRole(orgName, "OrgAuditor")
   225  				})
   226  
   227  				AfterEach(func() {
   228  					helpers.ClearTarget()
   229  					Expect(user).To(MatchRegexp(`^INTEGRATION-USER-[\da-f-]+$`))
   230  					helpers.DeleteUser(user)
   231  				})
   232  
   233  				It("fails with an error telling the user that they are not authorized", func() {
   234  					session := helpers.CF("create-space", spaceName, "-o", orgName)
   235  					Eventually(session).Should(Say(`Creating space %s in org %s as %s\.\.\.`, spaceName, orgName, user))
   236  					Eventually(session.Err).Should(Say(`You are not authorized to perform the requested action`))
   237  					Eventually(session).Should(Say(`FAILED\n`))
   238  					Eventually(session).Should(Exit(1))
   239  				})
   240  			})
   241  		})
   242  	})
   243  })