fixup! fixup! refactor(api): moving organization API resourced based

added updateOrganization
This commit is contained in:
Iraq Jaber
2025-04-25 14:30:40 +02:00
parent 86f1154741
commit 56bacaf809
6 changed files with 111 additions and 15 deletions

View File

@@ -98,7 +98,7 @@ func (s *Server) AddOrg(ctx context.Context, req *mgmt_pb.AddOrgRequest) (*mgmt_
func (s *Server) UpdateOrg(ctx context.Context, req *mgmt_pb.UpdateOrgRequest) (*mgmt_pb.UpdateOrgResponse, error) {
ctxData := authz.GetCtxData(ctx)
org, err := s.command.ChangeOrg(ctx, ctxData.OrgID, req.Name)
org, err := s.command.UpdateOrg(ctx, ctxData.OrgID, req.Name)
if err != nil {
return nil, err
}

View File

@@ -187,6 +187,75 @@ func TestServer_CreateOrganization(t *testing.T) {
}
}
func TestServer_UpdateOrganization(t *testing.T) {
orgName := "new_org_name"
orgId, err := createOrg(orgName)
if err != nil {
assert.Fail(t, "unable to create org")
}
tests := []struct {
name string
ctx context.Context
req *org.UpdateOrganizationRequest
want *org.UpdateOrganizationResponse
wantErr bool
}{
{
name: "update org with same name",
ctx: Instance.WithAuthorization(context.Background(), integration.UserTypeOrgOwner),
req: &org.UpdateOrganizationRequest{
Id: orgId,
Name: orgName,
},
},
{
name: "update org with new name",
ctx: Instance.WithAuthorization(context.Background(), integration.UserTypeOrgOwner),
req: &org.UpdateOrganizationRequest{
Id: orgId,
Name: "new org name",
},
},
{
name: "update org with no id",
ctx: Instance.WithAuthorization(context.Background(), integration.UserTypeOrgOwner),
req: &org.UpdateOrganizationRequest{
Id: orgId,
// Name: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Client.UpdateOrganization(tt.ctx, tt.req)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
// check details
assert.NotZero(t, got.GetDetails().GetSequence())
gotCD := got.GetDetails().GetChangeDate().AsTime()
now := time.Now()
assert.WithinRange(t, gotCD, now.Add(-time.Minute), now.Add(time.Minute))
assert.NotEmpty(t, got.GetDetails().GetResourceOwner())
})
}
}
func createOrg(orgName string) (string, error) {
org, err := Client.CreateOrganization(CTX,
&org.CreateOrganizationRequest{
Name: orgName,
},
)
return org.OrganizationId, err
}
func assertCreatedAdmin(t *testing.T, expected, got *org.CreateOrganizationResponse_CreatedAdmin) {
if expected.GetUserId() != "" {
assert.NotEmpty(t, got.GetUserId())

View File

@@ -22,6 +22,17 @@ func (s *Server) CreateOrganization(ctx context.Context, request *org.CreateOrga
return createdOrganizationToPb(createdOrg)
}
func (s *Server) UpdateOrganization(ctx context.Context, request *org.UpdateOrganizationRequest) (*org.UpdateOrganizationResponse, error) {
updated_org, err := s.command.UpdateOrg(ctx, request.Id, request.Name)
if err != nil {
return nil, err
}
return &org.UpdateOrganizationResponse{
Details: object.DomainToDetailsPb(updated_org),
}, nil
}
func createOrganizationRequestToCommand(request *org.CreateOrganizationRequest) (*command.OrgSetup, error) {
admins, err := createOrganizationRequestAdminsToCommand(request.GetAdmins())
if err != nil {

View File

@@ -342,7 +342,7 @@ func (c *Commands) addOrgWithIDAndMember(ctx context.Context, name, userID, reso
return orgWriteModelToOrg(addedOrg), nil
}
func (c *Commands) ChangeOrg(ctx context.Context, orgID, name string) (*domain.ObjectDetails, error) {
func (c *Commands) UpdateOrg(ctx context.Context, orgID, name string) (*domain.ObjectDetails, error) {
name = strings.TrimSpace(name)
if orgID == "" || name == "" {
return nil, zerrors.ThrowInvalidArgument(nil, "EVENT-Mf9sd", "Errors.Org.Invalid")

View File

@@ -756,7 +756,7 @@ func TestCommandSide_ChangeOrg(t *testing.T) {
r := &Commands{
eventstore: tt.fields.eventstore,
}
_, err := r.ChangeOrg(tt.args.ctx, tt.args.orgID, tt.args.name)
_, err := r.UpdateOrg(tt.args.ctx, tt.args.orgID, tt.args.name)
if tt.res.err == nil {
assert.NoError(t, err)
}

View File

@@ -145,8 +145,13 @@ service OrganizationService {
body: "*"
};
option (zitadel.v1.auth_option) = {
permission: "org.write"
option (zitadel.protoc_gen_zitadel.v2.options) = {
auth_option: {
permission: "org.write"
}
http_response: {
success_code: 200
}
};
option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
@@ -198,17 +203,28 @@ message CreateOrganizationResponse{
}
message UpdateOrganizationRequest {
string name = 1 [
(validate.rules).string = {min_len: 1, max_len: 200},
(google.api.field_behavior) = REQUIRED,
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
min_length: 1;
max_length: 200;
example: "\"Customer 1\"";
}
];
string id = 1 [
(validate.rules).string = {min_len: 1, max_len: 200},
(google.api.field_behavior) = REQUIRED,
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
min_length: 1;
max_length: 200;
example: "\"69629012906488334\"";
description: "Organization ID of the organization you want to update."
}
];
string name = 2 [
(validate.rules).string = {min_len: 1, max_len: 200},
(google.api.field_behavior) = REQUIRED,
(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
min_length: 1;
max_length: 200;
example: "\"Customer 1\"";
}
];
}
message UpdateOrganizationResponse {
zitadel.v1.ObjectDetails details = 1;
zitadel.object.v2beta.Details details = 1;
}