feat(rt): project repository (#10789)

# Which Problems Are Solved

Add projects to the relational tables

# How the Problems Are Solved

- Define table migrations
- Define and implement Project and Project Role repositories.
- Provide projection handlers to populate the relational tables.

# Additional Changes

- Statement Builder now has a constructor which allows setting of a base
query with arguments.
- Certain operations, like Get, Update and Delete require the Primary
Key to be set as conditions. However, this requires knowledge of the
implementation and table definition. This PR proposes an additional
condition for repositories: `PrimaryKeyCondition`. This gives clarity on
the required IDs for these operations.
- Added couple of helpers to the repository package, to allow more DRY
code.
- getOne / getMany: generic functions for query execution and scanning.
- checkRestrictingColumns, checkPkCondition: simplify condition
checking, instead of using ladders of conditionals.
- Added a couple of helpers to the repository test package:
  - Transaction, savepoint and rollback helpers.
- Create instance and organization helpers for objects that depend on
them (like projects).

# Additional Context

- after https://github.com/zitadel/zitadel/pull/10809
- closes #10765
This commit is contained in:
Tim Möhlmann
2025-10-01 09:47:04 +00:00
committed by GitHub
parent 28db24fa67
commit a45908b364
21 changed files with 2512 additions and 12 deletions
@@ -19,20 +19,22 @@ import (
v2beta "github.com/zitadel/zitadel/pkg/grpc/instance/v2beta"
mgmt "github.com/zitadel/zitadel/pkg/grpc/management"
v2beta_org "github.com/zitadel/zitadel/pkg/grpc/org/v2beta"
v2beta_project "github.com/zitadel/zitadel/pkg/grpc/project/v2beta"
"github.com/zitadel/zitadel/pkg/grpc/system"
)
const ConnString = "host=localhost port=5432 user=zitadel password=zitadel dbname=zitadel sslmode=disable"
var (
dbPool *pgxpool.Pool
CTX context.Context
IAMCTX context.Context
Instance *integration.Instance
SystemClient system.SystemServiceClient
OrgClient v2beta_org.OrganizationServiceClient
AdminClient admin.AdminServiceClient
MgmtClient mgmt.ManagementServiceClient
dbPool *pgxpool.Pool
CTX context.Context
IAMCTX context.Context
Instance *integration.Instance
SystemClient system.SystemServiceClient
OrgClient v2beta_org.OrganizationServiceClient
ProjectClient v2beta_project.ProjectServiceClient
AdminClient admin.AdminServiceClient
MgmtClient mgmt.ManagementServiceClient
)
var pool database.Pool
@@ -48,6 +50,7 @@ func TestMain(m *testing.M) {
IAMCTX = Instance.WithAuthorization(ctx, integration.UserTypeIAMOwner)
SystemClient = integration.SystemClient()
OrgClient = Instance.Client.OrgV2beta
ProjectClient = Instance.Client.Projectv2Beta
AdminClient = Instance.Client.Admin
MgmtClient = Instance.Client.Mgmt
@@ -0,0 +1,94 @@
//go:build integration
package events_test
import (
"testing"
"time"
"github.com/muhlemmer/gu"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"github.com/zitadel/zitadel/backend/v3/storage/database"
"github.com/zitadel/zitadel/backend/v3/storage/database/repository"
"github.com/zitadel/zitadel/internal/integration"
v2beta_project "github.com/zitadel/zitadel/pkg/grpc/project/v2beta"
)
func TestServer_ProjectRoleReduces(t *testing.T) {
instanceID := Instance.ID()
orgID := Instance.DefaultOrg.Id
roleRepo := repository.ProjectRepository().Role()
projectRes, err := ProjectClient.CreateProject(CTX, &v2beta_project.CreateProjectRequest{
OrganizationId: orgID,
Name: integration.ProjectName(),
ProjectRoleAssertion: true,
AuthorizationRequired: true,
ProjectAccessRequired: true,
})
require.NoError(t, err)
projectID := projectRes.GetId()
_, err = ProjectClient.AddProjectRole(CTX, &v2beta_project.AddProjectRoleRequest{
ProjectId: projectID,
RoleKey: "key",
DisplayName: "display name",
Group: proto.String("group"),
})
require.NoError(t, err)
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
t.Run("add project role reduces", func(t *testing.T) {
require.EventuallyWithT(t, func(collect *assert.CollectT) {
dbRole, err := roleRepo.Get(CTX, pool, database.WithCondition(
roleRepo.PrimaryKeyCondition(instanceID, projectID, "key"),
))
require.NoError(collect, err)
require.NotNil(collect, dbRole)
assert.Equal(collect, projectID, dbRole.ProjectID)
assert.Equal(collect, "key", dbRole.Key)
assert.Equal(collect, "display name", dbRole.DisplayName)
assert.Equal(collect, gu.Ptr("group"), dbRole.RoleGroup)
assert.NotNil(collect, dbRole.CreatedAt)
assert.NotNil(collect, dbRole.UpdatedAt)
}, retryDuration, tick, "project role not found within %v: %v", retryDuration, err)
})
t.Run("update project role reduces", func(t *testing.T) {
_, err := ProjectClient.UpdateProjectRole(CTX, &v2beta_project.UpdateProjectRoleRequest{
ProjectId: projectID,
RoleKey: "key",
DisplayName: proto.String("new display name"),
Group: proto.String("new group"),
})
require.NoError(t, err)
require.EventuallyWithT(t, func(collect *assert.CollectT) {
dbRole, err := roleRepo.Get(CTX, pool, database.WithCondition(
roleRepo.PrimaryKeyCondition(instanceID, projectID, "key"),
))
require.NoError(collect, err)
require.NotNil(collect, dbRole)
assert.Equal(collect, "new display name", dbRole.DisplayName)
assert.Equal(collect, gu.Ptr("new group"), dbRole.RoleGroup)
}, retryDuration, tick, "project role not updated within %v: %v", retryDuration, err)
})
t.Run("remove project role reduces", func(t *testing.T) {
_, err := ProjectClient.RemoveProjectRole(CTX, &v2beta_project.RemoveProjectRoleRequest{
ProjectId: projectID,
RoleKey: "key",
})
require.NoError(t, err)
require.EventuallyWithT(t, func(collect *assert.CollectT) {
_, err := roleRepo.Get(CTX, pool, database.WithCondition(
roleRepo.PrimaryKeyCondition(instanceID, projectID, "key"),
))
require.ErrorIs(collect, err, database.NewNoRowFoundError(nil))
}, retryDuration, tick, "project role not deleted within %v: %v", retryDuration, err)
})
}
@@ -0,0 +1,120 @@
//go:build integration
package events_test
import (
"testing"
"time"
"github.com/muhlemmer/gu"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zitadel/zitadel/backend/v3/domain"
"github.com/zitadel/zitadel/backend/v3/storage/database"
"github.com/zitadel/zitadel/backend/v3/storage/database/repository"
"github.com/zitadel/zitadel/internal/integration"
v2beta_project "github.com/zitadel/zitadel/pkg/grpc/project/v2beta"
)
func TestServer_ProjectReduces(t *testing.T) {
instanceID := Instance.ID()
orgID := Instance.DefaultOrg.Id
projectRepo := repository.ProjectRepository()
projectName := integration.ProjectName()
createRes, err := ProjectClient.CreateProject(CTX, &v2beta_project.CreateProjectRequest{
OrganizationId: orgID,
Name: projectName,
ProjectRoleAssertion: true,
AuthorizationRequired: true,
ProjectAccessRequired: true,
})
require.NoError(t, err)
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
t.Run("create project reduces", func(t *testing.T) {
require.EventuallyWithT(t, func(collect *assert.CollectT) {
dbProject, err := projectRepo.Get(CTX, pool, database.WithCondition(
projectRepo.PrimaryKeyCondition(instanceID, createRes.GetId()),
))
require.NoError(collect, err)
assert.Equal(collect, createRes.GetId(), dbProject.ID)
assert.Equal(collect, orgID, dbProject.OrganizationID)
assert.Equal(collect, projectName, dbProject.Name)
assert.Equal(collect, domain.ProjectStateActive, dbProject.State)
assert.True(collect, dbProject.ShouldAssertRole)
assert.True(collect, dbProject.IsAuthorizationRequired)
assert.True(collect, dbProject.IsProjectAccessRequired)
assert.NotNil(collect, dbProject.CreatedAt)
assert.NotNil(collect, dbProject.UpdatedAt)
}, retryDuration, tick, "project not found within %v: %v", retryDuration, err)
})
t.Run("update project reduces", func(t *testing.T) {
_, err := ProjectClient.UpdateProject(CTX, &v2beta_project.UpdateProjectRequest{
Id: createRes.GetId(),
Name: gu.Ptr("new name"),
ProjectRoleAssertion: gu.Ptr(false),
ProjectRoleCheck: gu.Ptr(false),
HasProjectCheck: gu.Ptr(false),
PrivateLabelingSetting: gu.Ptr(v2beta_project.PrivateLabelingSetting_PRIVATE_LABELING_SETTING_ALLOW_LOGIN_USER_RESOURCE_OWNER_POLICY),
})
require.NoError(t, err)
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
dbProject, err := projectRepo.Get(CTX, pool, database.WithCondition(
projectRepo.PrimaryKeyCondition(instanceID, createRes.GetId()),
))
require.NoError(collect, err)
assert.Equal(collect, "new name", dbProject.Name)
assert.False(collect, dbProject.ShouldAssertRole)
assert.False(collect, dbProject.IsAuthorizationRequired)
assert.False(collect, dbProject.IsProjectAccessRequired)
assert.Equal(collect, int16(2), dbProject.UsedLabelingSettingOwner)
}, retryDuration, tick, "project not updated within %v: %v", retryDuration, err)
})
t.Run("(de)activate project reduces", func(t *testing.T) {
_, err := ProjectClient.DeactivateProject(CTX, &v2beta_project.DeactivateProjectRequest{
Id: createRes.GetId(),
})
require.NoError(t, err)
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
dbProject, err := projectRepo.Get(CTX, pool, database.WithCondition(
projectRepo.PrimaryKeyCondition(instanceID, createRes.GetId()),
))
require.NoError(collect, err)
assert.Equal(collect, domain.ProjectStateInactive, dbProject.State)
}, retryDuration, tick, "project not deactivated within %v: %v", retryDuration, err)
_, err = ProjectClient.ActivateProject(CTX, &v2beta_project.ActivateProjectRequest{
Id: createRes.GetId(),
})
require.NoError(t, err)
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
dbProject, err := projectRepo.Get(CTX, pool, database.WithCondition(
projectRepo.PrimaryKeyCondition(instanceID, createRes.GetId()),
))
require.NoError(collect, err)
assert.Equal(collect, domain.ProjectStateActive, dbProject.State)
}, retryDuration, tick, "project not activated within %v: %v", retryDuration, err)
})
t.Run("delete project reduces", func(t *testing.T) {
_, err := ProjectClient.DeleteProject(CTX, &v2beta_project.DeleteProjectRequest{
Id: createRes.GetId(),
})
require.NoError(t, err)
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
_, err := projectRepo.Get(CTX, pool, database.WithCondition(
projectRepo.PrimaryKeyCondition(instanceID, createRes.GetId()),
))
require.ErrorIs(collect, err, database.NewNoRowFoundError(nil))
}, retryDuration, tick, "project not deleted within %v: %v", retryDuration, err)
})
}