Skip to content

Commit ebeabb0

Browse files
committed
Fix test action
1 parent e4b5114 commit ebeabb0

File tree

5 files changed

+71
-29
lines changed

5 files changed

+71
-29
lines changed

.github/workflows/test.yml

+4-7
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ jobs:
1010
image: postgres
1111
env:
1212
POSTGRES_PASSWORD: postgres
13+
ports:
14+
- 5432:5432
1315
options: >-
1416
--health-cmd pg_isready
1517
--health-interval 10s
1618
--health-timeout 5s
1719
--health-retries 5
1820
strategy:
1921
matrix:
20-
go-version: [1.14.x]
21-
platform: [ubuntu-latest, macos-latest, windows-latest]
22-
runs-on: ${{ matrix.platform }}
22+
go-version: [1.13.x, 1.14.x]
23+
runs-on: ubuntu-latest
2324
steps:
2425
- name: Setup Go
2526
uses: actions/setup-go@v1
@@ -30,7 +31,3 @@ jobs:
3031
- run: git branch
3132
- run: cat handlers.go
3233
- run: go test -v ./...
33-
env:
34-
DATABASE_URL: postgres://postgres:postgres@postgres/postgres
35-
SECRET_KEY: 4tests0nly
36-
HTTPS_ONLY: False

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/gorilla/mux v1.7.4
1010
github.com/segmentio/encoding v0.1.14 // indirect
1111
github.com/spf13/viper v1.7.0
12+
golang.org/x/net v0.0.0-20200602114024-627f9648deb9
1213
golang.org/x/sys v0.0.0-20200620081246-981b61492c35 // indirect
1314
)
1415

handlers_test.go

+51-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package tornote
1818

1919
import (
2020
"bytes"
21+
"golang.org/x/net/html"
2122
"net/http"
2223
"net/http/httptest"
2324
"net/url"
@@ -26,6 +27,47 @@ import (
2627

2728
const TestRandomString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
2829

30+
func requestMainForm(s *Server) (w *httptest.ResponseRecorder) {
31+
w = httptest.NewRecorder()
32+
req, _ := http.NewRequest("GET", "/", nil)
33+
s.router.ServeHTTP(w, req)
34+
return
35+
}
36+
37+
func getCSRFToken(body *bytes.Buffer) (token string) {
38+
root, err := html.Parse(body)
39+
if err != nil {
40+
return ""
41+
}
42+
if n, ok := getElementByName("csrf_token", root); ok {
43+
token = getAttributeByKey("value", n)
44+
}
45+
return
46+
}
47+
48+
func getElementByName(name string, n *html.Node) (el *html.Node, ok bool) {
49+
for _, a := range n.Attr {
50+
if a.Key == "name" && a.Val == name {
51+
return n, true
52+
}
53+
}
54+
for c := n.FirstChild; c != nil; c = c.NextSibling {
55+
if el, ok = getElementByName(name, c); ok {
56+
return
57+
}
58+
}
59+
return
60+
}
61+
62+
func getAttributeByKey(key string, n *html.Node) string {
63+
for _, attr := range n.Attr {
64+
if attr.Key == key {
65+
return attr.Val
66+
}
67+
}
68+
return ""
69+
}
70+
2971
func TestMainFormHandler(t *testing.T) {
3072
w := httptest.NewRecorder()
3173
s := stubServer()
@@ -52,19 +94,19 @@ func TestHealthStatusHandler(t *testing.T) {
5294
}
5395

5496
func TestCreateNoteHandler(t *testing.T) {
55-
w := httptest.NewRecorder()
56-
s := stubServer()
57-
58-
// First request page and set CSRF protected cookie on request
59-
req1, _ := http.NewRequest("GET", "/", nil)
60-
s.router.ServeHTTP(w, req1)
97+
srv := stubServer()
98+
w := requestMainForm(srv)
6199

62100
data := url.Values{}
101+
cookies := w.Result().Cookies()
63102
data.Set("body", TestRandomString)
103+
data.Set("csrf_token", getCSRFToken(w.Body))
64104

65-
req2, _ := http.NewRequest("POST", "/note", bytes.NewBufferString(data.Encode()))
66-
req2.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
67-
s.router.ServeHTTP(w, req2)
105+
w = httptest.NewRecorder()
106+
req, _ := http.NewRequest("POST", "/note", bytes.NewBufferString(data.Encode()))
107+
req.AddCookie(cookies[0])
108+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
109+
srv.router.ServeHTTP(w, req)
68110

69111
if w.Code != http.StatusCreated {
70112
t.Errorf("return code %d instead %d", w.Code, http.StatusCreated)

server.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,21 @@ func (s *Server) Init() {
160160
if err := s.compileTemplates(); err != nil {
161161
panic(err)
162162
}
163-
}
164163

165-
// Listen server on specified port with opened database connection.
166-
func (s *Server) Listen() error {
167164
// Connecting to database
168165
if err := s.connectDB(); err != nil {
169-
return err
166+
panic(err)
170167
}
171-
defer s.db.Close()
172168

173-
// Bootstrap database if not exists
169+
// Bootstrap tables if not exists
174170
if err := s.createSchema(); err != nil {
175-
return err
171+
panic(err)
176172
}
173+
}
174+
175+
// Listen server on specified port with opened database connection.
176+
func (s *Server) Listen() error {
177+
defer s.db.Close()
177178

178179
// Start the server
179180
if s.opts.HTTPSOnly {

server_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,24 @@
1818
package tornote
1919

2020
const (
21-
TestDSN = "postgres://postgres:postgres@localhost/testdb"
21+
TestDSN = "postgres://postgres:postgres@localhost/postgres?sslmode=disable"
2222
TestPort = 31337
23-
TestSecret = "0123456789"
23+
TestSecret = "4tests0nly"
2424
)
2525

2626
func stubServer() *Server {
27-
srv := NewServer(ServerOpts{})
27+
o := testServerOpts()
28+
srv := NewServer(o)
2829
srv.Init()
2930
return srv
3031
}
3132

3233
func testServerOpts() ServerOpts {
3334
return ServerOpts{
34-
Port: 8000,
35-
DSN: "postgres://postgres:postgres@postgres/postgres",
35+
Port: TestPort,
36+
DSN: TestDSN,
37+
Secret: TestSecret,
3638
HTTPSOnly: false,
37-
Secret: "4tests0nly",
3839
}
3940
}
4041

0 commit comments

Comments
 (0)