1package policy_test 2 3import ( 4 "errors" 5 6 "github.com/concourse/concourse/atc/policy" 7 "github.com/concourse/concourse/atc/policy/policyfakes" 8 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11) 12 13var _ = Describe("Policy checker", func() { 14 15 var ( 16 checker *policy.Checker 17 filter policy.Filter 18 err error 19 ) 20 21 BeforeEach(func() { 22 filter = policy.Filter{ 23 HttpMethods: []string{"POST", "PUT"}, 24 Actions: []string{"do_1", "do_2"}, 25 ActionsToSkip: []string{"skip_1", "skip_2"}, 26 } 27 28 fakeAgent = new(policyfakes.FakeAgent) 29 fakeAgentFactory.NewAgentReturns(fakeAgent, nil) 30 }) 31 32 JustBeforeEach(func() { 33 checker, err = policy.Initialize(testLogger, "some-cluster", "some-version", filter) 34 }) 35 36 // fakeAgent is configured in BeforeSuite. 37 Context("Initialize", func() { 38 It("new agent should be returned", func() { 39 Expect(fakeAgentFactory.NewAgentCallCount()).To(Equal(1)) 40 }) 41 42 It("should return a checker", func() { 43 Expect(err).ToNot(HaveOccurred()) 44 Expect(checker).ToNot(BeNil()) 45 }) 46 47 Context("Checker", func() { 48 Context("ShouldCheckHttpMethod", func() { 49 It("should return correct result", func() { 50 Expect(checker.ShouldCheckHttpMethod("GET")).To(BeFalse()) 51 Expect(checker.ShouldCheckHttpMethod("DELETE")).To(BeFalse()) 52 Expect(checker.ShouldCheckHttpMethod("PUT")).To(BeTrue()) 53 Expect(checker.ShouldCheckHttpMethod("POST")).To(BeTrue()) 54 }) 55 }) 56 57 Context("ShouldCheckAction", func() { 58 It("should return correct result", func() { 59 Expect(checker.ShouldCheckAction("did_1")).To(BeFalse()) 60 Expect(checker.ShouldCheckAction("did_2")).To(BeFalse()) 61 Expect(checker.ShouldCheckAction("do_1")).To(BeTrue()) 62 Expect(checker.ShouldCheckAction("do_2")).To(BeTrue()) 63 }) 64 }) 65 66 Context("ShouldSkipAction", func() { 67 It("should return correct result", func() { 68 Expect(checker.ShouldSkipAction("did_1")).To(BeFalse()) 69 Expect(checker.ShouldSkipAction("did_2")).To(BeFalse()) 70 Expect(checker.ShouldSkipAction("skip_1")).To(BeTrue()) 71 Expect(checker.ShouldSkipAction("skip_2")).To(BeTrue()) 72 }) 73 }) 74 75 Context("Check", func() { 76 var ( 77 input policy.PolicyCheckInput 78 output policy.PolicyCheckOutput 79 checkErr error 80 ) 81 BeforeEach(func() { 82 input = policy.PolicyCheckInput{} 83 }) 84 JustBeforeEach(func() { 85 output, checkErr = checker.Check(input) 86 }) 87 88 It("agent should be called", func() { 89 Expect(fakeAgent.CheckCallCount()).To(Equal(1)) 90 }) 91 It("cluster name should be injected into input", func() { 92 realInput := fakeAgent.CheckArgsForCall(0) 93 Expect(realInput).To(Equal(policy.PolicyCheckInput{ 94 Service: "concourse", 95 ClusterName: "some-cluster", 96 ClusterVersion: "some-version", 97 })) 98 }) 99 100 Context("when agent says pass", func() { 101 BeforeEach(func() { 102 fakeAgent.CheckReturns(policy.PassedPolicyCheck(), nil) 103 }) 104 105 It("it should pass", func() { 106 Expect(checkErr).ToNot(HaveOccurred()) 107 Expect(output.Allowed).To(BeTrue()) 108 }) 109 }) 110 111 Context("when agent says not-pass", func() { 112 BeforeEach(func() { 113 fakeAgent.CheckReturns(policy.FailedPolicyCheck(), nil) 114 }) 115 116 It("should not pass", func() { 117 Expect(checkErr).ToNot(HaveOccurred()) 118 Expect(output.Allowed).To(BeFalse()) 119 }) 120 }) 121 122 Context("when agent includes reasons", func() { 123 BeforeEach(func() { 124 fakeAgent.CheckReturns( 125 policy.PolicyCheckOutput{ 126 Allowed: false, 127 Reasons: []string{"a policy says you can't do that"}, 128 }, 129 nil, 130 ) 131 }) 132 133 It("should include reasons", func() { 134 Expect(checkErr).ToNot(HaveOccurred()) 135 Expect(output.Reasons).To(ConsistOf("a policy says you can't do that")) 136 }) 137 }) 138 139 Context("when agent says error", func() { 140 BeforeEach(func() { 141 fakeAgent.CheckReturns(policy.FailedPolicyCheck(), errors.New("some-error")) 142 }) 143 144 It("should not pass", func() { 145 Expect(checkErr).To(HaveOccurred()) 146 Expect(checkErr.Error()).To(Equal("some-error")) 147 Expect(output.Allowed).To(BeFalse()) 148 }) 149 }) 150 }) 151 }) 152 }) 153}) 154