github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/integration/shared/isolated/create_space_command_test.go (about)

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