github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/api/pipelineserver/scoped_handler_factory_test.go (about) 1 package pipelineserver_test 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 "net/http/httptest" 8 9 "github.com/pf-qiu/concourse/v6/atc" 10 "github.com/pf-qiu/concourse/v6/atc/api/auth" 11 "github.com/pf-qiu/concourse/v6/atc/api/pipelineserver" 12 "github.com/pf-qiu/concourse/v6/atc/db" 13 "github.com/pf-qiu/concourse/v6/atc/db/dbfakes" 14 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 ) 18 19 var _ = Describe("Handler", func() { 20 var ( 21 response *http.Response 22 server *httptest.Server 23 delegate *delegateHandler 24 25 dbTeamFactory *dbfakes.FakeTeamFactory 26 fakeTeam *dbfakes.FakeTeam 27 fakePipeline *dbfakes.FakePipeline 28 29 handler http.Handler 30 ) 31 32 BeforeEach(func() { 33 delegate = &delegateHandler{} 34 35 dbTeamFactory = new(dbfakes.FakeTeamFactory) 36 fakeTeam = new(dbfakes.FakeTeam) 37 fakePipeline = new(dbfakes.FakePipeline) 38 39 handlerFactory := pipelineserver.NewScopedHandlerFactory(dbTeamFactory) 40 handler = handlerFactory.HandlerFor(delegate.GetHandler) 41 }) 42 43 JustBeforeEach(func() { 44 server = httptest.NewServer(handler) 45 46 request, err := http.NewRequest("POST", server.URL+"?:team_name=some-team&:pipeline_name=some-pipeline", nil) 47 Expect(err).NotTo(HaveOccurred()) 48 49 response, err = new(http.Client).Do(request) 50 Expect(err).NotTo(HaveOccurred()) 51 }) 52 53 var _ = AfterEach(func() { 54 server.Close() 55 }) 56 57 Context("when pipeline is in request context", func() { 58 var contextPipeline *dbfakes.FakePipeline 59 60 BeforeEach(func() { 61 contextPipeline = new(dbfakes.FakePipeline) 62 handler = &wrapHandler{handler, contextPipeline} 63 }) 64 65 It("calls scoped handler with pipeline from context", func() { 66 Expect(delegate.IsCalled).To(BeTrue()) 67 Expect(delegate.Pipeline).To(BeIdenticalTo(contextPipeline)) 68 }) 69 }) 70 71 Context("when pipeline is not in request context", func() { 72 Context("when the team does not exist", func() { 73 BeforeEach(func() { 74 dbTeamFactory.FindTeamReturns(nil, false, nil) 75 }) 76 77 It("returns 404", func() { 78 Expect(response.StatusCode).To(Equal(http.StatusNotFound)) 79 }) 80 81 It("does not call the scoped handler", func() { 82 Expect(delegate.IsCalled).To(BeFalse()) 83 }) 84 }) 85 86 Context("when finding the team fails", func() { 87 BeforeEach(func() { 88 dbTeamFactory.FindTeamReturns(nil, false, errors.New("error")) 89 }) 90 91 It("returns 500", func() { 92 Expect(response.StatusCode).To(Equal(http.StatusInternalServerError)) 93 }) 94 95 It("does not call the scoped handler", func() { 96 Expect(delegate.IsCalled).To(BeFalse()) 97 }) 98 }) 99 100 Context("when pipeline exists", func() { 101 BeforeEach(func() { 102 fakeTeam.NameReturns("some-team") 103 dbTeamFactory.FindTeamReturns(fakeTeam, true, nil) 104 }) 105 106 It("looks up the team by the right name", func() { 107 Expect(dbTeamFactory.FindTeamCallCount()).To(Equal(1)) 108 Expect(dbTeamFactory.FindTeamArgsForCall(0)).To(Equal("some-team")) 109 }) 110 111 Context("when the pipeline exists", func() { 112 BeforeEach(func() { 113 fakePipeline.NameReturns("some-pipeline") 114 fakeTeam.PipelineReturns(fakePipeline, true, nil) 115 }) 116 117 It("looks up the pipeline by the right name", func() { 118 Expect(fakeTeam.PipelineCallCount()).To(Equal(1)) 119 Expect(fakeTeam.PipelineArgsForCall(0)).To(Equal(atc.PipelineRef{Name: "some-pipeline"})) 120 }) 121 122 It("returns 200", func() { 123 Expect(response.StatusCode).To(Equal(http.StatusOK)) 124 }) 125 126 It("calls the scoped handler", func() { 127 Expect(delegate.IsCalled).To(BeTrue()) 128 }) 129 }) 130 131 Context("when the pipeline does not exist", func() { 132 BeforeEach(func() { 133 fakeTeam.PipelineReturns(nil, false, nil) 134 }) 135 136 It("returns 404", func() { 137 Expect(response.StatusCode).To(Equal(http.StatusNotFound)) 138 }) 139 140 It("does not call the scoped handler", func() { 141 Expect(delegate.IsCalled).To(BeFalse()) 142 }) 143 }) 144 145 Context("when finding the pipeline fails", func() { 146 BeforeEach(func() { 147 fakeTeam.PipelineReturns(nil, false, errors.New("error")) 148 }) 149 150 It("returns 500", func() { 151 Expect(response.StatusCode).To(Equal(http.StatusInternalServerError)) 152 }) 153 154 It("does not call the scoped handler", func() { 155 Expect(delegate.IsCalled).To(BeFalse()) 156 }) 157 }) 158 }) 159 }) 160 }) 161 162 type delegateHandler struct { 163 IsCalled bool 164 Pipeline db.Pipeline 165 } 166 167 func (handler *delegateHandler) GetHandler(dbPipeline db.Pipeline) http.Handler { 168 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 169 handler.IsCalled = true 170 handler.Pipeline = dbPipeline 171 }) 172 } 173 174 type wrapHandler struct { 175 delegate http.Handler 176 contextPipeline db.Pipeline 177 } 178 179 func (h *wrapHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 180 ctx := context.WithValue(r.Context(), auth.PipelineContextKey, h.contextPipeline) 181 h.delegate.ServeHTTP(w, r.WithContext(ctx)) 182 }