github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/integration/isolated/unbind_security_group_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     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  	. "github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("unbind-security-group command", func() {
    16  	var (
    17  		orgName           string
    18  		securityGroupName string
    19  		spaceName         string
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		orgName = helpers.NewOrgName()
    24  		securityGroupName = helpers.NewSecurityGroupName()
    25  		spaceName = helpers.NewSpaceName()
    26  
    27  		helpers.LoginCF()
    28  	})
    29  
    30  	Describe("help", func() {
    31  		Context("when --help flag is set", func() {
    32  			It("Displays command usage to output", func() {
    33  				session := helpers.CF("unbind-security-group", "--help")
    34  				Eventually(session).Should(Say("NAME:"))
    35  				Eventually(session).Should(Say("\\s+unbind-security-group - Unbind a security group from a space"))
    36  				Eventually(session).Should(Say("USAGE:"))
    37  				Eventually(session).Should(Say("\\s+cf unbind-security-group SECURITY_GROUP ORG SPACE \\[--lifecycle \\(running \\| staging\\)\\]"))
    38  				Eventually(session).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\."))
    39  				Eventually(session).Should(Say("OPTIONS:"))
    40  				Eventually(session).Should(Say("\\s+--lifecycle      Lifecycle phase the group applies to \\(Default: running\\)"))
    41  				Eventually(session).Should(Say("SEE ALSO:"))
    42  				Eventually(session).Should(Say("\\s+apps, restart, security-groups"))
    43  				Eventually(session).Should(Exit(0))
    44  			})
    45  		})
    46  	})
    47  
    48  	Context("when the lifecycle flag is invalid", func() {
    49  		It("outputs a message and usage", func() {
    50  			session := helpers.CF("unbind-security-group", securityGroupName, "some-org", "--lifecycle", "invalid")
    51  			Eventually(session.Err).Should(Say("Incorrect Usage: Invalid value `invalid' for option `--lifecycle'. Allowed values are: running or staging"))
    52  			Eventually(session).Should(Say("USAGE:"))
    53  			Eventually(session).Should(Exit(1))
    54  		})
    55  	})
    56  
    57  	Context("when the lifecycle flag has no argument", func() {
    58  		It("outputs a message and usage", func() {
    59  			session := helpers.CF("unbind-security-group", securityGroupName, "some-org", "--lifecycle")
    60  			Eventually(session.Err).Should(Say("Incorrect Usage: expected argument for flag `--lifecycle'"))
    61  			Eventually(session).Should(Say("USAGE:"))
    62  			Eventually(session).Should(Exit(1))
    63  		})
    64  	})
    65  
    66  	Context("when the environment is not setup correctly", func() {
    67  		It("fails with the appropriate errors", func() {
    68  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "unbind-security-group", securityGroupName)
    69  		})
    70  	})
    71  
    72  	Context("when the server's API version is too low", func() {
    73  		var server *Server
    74  
    75  		BeforeEach(func() {
    76  			server = NewTLSServer()
    77  			server.AppendHandlers(
    78  				CombineHandlers(
    79  					VerifyRequest(http.MethodGet, "/v2/info"),
    80  					RespondWith(http.StatusOK, `{"api_version":"2.34.0"}`),
    81  				),
    82  				CombineHandlers(
    83  					VerifyRequest(http.MethodGet, "/v2/info"),
    84  					RespondWith(http.StatusOK, fmt.Sprintf(`{"api_version":"2.34.0", "authorization_endpoint": "%s"}`, server.URL())),
    85  				),
    86  				CombineHandlers(
    87  					VerifyRequest(http.MethodGet, "/login"),
    88  					RespondWith(http.StatusOK, `{}`),
    89  				),
    90  			)
    91  			Eventually(helpers.CF("api", server.URL(), "--skip-ssl-validation")).Should(Exit(0))
    92  		})
    93  
    94  		AfterEach(func() {
    95  			server.Close()
    96  		})
    97  
    98  		It("reports an error with a minimum-version message", func() {
    99  			session := helpers.CF("unbind-security-group", securityGroupName, orgName, spaceName, "--lifecycle", "staging")
   100  
   101  			Eventually(session.Err).Should(Say("Lifecycle value 'staging' requires CF API version 2\\.68\\.0\\ or higher. Your target is 2\\.34\\.0\\."))
   102  			Eventually(session).Should(Exit(1))
   103  		})
   104  	})
   105  
   106  	Context("when the input is invalid", func() {
   107  		Context("when the security group is not provided", func() {
   108  			It("fails with an incorrect usage message and displays help", func() {
   109  				session := helpers.CF("unbind-security-group")
   110  				Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `SECURITY_GROUP` was not provided"))
   111  				Eventually(session).Should(Say("USAGE:"))
   112  				Eventually(session).Should(Exit(1))
   113  			})
   114  		})
   115  
   116  		Context("when the space is not provided", func() {
   117  			It("fails with an incorrect usage message and displays help", func() {
   118  				session := helpers.CF("unbind-security-group", securityGroupName, "some-org")
   119  				Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `SECURITY_GROUP`, `ORG`, and `SPACE` were not provided"))
   120  				Eventually(session).Should(Say("USAGE:"))
   121  				Eventually(session).Should(Exit(1))
   122  			})
   123  		})
   124  	})
   125  
   126  	Context("when the security group doesn't exist", func() {
   127  		BeforeEach(func() {
   128  			helpers.CreateOrgAndSpace(orgName, spaceName)
   129  		})
   130  
   131  		AfterEach(func() {
   132  			helpers.QuickDeleteOrg(orgName)
   133  		})
   134  
   135  		It("fails with a 'security group not found' message", func() {
   136  			session := helpers.CF("unbind-security-group", "some-other-security-group", orgName, spaceName)
   137  			Eventually(session).Should(Say("FAILED"))
   138  			Eventually(session.Err).Should(Say("Security group 'some-other-security-group' not found\\."))
   139  			Eventually(session).Should(Exit(1))
   140  		})
   141  	})
   142  
   143  	Context("when the security group exists", func() {
   144  		BeforeEach(func() {
   145  			someSecurityGroup := helpers.NewSecurityGroup(securityGroupName, "tcp", "127.0.0.1", "8443", "some-description")
   146  			someSecurityGroup.Create()
   147  		})
   148  
   149  		Context("when the org doesn't exist", func() {
   150  			It("fails with an 'org not found' message", func() {
   151  				session := helpers.CF("unbind-security-group", securityGroupName, "some-other-org", "some-other-space")
   152  				Eventually(session).Should(Say("FAILED"))
   153  				Eventually(session.Err).Should(Say("Organization 'some-other-org' not found\\."))
   154  				Eventually(session).Should(Exit(1))
   155  			})
   156  		})
   157  
   158  		Context("when the org exists", func() {
   159  			var username string
   160  
   161  			BeforeEach(func() {
   162  				username, _ = helpers.GetCredentials()
   163  
   164  				helpers.CreateOrg(orgName)
   165  				helpers.TargetOrg(orgName)
   166  			})
   167  
   168  			AfterEach(func() {
   169  				helpers.QuickDeleteOrg(orgName)
   170  			})
   171  
   172  			Context("when the space doesn't exist", func() {
   173  				It("fails with a 'space not found' message", func() {
   174  					session := helpers.CF("unbind-security-group", securityGroupName, orgName, "some-other-space")
   175  					Eventually(session).Should(Say("FAILED"))
   176  					Eventually(session.Err).Should(Say("Space 'some-other-space' not found\\."))
   177  					Eventually(session).Should(Exit(1))
   178  				})
   179  			})
   180  
   181  			Context("when the space exists", func() {
   182  				BeforeEach(func() {
   183  					helpers.CreateSpace(spaceName)
   184  				})
   185  
   186  				Context("when the space isn't bound to the security group in any lifecycle", func() {
   187  					It("successfully runs the command", func() {
   188  						session := helpers.CF("unbind-security-group", securityGroupName, orgName, spaceName)
   189  						Eventually(session).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username))
   190  						Eventually(session).Should(Say("OK"))
   191  						Eventually(session).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\."))
   192  						Eventually(session).Should(Exit(0))
   193  					})
   194  				})
   195  
   196  				Context("when a space is bound to a security group in the running lifecycle", func() {
   197  					BeforeEach(func() {
   198  						Eventually(helpers.CF("bind-security-group", securityGroupName, orgName, spaceName)).Should(Exit(0))
   199  					})
   200  
   201  					Context("when the lifecycle flag is not set", func() {
   202  						Context("when the org and space are not provided", func() {
   203  							BeforeEach(func() {
   204  								helpers.TargetOrgAndSpace(orgName, spaceName)
   205  							})
   206  
   207  							It("successfully unbinds the space from the security group", func() {
   208  								session := helpers.CF("unbind-security-group", securityGroupName)
   209  								Eventually(session).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username))
   210  								Eventually(session).Should(Say("OK"))
   211  								Eventually(session).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\."))
   212  								Eventually(session).Should(Exit(0))
   213  							})
   214  						})
   215  
   216  						Context("when the org and space are provided", func() {
   217  							BeforeEach(func() {
   218  								helpers.ClearTarget()
   219  							})
   220  
   221  							It("successfully unbinds the space from the security group", func() {
   222  								session := helpers.CF("unbind-security-group", securityGroupName, orgName, spaceName)
   223  								Eventually(session).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username))
   224  								Eventually(session).Should(Say("OK"))
   225  								Eventually(session).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\."))
   226  								Eventually(session).Should(Exit(0))
   227  							})
   228  						})
   229  					})
   230  
   231  					Context("when the lifecycle flag is running", func() {
   232  						Context("when the org and space are not provided", func() {
   233  							BeforeEach(func() {
   234  								helpers.TargetOrgAndSpace(orgName, spaceName)
   235  							})
   236  
   237  							It("successfully unbinds the space from the security group", func() {
   238  								session := helpers.CF("unbind-security-group", securityGroupName, "--lifecycle", "running")
   239  								Eventually(session).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username))
   240  								Eventually(session).Should(Say("OK"))
   241  								Eventually(session).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\."))
   242  								Eventually(session).Should(Exit(0))
   243  							})
   244  						})
   245  
   246  						Context("when the org and space are provided", func() {
   247  							BeforeEach(func() {
   248  								helpers.ClearTarget()
   249  							})
   250  
   251  							It("successfully unbinds the space from the security group", func() {
   252  								session := helpers.CF("unbind-security-group", securityGroupName, orgName, spaceName, "--lifecycle", "running")
   253  								Eventually(session).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username))
   254  								Eventually(session).Should(Say("OK"))
   255  								Eventually(session).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\."))
   256  								Eventually(session).Should(Exit(0))
   257  							})
   258  						})
   259  					})
   260  
   261  					Context("when the lifecycle flag is staging", func() {
   262  						Context("when the org and space are not provided", func() {
   263  							BeforeEach(func() {
   264  								helpers.TargetOrgAndSpace(orgName, spaceName)
   265  							})
   266  
   267  							It("displays an error and exits 1", func() {
   268  								session := helpers.CF("unbind-security-group", securityGroupName, "--lifecycle", "staging")
   269  								Eventually(session).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username))
   270  								Eventually(session).Should(Say("OK"))
   271  								Eventually(session.Err).Should(Say("Security group %s not bound to this space for lifecycle phase 'staging'\\.", securityGroupName))
   272  								Eventually(session).Should(Exit(0))
   273  							})
   274  						})
   275  
   276  						Context("when the org and space are provided", func() {
   277  							BeforeEach(func() {
   278  								helpers.ClearTarget()
   279  							})
   280  
   281  							It("displays an error and exits 1", func() {
   282  								session := helpers.CF("unbind-security-group", securityGroupName, orgName, spaceName, "--lifecycle", "staging")
   283  								Eventually(session).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username))
   284  								Eventually(session).Should(Say("OK"))
   285  								Eventually(session.Err).Should(Say("Security group %s not bound to this space for lifecycle phase 'staging'\\.", securityGroupName))
   286  								Eventually(session).Should(Exit(0))
   287  							})
   288  						})
   289  					})
   290  				})
   291  
   292  				Context("when a space is bound to a security group in the staging lifecycle", func() {
   293  					BeforeEach(func() {
   294  						Eventually(helpers.CF("bind-security-group", securityGroupName, orgName, spaceName, "--lifecycle", "staging")).Should(Exit(0))
   295  					})
   296  
   297  					Context("when the lifecycle flag is not set", func() {
   298  						Context("when the org and space are not provided", func() {
   299  							BeforeEach(func() {
   300  								helpers.TargetOrgAndSpace(orgName, spaceName)
   301  							})
   302  
   303  							It("displays an error and exits 1", func() {
   304  								session := helpers.CF("unbind-security-group", securityGroupName)
   305  								Eventually(session).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username))
   306  								Eventually(session).Should(Say("OK"))
   307  								Eventually(session.Err).Should(Say("Security group %s not bound to this space for lifecycle phase 'running'\\.", securityGroupName))
   308  								Eventually(session).Should(Exit(0))
   309  							})
   310  						})
   311  
   312  						Context("when the org and space are provided", func() {
   313  							BeforeEach(func() {
   314  								helpers.ClearTarget()
   315  							})
   316  
   317  							It("displays an error and exits 1", func() {
   318  								session := helpers.CF("unbind-security-group", securityGroupName, orgName, spaceName)
   319  								Eventually(session).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username))
   320  								Eventually(session).Should(Say("OK"))
   321  								Eventually(session.Err).Should(Say("Security group %s not bound to this space for lifecycle phase 'running'\\.", securityGroupName))
   322  								Eventually(session).Should(Exit(0))
   323  							})
   324  						})
   325  					})
   326  
   327  					Context("when the lifecycle flag is running", func() {
   328  						Context("when the org and space are not provided", func() {
   329  							BeforeEach(func() {
   330  								helpers.TargetOrgAndSpace(orgName, spaceName)
   331  							})
   332  
   333  							It("displays an error and exits 1", func() {
   334  								session := helpers.CF("unbind-security-group", securityGroupName, "--lifecycle", "running")
   335  								Eventually(session).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username))
   336  								Eventually(session).Should(Say("OK"))
   337  								Eventually(session.Err).Should(Say("Security group %s not bound to this space for lifecycle phase 'running'\\.", securityGroupName))
   338  								Eventually(session).Should(Exit(0))
   339  							})
   340  						})
   341  
   342  						Context("when the org and space are provided", func() {
   343  							BeforeEach(func() {
   344  								helpers.ClearTarget()
   345  							})
   346  
   347  							It("displays an error and exits 1", func() {
   348  								session := helpers.CF("unbind-security-group", securityGroupName, orgName, spaceName, "--lifecycle", "running")
   349  								Eventually(session).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username))
   350  								Eventually(session).Should(Say("OK"))
   351  								Eventually(session.Err).Should(Say("Security group %s not bound to this space for lifecycle phase 'running'\\.", securityGroupName))
   352  								Eventually(session).Should(Exit(0))
   353  							})
   354  						})
   355  					})
   356  
   357  					Context("when the lifecycle flag is staging", func() {
   358  						Context("when the org and space are not provided", func() {
   359  							BeforeEach(func() {
   360  								helpers.TargetOrgAndSpace(orgName, spaceName)
   361  							})
   362  
   363  							It("successfully unbinds the space from the security group", func() {
   364  								session := helpers.CF("unbind-security-group", securityGroupName, "--lifecycle", "staging")
   365  								Eventually(session).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username))
   366  								Eventually(session).Should(Say("OK"))
   367  								Eventually(session).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\."))
   368  								Eventually(session).Should(Exit(0))
   369  							})
   370  						})
   371  
   372  						Context("when the org and space are provided", func() {
   373  							BeforeEach(func() {
   374  								helpers.ClearTarget()
   375  							})
   376  
   377  							It("successfully unbinds the space from the security group", func() {
   378  								session := helpers.CF("unbind-security-group", securityGroupName, orgName, spaceName, "--lifecycle", "staging")
   379  								Eventually(session).Should(Say("Unbinding security group %s from org %s / space %s as %s\\.\\.\\.", securityGroupName, orgName, spaceName, username))
   380  								Eventually(session).Should(Say("OK"))
   381  								Eventually(session).Should(Say("TIP: Changes require an app restart \\(for running\\) or restage \\(for staging\\) to apply to existing applications\\."))
   382  								Eventually(session).Should(Exit(0))
   383  							})
   384  						})
   385  					})
   386  				})
   387  			})
   388  		})
   389  	})
   390  })