github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/securitygroup/create_security_group_test.go (about)

     1  package securitygroup_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api/securitygroups/securitygroupsfakes"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/errors"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    13  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    14  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    15  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    16  
    17  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("create-security-group command", func() {
    23  	var (
    24  		ui                  *testterm.FakeUI
    25  		securityGroupRepo   *securitygroupsfakes.FakeSecurityGroupRepo
    26  		requirementsFactory *requirementsfakes.FakeFactory
    27  		configRepo          coreconfig.Repository
    28  		deps                commandregistry.Dependency
    29  	)
    30  
    31  	updateCommandDependency := func(pluginCall bool) {
    32  		deps.UI = ui
    33  		deps.RepoLocator = deps.RepoLocator.SetSecurityGroupRepository(securityGroupRepo)
    34  		deps.Config = configRepo
    35  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("create-security-group").SetDependency(deps, pluginCall))
    36  	}
    37  
    38  	BeforeEach(func() {
    39  		ui = &testterm.FakeUI{}
    40  		requirementsFactory = new(requirementsfakes.FakeFactory)
    41  		securityGroupRepo = new(securitygroupsfakes.FakeSecurityGroupRepo)
    42  		configRepo = testconfig.NewRepositoryWithDefaults()
    43  	})
    44  
    45  	runCommand := func(args ...string) bool {
    46  		return testcmd.RunCLICommand("create-security-group", args, requirementsFactory, updateCommandDependency, false, ui)
    47  	}
    48  
    49  	Describe("requirements", func() {
    50  		It("fails when the user is not logged in", func() {
    51  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    52  			Expect(runCommand("the-security-group")).To(BeFalse())
    53  		})
    54  
    55  		It("fails with usage when a name is not provided", func() {
    56  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    57  			runCommand()
    58  			Expect(ui.Outputs()).To(ContainSubstrings(
    59  				[]string{"Incorrect Usage", "Requires", "arguments"},
    60  			))
    61  		})
    62  
    63  		It("fails with usage when a rules file is not provided", func() {
    64  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    65  			runCommand("AWESOME_SECURITY_GROUP_NAME")
    66  			Expect(ui.Outputs()).To(ContainSubstrings(
    67  				[]string{"Incorrect Usage", "Requires", "arguments"},
    68  			))
    69  		})
    70  	})
    71  
    72  	Context("when the user is logged in", func() {
    73  		var tempFile *os.File
    74  
    75  		BeforeEach(func() {
    76  			tempFile, _ = ioutil.TempFile("", "")
    77  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    78  		})
    79  
    80  		AfterEach(func() {
    81  			tempFile.Close()
    82  			os.Remove(tempFile.Name())
    83  		})
    84  
    85  		JustBeforeEach(func() {
    86  			runCommand("my-group", tempFile.Name())
    87  		})
    88  
    89  		Context("when the file specified has valid json", func() {
    90  			BeforeEach(func() {
    91  				tempFile.Write([]byte(`[{"protocol":"udp","ports":"8080-9090","destination":"198.41.191.47/1"}]`))
    92  			})
    93  
    94  			It("displays a message describing what its going to do", func() {
    95  				Expect(ui.Outputs()).To(ContainSubstrings(
    96  					[]string{"Creating security group", "my-group", "my-user"},
    97  					[]string{"OK"},
    98  				))
    99  			})
   100  
   101  			It("creates the security group with those rules", func() {
   102  				_, rules := securityGroupRepo.CreateArgsForCall(0)
   103  				Expect(rules).To(Equal([]map[string]interface{}{
   104  					{"protocol": "udp", "ports": "8080-9090", "destination": "198.41.191.47/1"},
   105  				}))
   106  			})
   107  
   108  			Context("when the API returns an error", func() {
   109  				Context("some sort of awful terrible error that we were not prescient enough to anticipate", func() {
   110  					BeforeEach(func() {
   111  						securityGroupRepo.CreateReturns(errors.New("Wops I failed"))
   112  					})
   113  
   114  					It("fails loudly", func() {
   115  						Expect(ui.Outputs()).To(ContainSubstrings(
   116  							[]string{"Creating security group", "my-group"},
   117  							[]string{"FAILED"},
   118  						))
   119  					})
   120  				})
   121  
   122  				Context("when the group already exists", func() {
   123  					BeforeEach(func() {
   124  						securityGroupRepo.CreateReturns(errors.NewHTTPError(400, errors.SecurityGroupNameTaken, "The security group is taken: my-group"))
   125  					})
   126  
   127  					It("warns the user when group already exists", func() {
   128  						Expect(ui.Outputs()).ToNot(ContainSubstrings([]string{"FAILED"}))
   129  						Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"already exists"}))
   130  					})
   131  				})
   132  			})
   133  		})
   134  
   135  		Context("when the file specified has invalid json", func() {
   136  			BeforeEach(func() {
   137  				tempFile.Write([]byte(`[{noquote: thiswontwork}]`))
   138  			})
   139  
   140  			It("freaks out", func() {
   141  				Expect(ui.Outputs()).To(ContainSubstrings(
   142  					[]string{"FAILED"},
   143  					[]string{"Incorrect json format: file:", tempFile.Name()},
   144  					[]string{"Valid json file exampl"},
   145  				))
   146  			})
   147  		})
   148  	})
   149  })