|
| 1 | +package web |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/hmac" |
| 6 | + "crypto/sha256" |
| 7 | + "encoding/hex" |
| 8 | + "github.com/G-Node/gin-valid/internal/config" |
| 9 | + "github.com/gorilla/mux" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "os" |
| 13 | + "path/filepath" |
| 14 | + "testing" |
| 15 | +) |
| 16 | + |
| 17 | +func TestResultsUnsupportedV2(t *testing.T) { |
| 18 | + id := "1" |
| 19 | + content := "{\"empty\":\"json\"}" |
| 20 | + body := []byte("{}") |
| 21 | + router := mux.NewRouter() |
| 22 | + router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET") |
| 23 | + r, _ := http.NewRequest("GET", filepath.Join("/results/wtf", username, "/", reponame, "/", id), bytes.NewReader(body)) |
| 24 | + w := httptest.NewRecorder() |
| 25 | + srvcfg := config.Read() |
| 26 | + srvcfg.Settings.Validators = append(srvcfg.Settings.Validators, "wtf") |
| 27 | + config.Set(srvcfg) |
| 28 | + os.MkdirAll(filepath.Join(srvcfg.Dir.Result, "nix", username, reponame, id), 0755) |
| 29 | + f, _ := os.Create(filepath.Join(srvcfg.Dir.Result, "nix", username, reponame, id, srvcfg.Label.ResultsFile)) |
| 30 | + defer f.Close() |
| 31 | + f.WriteString(content) |
| 32 | + sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret)) |
| 33 | + sig.Write(body) |
| 34 | + r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil))) |
| 35 | + router.ServeHTTP(w, r) |
| 36 | + os.RemoveAll(filepath.Join(srvcfg.Dir.Result, "nix", username, reponame, id)) |
| 37 | + srvcfg.Settings.Validators = srvcfg.Settings.Validators[:len(srvcfg.Settings.Validators)-1] |
| 38 | + config.Set(srvcfg) |
| 39 | + status := w.Code |
| 40 | + if status != http.StatusOK { |
| 41 | + t.Fatalf(`Results(w http.ResponseWriter, r *http.Request) status code = %v`, status) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestResultsODML(t *testing.T) { |
| 46 | + id := "1" |
| 47 | + content := "{\"empty\":\"json\"}" |
| 48 | + body := []byte("{}") |
| 49 | + router := mux.NewRouter() |
| 50 | + router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET") |
| 51 | + r, _ := http.NewRequest("GET", filepath.Join("/results/odml", username, "/", reponame, "/", id), bytes.NewReader(body)) |
| 52 | + w := httptest.NewRecorder() |
| 53 | + srvcfg := config.Read() |
| 54 | + os.MkdirAll(filepath.Join(srvcfg.Dir.Result, "odml", username, reponame, id), 0755) |
| 55 | + f, _ := os.Create(filepath.Join(srvcfg.Dir.Result, "odml", username, reponame, id, srvcfg.Label.ResultsFile)) |
| 56 | + defer f.Close() |
| 57 | + f.WriteString(content) |
| 58 | + sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret)) |
| 59 | + sig.Write(body) |
| 60 | + r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil))) |
| 61 | + router.ServeHTTP(w, r) |
| 62 | + os.RemoveAll(filepath.Join(srvcfg.Dir.Result, "odml", username, reponame, id)) |
| 63 | + status := w.Code |
| 64 | + if status != http.StatusOK { |
| 65 | + t.Fatalf(`Results(w http.ResponseWriter, r *http.Request) status code = %v`, status) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestResultsNIX(t *testing.T) { |
| 70 | + id := "1" |
| 71 | + content := "{\"empty\":\"json\"}" |
| 72 | + body := []byte("{}") |
| 73 | + router := mux.NewRouter() |
| 74 | + router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET") |
| 75 | + r, _ := http.NewRequest("GET", filepath.Join("/results/nix", username, "/", reponame, "/", id), bytes.NewReader(body)) |
| 76 | + w := httptest.NewRecorder() |
| 77 | + srvcfg := config.Read() |
| 78 | + os.MkdirAll(filepath.Join(srvcfg.Dir.Result, "nix", username, reponame, id), 0755) |
| 79 | + f, _ := os.Create(filepath.Join(srvcfg.Dir.Result, "nix", username, reponame, id, srvcfg.Label.ResultsFile)) |
| 80 | + defer f.Close() |
| 81 | + f.WriteString(content) |
| 82 | + sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret)) |
| 83 | + sig.Write(body) |
| 84 | + r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil))) |
| 85 | + router.ServeHTTP(w, r) |
| 86 | + os.RemoveAll(filepath.Join(srvcfg.Dir.Result, "nix", username, reponame, id)) |
| 87 | + status := w.Code |
| 88 | + if status != http.StatusOK { |
| 89 | + t.Fatalf(`Results(w http.ResponseWriter, r *http.Request) status code = %v`, status) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestResultsInJSON(t *testing.T) { |
| 94 | + id := "1" |
| 95 | + content := "{\"empty\":\"json\"}" |
| 96 | + body := []byte("{}") |
| 97 | + router := mux.NewRouter() |
| 98 | + router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET") |
| 99 | + r, _ := http.NewRequest("GET", filepath.Join("/results/bids", username, "/", reponame, "/", id), bytes.NewReader(body)) |
| 100 | + w := httptest.NewRecorder() |
| 101 | + srvcfg := config.Read() |
| 102 | + os.MkdirAll(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id), 0755) |
| 103 | + f, _ := os.Create(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id, srvcfg.Label.ResultsFile)) |
| 104 | + defer f.Close() |
| 105 | + f.WriteString(content) |
| 106 | + sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret)) |
| 107 | + sig.Write(body) |
| 108 | + r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil))) |
| 109 | + router.ServeHTTP(w, r) |
| 110 | + os.RemoveAll(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id)) |
| 111 | + status := w.Code |
| 112 | + if status != http.StatusOK { |
| 113 | + t.Fatalf(`Results(w http.ResponseWriter, r *http.Request) status code = %v`, status) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestResultsInProgress(t *testing.T) { |
| 118 | + id := "1" |
| 119 | + content := progressmsg |
| 120 | + body := []byte("{}") |
| 121 | + router := mux.NewRouter() |
| 122 | + router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET") |
| 123 | + r, _ := http.NewRequest("GET", filepath.Join("/results/bids", username, "/", reponame, "/", id), bytes.NewReader(body)) |
| 124 | + w := httptest.NewRecorder() |
| 125 | + srvcfg := config.Read() |
| 126 | + os.MkdirAll(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id), 0755) |
| 127 | + f, _ := os.Create(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id, srvcfg.Label.ResultsFile)) |
| 128 | + defer f.Close() |
| 129 | + f.WriteString(content) |
| 130 | + sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret)) |
| 131 | + sig.Write(body) |
| 132 | + r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil))) |
| 133 | + router.ServeHTTP(w, r) |
| 134 | + os.RemoveAll(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id)) |
| 135 | + status := w.Code |
| 136 | + if status != http.StatusOK { |
| 137 | + t.Fatalf(`Results(w http.ResponseWriter, r *http.Request) status code = %v`, status) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func TestResultsSomeResults(t *testing.T) { |
| 142 | + id := "1" |
| 143 | + content := "wtf" |
| 144 | + body := []byte("{}") |
| 145 | + router := mux.NewRouter() |
| 146 | + router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET") |
| 147 | + r, _ := http.NewRequest("GET", filepath.Join("/results/bids", username, "/", reponame, "/", id), bytes.NewReader(body)) |
| 148 | + w := httptest.NewRecorder() |
| 149 | + srvcfg := config.Read() |
| 150 | + os.MkdirAll(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id), 0755) |
| 151 | + f, _ := os.Create(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id, srvcfg.Label.ResultsFile)) |
| 152 | + defer f.Close() |
| 153 | + f.WriteString(content) |
| 154 | + sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret)) |
| 155 | + sig.Write(body) |
| 156 | + r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil))) |
| 157 | + router.ServeHTTP(w, r) |
| 158 | + os.RemoveAll(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, id)) |
| 159 | + status := w.Code |
| 160 | + if status != http.StatusOK { |
| 161 | + t.Fatalf(`Results(w http.ResponseWriter, r *http.Request) status code = %v`, status) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +func TestResultsNoResults(t *testing.T) { |
| 166 | + body := []byte("{}") |
| 167 | + router := mux.NewRouter() |
| 168 | + router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET") |
| 169 | + r, _ := http.NewRequest("GET", "/results/bids/whatever/whatever/whatever", bytes.NewReader(body)) |
| 170 | + w := httptest.NewRecorder() |
| 171 | + srvcfg := config.Read() |
| 172 | + sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret)) |
| 173 | + sig.Write(body) |
| 174 | + r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil))) |
| 175 | + router.ServeHTTP(w, r) |
| 176 | + status := w.Code |
| 177 | + if status != http.StatusOK { |
| 178 | + t.Fatalf(`Results(w http.ResponseWriter, r *http.Request) status code = %v`, status) |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +func TestResultsUnsupportedValidator(t *testing.T) { |
| 183 | + body := []byte("{}") |
| 184 | + router := mux.NewRouter() |
| 185 | + router.HandleFunc("/results/{validator}/{user}/{repo}/{id}", Results).Methods("GET") |
| 186 | + r, _ := http.NewRequest("GET", "/results/wtf/whatever/whatever/whatever", bytes.NewReader(body)) |
| 187 | + w := httptest.NewRecorder() |
| 188 | + srvcfg := config.Read() |
| 189 | + sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret)) |
| 190 | + sig.Write(body) |
| 191 | + r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil))) |
| 192 | + router.ServeHTTP(w, r) |
| 193 | + status := w.Code |
| 194 | + if status != http.StatusOK { |
| 195 | + t.Fatalf(`Results(w http.ResponseWriter, r *http.Request) status code = %v`, status) |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +func TestResultsIDNotSpecified(t *testing.T) { |
| 200 | + body := []byte("{}") |
| 201 | + router := mux.NewRouter() |
| 202 | + router.HandleFunc("/results/{validator}/{user}/{repo}/", Results).Methods("GET") |
| 203 | + r, _ := http.NewRequest("GET", "/results/bids/whatever/whatever/", bytes.NewReader(body)) |
| 204 | + w := httptest.NewRecorder() |
| 205 | + srvcfg := config.Read() |
| 206 | + sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret)) |
| 207 | + sig.Write(body) |
| 208 | + r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil))) |
| 209 | + router.ServeHTTP(w, r) |
| 210 | + status := w.Code |
| 211 | + if status != http.StatusOK { |
| 212 | + t.Fatalf(`Results(w http.ResponseWriter, r *http.Request) status code = %v`, status) |
| 213 | + } |
| 214 | +} |
0 commit comments