-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
306 lines (252 loc) · 7.49 KB
/
main_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package test
import (
"bytes"
"context"
"fmt"
"html/template"
"math/rand"
"net/http"
"net/http/cookiejar"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/alexedwards/scs/v2"
"github.com/jmoiron/sqlx"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"github.com/plutov/paypal/v4"
"github.com/jatolentino/tutorialspoint/api"
"github.com/jatolentino/tutorialspoint/api/background"
"github.com/jatolentino/tutorialspoint/config"
"github.com/jatolentino/tutorialspoint/database"
"github.com/sirupsen/logrus"
"github.com/stripe/stripe-go/v74"
stripecl "github.com/stripe/stripe-go/v74/client"
"golang.org/x/crypto/bcrypt"
)
var dbTest config.DB
func TestMain(m *testing.M) {
dbTest = config.DB{
User: "test_user",
Password: "test_pass",
Name: "test_db",
DisableTLS: true,
}
c, purge, err := startDB(dbTest.User, dbTest.Password, dbTest.Name)
if err != nil {
fmt.Printf("cannot run postgres in a docker container: %s", err)
os.Exit(1)
}
dbTest.Host = c.GetHostPort("5432/tcp")
// rand is used to generate random IDs and names in tests.
// Let's use a different random seed for each test execution.
rand.Seed(time.Now().UTC().UnixNano())
code := m.Run()
if err := purge(); err != nil {
fmt.Printf("%s", err)
}
os.Exit(code)
}
func startDB(user string, pass string, dbname string) (*dockertest.Resource, func() error, error) {
pool, err := dockertest.NewPool("")
if err != nil {
return nil, nil, fmt.Errorf("could not connect to docker: %w", err)
}
container, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "postgres",
Tag: "11",
Env: []string{
"POSTGRES_USER=" + user,
"POSTGRES_PASSWORD=" + pass,
"POSTGRES_DB=" + dbname,
"listen_addresses = '*'",
},
}, func(config *docker.HostConfig) {
config.AutoRemove = true
config.RestartPolicy = docker.RestartPolicy{Name: "no"}
})
if err != nil {
return nil, nil, fmt.Errorf("could not start db container: %w", err)
}
container.Expire(120)
var db *sqlx.DB
pool.MaxWait = 120 * time.Second
err = pool.Retry(func() error {
db, err = database.Open(config.DB{
User: "test_user",
Password: "test_pass",
Name: "test_db",
Host: container.GetHostPort("5432/tcp"),
DisableTLS: true,
})
return err
})
if err != nil {
return nil, nil, fmt.Errorf("could not connect to db: %w", err)
}
db.Close()
purge := func() error {
if err := pool.Purge(container); err != nil {
return fmt.Errorf("cleaning container %s: %w", container.Container.Name, err)
}
return nil
}
return container, purge, nil
}
type mockMailer struct {
token string
}
func (m *mockMailer) SendActivationToken(token string, dst string) error {
m.token = token
return nil
}
func (m *mockMailer) SendRecoveryToken(token string, dst string) error {
m.token = token
return nil
}
const seedTest = `
INSERT INTO users (user_id, name, email, role, active, password_hash, created_at, updated_at) VALUES
('ae127240-ce13-4789-aafd-d2f31e7ee487', 'Admin', '{{ .AdminEmail}}', 'ADMIN', TRUE, '{{ .AdminPassHash}}', '2022-09-16 00:00:00', '2022-09-16 00:00:00'),
('45b5fbd3-755f-4379-8f07-a58d4a30fa2f', 'User Test', '{{ .UserEmail}}', 'USER', TRUE, '{{ .UserPassHash}}', '2019-03-24 00:00:00', '2019-03-24 00:00:00')
ON CONFLICT DO NOTHING;
`
type TestEnv struct {
*httptest.Server
// Admin test credentials in seed.
AdminEmail string
AdminPass string
// User test credentials in seed.
UserEmail string
UserPass string
// Collect mocked dependencies here to make them
// available to all tests.
Mailer *mockMailer
Paypal *mockPaypal
Stripe *mockStripe
WebhookSecret string
}
func (te *TestEnv) parseSeed() (string, error) {
tmp := struct {
AdminEmail string
AdminPassHash string
UserEmail string
UserPassHash string
}{
AdminEmail: te.AdminEmail,
UserEmail: te.UserEmail,
}
h, err := bcrypt.GenerateFromPassword([]byte(te.AdminPass), bcrypt.DefaultCost)
if err != nil {
fmt.Println(err)
}
tmp.AdminPassHash = string(h)
h, err = bcrypt.GenerateFromPassword([]byte(te.UserPass), bcrypt.DefaultCost)
if err != nil {
fmt.Println(err)
}
tmp.UserPassHash = string(h)
t, err := template.New("seed").Parse(seedTest)
if err != nil {
return "", err
}
var res bytes.Buffer
if err = t.Execute(&res, tmp); err != nil {
return "", err
}
return res.String(), nil
}
func NewTestEnv(t *testing.T, dbname string) (*TestEnv, error) {
// Create a new database for each env.
dbMain, err := database.Open(dbTest)
if err != nil {
return nil, fmt.Errorf("could not connect to db: %v", err)
}
if _, err := dbMain.ExecContext(context.Background(), "CREATE DATABASE "+dbname); err != nil {
return nil, fmt.Errorf("creating database %s: %v", dbname, err)
}
dbMain.Close()
// Connect to the new db and perform migrations.
dbt := dbTest
dbt.Name = dbname
dbEnv, err := database.Open(dbt)
if err := database.Migrate(dbEnv); err != nil {
return nil, fmt.Errorf("cannot complete migration on new db: %v", err)
}
// Setup the test environment with some users.
te := &TestEnv{
AdminEmail: "admin@tutorialspoint.com",
AdminPass: "admin-password123",
UserEmail: "user@tutorialspoint.com",
UserPass: "user-password123",
}
seed, err := te.parseSeed()
if err != nil {
return nil, err
}
// Apply the seed to the new db.
if err := database.Seed(dbEnv, seed); err != nil {
return nil, fmt.Errorf("cannot init db with seed: %v", err)
}
// Redirect log to stdout.
log := logrus.New()
log.SetOutput(os.Stdout)
// Init a new session for authentications.
sess := scs.New()
sess.Lifetime = 24 * time.Hour
// Build a mocked mailer to allow signup in tests.
mail := &mockMailer{}
te.Mailer = mail
// Init a background manager to safely spawn go-routines.
bg := background.New(log)
// Setup the mock for paypal payments.
te.Paypal = &mockPaypal{}
ppserver := httptest.NewServer(te.Paypal.handle())
// Build the paypal client to allow payments and make it pointing to the mocked server.
// No need to generate a token since the server is mocked.
pp, err := paypal.NewClient("test", "test", ppserver.URL)
if err != nil {
return nil, fmt.Errorf("failed to build the paypal client: %w", err)
}
// Setup the mock for stripe payments.
te.Stripe = &mockStripe{}
strpserver := httptest.NewServer(te.Stripe.handle())
// Build the stripe client to allow payments.
strpcfg := config.Stripe{
APISecret: "random-key",
WebhookSecret: "random-test-secret",
SuccessURL: "/success.html",
CancelURL: "/cart.html",
}
te.WebhookSecret = strpcfg.WebhookSecret
strp := &stripecl.API{}
// Point to the mocked stripe server.
strp.Init(strpcfg.APISecret, &stripe.Backends{
API: stripe.GetBackendWithConfig(stripe.APIBackend, &stripe.BackendConfig{URL: &strpserver.URL}),
Connect: stripe.GetBackend(stripe.ConnectBackend),
Uploads: stripe.GetBackend(stripe.UploadsBackend),
})
api := api.APIMux(api.APIConfig{
CorsOrigin: "",
Log: log,
DB: dbEnv,
Session: sess,
Mailer: mail,
TokenTimeout: time.Nanosecond,
Background: bg,
Paypal: pp,
Stripe: strp,
StripeCfg: strpcfg,
ActivationRequired: true,
})
jar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
te.Server = httptest.NewTLSServer(api)
te.Server.Client().Jar = jar
te.Server.Client().CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
return te, nil
}