plugin http fixes and tests (#7929)
Этот коммит содержится в:
коммит произвёл
Harrison Healey
родитель
9791c3e8d2
Коммит
1c1c184bed
@@ -4,9 +4,15 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
)
|
||||
|
||||
func TestPluginKeyValueStore(t *testing.T) {
|
||||
@@ -33,3 +39,62 @@ func TestPluginKeyValueStore(t *testing.T) {
|
||||
assert.Nil(t, th.App.DeletePluginKey(pluginId, "postkey"))
|
||||
assert.Nil(t, th.App.DeletePluginKey(pluginId, "notrealkey"))
|
||||
}
|
||||
|
||||
func TestServePluginRequest(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.PluginSettings.Enable = false })
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("GET", "/plugins/foo/bar", nil)
|
||||
th.App.ServePluginRequest(w, r)
|
||||
assert.Equal(t, http.StatusNotImplemented, w.Result().StatusCode)
|
||||
}
|
||||
|
||||
func TestHandlePluginRequest(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.PluginSettings.Enable = false
|
||||
*cfg.ServiceSettings.EnableUserAccessTokens = true
|
||||
})
|
||||
|
||||
token, err := th.App.CreateUserAccessToken(&model.UserAccessToken{
|
||||
UserId: th.BasicUser.Id,
|
||||
})
|
||||
require.Nil(t, err)
|
||||
|
||||
var assertions func(*http.Request)
|
||||
router := mux.NewRouter()
|
||||
router.HandleFunc("/plugins/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}/{anything:.*}", func(_ http.ResponseWriter, r *http.Request) {
|
||||
th.App.servePluginRequest(nil, r, func(_ http.ResponseWriter, r *http.Request) {
|
||||
assertions(r)
|
||||
})
|
||||
})
|
||||
|
||||
r := httptest.NewRequest("GET", "/plugins/foo/bar", nil)
|
||||
r.Header.Add("Authorization", "Bearer "+token.Token)
|
||||
assertions = func(r *http.Request) {
|
||||
assert.Equal(t, "/bar", r.URL.Path)
|
||||
assert.Equal(t, th.BasicUser.Id, r.Header.Get("Mattermost-User-Id"))
|
||||
}
|
||||
router.ServeHTTP(nil, r)
|
||||
|
||||
r = httptest.NewRequest("GET", "/plugins/foo/bar?a=b&access_token="+token.Token+"&c=d", nil)
|
||||
assertions = func(r *http.Request) {
|
||||
assert.Equal(t, "/bar", r.URL.Path)
|
||||
assert.Equal(t, "a=b&c=d", r.URL.RawQuery)
|
||||
assert.Equal(t, th.BasicUser.Id, r.Header.Get("Mattermost-User-Id"))
|
||||
}
|
||||
router.ServeHTTP(nil, r)
|
||||
|
||||
r = httptest.NewRequest("GET", "/plugins/foo/bar?a=b&access_token=asdf&c=d", nil)
|
||||
assertions = func(r *http.Request) {
|
||||
assert.Equal(t, "/bar", r.URL.Path)
|
||||
assert.Equal(t, "a=b&c=d", r.URL.RawQuery)
|
||||
assert.Empty(t, r.Header.Get("Mattermost-User-Id"))
|
||||
}
|
||||
router.ServeHTTP(nil, r)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user