Skip to content

Commit 5683eb9

Browse files
authored
Merge pull request #77 from pavelsne/tests
Added unit testing
2 parents 31234a3 + ecdbf6d commit 5683eb9

File tree

10 files changed

+967
-199
lines changed

10 files changed

+967
-199
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
name: run-build
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- dev
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
linters:
13+
# go vet and go fmt are mandatory.
14+
# Other linters are optional but should dispay issues.
15+
name: Linters
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v2
19+
- name: Setup go 1.16
20+
uses: actions/setup-go@v2
21+
with:
22+
go-version: '1.16'
23+
- name: Run go vet
24+
run: |
25+
go mod download github.com/mattn/go-isatty
26+
go vet ./...
27+
- name: Run gofmt
28+
run: |
29+
gofmt -s -l .
30+
if [ "$(gofmt -l . | wc -l)" -gt 0 ]; then exit 1; fi
31+
- name: Run golint
32+
run: |
33+
go get golang.org/x/lint/golint
34+
golint ./...
35+
continue-on-error: true
36+
- name: Run staticcheck
37+
run: |
38+
go get honnef.co/go/tools/cmd/staticcheck
39+
staticcheck ./...
40+
continue-on-error: true
41+
- name: Run errcheck
42+
run: |
43+
go get github.com/kisielk/errcheck
44+
errcheck ./...
45+
continue-on-error: true
46+
47+
build:
48+
name: build
49+
strategy:
50+
matrix:
51+
go-version: [1.14.x, 1.15.x, 1.16.x]
52+
platform: [ubuntu-latest]
53+
runs-on: ${{ matrix.platform }}
54+
steps:
55+
- name: Install Go
56+
uses: actions/setup-go@v1
57+
with:
58+
go-version: ${{ matrix.go-version }}
59+
- name: Checkout code
60+
uses: actions/checkout@v2
61+
- name: Run unit tests
62+
run: |
63+
go mod download github.com/mattn/go-isatty
64+
go build ./cmd/ginvalid
65+
tests:
66+
runs-on: [ubuntu-latest]
67+
steps:
68+
- uses: actions/checkout@v2
69+
- name: Setup Go 1.16
70+
uses: actions/setup-go@v2
71+
with:
72+
go-version: '1.16'
73+
- name: Install git annex dependency
74+
run: |
75+
bash <(wget -q -O- http://neuro.debian.net/_files/neurodebian-travis.sh)
76+
sudo apt-get update -qq
77+
sudo apt-get install git-annex-standalone
78+
git version
79+
git annex version
80+
- name: Show Go version
81+
run: go version
82+
- name: Fetch dependencies
83+
run: go get -d ./...
84+
- name: Run build
85+
run: go build ./cmd/ginvalid
86+
- name: Run tests
87+
run: go test ./...
88+
run-coverall:
89+
runs-on: [ubuntu-latest]
90+
steps:
91+
- uses: actions/checkout@v3
92+
- name: Use golang version 1.17
93+
uses: actions/setup-go@v3
94+
with:
95+
go-version: '1.17'
96+
- name: Install git annex dependency
97+
run: |
98+
bash <(wget -q -O- http://neuro.debian.net/_files/neurodebian-travis.sh)
99+
sudo apt-get update -qq
100+
sudo apt-get install git-annex-standalone
101+
git version
102+
git annex version
103+
- name: Fetch dependencies
104+
run: |
105+
go get -d ./...
106+
go get github.com/mattn/goveralls
107+
- name: Create test coverage
108+
run: go test -covermode atomic -coverprofile=covprofile ./...
109+
- name: Send coverage
110+
env:
111+
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
112+
run: goveralls -coverprofile=covprofile -service=github

.github/workflows/run-build.yml

Lines changed: 0 additions & 60 deletions
This file was deleted.

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+
}

0 commit comments

Comments
 (0)