Skip to content

Commit b22f78e

Browse files
Consolidate test constants and use existing timeout constants
- Add comprehensive test constants for hosts, ports, and test data - Replace hardcoded test values with named constants across authz test suite - Use existing defaultInventoryTimeout instead of hardcoded 3-second timeout - Improve test maintainability and consistency - Address goconst linting issues in test files Test constants added: - testCSR, testUserIP for consistent test data - testInventoryHost, testOPAHost, testAuthzPort for service endpoints - testCAName constant usage in PKI tests Timeout improvement: - Use defaultInventoryTimeout (3s) instead of hardcoded value in inventory client Co-authored-by: Amp <[email protected]> Amp-Thread-ID: https://ampcode.com/threads/T-5be4213f-26eb-400c-bb7b-d4c79b7ee6fe
1 parent 5d932eb commit b22f78e

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

pkg/pki/device_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const (
1818
benchKeyName = "bench.key"
1919
msgGenerateKeyFail = "Failed to generate key: %v"
2020
testDeviceCN = "test-device"
21+
testCAName = "test-ca"
2122
)
2223

2324
func TestGenerateSigningKey(t *testing.T) {
@@ -429,7 +430,7 @@ func TestCertificateExpiry(t *testing.T) {
429430
certPath := filepath.Join(tmpDir, "ca.pem")
430431
keyPath := filepath.Join(tmpDir, "ca-key.pem")
431432

432-
ca, err := LoadOrCreateCA(certPath, keyPath, "test-ca", 24*time.Hour)
433+
ca, err := LoadOrCreateCA(certPath, keyPath, testCAName, 24*time.Hour)
433434
if err != nil {
434435
t.Fatalf("Failed to create CA: %v", err)
435436
}

services/authz/server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ func configureInventoryClient(cfg Config) (*http.Client, error) {
724724
}
725725

726726
return telemetry.WrapClient(&http.Client{
727-
Timeout: 3 * time.Second,
727+
Timeout: defaultInventoryTimeout,
728728
Transport: transport,
729729
}), nil
730730
}

services/authz/server/server_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ const (
2121
decisionKey = "decision"
2222
testRejectsNonPOST = "rejects non-POST methods"
2323
testRejectsInvalidJSON = "rejects invalid JSON"
24+
testCSR = "test-csr"
25+
testUserIP = "192.168.1.1"
26+
testInventoryHost = "test-inventory:8080"
27+
testOPAHost = "test-opa:8181"
28+
testAuthzPort = ":8443"
2429
)
2530

2631
// TestServer_healthHandler tests the health endpoint
@@ -80,7 +85,7 @@ func TestServer_verifyHandler(t *testing.T) {
8085
reqBody := verifyRequest{
8186
Token: "invalid.jwt.token",
8287
DeviceID: testDeviceID,
83-
ClientIP: "192.168.1.1",
88+
ClientIP: testUserIP,
8489
}
8590
body, err := json.Marshal(reqBody)
8691
if err != nil {
@@ -137,10 +142,10 @@ func TestServer_tailscaleStatusHandler(t *testing.T) {
137142
// createTestServer creates a minimal server for testing
138143
func createTestServer(_ *testing.T) *Server {
139144
cfg := Config{
140-
HTTPAddr: ":8443",
145+
HTTPAddr: testAuthzPort,
141146
GoogleClientID: "test-client-id",
142-
OPAURL: "http://test-opa:8181",
143-
InventoryAPI: "http://test-inventory:8080",
147+
OPAURL: "http://" + testOPAHost,
148+
InventoryAPI: "http://" + testInventoryHost,
144149
}
145150

146151
return &Server{
@@ -217,7 +222,7 @@ func TestServer_envoyAuthHandler(t *testing.T) {
217222
"request": map[string]interface{}{
218223
"http": map[string]interface{}{
219224
"headers": map[string]string{
220-
"x-device-id": "test-device",
225+
"x-device-id": testDeviceID,
221226
},
222227
},
223228
},
@@ -408,7 +413,7 @@ func TestServer_evaluateOPA(t *testing.T) {
408413
// createTestServerWithMocks creates a test server with mock OPA and inventory URLs
409414
func createTestServerWithMocks(_ *testing.T, opaURL, inventoryURL string) *Server {
410415
cfg := Config{
411-
HTTPAddr: ":8443",
416+
HTTPAddr: testAuthzPort,
412417
GoogleClientID: "test-client-id",
413418
OPAURL: opaURL,
414419
InventoryAPI: inventoryURL,
@@ -540,7 +545,7 @@ func TestServer_deviceCertHandler(t *testing.T) {
540545
t.Run("rejects empty device ID", func(t *testing.T) {
541546
reqBody := map[string]string{
542547
"device_id": "",
543-
"csr": "test-csr",
548+
"csr": testCSR,
544549
}
545550
body, err := json.Marshal(reqBody)
546551
if err != nil {

0 commit comments

Comments
 (0)