11package main
22
33import (
4+ "bytes"
5+ "encoding/json"
46 "errors"
57 "net/http"
68 "net/http/httptest"
@@ -62,7 +64,7 @@ func TestGitHandler_InvalidURL(t *testing.T) {
6264 }
6365
6466 for _ , tt := range tests {
65- req , err := http .NewRequest (http .MethodGet , "/getgit ?url=" + tt .url , nil )
67+ req , err := http .NewRequest (http .MethodGet , "/git ?url=" + tt .url , nil )
6668 if err != nil {
6769 t .Fatal (err )
6870 }
@@ -114,7 +116,7 @@ func TestGitHandler_Integration(t *testing.T) {
114116
115117 for _ , tt := range tests {
116118 t .Run (tt .name , func (t * testing.T ) {
117- req , err := http .NewRequest (http .MethodGet , "/getgit ?url=" + tt .url , nil )
119+ req , err := http .NewRequest (http .MethodGet , "/git ?url=" + tt .url , nil )
118120 if err != nil {
119121 t .Fatal (err )
120122 }
@@ -128,3 +130,142 @@ func TestGitHandler_Integration(t *testing.T) {
128130 })
129131 }
130132}
133+
134+ func TestCacheHandler (t * testing.T ) {
135+ if testing .Short () {
136+ t .Skip ("skipping integration test in short mode" )
137+ }
138+
139+ // Setup valid workdir
140+ tmpDir := t .TempDir ()
141+
142+ // Override global variables for test
143+ // Note: In a real app we might want to dependency inject these,
144+ // but for this simple script we modify package globals.
145+ gitStorePath = tmpDir
146+ fetchTimeout = time .Minute
147+ // Ensure lastFetch map is initialized
148+ if lastFetch == nil {
149+ loadLastFetchMap ()
150+ }
151+
152+ tests := []struct {
153+ name string
154+ url string
155+ expectedCode int
156+ }{
157+ {
158+ name : "Valid public repo" ,
159+ url : "https://github.com/google/oss-fuzz-vulns.git" , // Small repo
160+ expectedCode : http .StatusOK ,
161+ },
162+ {
163+ name : "Non-existent repo" ,
164+ url : "https://github.com/google/this-repo-does-not-exist-12345.git" ,
165+ expectedCode : http .StatusForbidden ,
166+ },
167+ }
168+
169+ for _ , tt := range tests {
170+ t .Run (tt .name , func (t * testing.T ) {
171+ body , _ := json .Marshal (map [string ]string {"url" : tt .url })
172+ req , err := http .NewRequest (http .MethodPost , "/cache" , bytes .NewBuffer (body ))
173+ if err != nil {
174+ t .Fatal (err )
175+ }
176+ rr := httptest .NewRecorder ()
177+ cacheHandler (rr , req )
178+
179+ if status := rr .Code ; status != tt .expectedCode {
180+ t .Errorf ("handler returned wrong status code: got %v want %v" ,
181+ status , tt .expectedCode )
182+ }
183+ })
184+ }
185+ }
186+
187+ func TestAffectedCommitsHandler (t * testing.T ) {
188+ if testing .Short () {
189+ t .Skip ("skipping integration test in short mode" )
190+ }
191+
192+ // Setup valid workdir
193+ tmpDir := t .TempDir ()
194+
195+ // Override global variables for test
196+ // Note: In a real app we might want to dependency inject these,
197+ // but for this simple script we modify package globals.
198+ gitStorePath = tmpDir
199+ fetchTimeout = time .Minute
200+ // Ensure lastFetch map is initialized
201+ if lastFetch == nil {
202+ loadLastFetchMap ()
203+ }
204+
205+ tests := []struct {
206+ name string
207+ url string
208+ introduced []string
209+ fixed []string
210+ lastAffected []string
211+ limit []string
212+ expectedCode int
213+ }{
214+ {
215+ name : "Valid range in public repo" ,
216+ url : "https://github.com/google/oss-fuzz-vulns.git" ,
217+ introduced : []string {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" },
218+ fixed : []string {"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" },
219+ expectedCode : http .StatusOK ,
220+ },
221+ {
222+ name : "Invalid mixed limit and fixed" ,
223+ url : "https://github.com/google/oss-fuzz-vulns.git" ,
224+ fixed : []string {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" },
225+ limit : []string {"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" },
226+ expectedCode : http .StatusBadRequest ,
227+ },
228+ {
229+ name : "Non-existent repo" ,
230+ url : "https://github.com/google/this-repo-does-not-exist-12345.git" ,
231+ introduced : []string {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" },
232+ expectedCode : http .StatusForbidden ,
233+ },
234+ }
235+
236+ for _ , tt := range tests {
237+ t .Run (tt .name , func (t * testing.T ) {
238+ var events []Event
239+ for _ , h := range tt .introduced {
240+ events = append (events , Event {EventType : "introduced" , Hash : h })
241+ }
242+ for _ , h := range tt .fixed {
243+ events = append (events , Event {EventType : "fixed" , Hash : h })
244+ }
245+ for _ , h := range tt .lastAffected {
246+ events = append (events , Event {EventType : "last_affected" , Hash : h })
247+ }
248+ for _ , h := range tt .limit {
249+ events = append (events , Event {EventType : "limit" , Hash : h })
250+ }
251+
252+ reqBody := map [string ]interface {}{
253+ "url" : tt .url ,
254+ "events" : events ,
255+ }
256+
257+ body , _ := json .Marshal (reqBody )
258+ req , err := http .NewRequest (http .MethodPost , "/affected-commits" , bytes .NewBuffer (body ))
259+ if err != nil {
260+ t .Fatal (err )
261+ }
262+ rr := httptest .NewRecorder ()
263+ affectedCommitsHandler (rr , req )
264+
265+ if status := rr .Code ; status != tt .expectedCode {
266+ t .Errorf ("handler returned wrong status code: got %v want %v" ,
267+ status , tt .expectedCode )
268+ }
269+ })
270+ }
271+ }
0 commit comments