-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
152 lines (125 loc) · 3.58 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
package magiclinksdev_test
import (
"context"
_ "embed"
"encoding/json"
"fmt"
"io"
"log"
"log/slog"
"net/http"
"os"
"testing"
"time"
"github.com/MicahParks/jwkset"
mld "github.com/MicahParks/magiclinksdev"
"github.com/MicahParks/magiclinksdev/handle"
"github.com/MicahParks/magiclinksdev/mldtest"
"github.com/MicahParks/magiclinksdev/model"
"github.com/MicahParks/magiclinksdev/network"
"github.com/MicahParks/magiclinksdev/network/middleware/ctxkey"
"github.com/MicahParks/magiclinksdev/setup"
"github.com/MicahParks/magiclinksdev/storage"
)
var (
assets *testAssets
server *handle.Server
//go:embed config.test.json
testConfig []byte
)
type testAssets struct {
conf setup.TestConfig
keys []jwkset.JWK
mux *http.ServeMux
sa model.ServiceAccount
}
func readTestConfig() (setup.TestConfig, error) {
var conf setup.TestConfig
err := json.Unmarshal(testConfig, &conf)
if err != nil {
return setup.TestConfig{}, fmt.Errorf("failed to unmarshal configuration: %w", err)
}
conf, err = conf.DefaultsAndValidate()
if err != nil {
return setup.TestConfig{}, fmt.Errorf("failed to validate and apply defaults to confiuration: %w", err)
}
return conf, nil
}
func TestMain(m *testing.M) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
logger := log.New(os.Stdout, "", 0)
conf, err := readTestConfig()
if err != nil {
logger.Fatalf(mld.LogFmt, "Failed to read config.", err)
}
truncateDatabase(ctx, conf.Storage, logger)
server, err = setup.CreateTestingProvider(ctx, conf, setup.ServerOptions{
Logger: slog.New(slog.NewJSONHandler(io.Discard, nil)),
})
if err != nil {
logger.Fatalf(mld.LogFmt, "Failed to create server.", err)
}
keys := createKeyIfNotExists(ctx, server.Store, logger)
mux, err := network.CreateHTTPHandlers(server)
if err != nil {
logger.Fatalf(mld.LogFmt, "Failed to create HTTP handlers.", err)
}
sa := model.ServiceAccount{
UUID: mldtest.SAUUID,
APIKey: mldtest.APIKey,
Aud: mldtest.Aud,
Admin: true,
}
assets = &testAssets{
conf: conf,
keys: keys,
mux: mux,
sa: sa,
}
os.Exit(m.Run())
}
func createKeyIfNotExists(ctx context.Context, store storage.Storage, logger *log.Logger) []jwkset.JWK {
tx, err := store.Begin(ctx)
if err != nil {
logger.Fatalf(mld.LogFmt, "Failed to begin transaction.", err)
}
//goland:noinspection GoUnhandledErrorResult
defer tx.Rollback(ctx)
ctx = context.WithValue(ctx, ctxkey.Tx, tx)
keys, existed, err := setup.CreateKeysIfNotExists(ctx, store)
if err != nil {
logger.Fatalf(mld.LogFmt, "Failed to create keys.", err)
}
if !existed {
logger.Fatalf("Keys should have been created by setup.CreateMultiProviderServer function call.")
}
err = tx.Commit(ctx)
if err != nil {
logger.Fatalf(mld.LogFmt, "Failed to commit transaction.", err)
}
return keys
}
func truncateDatabase(ctx context.Context, config storage.Config, logger *log.Logger) {
store, _, err := storage.NewWithSetup(ctx, config, slog.New(slog.NewJSONHandler(io.Discard, nil)))
if err != nil {
logger.Fatalf(mld.LogFmt, "Failed to create storage.", err)
}
//goland:noinspection GoUnhandledErrorResult
defer store.Close(ctx)
tx, err := store.Begin(ctx)
if err != nil {
logger.Fatalf(mld.LogFmt, "Failed to begin transaction.", err)
}
//goland:noinspection GoUnhandledErrorResult
defer tx.Rollback(ctx)
ctx = context.WithValue(ctx, ctxkey.Tx, tx)
err = store.TestingTruncate(ctx)
if err != nil {
logger.Fatalf(mld.LogFmt, "Failed to truncate tables.", err)
}
err = tx.Commit(ctx)
if err != nil {
logger.Fatalf(mld.LogFmt, "Failed to commit transaction.", err)
}
}