Skip to content

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

move role creation to a fixture

adilson627098dd4da2
diff --git a/tests/docs/client.md b/tests/docs/client.md
index 43b0e4f241..2459aa83f0 100644
--- a/tests/docs/client.md
+++ b/tests/docs/client.md
@@ -43,7 +43,7 @@ the spec is correct, and every Node API consumer reads it.
- Narrow the action to the schema that fails. The existing one replaces only the
`createProjectRole` body's permission items with a plain object, and the suite sends
- rules as JSON (`rolePermission` in `suites/secretmanager/secrets/helpers_test.go`).
+ rules as JSON (`fixture.Permission` in `fixture/role.go`).
- Overlays are strict: an action whose target matches nothing fails generation, so a
renamed route shows up instead of silently undoing the patch.
- Remove the action once an oapi-codegen release fixes the bug.
diff --git a/tests/docs/writing-tests.md b/tests/docs/writing-tests.md
index 3a534c44d0..8a0feda493 100644
--- a/tests/docs/writing-tests.md
+++ b/tests/docs/writing-tests.md
@@ -118,6 +118,7 @@ h.InstanceAdmin(t) // super admin; Isolated only
proj := fixture.NewProject(t, tn, fixture.WithProjectType("secret-manager"))
proj.NewUser(t, fixture.WithPrincipalName("bob"), fixture.WithRoles("admin"))
proj.Grant(t, principal, "developer")
+proj.NewRole(t, "reader", fixture.Permission{Subject: "secrets", Action: []string{"read"}})
secretmanager.CreateSecret(t, proj, "dev", "DB_URL", "value", secretmanager.As(member))
secretmanager.GetSecret(t, proj, "dev", "DB_URL")
diff --git a/tests/fixture/role.go b/tests/fixture/role.go
new file mode 100644
index 0000000000..0c4cef5d01
--- /dev/null
+++ b/tests/fixture/role.go
@@ -0,0 +1,33 @@
+package fixture
+
+import (
+ "bytes"
+ "encoding/json"
+ "net/http"
+ "testing"
+
+ "github.com/Infisical/infisical/tests/internal/apierr"
+ "github.com/stretchr/testify/require"
+)
+
+// Permission is one rule of a project role. It is sent as raw JSON because the generated
+// body cannot express it; see docs/client.md.
+type Permission struct {
+ Subject string `json:"subject"`
+ Action []string `json:"action"`
+ Conditions map[string]any `json:"conditions,omitempty"`
+ Inverted bool `json:"inverted,omitempty"`
+}
+
+// NewRole creates a custom project role. Hand its slug to WithRoles or Grant.
+func (p *Project) NewRole(tt *testing.T, slug string, permissions ...Permission) {
+ tt.Helper()
+
+ body, err := json.Marshal(map[string]any{"slug": slug, "name": slug, "permissions": permissions})
+ require.NoError(tt, err)
+ res, err := p.tn.Admin.API.CreateProjectRoleWithBodyWithResponse(tt.Context(), p.ID,
+ "application/json", bytes.NewReader(body))
+ require.NoErrorf(tt, err, "creating role %s in %s", slug, p.Slug)
+ require.Equalf(tt, http.StatusOK, res.StatusCode(), "creating role %s in %s: %s",
+ slug, p.Slug, apierr.Body(res.Body))
+}
diff --git a/tests/suites/secretmanager/secrets/helpers_test.go b/tests/suites/secretmanager/secrets/helpers_test.go
index 35d5d44ebb..daf58d3e3b 100644
--- a/tests/suites/secretmanager/secrets/helpers_test.go
+++ b/tests/suites/secretmanager/secrets/helpers_test.go
@@ -1,8 +1,6 @@
package secrets_test
import (
- "bytes"
- "encoding/json"
"net/http"
"testing"
@@ -30,31 +28,14 @@ func newTag(t *testing.T, proj *fixture.Project, slug string) string {
return res.JSON200.Tag.Id.String()
}
-type rolePermission struct {
- Subject string `json:"subject"`
- Action []string `json:"action"`
- Conditions map[string]any `json:"conditions,omitempty"`
- Inverted bool `json:"inverted,omitempty"`
-}
-
-func readSecretsTagged(slugs ...string) rolePermission {
- return rolePermission{
+func readSecretsTagged(slugs ...string) fixture.Permission {
+ return fixture.Permission{
Subject: "secrets",
Action: []string{"read"},
Conditions: map[string]any{"secretTags": map[string]any{"$in": slugs}},
}
}
-func newRole(t *testing.T, proj *fixture.Project, slug string, permissions ...rolePermission) {
- t.Helper()
- body, err := json.Marshal(map[string]any{"slug": slug, "name": slug, "permissions": permissions})
- require.NoError(t, err)
- res, err := proj.Tenant().Admin.API.CreateProjectRoleWithBodyWithResponse(t.Context(), proj.ID,
- "application/json", bytes.NewReader(body))
- require.NoError(t, err)
- require.Equalf(t, http.StatusOK, res.StatusCode(), "creating role %s: %s", slug, apierr.Body(res.Body))
-}
-
func newTaggedSecret(t *testing.T, proj *fixture.Project, name string, tagIDs ...string) {
t.Helper()
res, err := proj.Tenant().Admin.API.CreateSecretV4WithResponse(t.Context(), name,
diff --git a/tests/suites/secretmanager/secrets/permission_test.go b/tests/suites/secretmanager/secrets/permission_test.go
index 5d3f35218c..bb3097ea33 100644
--- a/tests/suites/secretmanager/secrets/permission_test.go
+++ b/tests/suites/secretmanager/secrets/permission_test.go
@@ -21,7 +21,7 @@ func TestSecret_ListTagPermissions(t *testing.T) {
newTaggedSecret(t, proj, "ALLOWED_KEY", newTag(t, proj, "read-allowed"))
newTaggedSecret(t, proj, "OTHER_KEY", newTag(t, proj, "other"))
secretmanager.CreateSecret(t, proj, "dev", "UNTAGGED", "v")
- newRole(t, proj, "tag-reader", readSecretsTagged("read-allowed"))
+ proj.NewRole(t, "tag-reader", readSecretsTagged("read-allowed"))
reader := proj.NewMachineIdentity(t, fixture.WithRoles("tag-reader"))
// Action
@@ -41,7 +41,7 @@ func TestSecret_ListTagPermissions(t *testing.T) {
newTaggedSecret(t, proj, "RESTRICTED_KEY", plain, restricted)
deny := readSecretsTagged("restricted")
deny.Inverted = true
- newRole(t, proj, "restricted-tag-reader", rolePermission{Subject: "secrets", Action: []string{"read"}}, deny)
+ proj.NewRole(t, "restricted-tag-reader", fixture.Permission{Subject: "secrets", Action: []string{"read"}}, deny)
reader := proj.NewMachineIdentity(t, fixture.WithRoles("restricted-tag-reader"))
// Action
@@ -61,9 +61,9 @@ func TestSecret_ListTagPermissions(t *testing.T) {
newTaggedSecret(t, proj, "RESTRICTED_KEY", plain, restricted)
deny := readSecretsTagged("restricted")
deny.Inverted = true
- newRole(t, proj, "restricted-tag-reader",
- rolePermission{Subject: "secrets", Action: []string{"read"}},
- rolePermission{Subject: "secret-imports", Action: []string{"read"}},
+ proj.NewRole(t, "restricted-tag-reader",
+ fixture.Permission{Subject: "secrets", Action: []string{"read"}},
+ fixture.Permission{Subject: "secret-imports", Action: []string{"read"}},
deny)
reader := proj.NewUser(t, fixture.WithRoles("restricted-tag-reader"))
@@ -82,8 +82,8 @@ func TestSecret_ListTagPermissions(t *testing.T) {
newTaggedSecret(t, proj, "SHARED_KEY", newTag(t, proj, "shared"))
deny := readSecretsTagged("shared")
deny.Inverted = true
- newRole(t, proj, "shared-allow", readSecretsTagged("shared"))
- newRole(t, proj, "shared-deny", deny)
+ proj.NewRole(t, "shared-allow", readSecretsTagged("shared"))
+ proj.NewRole(t, "shared-deny", deny)
allowOnly := proj.NewMachineIdentity(t, fixture.WithRoles("shared-allow"))
both := proj.NewMachineIdentity(t, fixture.WithRoles("shared-allow", "shared-deny"))