/
envtest.go
135 lines (113 loc) · 3.83 KB
/
envtest.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
// Copyright 2023 The Authors (see AUTHORS file)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package envtest
import (
"context"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
kms "cloud.google.com/go/kms/apiv1"
"cloud.google.com/go/kms/apiv1/kmspb"
"google.golang.org/api/option"
"google.golang.org/grpc"
"github.com/abcxyz/jvs/assets"
"github.com/abcxyz/jvs/pkg/config"
"github.com/abcxyz/jvs/pkg/justification"
"github.com/abcxyz/jvs/pkg/jvscrypto"
"github.com/abcxyz/jvs/pkg/testutil"
"github.com/abcxyz/pkg/cfgloader"
"github.com/abcxyz/pkg/logging"
"github.com/abcxyz/pkg/renderer"
pkgtestutil "github.com/abcxyz/pkg/testutil"
)
// ServerConfigResponse is the response from creating a server config.
type ServerConfigResponse struct {
Config *config.UIServiceConfig
Renderer *renderer.Renderer
Processor *justification.Processor
}
// BuildFormRequest builds an http request and http response recorder for the
// given form values (expressed as url.Values). It sets the proper headers and
// response types to post as a form and expect HTML in return.
func BuildFormRequest(ctx context.Context, tb testing.TB, meth, pth string, v *url.Values) (*httptest.ResponseRecorder, *http.Request) {
tb.Helper()
var body io.Reader
if v != nil {
body = strings.NewReader(v.Encode())
}
req, err := http.NewRequestWithContext(ctx, meth, pth, body)
if err != nil {
tb.Fatalf("failed to create request: %v", err)
}
// default headers
req.Header.Set("Accept", "text/html")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Referer", "/back")
return httptest.NewRecorder(), req
}
// NewServerConfig creates a new server configuration. It creates all the keys,
// databases, and cacher, but does not actually start the server. All cleanup is
// scheduled by t.Cleanup.
func NewServerConfig(tb testing.TB, port string, allowlist []string, devMode bool) *ServerConfigResponse {
tb.Helper()
logger := logging.TestLogger(tb)
ctx := logging.WithLogger(context.Background(), logger)
key := "projects/[PROJECT]/locations/[LOCATION]/keyRings/[KEY_RING]/cryptoKeys/[CRYPTO_KEY]"
version := key + "/cryptoKeyVersions/[VERSION]"
uiCfg := &config.UIServiceConfig{
JustificationConfig: &config.JustificationConfig{
ProjectID: "example-project",
KeyName: key,
Port: port,
DevMode: devMode,
},
Allowlist: allowlist,
}
if err := cfgloader.Load(ctx, uiCfg); err != nil {
tb.Fatal(err)
}
// Create the renderer.
r, err := renderer.New(ctx, assets.ServerFS(),
renderer.WithDebug(true),
renderer.WithOnError(func(err error) {
logger.ErrorContext(ctx, "failed to render", "error", err)
}))
if err != nil {
tb.Fatal(err)
}
// Mock KMS.
mockKMS := testutil.NewMockKeyManagementServer(key, version, jvscrypto.PrimaryLabelPrefix+"[VERSION]"+"-0")
serv := grpc.NewServer()
kmspb.RegisterKeyManagementServiceServer(serv, mockKMS)
_, conn := pkgtestutil.FakeGRPCServer(tb, func(s *grpc.Server) {
kmspb.RegisterKeyManagementServiceServer(s, mockKMS)
})
opt := option.WithGRPCConn(conn)
tb.Cleanup(func() {
conn.Close()
})
kmsClient, err := kms.NewKeyManagementClient(ctx, opt)
if err != nil {
tb.Fatal(err)
}
p := justification.NewProcessor(kmsClient, uiCfg.JustificationConfig)
return &ServerConfigResponse{
Config: uiCfg,
Renderer: r,
Processor: p,
}
}