-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.go
More file actions
66 lines (55 loc) · 1.84 KB
/
Copy pathtesting.go
File metadata and controls
66 lines (55 loc) · 1.84 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
package documentstore
import (
"testing"
"azugo.io/azugo"
"azugo.io/azugo/token"
"azugo.io/azugo/user"
"github.com/go-quicktest/qt"
)
// TestApp builds an App for tests: in-memory metadata + blob stores, an ephemeral
// dev KMS key, no signer / GDPR-audit / broker wiring, and a stub auth middleware
// driven by the X-Test-Scopes request header (production always uses the
// go-authbyte DPoP middleware).
func TestApp(tb testing.TB) *App {
tb.Helper()
tb.Setenv("METRICS_ENABLED", "false")
tb.Setenv("SERVICE_NAME", "document-store")
tb.Setenv("ENVIRONMENT", "development")
tb.Setenv("AUTH_ISSUER_URL", "http://localhost:8080")
tb.Setenv("SERVICE_AUDIENCE", "svc:document")
app, err := New(nil, "0.0.0-test")
qt.Assert(tb, qt.IsNil(err))
app.SetAuthMiddleware(TestAuthMiddleware())
return app
}
// TestAuthMiddleware authenticates requests from the X-Test-Scopes header
// (comma-separated scopes, e.g. "documents:read,documents:write") and uses the
// optional X-Test-Sub header as the caller identity / document owner (default
// "svc:test-client"). Requests without scopes are rejected 401 — mirroring the
// production contract.
func TestAuthMiddleware() azugo.RequestHandlerFunc {
return func(next azugo.RequestHandler) azugo.RequestHandler {
return func(ctx *azugo.Context) {
scopes := ctx.Header.Get("X-Test-Scopes")
if scopes == "" {
ctx.StatusCode(401)
ctx.Text("unauthorized")
return
}
sub := ctx.Header.Get("X-Test-Sub")
if sub == "" {
sub = "svc:test-client"
}
claims := map[string]token.ClaimStrings{
"sub": {sub},
"scope": {scopes},
}
// Optional eIDAS serial — the ACL matches an invited co-signer on it.
if serial := ctx.Header.Get("X-Test-Serial"); serial != "" {
claims["serial_number"] = token.ClaimStrings{serial}
}
ctx.SetUser(user.New(claims))
next(ctx)
}
}
}