11package adapter
22
33import (
4+ "bytes"
5+ "context"
46 "encoding/json"
57 "net/http"
8+ "net/http/httptest"
69 "testing"
710
811 "github.com/google/go-github/v43/github"
12+ "github.com/openshift-pipelines/pipelines-as-code/pkg/params"
13+ "github.com/openshift-pipelines/pipelines-as-code/pkg/params/clients"
14+ testclient "github.com/openshift-pipelines/pipelines-as-code/pkg/test/clients"
915 "go.uber.org/zap"
1016 zapobserver "go.uber.org/zap/zaptest/observer"
1117 "gotest.tools/v3/assert"
18+ rtesting "knative.dev/pkg/reconciler/testing"
1219)
1320
1421func getLogger () * zap.SugaredLogger {
@@ -17,6 +24,95 @@ func getLogger() *zap.SugaredLogger {
1724 return logger
1825}
1926
27+ func TestHandleEvent (t * testing.T ) {
28+ ctx , _ := rtesting .SetupFakeContext (t )
29+ cs , _ := testclient .SeedTestData (t , ctx , testclient.Data {})
30+
31+ l := listener {
32+ run : & params.Run {
33+ Clients : clients.Clients {
34+ PipelineAsCode : cs .PipelineAsCode ,
35+ Log : getLogger (),
36+ },
37+ },
38+ logger : getLogger (),
39+ }
40+
41+ ts := httptest .NewServer (l .handleEvent ())
42+ defer ts .Close ()
43+
44+ // valid push event
45+ testEvent := github.PushEvent {Pusher : & github.User {ID : github .Int64 (101 )}}
46+ event , err := json .Marshal (testEvent )
47+ assert .NilError (t , err )
48+
49+ // invalid push event which will be skipped
50+ skippedEvent , err := json .Marshal (github.PushEvent {})
51+ assert .NilError (t , err )
52+
53+ tests := []struct {
54+ name string
55+ event []byte
56+ eventType string
57+ requestType string
58+ statusCode int
59+ }{
60+ {
61+ name : "get http call" ,
62+ requestType : "GET" ,
63+ event : []byte ("event" ),
64+ statusCode : 200 ,
65+ },
66+ {
67+ name : "invalid json body" ,
68+ requestType : "POST" ,
69+ event : []byte ("some random string for invalid json body" ),
70+ statusCode : 400 ,
71+ },
72+ {
73+ name : "valid event" ,
74+ requestType : "POST" ,
75+ eventType : "push" ,
76+ event : event ,
77+ statusCode : 202 ,
78+ },
79+ {
80+ name : "skip event" ,
81+ requestType : "POST" ,
82+ eventType : "push" ,
83+ event : skippedEvent ,
84+ statusCode : 200 ,
85+ },
86+ {
87+ name : "git provider not detected" ,
88+ requestType : "POST" ,
89+ eventType : "" ,
90+ event : event ,
91+ statusCode : 200 ,
92+ },
93+ }
94+
95+ for _ , tt := range tests {
96+ t .Run (tt .name , func (t * testing.T ) {
97+ req , err := http .NewRequestWithContext (context .TODO (), tt .requestType , ts .URL , bytes .NewReader (tt .event ))
98+ if err != nil {
99+ t .Fatalf ("error creating request: %s" , err )
100+ }
101+ req .Header .Set ("X-Github-Event" , tt .eventType )
102+
103+ resp , err := http .DefaultClient .Do (req )
104+ if err != nil {
105+ t .Fatalf ("error sending request: %s" , err )
106+ }
107+ defer resp .Body .Close ()
108+
109+ if resp .StatusCode != tt .statusCode {
110+ t .Fatalf ("expected status code : %v but got %v " , tt .statusCode , resp .StatusCode )
111+ }
112+ })
113+ }
114+ }
115+
20116func TestWhichProvider (t * testing.T ) {
21117 l := listener {
22118 logger : getLogger (),
0 commit comments