Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat: added middleware folder and standardized test names

=6047471ee2b6
diff --git a/backend-go/CLAUDE.md b/backend-go/CLAUDE.md
index c219b17b3e..d12f979a7e 100644
--- a/backend-go/CLAUDE.md
+++ b/backend-go/CLAUDE.md
@@ -29,6 +29,10 @@ Never edit `gen/` directories — always regenerate.
errutil.Forbidden("Access denied").WithErrf("FuncName: permission check failed")
```
- **Lean code**: Inline single-use helpers.
+- **Test naming**: Follow `Test<FunctionName>_<Scenario>` pattern. Examples:
+ - `TestSymmetricEncrypt_RoundTrip`
+ - `TestGetSecrets_FilterByTags`
+ - `TestStart_Success`
## Architecture
@@ -44,18 +48,22 @@ internal/
│ │ └── pglock/ # PostgreSQL advisory locks
│ └── redis/ # Redis client
├── libs/
+│ ├── crypto/ # Cryptographic utilities (cipher, hash, sign)
+│ ├── errutil/ # Error types and formatting
│ ├── fn/ # Generic utilities (AppendUnique, etc.)
-│ └── errutil/ # Error types and formatting
+│ └── logutil/ # Logging utilities (context handler)
├── server/
│ ├── design/ # Goa DSL definitions
│ ├── gen/ # Goa generated (DO NOT EDIT)
-│ └── api/ # Endpoint implementations + DI wiring
+│ ├── api/ # Endpoint implementations + DI wiring
+│ └── middlewares/ # HTTP middlewares (httpinfo, etc.)
├── services/ # Shared business logic (auth, permission, kms)
├── keystore/ # Redis key-value operations
└── testutil/ # Test infra (testcontainers)
```
**Two tiers:**
+
- `server/api/` — Goa endpoint implementations. 1:1 with endpoints. Not imported elsewhere.
- `services/` — Shared logic. No Goa dependency. Directly uses `pg.DB` for queries.
@@ -66,6 +74,7 @@ internal/
### Query Helpers
**`qb` package** — Query builders for dynamic SQL:
+
```go
// WHERE builder (for SELECT with named args)
args := pgx.NamedArgs{"folderID": folderID}
@@ -98,8 +107,9 @@ sql, args := qb.Delete("secrets").
```
**`sqln` package** — Transform flat LEFT JOIN rows into nested structs:
+
```go
-grouper := sqln.Grouper[Secret, uuid.UUID]{
+secrets := sqln.GroupRows(flatSecrets, sqln.Grouper[Secret, uuid.UUID]{
Key: func(s *Secret) uuid.UUID { return s.ID },
Merge: func(existing, row *Secret) {
if len(row.Tags) > 0 {
@@ -107,11 +117,11 @@ grouper := sqln.Grouper[Secret, uuid.UUID]{
func(t Tag) uuid.UUID { return t.ID })
}
},
-}
-secrets := sqln.GroupRows(flatSecrets, grouper)
+})
```
**`pglock` package** — PostgreSQL advisory locks:
+
```go
tx, _ := db.Primary().Begin(ctx)
lock, err := pglock.AcquireBlockingLock(ctx, tx, "my_lock_id")
@@ -129,6 +139,7 @@ lock.Release(ctx) // commits transaction
### Writing Raw Queries
Use `pgx.NamedArgs` for parameterized queries:
+
```go
query := `
SELECT id, key, encrypted_value
@@ -143,6 +154,7 @@ rows, err := db.Replica().Query(ctx, query, args)
**Table aliases**: Use clear, readable aliases — not single letters. For example, `memberships m`, `membership_roles mr`, `organizations o`, `additional_privileges ap`. Aliases should be obvious abbreviations of the table name.
Scan rows using `pgx.CollectRows` or manual scanning:
+
```go
secrets, err := pgx.CollectRows(rows, pgx.RowToStructByName[Secret])
```
diff --git a/backend-go/internal/hsm/hsm_test.go b/backend-go/internal/hsm/hsm_test.go
index dc4a0106f5..bd71856dde 100644
--- a/backend-go/internal/hsm/hsm_test.go
+++ b/backend-go/internal/hsm/hsm_test.go
@@ -56,12 +56,12 @@ func setupHSM(t *testing.T) *hsm.Service {
return svc
}
-func TestStartServiceAndIsActive(t *testing.T) {
+func TestStartService_IsActive(t *testing.T) {
svc := setupHSM(t)
assert.True(t, svc.IsActive())
}
-func TestEncryptDecryptRoundTrip(t *testing.T) {
+func TestEncrypt_RoundTrip(t *testing.T) {
svc := setupHSM(t)
plaintext := []byte("hello from the HSM")
@@ -77,7 +77,7 @@ func TestEncryptDecryptRoundTrip(t *testing.T) {
assert.Equal(t, plaintext, got)
}
-func TestEncryptDecryptEmptyData(t *testing.T) {
+func TestEncrypt_EmptyData(t *testing.T) {
svc := setupHSM(t)
blob, err := svc.Encrypt([]byte{})
@@ -88,7 +88,7 @@ func TestEncryptDecryptEmptyData(t *testing.T) {
assert.Equal(t, []byte{}, got)
}
-func TestEncryptDecryptLargeData(t *testing.T) {
+func TestEncrypt_LargeData(t *testing.T) {
svc := setupHSM(t)
plaintext := make([]byte, 500)
@@ -104,7 +104,7 @@ func TestEncryptDecryptLargeData(t *testing.T) {
assert.Equal(t, plaintext, got)
}
-func TestDecryptTamperedHMAC(t *testing.T) {
+func TestDecrypt_TamperedHMAC(t *testing.T) {
svc := setupHSM(t)
blob, err := svc.Encrypt([]byte("tamper test"))
@@ -117,7 +117,7 @@ func TestDecryptTamperedHMAC(t *testing.T) {
assert.Error(t, err)
}
-func TestDecryptTamperedCiphertext(t *testing.T) {
+func TestDecrypt_TamperedCiphertext(t *testing.T) {
svc := setupHSM(t)
blob, err := svc.Encrypt([]byte("tamper ciphertext"))
@@ -130,7 +130,7 @@ func TestDecryptTamperedCiphertext(t *testing.T) {
assert.Error(t, err)
}
-func TestDecryptTamperedIV(t *testing.T) {
+func TestDecrypt_TamperedIV(t *testing.T) {
svc := setupHSM(t)
blob, err := svc.Encrypt([]byte("tamper iv"))
@@ -143,14 +143,14 @@ func TestDecryptTamperedIV(t *testing.T) {
assert.Error(t, err)
}
-func TestDecryptTooShort(t *testing.T) {
+func TestDecrypt_TooShort(t *testing.T) {
svc := setupHSM(t)
_, err := svc.Decrypt([]byte("short"))
assert.Error(t, err)
}
-func TestRandomBytes(t *testing.T) {
+func TestRandomBytes_ReturnsUniqueBytes(t *testing.T) {
svc := setupHSM(t)
a, err := svc.RandomBytes(32)
@@ -164,7 +164,7 @@ func TestRandomBytes(t *testing.T) {
assert.NotEqual(t, a, b)
}
-func TestEncryptProducesDifferentBlobs(t *testing.T) {
+func TestEncrypt_ProducesDifferentBlobs(t *testing.T) {
svc := setupHSM(t)
blob1, err := svc.Encrypt([]byte("same"))
diff --git a/backend-go/internal/libs/crypto/cipher/cipher_test.go b/backend-go/internal/libs/crypto/cipher/cipher_test.go
index e3debc67e2..50fe322b6c 100644
--- a/backend-go/internal/libs/crypto/cipher/cipher_test.go
+++ b/backend-go/internal/libs/crypto/cipher/cipher_test.go
@@ -8,7 +8,7 @@ import (
"github.com/infisical/api/internal/libs/crypto/cipher"
)
-func TestEncryptDecryptRoundTrip(t *testing.T) {
+func TestSymmetricEncrypt_RoundTrip(t *testing.T) {
key := make([]byte, 32) // AES-256
if _, err := rand.Read(key); err != nil {
t.Fatal(err)
@@ -36,7 +36,7 @@ func TestEncryptDecryptRoundTrip(t *testing.T) {
}
}
-func TestDecryptWrongKey(t *testing.T) {
+func TestSymmetricDecrypt_WrongKey(t *testing.T) {
key1 := make([]byte, 32)
key2 := make([]byte, 32)
rand.Read(key1)
@@ -53,14 +53,14 @@ func TestDecryptWrongKey(t *testing.T) {
}
}
-func TestDecryptTooShort(t *testing.T) {
+func TestSymmetricDecrypt_TooShort(t *testing.T) {
_, err := cipher.SymmetricDecrypt([]byte("short"), make([]byte, 32))
if err == nil {
t.Fatal("expected error for short ciphertext")
}
}
-func TestEncryptProducesDifferentBlobs(t *testing.T) {
+func TestSymmetricEncrypt_ProducesDifferentBlobs(t *testing.T) {
key := make([]byte, 32)
rand.Read(key)
@@ -72,7 +72,7 @@ func TestEncryptProducesDifferentBlobs(t *testing.T) {
}
}
-func TestAES128Key(t *testing.T) {
+func TestSymmetricEncrypt_AES128Key(t *testing.T) {
key := make([]byte, 16) // AES-128
rand.Read(key)
diff --git a/backend-go/internal/libs/crypto/sign/sign_test.go b/backend-go/internal/libs/crypto/sign/sign_test.go
index 25ce687570..59bcecc8f1 100644
--- a/backend-go/internal/libs/crypto/sign/sign_test.go
+++ b/backend-go/internal/libs/crypto/sign/sign_test.go
@@ -6,7 +6,7 @@ import (
"github.com/infisical/api/internal/libs/crypto/sign"
)
-func TestRSAGenerateAndPublicKey(t *testing.T) {
+func TestGeneratePrivateKey_RSA4096(t *testing.T) {
privPEM, err := sign.GeneratePrivateKey(sign.RSA4096)
if err != nil {
t.Fatalf("GeneratePrivateKey(RSA4096): %v", err)
@@ -22,7 +22,7 @@ func TestRSAGenerateAndPublicKey(t *testing.T) {
}
}
-func TestECCGenerateAndPublicKey(t *testing.T) {
+func TestGeneratePrivateKey_ECCP256(t *testing.T) {
privPEM, err := sign.GeneratePrivateKey(sign.ECCP256)
if err != nil {
t.Fatalf("GeneratePrivateKey(ECCP256): %v", err)
@@ -38,7 +38,7 @@ func TestECCGenerateAndPublicKey(t *testing.T) {
}
}
-func TestRSAPKCS1SignVerify(t *testing.T) {
+func TestSign_RSAPKCS1(t *testing.T) {
privPEM, _ := sign.GeneratePrivateKey(sign.RSA4096)
pubDER, _ := sign.PublicKeyFromPrivate(privPEM)
data := []byte("hello KMS")
@@ -72,7 +72,7 @@ func TestRSAPKCS1SignVerify(t *testing.T) {
}
}
-func TestRSAPSSSignVerify(t *testing.T) {
+func TestSign_RSAPSS(t *testing.T) {
privPEM, _ := sign.GeneratePrivateKey(sign.RSA4096)
pubDER, _ := sign.PublicKeyFromPrivate(privPEM)
data := []byte("PSS test")
@@ -97,7 +97,7 @@ func TestRSAPSSSignVerify(t *testing.T) {
}
}
-func TestRSAPSSRejectsDigest(t *testing.T) {
+func TestSign_RSAPSSRejectsDigest(t *testing.T) {
privPEM, _ := sign.GeneratePrivateKey(sign.RSA4096)
_, err := sign.Sign([]byte("data"), privPEM, sign.RSASSA_PSS_SHA256, true)
@@ -106,7 +106,7 @@ func TestRSAPSSRejectsDigest(t *testing.T) {
}
}
-func TestECDSASignVerify(t *testing.T) {
+func TestSign_ECDSA(t *testing.T) {
privPEM, _ := sign.GeneratePrivateKey(sign.ECCP256)
pubDER, _ := sign.PublicKeyFromPrivate(privPEM)
data := []byte("ECDSA test")
@@ -131,7 +131,7 @@ func TestECDSASignVerify(t *testing.T) {
}
}
-func TestAlgorithmKeyMismatch(t *testing.T) {
+func TestSign_AlgorithmKeyMismatch(t *testing.T) {
rsaKey, _ := sign.GeneratePrivateKey(sign.RSA4096)
ecKey, _ := sign.GeneratePrivateKey(sign.ECCP256)
@@ -148,7 +148,7 @@ func TestAlgorithmKeyMismatch(t *testing.T) {
}
}
-func TestDigestSignVerify(t *testing.T) {
+func TestSign_WithDigest(t *testing.T) {
privPEM, _ := sign.GeneratePrivateKey(sign.RSA4096)
pubDER, _ := sign.PublicKeyFromPrivate(privPEM)
@@ -170,7 +170,7 @@ func TestDigestSignVerify(t *testing.T) {
}
}
-func TestUnsupportedAlgorithm(t *testing.T) {
+func TestSign_UnsupportedAlgorithm(t *testing.T) {
privPEM, _ := sign.GeneratePrivateKey(sign.RSA4096)
_, err := sign.Sign([]byte("data"), privPEM, "INVALID_ALGO", false)
@@ -179,7 +179,7 @@ func TestUnsupportedAlgorithm(t *testing.T) {
}
}
-func TestUnsupportedKeyAlgorithm(t *testing.T) {
+func TestGeneratePrivateKey_UnsupportedAlgorithm(t *testing.T) {
_, err := sign.GeneratePrivateKey("DSA_2048")
if err == nil {
t.Fatal("expected error for unsupported key algorithm")
diff --git a/backend-go/internal/libs/errutil/error_test.go b/backend-go/internal/libs/errutil/error_test.go
index d9aff92393..661f09875f 100644
--- a/backend-go/internal/libs/errutil/error_test.go
+++ b/backend-go/internal/libs/errutil/error_test.go
@@ -14,7 +14,7 @@ import (
"github.com/infisical/api/internal/libs/errutil"
)
-func TestConstructors(t *testing.T) {
+func TestErrorConstructors_ReturnsCorrectStatusAndName(t *testing.T) {
t.Parallel()
tests := []struct {
@@ -51,7 +51,7 @@ func TestConstructors(t *testing.T) {
}
}
-func TestErrorString(t *testing.T) {
+func TestError_String(t *testing.T) {
t.Parallel()
t.Run("without cause", func(t *testing.T) {
@@ -75,7 +75,7 @@ func TestErrorString(t *testing.T) {
})
}
-func TestUnwrap(t *testing.T) {
+func TestError_Unwrap(t *testing.T) {
t.Parallel()
cause := fmt.Errorf("root cause")
@@ -86,7 +86,7 @@ func TestUnwrap(t *testing.T) {
}
}
-func TestErrorsAs(t *testing.T) {
+func TestError_ErrorsAs(t *testing.T) {
t.Parallel()
t.Run("direct", func(t *testing.T) {
@@ -135,7 +135,7 @@ func TestErrorsAs(t *testing.T) {
})
}
-func TestWithDetails(t *testing.T) {
+func TestError_WithDetails(t *testing.T) {
t.Parallel()
details := map[string]string{"field": "name"}
@@ -154,7 +154,7 @@ func TestWithDetails(t *testing.T) {
}
}
-func TestFormatter(t *testing.T) {
+func TestNewFormatter_FormatsErrors(t *testing.T) {
t.Parallel()
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
diff --git a/backend-go/internal/server/api/platform/projects/projects_integration_test.go b/backend-go/internal/server/api/platform/projects/projects_integration_test.go
index 899eec84aa..1962a39314 100644
--- a/backend-go/internal/server/api/platform/projects/projects_integration_test.go
+++ b/backend-go/internal/server/api/platform/projects/projects_integration_test.go
@@ -53,7 +53,7 @@ func setupMux(t *testing.T) *testutil.TestMux {
return mux
}
-func TestGetHealth(t *testing.T) {
+func TestGetHealth_ReturnsOK(t *testing.T) {
mux := setupMux(t)
mux.Request(t, http.MethodGet, "/api/v1/platform/projects/health").
@@ -61,7 +61,7 @@ func TestGetHealth(t *testing.T) {
ExpectStatus(http.StatusOK)
}
-func TestCreateProject(t *testing.T) {
+func TestCreateProject_Success(t *testing.T) {
mux := setupMux(t)
var result map[string]any
diff --git a/backend-go/internal/server/httpinfo.go b/backend-go/internal/server/middlewares/httpinfo.go
similarity index 98%
rename from backend-go/internal/server/httpinfo.go
rename to backend-go/internal/server/middlewares/httpinfo.go
index e331b8b36b..aaa908cd49 100644
--- a/backend-go/internal/server/httpinfo.go
+++ b/backend-go/internal/server/middlewares/httpinfo.go
@@ -1,4 +1,4 @@
-package server
+package middlewares
import (
"net"
diff --git a/backend-go/internal/server/server.go b/backend-go/internal/server/server.go
index 25a2f6f3aa..e2ff7ecec9 100644
--- a/backend-go/internal/server/server.go
+++ b/backend-go/internal/server/server.go
@@ -12,6 +12,7 @@ import (
"github.com/infisical/api/internal/libs/errutil"
"github.com/infisical/api/internal/libs/requestid"
"github.com/infisical/api/internal/server/api"
+ "github.com/infisical/api/internal/server/middlewares"
)
type Server struct {
@@ -48,7 +49,7 @@ func (s *Server) Listen(ctx context.Context, addr string, wg *sync.WaitGroup, er
var handler http.Handler = s.mux
// the order is other way around. Last one wraps the previous one, so request ID is set before logging.
handler = requestLogger(handler, s.logger)
- handler = HTTPInfoMiddleware(handler)
+ handler = middlewares.HTTPInfoMiddleware(handler)
handler = requestid.Middleware(handler)
srv := &http.Server{
diff --git a/backend-go/internal/services/auth/classify_test.go b/backend-go/internal/services/auth/classify_test.go
index f283507524..488ff27754 100644
--- a/backend-go/internal/services/auth/classify_test.go
+++ b/backend-go/internal/services/auth/classify_test.go
@@ -14,7 +14,7 @@ func makeJWT(claims map[string]any) string {
return header + "." + payloadEnc + ".fakesignature"
}
-func TestClassifyToken(t *testing.T) {
+func TestClassifyToken_VariousInputTypes(t *testing.T) {
t.Run("service token", func(t *testing.T) {
mode := ClassifyToken("st.abc123.secretpart")
if mode != AuthModeServiceToken {
diff --git a/backend-go/internal/services/auth/ip_test.go b/backend-go/internal/services/auth/ip_test.go
index 6a5560a4f4..3d4de83194 100644
--- a/backend-go/internal/services/auth/ip_test.go
+++ b/backend-go/internal/services/auth/ip_test.go
@@ -4,7 +4,7 @@ import (
"testing"
)
-func TestParseTrustedIPs(t *testing.T) {
+func TestParseTrustedIPs_VariousInputs(t *testing.T) {
t.Run("empty string returns nil", func(t *testing.T) {
ips, err := parseTrustedIPs("")
if err != nil {
@@ -40,7 +40,7 @@ func TestParseTrustedIPs(t *testing.T) {
})
}
-func TestCheckIPAgainstBlocklist(t *testing.T) {
+func TestCheckIPAgainstBlocklist_VariousScenarios(t *testing.T) {
t.Run("empty list allows all", func(t *testing.T) {
err := checkIPAgainstBlocklist("1.2.3.4", nil)
if err != nil {
@@ -140,7 +140,7 @@ func TestCheckIPAgainstBlocklist(t *testing.T) {
})
}
-func TestItoa(t *testing.T) {
+func TestItoa_ConvertsIntToString(t *testing.T) {
tests := []struct {
input int
expected string
diff --git a/backend-go/internal/services/kms/kms_integration_test.go b/backend-go/internal/services/kms/kms_integration_test.go
index abcfbb8d5f..5db42840a8 100644
--- a/backend-go/internal/services/kms/kms_integration_test.go
+++ b/backend-go/internal/services/kms/kms_integration_test.go
@@ -64,7 +64,7 @@ func startedService(t *testing.T) *kms.Service {
// Start
// ==========================================================================
-func TestStart(t *testing.T) {
+func TestStart_Success(t *testing.T) {
svc := setupService(t)
err := svc.Start(context.Background(), false)
require.NoError(t, err)
diff --git a/backend-go/internal/services/permission/operators_test.go b/backend-go/internal/services/permission/operators_test.go
index 2e1b638b74..5d9b8486e0 100644
--- a/backend-go/internal/services/permission/operators_test.go
+++ b/backend-go/internal/services/permission/operators_test.go
@@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
)
-func TestOpGlob(t *testing.T) {
+func TestOpGlob_PatternMatching(t *testing.T) {
t.Parallel()
tests := []struct {
@@ -72,7 +72,7 @@ func TestOpGlob(t *testing.T) {
}
}
-func TestPermissionFieldOps(t *testing.T) {
+func TestPermissionFieldOps_ContainsExpectedOperators(t *testing.T) {
t.Parallel()
ops := PermissionFieldOps()