MM-37417: Push notification authz fix. (#18009)

* MM-37417: Push notification authz fix.

* MM-37417: Tests new app method.
Этот коммит содержится в:
Martin Kraft
2021-07-28 13:47:45 -04:00
коммит произвёл GitHub
родитель 285de45988
Коммит 37b1e6d048
7 изменённых файлов: 99 добавлений и 34 удалений

Просмотреть файл

@@ -4,6 +4,7 @@
package api4
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
@@ -733,10 +734,11 @@ func TestServerBusy503(t *testing.T) {
}
func TestPushNotificationAck(t *testing.T) {
th := Setup(t)
th := Setup(t).InitBasic()
api := Init(th.App, th.Server.Router)
session, _ := th.App.GetSession(th.Client.AuthToken)
defer th.TearDown()
t.Run("should return error when the ack body is not passed", func(t *testing.T) {
handler := api.ApiHandler(pushNotificationAck)
resp := httptest.NewRecorder()
@@ -747,4 +749,20 @@ func TestPushNotificationAck(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, resp.Code)
assert.NotNil(t, resp.Body)
})
t.Run("should return error when the ack post is not authorized for the user", func(t *testing.T) {
privateChannel := th.CreateChannelWithClient(th.SystemAdminClient, model.ChannelTypePrivate)
privatePost := th.CreatePostWithClient(th.SystemAdminClient, privateChannel)
handler := api.ApiHandler(pushNotificationAck)
resp := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/api/v4/notifications/ack", nil)
req.Header.Set(model.HeaderAuth, "Bearer "+session.Token)
req.Body = ioutil.NopCloser(bytes.NewBufferString(fmt.Sprintf(`{"id":"123", "is_id_loaded":true, "post_id":"%s"}`, privatePost.Id)))
handler.ServeHTTP(resp, req)
assert.Equal(t, http.StatusForbidden, resp.Code)
fmt.Printf("DEBUG/resp.Body: %+v\n", resp.Body)
assert.NotNil(t, resp.Body)
})
}