Expose an admin-only endpoint that validates the saved plugin configuration against the Shelfmark server, and surface it in the plugin settings UI. Co-authored-by: Cursor <cursoragent@cursor.com>
122 lines
3.4 KiB
Go
122 lines
3.4 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/plugin"
|
|
"github.com/mattermost/mattermost/server/public/plugin/plugintest"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestHandleTestConnection_Forbidden(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
api.On("HasPermissionTo", "user1", model.PermissionManageSystem).Return(false)
|
|
|
|
p := &Plugin{}
|
|
p.API = api
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/config/test-connection", nil)
|
|
req.Header.Set("Mattermost-User-Id", "user1")
|
|
rec := httptest.NewRecorder()
|
|
|
|
p.ServeHTTP(&plugin.Context{}, rec, req)
|
|
|
|
assert.Equal(t, http.StatusForbidden, rec.Code)
|
|
api.AssertExpectations(t)
|
|
}
|
|
|
|
func TestHandleTestConnection_MissingHost(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
api.On("HasPermissionTo", "admin1", model.PermissionManageSystem).Return(true)
|
|
|
|
p := &Plugin{}
|
|
p.API = api
|
|
p.setConfiguration(&configuration{})
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/config/test-connection", nil)
|
|
req.Header.Set("Mattermost-User-Id", "admin1")
|
|
rec := httptest.NewRecorder()
|
|
|
|
p.ServeHTTP(&plugin.Context{}, rec, req)
|
|
|
|
assert.Equal(t, http.StatusBadGateway, rec.Code)
|
|
|
|
var resp testConnectionResponse
|
|
require.NoError(t, json.NewDecoder(rec.Body).Decode(&resp))
|
|
assert.False(t, resp.Success)
|
|
assert.Contains(t, resp.Error, "shelfmark server URL must be configured")
|
|
}
|
|
|
|
func TestHandleTestConnection_Success(t *testing.T) {
|
|
srv := newShelfmarkServer(t)
|
|
|
|
api := &plugintest.API{}
|
|
api.On("HasPermissionTo", "admin1", model.PermissionManageSystem).Return(true)
|
|
|
|
p := &Plugin{}
|
|
p.API = api
|
|
p.setConfiguration(&configuration{
|
|
ShelfmarkHost: srv.URL,
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/config/test-connection", nil)
|
|
req.Header.Set("Mattermost-User-Id", "admin1")
|
|
rec := httptest.NewRecorder()
|
|
|
|
p.ServeHTTP(&plugin.Context{}, rec, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
var resp testConnectionResponse
|
|
require.NoError(t, json.NewDecoder(rec.Body).Decode(&resp))
|
|
assert.True(t, resp.Success)
|
|
assert.Contains(t, resp.Message, "Successfully connected")
|
|
}
|
|
|
|
func TestHandleTestConnection_Unreachable(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
api.On("HasPermissionTo", "admin1", model.PermissionManageSystem).Return(true)
|
|
|
|
p := &Plugin{}
|
|
p.API = api
|
|
p.setConfiguration(&configuration{
|
|
ShelfmarkHost: "http://127.0.0.1:1",
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/config/test-connection", nil)
|
|
req.Header.Set("Mattermost-User-Id", "admin1")
|
|
rec := httptest.NewRecorder()
|
|
|
|
p.ServeHTTP(&plugin.Context{}, rec, req)
|
|
|
|
assert.Equal(t, http.StatusBadGateway, rec.Code)
|
|
|
|
body, err := io.ReadAll(rec.Body)
|
|
require.NoError(t, err)
|
|
|
|
var resp testConnectionResponse
|
|
require.NoError(t, json.Unmarshal(body, &resp))
|
|
assert.False(t, resp.Success)
|
|
assert.Contains(t, resp.Error, "Cannot reach the Shelfmark server.")
|
|
}
|
|
|
|
func TestTestShelfmarkConnection_Success(t *testing.T) {
|
|
srv := newShelfmarkServer(t)
|
|
|
|
config := &configuration{ShelfmarkHost: srv.URL}
|
|
err := config.testShelfmarkConnection()
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestTestShelfmarkConnection_MissingHost(t *testing.T) {
|
|
config := &configuration{}
|
|
err := config.testShelfmarkConnection()
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "shelfmark server URL must be configured")
|
|
}
|