[MM-60886] Fix TypeAssertionError in api4.addChannelMember (#28976)
* Fix TypeAssertionError in api4.addChannelMember * Fix
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
a65ba84697
Коммит
b4f337f191
@@ -1780,7 +1780,14 @@ func addChannelMember(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
for _, userId := range interfaceIds {
|
||||
userIds = append(userIds, userId.(string))
|
||||
uid, isString := userId.(string)
|
||||
|
||||
if !isString || !model.IsValidId(uid) {
|
||||
c.SetInvalidParam("user_id in user_ids")
|
||||
return
|
||||
}
|
||||
|
||||
userIds = append(userIds, uid)
|
||||
}
|
||||
} else {
|
||||
userId, ok2 := props["user_id"].(string)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -3491,6 +3492,38 @@ func TestAddChannelMember(t *testing.T) {
|
||||
_, _, err = client.AddChannelMember(context.Background(), privateChannel.Id, user.Id)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("invalid request data", func(t *testing.T) {
|
||||
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
|
||||
// correct type for user ids (string) but invalid value.
|
||||
requestBody := map[string]any{"user_ids": []string{"invalid", user2.Id}}
|
||||
requestData, err := json.Marshal(requestBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
res, err := client.DoAPIPost(context.Background(), "/channels/"+publicChannel.Id+"/members", string(requestData))
|
||||
if client == th.LocalClient {
|
||||
require.EqualError(t, err, "Invalid or missing user_id in request body.")
|
||||
} else {
|
||||
require.EqualError(t, err, "Invalid or missing user_id in user_ids in request body.")
|
||||
}
|
||||
defer res.Body.Close()
|
||||
io.Copy(io.Discard, res.Body)
|
||||
|
||||
// invalid type for user ids (should be string).
|
||||
requestBody = map[string]any{"user_ids": []any{45, user2.Id}}
|
||||
requestData, err = json.Marshal(requestBody)
|
||||
require.NoError(t, err)
|
||||
|
||||
res, err = client.DoAPIPost(context.Background(), "/channels/"+privateChannel.Id+"/members", string(requestData))
|
||||
if client == th.LocalClient {
|
||||
require.EqualError(t, err, "Invalid or missing user_id in request body.")
|
||||
} else {
|
||||
require.EqualError(t, err, "Invalid or missing user_id in user_ids in request body.")
|
||||
}
|
||||
defer res.Body.Close()
|
||||
io.Copy(io.Discard, res.Body)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestAddChannelMembers(t *testing.T) {
|
||||
|
||||
Ссылка в новой задаче
Block a user