-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
home_test.go
195 lines (161 loc) · 4.93 KB
/
home_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package peerdb_test
import (
"context"
"crypto/tls"
"embed"
"io"
"io/fs"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"testing/fstest"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/tozd/go/x"
z "gitlab.com/tozd/go/zerolog"
"gitlab.com/tozd/identifier"
"gitlab.com/tozd/waf"
"gitlab.com/peerdb/peerdb"
)
//go:embed public
var publicFiles embed.FS
//nolint:exhaustruct
var testFiles = fstest.MapFS{ //nolint:gochecknoglobals
"dist/index.html": &fstest.MapFile{
Data: []byte("<html><body>dummy test content</body></html>"),
},
// Symlinks are not included in publicFiles.
"dist/LICENSE.txt": &fstest.MapFile{
Data: []byte("test license file"),
},
"dist/NOTICE.txt": &fstest.MapFile{
Data: []byte("test notice file"),
},
}
func init() { //nolint:gochecknoinits
f, err := fs.Sub(publicFiles, "public")
if err != nil {
panic(err)
}
err = fs.WalkDir(f, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
data, err := f.(fs.ReadFileFS).ReadFile(path)
if err != nil {
return err //nolint:wrapcheck
}
info, err := d.Info()
if err != nil {
return err //nolint:wrapcheck
}
testFiles[filepath.Join("dist", path)] = &fstest.MapFile{
Data: data,
Mode: info.Mode(),
ModTime: info.ModTime(),
Sys: info.Sys(),
}
return nil
})
if err != nil {
panic(err)
}
}
func testStaticFile(t *testing.T, route, filePath, contentType string) {
t.Helper()
ts, service := startTestServer(t)
path, errE := service.Reverse(route, nil, nil)
require.NoError(t, errE, "% -+#.1v", errE)
expected, err := testFiles.ReadFile(filePath)
require.NoError(t, err)
resp, err := ts.Client().Get(ts.URL + path) //nolint:noctx,bodyclose
if assert.NoError(t, err) {
t.Cleanup(func(r *http.Response) func() { return func() { r.Body.Close() } }(resp))
out, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, 2, resp.ProtoMajor)
assert.Equal(t, contentType, resp.Header.Get("Content-Type"))
assert.Equal(t, string(expected), string(out))
}
}
func TestRouteHome(t *testing.T) {
t.Parallel()
// Regular GET should just return the SPA index page.
testStaticFile(t, "Home", "dist/index.html", "text/html; charset=utf-8")
}
func startTestServer(t *testing.T) (*httptest.Server, *peerdb.Service) {
t.Helper()
if os.Getenv("ELASTIC") == "" {
t.Skip("ELASTIC is not available")
}
if os.Getenv("POSTGRES") == "" {
t.Skip("POSTGRES is not available")
}
tempDir := t.TempDir()
certPath := filepath.Join(tempDir, "test_cert.pem")
keyPath := filepath.Join(tempDir, "test_key.pem")
errE := x.CreateTempCertificateFiles(certPath, keyPath, []string{"localhost"})
require.NoError(t, errE, "% -+#.1v", errE)
logger := zerolog.New(zerolog.NewTestWriter(t)).With().Timestamp().Logger()
globals := &peerdb.Globals{ //nolint:exhaustruct
LoggingConfig: z.LoggingConfig{ //nolint:exhaustruct
Logger: logger,
},
Postgres: peerdb.PostgresConfig{
URL: []byte(os.Getenv("POSTGRES")),
Schema: identifier.New().String(),
},
Elastic: peerdb.ElasticConfig{
URL: os.Getenv("ELASTIC"),
Index: strings.ToLower(identifier.New().String()),
SizeField: false,
},
}
populate := peerdb.PopulateCommand{}
errE = populate.Run(globals)
require.NoError(t, errE, "% -+#.1v", errE)
serve := peerdb.ServeCommand{ //nolint:exhaustruct
Server: waf.Server[*peerdb.Site]{ //nolint:exhaustruct
TLS: waf.TLS{ //nolint:exhaustruct
CertFile: certPath,
KeyFile: keyPath,
},
Development: true,
// httptest.Server allocates a random port for its listener (but does not use serve.Server.Addr to do so).
// Having 0 for port here makes the rest of the codebase expect a random port and wait for its assignment.
Addr: "localhost:0",
},
Title: peerdb.DefaultTitle,
}
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
handler, service, errE := serve.Init(ctx, globals, testFiles)
require.NoError(t, errE, "% -+#.1v", errE)
ts := httptest.NewUnstartedServer(nil)
ts.EnableHTTP2 = true
t.Cleanup(ts.Close)
ts.Config = serve.Server.HTTPServer
ts.Config.Handler = handler
ts.TLS = serve.Server.HTTPServer.TLSConfig.Clone()
// We have to call GetCertificate ourselves.
// See: https://github.com/golang/go/issues/63812
cert, err := ts.TLS.GetCertificate(&tls.ClientHelloInfo{ //nolint:exhaustruct
ServerName: "localhost",
})
require.NoError(t, err, "% -+#.1v", err)
// By setting Certificates, we force testing server and testing client to use our certificate.
ts.TLS.Certificates = []tls.Certificate{*cert}
// This does not start server's managers, but that is OK for this test.
ts.StartTLS()
// Our certificate is for localhost domain and not 127.0.0.1 IP.
ts.URL = strings.ReplaceAll(ts.URL, "127.0.0.1", "localhost")
return ts, service
}