From acc909307294ac404174291f942c8dc93b129075 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Wed, 2 Oct 2024 07:37:04 +0200 Subject: [PATCH] Error when generating invite codes for a confirmed remote cluster (#28337) --- api/v4/source/remoteclusters.yaml | 2 ++ server/channels/api4/remote_cluster.go | 5 +++++ server/channels/api4/remote_cluster_test.go | 14 +++++++++++++- server/i18n/en.json | 4 ++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/api/v4/source/remoteclusters.yaml b/api/v4/source/remoteclusters.yaml index fcec494b47..86d004fb54 100644 --- a/api/v4/source/remoteclusters.yaml +++ b/api/v4/source/remoteclusters.yaml @@ -115,6 +115,8 @@ description: | The password generated by the server if none was sent on the create request + "400": + $ref: "#/components/responses/BadRequest" "401": $ref: "#/components/responses/Unauthorized" "403": diff --git a/server/channels/api4/remote_cluster.go b/server/channels/api4/remote_cluster.go index b9215e3a46..2852fc07ef 100644 --- a/server/channels/api4/remote_cluster.go +++ b/server/channels/api4/remote_cluster.go @@ -533,6 +533,11 @@ func generateRemoteClusterInvite(c *Context, w http.ResponseWriter, r *http.Requ return } + if rc.IsConfirmed() { + c.Err = model.NewAppError("generateRemoteClusterInvite", "api.remote_cluster.generate_invite_cluster_is_confirmed", nil, "", http.StatusBadRequest) + return + } + inviteCode, invErr := c.App.CreateRemoteClusterInvite(rc.RemoteId, url, rc.Token, password) if invErr != nil { c.Err = invErr diff --git a/server/channels/api4/remote_cluster_test.go b/server/channels/api4/remote_cluster_test.go index b436157e0d..3638f9aee9 100644 --- a/server/channels/api4/remote_cluster_test.go +++ b/server/channels/api4/remote_cluster_test.go @@ -358,7 +358,7 @@ func TestGenerateRemoteClusterInvite(t *testing.T) { newRC := &model.RemoteCluster{ Name: "remotecluster", - SiteURL: "http://example.com", + SiteURL: model.SiteURLPending + model.NewId(), Token: model.NewId(), } @@ -429,6 +429,18 @@ func TestGenerateRemoteClusterInvite(t *testing.T) { require.Equal(t, rc.RemoteId, invite.RemoteId) require.Equal(t, rc.Token, invite.Token) }) + + t.Run("should return bad request if the cluster is already confirmed", func(t *testing.T) { + rc.SiteURL = "http://example.com" + savedRC, appErr := th.App.UpdateRemoteCluster(rc) + require.Nil(t, appErr) + require.Equal(t, rc.SiteURL, savedRC.SiteURL) + + inviteCode, resp, err := th.SystemAdminClient.GenerateRemoteClusterInvite(context.Background(), rc.RemoteId, password) + CheckBadRequestStatus(t, resp) + require.Error(t, err) + require.Empty(t, inviteCode) + }) } func TestGetRemoteCluster(t *testing.T) { diff --git a/server/i18n/en.json b/server/i18n/en.json index 101b75238d..10b243a862 100644 --- a/server/i18n/en.json +++ b/server/i18n/en.json @@ -2754,6 +2754,10 @@ "id": "api.remote_cluster.encrypt_invite_error", "translation": "Could not encrypt the remote cluster invite using the provided password" }, + { + "id": "api.remote_cluster.generate_invite_cluster_is_confirmed", + "translation": "Cannot generate invite code for a confirmed cluster" + }, { "id": "api.remote_cluster.get.app_error", "translation": "We encountered an error retrieving a secure connection."