github.com/loafoe/cli@v7.1.0+incompatible/command/v7/create_security_group_command_test.go (about) 1 package v7_test 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 8 "code.cloudfoundry.org/cli/actor/actionerror" 9 "code.cloudfoundry.org/cli/actor/v7action" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 11 "code.cloudfoundry.org/cli/command/commandfakes" 12 "code.cloudfoundry.org/cli/command/flag" 13 v7 "code.cloudfoundry.org/cli/command/v7" 14 "code.cloudfoundry.org/cli/command/v7/v7fakes" 15 "code.cloudfoundry.org/cli/util/configv3" 16 "code.cloudfoundry.org/cli/util/ui" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 . "github.com/onsi/gomega/gbytes" 20 ) 21 22 var _ = Describe("create-security-group Command", func() { 23 var ( 24 cmd v7.CreateSecurityGroupCommand 25 testUI *ui.UI 26 fakeConfig *commandfakes.FakeConfig 27 fakeSharedActor *commandfakes.FakeSharedActor 28 fakeActor *v7fakes.FakeActor 29 30 binaryName string 31 executeErr error 32 securityGroupName string 33 securityGroupFilePath flag.PathWithExistenceCheck 34 ) 35 36 BeforeEach(func() { 37 securityGroupName = "some-name" 38 securityGroupFilePath = "some-path" 39 binaryName = "faceman" 40 fakeConfig = new(commandfakes.FakeConfig) 41 fakeSharedActor = new(commandfakes.FakeSharedActor) 42 fakeActor = new(v7fakes.FakeActor) 43 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 44 45 cmd = v7.CreateSecurityGroupCommand{ 46 BaseCommand: v7.BaseCommand{ 47 UI: testUI, 48 Config: fakeConfig, 49 SharedActor: fakeSharedActor, 50 Actor: fakeActor, 51 }, 52 RequiredArgs: flag.SecurityGroupArgs{ 53 SecurityGroup: securityGroupName, 54 PathToJSONRules: securityGroupFilePath, 55 }, 56 } 57 fakeConfig.HasTargetedOrganizationReturns(false) 58 fakeConfig.HasTargetedSpaceReturns(false) 59 fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil) 60 }) 61 62 JustBeforeEach(func() { 63 executeErr = cmd.Execute(nil) 64 }) 65 66 When("checking target fails", func() { 67 BeforeEach(func() { 68 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 69 }) 70 It("returns an error", func() { 71 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 72 73 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 74 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 75 Expect(checkTargetedOrg).To(BeFalse()) 76 Expect(checkTargetedSpace).To(BeFalse()) 77 }) 78 }) 79 80 When("the provided JSON is invalid", func() { 81 BeforeEach(func() { 82 fakeActor.CreateSecurityGroupReturns( 83 v7action.Warnings{"create-security-group-warning"}, 84 &json.SyntaxError{}) 85 }) 86 87 It("returns a custom error and provides an example", func() { 88 Expect(executeErr).To(HaveOccurred()) 89 Expect(executeErr).To(Equal(actionerror.SecurityGroupJsonSyntaxError{Path: "some-path"})) 90 Expect(testUI.Err).To(Say("create-security-group-warning")) 91 }) 92 }) 93 94 When("the provided JSON does not contain the required fields", func() { 95 BeforeEach(func() { 96 fakeActor.CreateSecurityGroupReturns( 97 v7action.Warnings{"create-security-group-warning"}, 98 &json.UnmarshalTypeError{}) 99 }) 100 101 It("returns a custom error and provides an example", func() { 102 Expect(executeErr).To(HaveOccurred()) 103 Expect(executeErr).To(Equal(actionerror.SecurityGroupJsonSyntaxError{Path: "some-path"})) 104 Expect(testUI.Err).To(Say("create-security-group-warning")) 105 }) 106 }) 107 108 When("the security group already exists", func() { 109 BeforeEach(func() { 110 fakeActor.CreateSecurityGroupReturns( 111 v7action.Warnings{}, 112 ccerror.SecurityGroupAlreadyExists{Message: fmt.Sprintf("Security group with name '%s' already exists", securityGroupName)}) 113 }) 114 115 It("returns a custom error and provides an example", func() { 116 Expect(executeErr).ToNot(HaveOccurred()) 117 Expect(testUI.Err).To(Say("Security group with name '%s' already exists", securityGroupName)) 118 Expect(testUI.Out).To(Say("OK")) 119 }) 120 }) 121 122 When("it can't create the security group", func() { 123 BeforeEach(func() { 124 fakeActor.CreateSecurityGroupReturns( 125 v7action.Warnings{"create-sec-grp-warning"}, 126 errors.New("some-error")) 127 }) 128 129 It("returns the error", func() { 130 Expect(executeErr).To(HaveOccurred()) 131 132 Expect(testUI.Err).To(Say("create-sec-grp-warning")) 133 }) 134 }) 135 })