-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
187 lines (152 loc) · 4.23 KB
/
integration_test.go
File metadata and controls
187 lines (152 loc) · 4.23 KB
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
package ctfdsetup_test
import (
"bytes"
"context"
_ "embed"
"os/exec"
"testing"
ctfdsetup "github.com/ctfer-io/ctfd-setup"
"github.com/ctfer-io/go-ctfd/api"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/trace/noop"
"gopkg.in/yaml.v3"
)
var (
//go:embed examples/minimal/.ctfd.yaml
minimalConf []byte
)
func Test_I_Minimal(t *testing.T) {
ctx := t.Context()
t.Cleanup(func() {
require.NoError(t, reset(context.WithoutCancel(ctx)))
})
// The infrastructure team sets up CTFd manually.
conf := ctfdsetup.NewConfig()
dec := yaml.NewDecoder(bytes.NewReader(minimalConf))
dec.KnownFields(true)
err := dec.Decode(conf)
require.NoError(t, err)
err = ctfdsetup.Setup(ctx, CTFdURL, "", conf)
require.NoError(t, err)
// Then they create an API key for automation purposes
nonce, session, err := api.GetNonceAndSession(CTFdURL, api.WithContext(ctx))
require.NoError(t, err)
client := api.NewClient(CTFdURL, nonce, session, "")
err = client.Login(&api.LoginParams{
Name: "ctfer",
Password: "ctfer",
}, api.WithContext(ctx))
require.NoError(t, err)
token, err := client.PostTokens(&api.PostTokensParams{
Expiration: "2222-01-01",
Description: "Automation API key.",
})
require.NoError(t, err)
// Finally, the automation triggers thus re-run the setup
err = ctfdsetup.Setup(ctx, CTFdURL, *token.Value, conf)
require.NoError(t, err)
}
func Test_I_NoFile(t *testing.T) {
t.Cleanup(func() {
require.NoError(t, reset(context.WithoutCancel(t.Context())))
})
cmd := exec.CommandContext(t.Context(),
"./ctfd-setup",
"--url", CTFdURL,
"--appearance.name", "NoFileCTF",
"--appearance.description", "A CTF configured with no file",
"--admin.name", "ctfer",
"--admin.email", "ctfer-io@protonmail.com",
"--admin.password", "ctfer",
)
cmd.Env = envs
out, err := cmd.CombinedOutput()
require.NoError(t, err, string(out))
}
func Test_I_NoBrackets2024(t *testing.T) {
t.Cleanup(func() {
require.NoError(t, reset(context.WithoutCancel(t.Context())))
})
cmd := exec.CommandContext(t.Context(),
"./ctfd-setup",
"--url", CTFdURL,
"--directory", "examples/nobrackets2024",
"--file", "examples/nobrackets2024/.ctfd.yaml",
)
cmd.Env = envs
out, err := cmd.CombinedOutput()
require.NoError(t, err, string(out))
}
func Test_I_CLIOVerride(t *testing.T) {
t.Cleanup(func() {
require.NoError(t, reset(context.WithoutCancel(t.Context())))
})
cmd := exec.CommandContext(t.Context(),
"./ctfd-setup",
"--url", CTFdURL,
"--file", "examples/cli-override/.ctfd.yaml",
"--admin.name", "ctfer",
"--admin.email", "ctfer-io@protonmail.com",
"--admin.password", "ctfer",
)
cmd.Env = envs
out, err := cmd.CombinedOutput()
require.NoError(t, err, string(out))
}
func Test_I_EnvOverride(t *testing.T) {
t.Cleanup(func() {
require.NoError(t, reset(context.WithoutCancel(t.Context())))
})
cmd := exec.CommandContext(t.Context(),
"./ctfd-setup",
"--url", CTFdURL,
"--file", "examples/cli-override/.ctfd.yaml",
)
cmd.Env = append(envs,
"ADMIN_NAME=ctfer",
"ADMIN_EMAIL=ctfer-io@protonmail.com",
"ADMIN_PASSWORD=ctfer",
)
out, err := cmd.CombinedOutput()
require.NoError(t, err, string(out))
}
func Test_I_Tracing(t *testing.T) {
ctx := t.Context()
t.Cleanup(func() {
require.NoError(t, reset(context.WithoutCancel(ctx)))
})
// You can define your own OTel setup
tp := noop.NewTracerProvider()
// Then load a configuration
conf := ctfdsetup.NewConfig()
dec := yaml.NewDecoder(bytes.NewReader(minimalConf))
dec.KnownFields(true)
err := dec.Decode(conf)
require.NoError(t, err)
// And set things up!
err = ctfdsetup.Setup(ctx, CTFdURL, "", conf, ctfdsetup.WithTracerProvider(tp))
require.NoError(t, err)
}
func reset(ctx context.Context) error {
nonce, session, err := api.GetNonceAndSession(CTFdURL, api.WithContext(ctx))
if err != nil {
return err
}
client := api.NewClient(CTFdURL, nonce, session, "")
if err := client.Login(&api.LoginParams{
Name: "ctfer",
Password: "ctfer",
}, api.WithContext(ctx)); err != nil {
return err
}
return client.Reset(&api.ResetParams{
Accounts: ptr("y"),
Submissions: ptr("y"),
Challenges: ptr("y"),
Pages: ptr("y"),
Notifications: ptr("y"),
}, api.WithContext(ctx))
}
func ptr[T any](t T) *T {
return &t
}