github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/securitygroup/security_groups_test.go (about)

     1  package securitygroup_test
     2  
     3  import (
     4  	fakeSecurityGroup "github.com/cloudfoundry/cli/cf/api/security_groups/fakes"
     5  	"github.com/cloudfoundry/cli/cf/command_registry"
     6  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     7  	"github.com/cloudfoundry/cli/cf/errors"
     8  	"github.com/cloudfoundry/cli/cf/models"
     9  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    10  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    11  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    12  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    13  
    14  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("list-security-groups command", func() {
    20  	var (
    21  		ui                  *testterm.FakeUI
    22  		repo                *fakeSecurityGroup.FakeSecurityGroupRepo
    23  		requirementsFactory *testreq.FakeReqFactory
    24  		configRepo          core_config.Repository
    25  		deps                command_registry.Dependency
    26  	)
    27  
    28  	updateCommandDependency := func(pluginCall bool) {
    29  		deps.Ui = ui
    30  		deps.RepoLocator = deps.RepoLocator.SetSecurityGroupRepository(repo)
    31  		deps.Config = configRepo
    32  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("security-groups").SetDependency(deps, pluginCall))
    33  	}
    34  
    35  	BeforeEach(func() {
    36  		ui = &testterm.FakeUI{}
    37  		requirementsFactory = &testreq.FakeReqFactory{}
    38  		repo = &fakeSecurityGroup.FakeSecurityGroupRepo{}
    39  		configRepo = testconfig.NewRepositoryWithDefaults()
    40  	})
    41  
    42  	runCommand := func(args ...string) bool {
    43  		return testcmd.RunCliCommand("security-groups", args, requirementsFactory, updateCommandDependency, false)
    44  	}
    45  
    46  	Describe("requirements", func() {
    47  		It("should fail if not logged in", func() {
    48  			Expect(runCommand()).To(BeFalse())
    49  		})
    50  
    51  		It("should fail with usage when provided any arguments", func() {
    52  			requirementsFactory.LoginSuccess = true
    53  			runCommand("why am I typing here")
    54  			Expect(ui.Outputs).To(ContainSubstrings(
    55  				[]string{"Incorrect Usage", "No argument"},
    56  			))
    57  		})
    58  	})
    59  
    60  	Context("when logged in", func() {
    61  		BeforeEach(func() {
    62  			requirementsFactory.LoginSuccess = true
    63  		})
    64  
    65  		It("tells the user what it's about to do", func() {
    66  			runCommand()
    67  			Expect(ui.Outputs).To(ContainSubstrings(
    68  				[]string{"Getting", "security groups", "my-user"},
    69  			))
    70  		})
    71  
    72  		It("handles api errors with an error message", func() {
    73  			repo.FindAllReturns([]models.SecurityGroup{}, errors.New("YO YO YO, ERROR YO"))
    74  
    75  			runCommand()
    76  			Expect(ui.Outputs).To(ContainSubstrings(
    77  				[]string{"FAILED"},
    78  			))
    79  		})
    80  
    81  		Context("when there are no security groups", func() {
    82  			It("Should tell the user that there are no security groups", func() {
    83  				repo.FindAllReturns([]models.SecurityGroup{}, nil)
    84  
    85  				runCommand()
    86  				Expect(ui.Outputs).To(ContainSubstrings([]string{"No security groups"}))
    87  			})
    88  		})
    89  
    90  		Context("when there is at least one security group", func() {
    91  			BeforeEach(func() {
    92  				securityGroup := models.SecurityGroup{}
    93  				securityGroup.Name = "my-group"
    94  				securityGroup.Guid = "group-guid"
    95  
    96  				repo.FindAllReturns([]models.SecurityGroup{securityGroup}, nil)
    97  			})
    98  
    99  			Describe("Where there are spaces assigned", func() {
   100  				BeforeEach(func() {
   101  					securityGroups := []models.SecurityGroup{
   102  						{
   103  							SecurityGroupFields: models.SecurityGroupFields{
   104  								Name: "my-group",
   105  								Guid: "group-guid",
   106  							},
   107  							Spaces: []models.Space{
   108  								{
   109  									SpaceFields:  models.SpaceFields{Guid: "my-space-guid-1", Name: "space-1"},
   110  									Organization: models.OrganizationFields{Guid: "my-org-guid-1", Name: "org-1"},
   111  								},
   112  								{
   113  									SpaceFields:  models.SpaceFields{Guid: "my-space-guid", Name: "space-2"},
   114  									Organization: models.OrganizationFields{Guid: "my-org-guid-2", Name: "org-2"},
   115  								},
   116  							},
   117  						},
   118  					}
   119  
   120  					repo.FindAllReturns(securityGroups, nil)
   121  				})
   122  
   123  				It("lists out the security group's: name, organization and space", func() {
   124  					runCommand()
   125  					Expect(ui.Outputs).To(ContainSubstrings(
   126  						[]string{"Getting", "security group", "my-user"},
   127  						[]string{"OK"},
   128  						[]string{"#0", "my-group", "org-1", "space-1"},
   129  					))
   130  
   131  					//If there is a panic in this test, it is likely due to the following
   132  					//Expectation to be false
   133  					Expect(ui.Outputs).ToNot(ContainSubstrings(
   134  						[]string{"#0", "my-group", "org-2", "space-2"},
   135  					))
   136  				})
   137  			})
   138  
   139  			Describe("Where there are no spaces assigned", func() {
   140  				It("lists out the security group's: name", func() {
   141  					runCommand()
   142  					Expect(ui.Outputs).To(ContainSubstrings(
   143  						[]string{"Getting", "security group", "my-user"},
   144  						[]string{"OK"},
   145  						[]string{"#0", "my-group"},
   146  					))
   147  				})
   148  			})
   149  		})
   150  	})
   151  })