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 {