github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/securitygroup/delete_security_group_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/configuration/core_config"
     6  	"github.com/cloudfoundry/cli/cf/errors"
     7  	"github.com/cloudfoundry/cli/cf/models"
     8  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     9  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    10  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    11  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    12  
    13  	. "github.com/cloudfoundry/cli/cf/commands/securitygroup"
    14  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("delete-security-group command", func() {
    20  	var (
    21  		ui                  *testterm.FakeUI
    22  		securityGroupRepo   *fakeSecurityGroup.FakeSecurityGroupRepo
    23  		requirementsFactory *testreq.FakeReqFactory
    24  		configRepo          core_config.ReadWriter
    25  	)
    26  
    27  	BeforeEach(func() {
    28  		ui = &testterm.FakeUI{}
    29  		requirementsFactory = &testreq.FakeReqFactory{}
    30  		securityGroupRepo = &fakeSecurityGroup.FakeSecurityGroupRepo{}
    31  		configRepo = testconfig.NewRepositoryWithDefaults()
    32  	})
    33  
    34  	runCommand := func(args ...string) bool {
    35  		cmd := NewDeleteSecurityGroup(ui, configRepo, securityGroupRepo)
    36  		return testcmd.RunCommand(cmd, args, requirementsFactory)
    37  	}
    38  
    39  	Describe("requirements", func() {
    40  		It("should fail if not logged in", func() {
    41  			Expect(runCommand("my-group")).To(BeFalse())
    42  		})
    43  
    44  		It("should fail with usage when not provided a single argument", func() {
    45  			requirementsFactory.LoginSuccess = true
    46  			runCommand("whoops", "I can't believe", "I accidentally", "the whole thing")
    47  			Expect(ui.FailedWithUsage).To(BeTrue())
    48  		})
    49  	})
    50  
    51  	Context("when logged in", func() {
    52  		BeforeEach(func() {
    53  			requirementsFactory.LoginSuccess = true
    54  		})
    55  
    56  		Context("when the group with the given name exists", func() {
    57  			BeforeEach(func() {
    58  				securityGroupRepo.ReadReturns(models.SecurityGroup{
    59  					SecurityGroupFields: models.SecurityGroupFields{
    60  						Name: "my-group",
    61  						Guid: "group-guid",
    62  					},
    63  				}, nil)
    64  			})
    65  
    66  			Context("delete a security group", func() {
    67  				It("when passed the -f flag", func() {
    68  					runCommand("-f", "my-group")
    69  					Expect(securityGroupRepo.ReadArgsForCall(0)).To(Equal("my-group"))
    70  					Expect(securityGroupRepo.DeleteArgsForCall(0)).To(Equal("group-guid"))
    71  
    72  					Expect(ui.Prompts).To(BeEmpty())
    73  				})
    74  
    75  				It("should prompt user when -f flag is not present", func() {
    76  					ui.Inputs = []string{"y"}
    77  
    78  					runCommand("my-group")
    79  					Expect(securityGroupRepo.ReadArgsForCall(0)).To(Equal("my-group"))
    80  					Expect(securityGroupRepo.DeleteArgsForCall(0)).To(Equal("group-guid"))
    81  
    82  					Expect(ui.Prompts).To(ContainSubstrings(
    83  						[]string{"Really delete the security group", "my-group"},
    84  					))
    85  				})
    86  
    87  				It("should not delete when user passes 'n' to prompt", func() {
    88  					ui.Inputs = []string{"n"}
    89  
    90  					runCommand("my-group")
    91  					Expect(securityGroupRepo.ReadCallCount()).To(Equal(0))
    92  					Expect(securityGroupRepo.DeleteCallCount()).To(Equal(0))
    93  
    94  					Expect(ui.Prompts).To(ContainSubstrings(
    95  						[]string{"Really delete the security group", "my-group"},
    96  					))
    97  				})
    98  			})
    99  
   100  			It("tells the user what it's about to do", func() {
   101  				runCommand("-f", "my-group")
   102  				Expect(ui.Outputs).To(ContainSubstrings(
   103  					[]string{"Deleting", "security group", "my-group", "my-user"},
   104  					[]string{"OK"},
   105  				))
   106  			})
   107  		})
   108  
   109  		Context("when finding the group returns an error", func() {
   110  			BeforeEach(func() {
   111  				securityGroupRepo.ReadReturns(models.SecurityGroup{}, errors.New("pbbbbbbbbbbt"))
   112  			})
   113  
   114  			It("fails and tells the user", func() {
   115  				runCommand("-f", "whoops")
   116  
   117  				Expect(ui.Outputs).To(ContainSubstrings([]string{"FAILED"}))
   118  			})
   119  		})
   120  
   121  		Context("when a group with that name does not exist", func() {
   122  			BeforeEach(func() {
   123  				securityGroupRepo.ReadReturns(models.SecurityGroup{}, errors.NewModelNotFoundError("Security group", "uh uh uh -- you didn't sahy the magick word"))
   124  			})
   125  
   126  			It("fails and tells the user", func() {
   127  				runCommand("-f", "whoop")
   128  
   129  				Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"whoop", "does not exist"}))
   130  			})
   131  		})
   132  
   133  		It("fails and warns the user if deleting fails", func() {
   134  			securityGroupRepo.DeleteReturns(errors.New("raspberry"))
   135  			runCommand("-f", "whoops")
   136  
   137  			Expect(ui.Outputs).To(ContainSubstrings([]string{"FAILED"}))
   138  		})
   139  	})
   140  })