github.com/covergates/covergates@v0.2.2-0.20201009050117-42ef8a19fb95/routers/api/repo/hook_test.go (about) 1 package repo 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7 8 "github.com/covergates/covergates/core" 9 "github.com/covergates/covergates/mock" 10 "github.com/gin-gonic/gin" 11 "github.com/golang/mock/gomock" 12 ) 13 14 var repo = &core.Repo{ 15 ID: uint(1), 16 ReportID: "ABC", 17 Name: "name", 18 NameSpace: "space", 19 SCM: core.Gitea, 20 } 21 22 func mockRepo(store *mock.MockRepoStore) *core.Repo { 23 store.EXPECT().Find(gomock.Eq( 24 &core.Repo{ 25 Name: repo.Name, 26 NameSpace: repo.NameSpace, 27 SCM: repo.SCM, 28 })).AnyTimes().Return(repo, nil) 29 return repo 30 } 31 32 func mockSCM( 33 ctrl *gomock.Controller, 34 SCM *mock.MockSCMService, 35 ) *mock.MockClient { 36 client := mock.NewMockClient(ctrl) 37 SCM.EXPECT().Client(gomock.Eq(repo.SCM)).Return(client, nil) 38 return client 39 } 40 41 func TestHook(t *testing.T) { 42 ctrl := gomock.NewController(t) 43 defer ctrl.Finish() 44 45 hook := struct{}{} 46 47 store := mock.NewMockRepoStore(ctrl) 48 scm := mock.NewMockSCMService(ctrl) 49 service := mock.NewMockHookService(ctrl) 50 webhook := mock.NewMockWebhookService(ctrl) 51 repo := mockRepo(store) 52 client := mockSCM(ctrl, scm) 53 client.EXPECT().Webhooks().Return(webhook) 54 webhook.EXPECT().Parse(gomock.Any()).Return(hook, nil) 55 service.EXPECT().Resolve(gomock.Any(), gomock.Eq(repo), hook).Return(nil) 56 57 r := gin.Default() 58 r.POST("/repos/:scm/:namespace/:name/hook", WithRepo(store), HandleHook(scm, service)) 59 60 req, _ := http.NewRequest("POST", "/repos/gitea/space/name/hook", nil) 61 testRequest(r, req, func(w *httptest.ResponseRecorder) { 62 rst := w.Result() 63 if rst.StatusCode != 200 { 64 t.Fatal("request fail") 65 } 66 }) 67 68 }