Files
zitadel/internal/api/grpc/org/v2/org_test.go
T
Livio SpringandGitHub 30d91cde58 fix(api): correctly handle organization_id on creation (#11396)
# Which Problems Are Solved

While moving the some requests from the v2beta organization service to
v2, the `org_id` property of the `AddOrganization` request was
deprecated in favor of `organization_id`. However, the internal logic
was adjusted, resulting in a ingored `organization_id`.

# How the Problems Are Solved

- properly favor `organization_id` over `org_id` and added a note to the
proto.

# Additional Changes

none

# Additional Context

- closes #11269
- requires backport to v4.x
2026-01-22 06:19:16 +00:00

219 lines
5.1 KiB
Go

package org
import (
"testing"
"time"
"connectrpc.com/connect"
"github.com/muhlemmer/gu"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/zitadel/zitadel/internal/command"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/zerrors"
"github.com/zitadel/zitadel/pkg/grpc/object/v2"
"github.com/zitadel/zitadel/pkg/grpc/org/v2"
"github.com/zitadel/zitadel/pkg/grpc/user/v2"
)
func Test_addOrganizationRequestToCommand(t *testing.T) {
type args struct {
request *org.AddOrganizationRequest
}
tests := []struct {
name string
args args
want *command.OrgSetup
wantErr error
}{
{
name: "nil user",
args: args{
request: &org.AddOrganizationRequest{
Name: "name",
Admins: []*org.AddOrganizationRequest_Admin{
{},
},
},
},
wantErr: zerrors.ThrowUnimplementedf(nil, "ORGv2-SD2r1", "userType oneOf %T in method AddOrganization not implemented", nil),
},
{
name: "custom org ID",
args: args{
request: &org.AddOrganizationRequest{
Name: "custom org ID",
OrgId: gu.Ptr("org-ID"),
},
},
want: &command.OrgSetup{
Name: "custom org ID",
CustomDomain: "",
Admins: []*command.OrgSetupAdmin{},
OrgID: "org-ID",
},
},
{
name: "custom organization ID",
args: args{
request: &org.AddOrganizationRequest{
Name: "custom org ID",
OrganizationId: gu.Ptr("organization-ID"),
},
},
want: &command.OrgSetup{
Name: "custom org ID",
CustomDomain: "",
Admins: []*command.OrgSetupAdmin{},
OrgID: "organization-ID",
},
},
{
name: "custom organization ID (precedence over org ID)",
args: args{
request: &org.AddOrganizationRequest{
Name: "custom org ID",
OrganizationId: gu.Ptr("organization-ID"),
OrgId: gu.Ptr("org-ID"), // will be ignored in favor of OrganizationId
},
},
want: &command.OrgSetup{
Name: "custom org ID",
CustomDomain: "",
Admins: []*command.OrgSetupAdmin{},
OrgID: "organization-ID",
},
},
{
name: "user ID",
args: args{
request: &org.AddOrganizationRequest{
Name: "name",
Admins: []*org.AddOrganizationRequest_Admin{
{
UserType: &org.AddOrganizationRequest_Admin_UserId{
UserId: "userID",
},
Roles: nil,
},
},
},
},
want: &command.OrgSetup{
Name: "name",
CustomDomain: "",
Admins: []*command.OrgSetupAdmin{
{
ID: "userID",
},
},
},
},
{
name: "human user",
args: args{
request: &org.AddOrganizationRequest{
Name: "name",
Admins: []*org.AddOrganizationRequest_Admin{
{
UserType: &org.AddOrganizationRequest_Admin_Human{
Human: &user.AddHumanUserRequest{
Profile: &user.SetHumanProfile{
GivenName: "firstname",
FamilyName: "lastname",
},
Email: &user.SetHumanEmail{
Email: "[email protected]",
},
},
},
Roles: nil,
},
},
},
},
want: &command.OrgSetup{
Name: "name",
CustomDomain: "",
Admins: []*command.OrgSetupAdmin{
{
Human: &command.AddHuman{
Username: "[email protected]",
FirstName: "firstname",
LastName: "lastname",
Email: command.Email{
Address: "[email protected]",
},
Metadata: make([]*command.AddMetadataEntry, 0),
Links: make([]*command.AddLink, 0),
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := addOrganizationRequestToCommand(tt.args.request)
assert.ErrorIs(t, err, tt.wantErr)
assert.Equal(t, tt.want, got)
})
}
}
func Test_createdOrganizationToPb(t *testing.T) {
now := time.Now()
type args struct {
createdOrg *command.CreatedOrg
}
tests := []struct {
name string
args args
want *connect.Response[org.AddOrganizationResponse]
wantErr error
}{
{
name: "human user with phone and email code",
args: args{
createdOrg: &command.CreatedOrg{
ObjectDetails: &domain.ObjectDetails{
Sequence: 1,
EventDate: now,
ResourceOwner: "orgID",
},
OrgAdmins: []command.OrgAdmin{
&command.CreatedOrgAdmin{
ID: "id",
EmailCode: gu.Ptr("emailCode"),
PhoneCode: gu.Ptr("phoneCode"),
},
},
},
},
want: connect.NewResponse(&org.AddOrganizationResponse{
Details: &object.Details{
Sequence: 1,
ChangeDate: timestamppb.New(now),
ResourceOwner: "orgID",
},
OrganizationId: "orgID",
CreatedAdmins: []*org.AddOrganizationResponse_CreatedAdmin{
{
UserId: "id",
EmailCode: gu.Ptr("emailCode"),
PhoneCode: gu.Ptr("phoneCode"),
},
},
}),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := createdOrganizationToPb(tt.args.createdOrg)
assert.ErrorIs(t, err, tt.wantErr)
assert.Equal(t, tt.want, got)
})
}
}