From 9fee0393c36f022744092543d334e4ffca412cb9 Mon Sep 17 00:00:00 2001 From: Ezekiel Date: Wed, 10 Apr 2024 17:04:41 +0800 Subject: [PATCH] Gh 24291 api meesage unclear when parameters are incorrect (#26457) Co-authored-by: Mattermost Build --- server/channels/api4/channel.go | 11 ++++++++++ server/channels/api4/channel_category_test.go | 21 +++++++++++-------- server/channels/api4/channel_test.go | 16 ++++++++++++++ server/i18n/en.json | 8 +++++++ 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/server/channels/api4/channel.go b/server/channels/api4/channel.go index cf16bb0354..3086117330 100644 --- a/server/channels/api4/channel.go +++ b/server/channels/api4/channel.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/mattermost/mattermost/server/public/model" + "github.com/mattermost/mattermost/server/public/shared/i18n" "github.com/mattermost/mattermost/server/public/shared/mlog" "github.com/mattermost/mattermost/server/v8/channels/app" "github.com/mattermost/mattermost/server/v8/channels/audit" @@ -89,6 +90,16 @@ func createChannel(c *Context, w http.ResponseWriter, r *http.Request) { return } + if channel.TeamId == "" { + c.SetInvalidParamWithDetails("team_id", i18n.T("api.channel.create_channel.missing_team_id.error")) + return + } + + if channel.DisplayName == "" { + c.SetInvalidParamWithDetails("display_name", i18n.T("api.channel.create_channel.missing_display_name.error")) + return + } + auditRec := c.MakeAuditRecord("createChannel", audit.Fail) defer c.LogAuditRec(auditRec) audit.AddEventParameterAuditable(auditRec, "channel", channel) diff --git a/server/channels/api4/channel_category_test.go b/server/channels/api4/channel_category_test.go index 9f5fafe37b..c22390d7ad 100644 --- a/server/channels/api4/channel_category_test.go +++ b/server/channels/api4/channel_category_test.go @@ -54,9 +54,10 @@ func TestCreateCategoryForTeamForUser(t *testing.T) { // Have another user create a channel that user isn't a part of channel, _, err := th.SystemAdminClient.CreateChannel(context.Background(), &model.Channel{ - TeamId: th.BasicTeam.Id, - Type: model.ChannelTypeOpen, - Name: "testchannel", + TeamId: th.BasicTeam.Id, + Type: model.ChannelTypeOpen, + Name: "testchannel", + DisplayName: "testchannel", }) require.NoError(t, err) @@ -326,9 +327,10 @@ func TestUpdateCategoryForTeamForUser(t *testing.T) { // Have another user create a channel that user isn't a part of channel, _, err := th.SystemAdminClient.CreateChannel(context.Background(), &model.Channel{ - TeamId: th.BasicTeam.Id, - Type: model.ChannelTypeOpen, - Name: "testchannel", + TeamId: th.BasicTeam.Id, + Type: model.ChannelTypeOpen, + Name: "testchannel", + DisplayName: "testchannel", }) require.NoError(t, err) @@ -491,9 +493,10 @@ func TestUpdateCategoriesForTeamForUser(t *testing.T) { // Have another user create a channel that user isn't a part of channel, _, err := th.SystemAdminClient.CreateChannel(context.Background(), &model.Channel{ - TeamId: th.BasicTeam.Id, - Type: model.ChannelTypeOpen, - Name: "testchannel", + TeamId: th.BasicTeam.Id, + Type: model.ChannelTypeOpen, + Name: "testchannel", + DisplayName: "testchannel", }) require.NoError(t, err) diff --git a/server/channels/api4/channel_test.go b/server/channels/api4/channel_test.go index 5d343f9b88..1d4c1f515e 100644 --- a/server/channels/api4/channel_test.go +++ b/server/channels/api4/channel_test.go @@ -144,6 +144,22 @@ func TestCreateChannel(t *testing.T) { require.NoError(t, err) require.Equal(t, *groupConstrainedChannel.GroupConstrained, *rchannel.GroupConstrained, "GroupConstrained flags do not match") + + t.Run("Test create channel with missing team id", func(t *testing.T) { + channel := &model.Channel{DisplayName: "Test API Name", Name: GenerateTestChannelName(), Type: model.ChannelTypeOpen, TeamId: ""} + + _, resp, err := client.CreateChannel(context.Background(), channel) + CheckErrorID(t, err, "api.context.invalid_body_param.app_error") + CheckBadRequestStatus(t, resp) + }) + + t.Run("Test create channel with missing display name", func(t *testing.T) { + channel := &model.Channel{DisplayName: "", Name: GenerateTestChannelName(), Type: model.ChannelTypeOpen, TeamId: team.Id} + + _, resp, err := client.CreateChannel(context.Background(), channel) + CheckErrorID(t, err, "api.context.invalid_body_param.app_error") + CheckBadRequestStatus(t, resp) + }) } func TestUpdateChannel(t *testing.T) { diff --git a/server/i18n/en.json b/server/i18n/en.json index d5e51cd584..53c0e7961c 100644 --- a/server/i18n/en.json +++ b/server/i18n/en.json @@ -307,6 +307,14 @@ "id": "api.channel.create_channel.max_channel_limit.app_error", "translation": "Unable to create more than {{.MaxChannelsPerTeam}} channels for current team." }, + { + "id": "api.channel.create_channel.missing_display_name.error", + "translation": "Missing display_name in request body" + }, + { + "id": "api.channel.create_channel.missing_team_id.error", + "translation": "Missing team_id in request body" + }, { "id": "api.channel.create_default_channels.off_topic", "translation": "Off-Topic"