github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/integration/shared/isolated/create_shared_domain_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     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  var _ = Describe("create-shared-domain command", func() {
    15  	Context("Help", func() {
    16  		It("displays the help information", func() {
    17  			session := helpers.CF("create-shared-domain", "--help")
    18  			Eventually(session).Should(Say("NAME:\n"))
    19  			Eventually(session).Should(Say(regexp.QuoteMeta("create-shared-domain - Create a domain that can be used by all orgs (admin-only)")))
    20  			Eventually(session).Should(Say("USAGE:\n"))
    21  			Eventually(session).Should(Say(regexp.QuoteMeta("cf create-shared-domain DOMAIN [--router-group ROUTER_GROUP]")))
    22  			Eventually(session).Should(Say("OPTIONS:\n"))
    23  			Eventually(session).Should(Say("--router-group\\s+Routes for this domain will be configured only on the specified router group"))
    24  			Eventually(session).Should(Say("SEE ALSO:\n"))
    25  			Eventually(session).Should(Say("create-domain, domains, router-groups"))
    26  			Eventually(session).Should(Exit(0))
    27  		})
    28  	})
    29  
    30  	When("user is logged in as admin", func() {
    31  		var (
    32  			orgName    string
    33  			spaceName  string
    34  			domainName string
    35  		)
    36  
    37  		BeforeEach(func() {
    38  			orgName = helpers.NewOrgName()
    39  			spaceName = helpers.NewSpaceName()
    40  			helpers.SetupCF(orgName, spaceName)
    41  			domainName = helpers.NewDomainName()
    42  		})
    43  
    44  		AfterEach(func() {
    45  			session := helpers.CF("delete-shared-domain", domainName, "-f")
    46  			Eventually(session).Should(Exit(0))
    47  			helpers.QuickDeleteOrg(orgName)
    48  		})
    49  
    50  		When("No optional flags are specified", func() {
    51  			When("domain name is valid", func() {
    52  				It("should create the shared domain", func() {
    53  					session := helpers.CF("create-shared-domain", domainName)
    54  
    55  					Eventually(session).Should(Say("Creating shared domain %s as admin...", domainName))
    56  					Eventually(session).Should(Say("OK"))
    57  					Eventually(session).Should(Exit(0))
    58  
    59  					session = helpers.CF("domains")
    60  					Eventually(session).Should(Say("%s\\s+shared", domainName))
    61  				})
    62  			})
    63  
    64  			When("domain name is invalid", func() {
    65  				BeforeEach(func() {
    66  					domainName = "invalid-domain-name%*$$#)*" + helpers.RandomName()
    67  				})
    68  
    69  				It("should fail and return an error", func() {
    70  					session := helpers.CF("create-shared-domain", domainName)
    71  
    72  					Eventually(session).Should(Say("Creating shared domain %s as admin...", regexp.QuoteMeta(domainName)))
    73  					Eventually(session).Should(Say("FAILED"))
    74  					Eventually(session).Should(Say(regexp.QuoteMeta("Server error, status code: 400, error code: 130001, message: The domain is invalid: name can contain multiple subdomains, each having only alphanumeric characters and hyphens of up to 63 characters, see RFC 1035.")))
    75  					Eventually(session).Should(Exit(1))
    76  				})
    77  			})
    78  
    79  			When("domain name is already taken", func() {
    80  				BeforeEach(func() {
    81  					session := helpers.CF("create-shared-domain", domainName)
    82  					Eventually(session).Should(Exit(0))
    83  				})
    84  
    85  				It("should fail and return an error", func() {
    86  					session := helpers.CF("create-shared-domain", domainName)
    87  					Eventually(session).Should(Say("Creating shared domain %s as admin...", domainName))
    88  					Eventually(session).Should(Say("FAILED"))
    89  					Eventually(session).Should(Say("Server error, status code: 400, error code: 130003, message: The domain name is taken: %s", domainName))
    90  					Eventually(session).Should(Exit(1))
    91  				})
    92  			})
    93  		})
    94  
    95  		When("With the --router-group flag", func() {
    96  			var routerGroupName string
    97  
    98  			BeforeEach(func() {
    99  				var response struct {
   100  					RoutingEndpoint string `json:"routing_endpoint"`
   101  				}
   102  				helpers.Curl(&response, "/v2/info")
   103  
   104  				// TODO: #161159794 remove this skip and check a nicer error message when available
   105  				if response.RoutingEndpoint == "" {
   106  					Skip("Test requires routing endpoint on /v2/info")
   107  				}
   108  			})
   109  
   110  			When("router-group exists", func() {
   111  				BeforeEach(func() {
   112  					routerGroupName = helpers.FindOrCreateTCPRouterGroup(GinkgoParallelNode())
   113  				})
   114  
   115  				It("should create a new shared domain", func() {
   116  					session := helpers.CF("create-shared-domain", domainName, "--router-group", routerGroupName)
   117  
   118  					Eventually(session).Should(Say("Creating shared domain %s as admin...", domainName))
   119  					Eventually(session).Should(Say("OK"))
   120  					Eventually(session).Should(Exit(0))
   121  
   122  					session = helpers.CF("domains")
   123  					Eventually(session).Should(Say("%s\\s+shared", domainName))
   124  
   125  					var sharedDomainResponse struct {
   126  						Resources []struct {
   127  							Entity struct {
   128  								RouterGroupGUID string `json:"router_group_guid"`
   129  							}
   130  						}
   131  					}
   132  
   133  					helpers.Curl(&sharedDomainResponse, "/v2/shared_domains?q=name:%s", domainName)
   134  					Expect(sharedDomainResponse.Resources).To(HaveLen(1))
   135  					currentRouterGroupGUID := sharedDomainResponse.Resources[0].Entity.RouterGroupGUID
   136  
   137  					var routerGroupListResponse []struct{ GUID string }
   138  
   139  					helpers.Curl(&routerGroupListResponse, "/routing/v1/router_groups?name=%s", routerGroupName)
   140  					Expect(routerGroupListResponse).To(HaveLen(1))
   141  					expectedRouterGroupGUID := routerGroupListResponse[0].GUID
   142  					Expect(currentRouterGroupGUID).Should(Equal(expectedRouterGroupGUID))
   143  				})
   144  
   145  			})
   146  
   147  			When("router-group does not exist", func() {
   148  				BeforeEach(func() {
   149  					routerGroupName = "not-a-real-router-group"
   150  					session := helpers.CF("router-groups")
   151  					Consistently(session).ShouldNot(Say(routerGroupName))
   152  					Eventually(session).Should(Exit(0))
   153  				})
   154  
   155  				It("should fail and return an error", func() {
   156  					session := helpers.CF("create-shared-domain", domainName, "--router-group", routerGroupName)
   157  					Eventually(session).Should(Say("FAILED"))
   158  					Eventually(session).Should(Say("Router group not-a-real-router-group not found"))
   159  					Eventually(session).Should(Exit(1))
   160  				})
   161  			})
   162  		})
   163  	})
   164  
   165  	When("user is not logged in as admin", func() {
   166  		var (
   167  			username string
   168  			password string
   169  		)
   170  
   171  		BeforeEach(func() {
   172  			helpers.LoginCF()
   173  			username, password = helpers.CreateUser()
   174  			helpers.LoginAs(username, password)
   175  		})
   176  
   177  		It("should not be able to create shared domain", func() {
   178  			session := helpers.CF("create-shared-domain", "some-domain-name.com")
   179  			Eventually(session).Should(Say(fmt.Sprintf("Creating shared domain some-domain-name.com as %s...", username)))
   180  			Eventually(session).Should(Say("FAILED"))
   181  			Eventually(session).Should(Say("Server error, status code: 403, error code: 10003, message: You are not authorized to perform the requested action"))
   182  			Eventually(session).Should(Exit(1))
   183  		})
   184  	})
   185  })