Gh 24291 api meesage unclear when parameters are incorrect (#26457)

Co-authored-by: Mattermost Build <build@mattermost.com>
This commit is contained in:
Ezekiel 2024-04-10 17:04:41 +08:00 committed by GitHub
parent 1806ba2cc7
commit 9fee0393c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 47 additions and 9 deletions

View File

@ -10,6 +10,7 @@ import (
"strings" "strings"
"github.com/mattermost/mattermost/server/public/model" "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/public/shared/mlog"
"github.com/mattermost/mattermost/server/v8/channels/app" "github.com/mattermost/mattermost/server/v8/channels/app"
"github.com/mattermost/mattermost/server/v8/channels/audit" "github.com/mattermost/mattermost/server/v8/channels/audit"
@ -89,6 +90,16 @@ func createChannel(c *Context, w http.ResponseWriter, r *http.Request) {
return 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) auditRec := c.MakeAuditRecord("createChannel", audit.Fail)
defer c.LogAuditRec(auditRec) defer c.LogAuditRec(auditRec)
audit.AddEventParameterAuditable(auditRec, "channel", channel) audit.AddEventParameterAuditable(auditRec, "channel", channel)

View File

@ -57,6 +57,7 @@ func TestCreateCategoryForTeamForUser(t *testing.T) {
TeamId: th.BasicTeam.Id, TeamId: th.BasicTeam.Id,
Type: model.ChannelTypeOpen, Type: model.ChannelTypeOpen,
Name: "testchannel", Name: "testchannel",
DisplayName: "testchannel",
}) })
require.NoError(t, err) require.NoError(t, err)
@ -329,6 +330,7 @@ func TestUpdateCategoryForTeamForUser(t *testing.T) {
TeamId: th.BasicTeam.Id, TeamId: th.BasicTeam.Id,
Type: model.ChannelTypeOpen, Type: model.ChannelTypeOpen,
Name: "testchannel", Name: "testchannel",
DisplayName: "testchannel",
}) })
require.NoError(t, err) require.NoError(t, err)
@ -494,6 +496,7 @@ func TestUpdateCategoriesForTeamForUser(t *testing.T) {
TeamId: th.BasicTeam.Id, TeamId: th.BasicTeam.Id,
Type: model.ChannelTypeOpen, Type: model.ChannelTypeOpen,
Name: "testchannel", Name: "testchannel",
DisplayName: "testchannel",
}) })
require.NoError(t, err) require.NoError(t, err)

View File

@ -144,6 +144,22 @@ func TestCreateChannel(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, *groupConstrainedChannel.GroupConstrained, *rchannel.GroupConstrained, "GroupConstrained flags do not match") 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) { func TestUpdateChannel(t *testing.T) {

View File

@ -307,6 +307,14 @@
"id": "api.channel.create_channel.max_channel_limit.app_error", "id": "api.channel.create_channel.max_channel_limit.app_error",
"translation": "Unable to create more than {{.MaxChannelsPerTeam}} channels for current team." "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", "id": "api.channel.create_default_channels.off_topic",
"translation": "Off-Topic" "translation": "Off-Topic"