Skip to content

Commit bb478f2

Browse files
committed
Added unit tests
Commit covers by tests only part of the project
1 parent 86e67e4 commit bb478f2

File tree

7 files changed

+816
-0
lines changed

7 files changed

+816
-0
lines changed

internal/web/fail_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package web
2+
3+
import (
4+
"github.com/G-Node/gin-valid/internal/resources/templates"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
)
9+
10+
func TestFailFailedToParse(t *testing.T) {
11+
w := httptest.NewRecorder()
12+
original := templates.Layout
13+
templates.Layout = "{{ WTF? }"
14+
fail(w, 200, "WTF")
15+
templates.Layout = original
16+
status := w.Code
17+
if status != http.StatusOK {
18+
t.Fatalf(`fail(w http.ResponseWriter, status int, message string) status code = %v`, status)
19+
}
20+
}
21+
22+
func TestFailFailedToParseFailPage(t *testing.T) {
23+
w := httptest.NewRecorder()
24+
original := templates.Fail
25+
templates.Fail = "{{ WTF? }"
26+
fail(w, 200, "WTF")
27+
templates.Fail = original
28+
status := w.Code
29+
if status != http.StatusOK {
30+
t.Fatalf(`fail(w http.ResponseWriter, status int, message string) status code = %v`, status)
31+
}
32+
}

internal/web/hooks_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
"path/filepath"
13+
"testing"
14+
)
15+
16+
func TestHooksDisable(t *testing.T) {
17+
body := []byte("{}")
18+
router := mux.NewRouter()
19+
router.HandleFunc("/repos/{user}/{repo}/{hookid}/disable", DisableHook).Methods("GET")
20+
r, _ := http.NewRequest("GET", filepath.Join("/repos/", username, "/", reponame, "/1/disable"), bytes.NewReader(body))
21+
w := httptest.NewRecorder()
22+
srvcfg := config.Read()
23+
sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret))
24+
sig.Write(body)
25+
r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil)))
26+
router.ServeHTTP(w, r)
27+
status := w.Code
28+
if status != http.StatusFound {
29+
t.Fatalf(`DisableHook(w http.ResponseWriter, r *http.Request) status code = %v`, status)
30+
}
31+
}
32+
33+
func TestHooksEnable(t *testing.T) {
34+
body := []byte("{}")
35+
router := mux.NewRouter()
36+
router.HandleFunc("/repos/{user}/{repo}/{validator}/enable", EnableHook).Methods("GET")
37+
r, _ := http.NewRequest("GET", filepath.Join("/repos/", username, "/", reponame, "/bids/enable"), bytes.NewReader(body))
38+
w := httptest.NewRecorder()
39+
srvcfg := config.Read()
40+
sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret))
41+
sig.Write(body)
42+
r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil)))
43+
router.ServeHTTP(w, r)
44+
status := w.Code
45+
if status != http.StatusFound {
46+
t.Fatalf(`EnableHook(w http.ResponseWriter, r *http.Request) status code = %v`, status)
47+
}
48+
}

internal/web/results_test.go

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
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+
}

internal/web/status_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 TestStatusOK(t *testing.T) {
18+
content := "wtf"
19+
body := []byte("{}")
20+
router := mux.NewRouter()
21+
router.HandleFunc("/status/{validator}/{user}/{repo}", Status).Methods("GET")
22+
r, _ := http.NewRequest("GET", filepath.Join("/status/bids", username, reponame), bytes.NewReader(body))
23+
w := httptest.NewRecorder()
24+
srvcfg := config.Read()
25+
sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret))
26+
sig.Write(body)
27+
r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil)))
28+
os.MkdirAll(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, srvcfg.Label.ResultsFolder), 0755)
29+
f, _ := os.Create(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, srvcfg.Label.ResultsFolder, srvcfg.Label.ResultsBadge))
30+
defer f.Close()
31+
f.WriteString(content)
32+
router.ServeHTTP(w, r)
33+
os.RemoveAll(filepath.Join(srvcfg.Dir.Result, "bids", username, reponame, srvcfg.Label.ResultsFolder))
34+
status := w.Code
35+
if status != http.StatusOK {
36+
t.Fatalf(`Status(w http.ResponseWriter, r *http.Request) status code = %v`, status)
37+
}
38+
}
39+
40+
func TestStatusNoConent(t *testing.T) {
41+
body := []byte("{}")
42+
router := mux.NewRouter()
43+
router.HandleFunc("/status/{validator}/{user}/{repo}", Status).Methods("GET")
44+
r, _ := http.NewRequest("GET", "/status/bids/whatever/whatever", bytes.NewReader(body))
45+
w := httptest.NewRecorder()
46+
srvcfg := config.Read()
47+
sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret))
48+
sig.Write(body)
49+
r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil)))
50+
router.ServeHTTP(w, r)
51+
status := w.Code
52+
if status != http.StatusOK {
53+
t.Fatalf(`Status(w http.ResponseWriter, r *http.Request) status code = %v`, status)
54+
}
55+
}
56+
57+
func TestStatusUnsupportedValidator(t *testing.T) {
58+
body := []byte("{}")
59+
router := mux.NewRouter()
60+
router.HandleFunc("/status/{validator}/{user}/{repo}", Status).Methods("GET")
61+
r, _ := http.NewRequest("GET", "/status/whatever/whatever/whatever", bytes.NewReader(body))
62+
w := httptest.NewRecorder()
63+
srvcfg := config.Read()
64+
sig := hmac.New(sha256.New, []byte(srvcfg.Settings.HookSecret))
65+
sig.Write(body)
66+
r.Header.Add("X-Gogs-Signature", hex.EncodeToString(sig.Sum(nil)))
67+
router.ServeHTTP(w, r)
68+
status := w.Code
69+
if status != http.StatusOK {
70+
t.Fatalf(`Status(w http.ResponseWriter, r *http.Request) status code = %v`, status)
71+
}
72+
}

0 commit comments

Comments
 (0)