Add early validation in OnConfigurationChange to verify the configured channel exists in Mattermost and the Shelfmark server is reachable, giving admins immediate feedback instead of discovering issues at runtime. - Add Ping() method to shelfmark.Client for lightweight reachability check - Add validateConfiguration() on Plugin that checks channel + Shelfmark - Add i18n messages for channel not found and Shelfmark unreachable - Add unit tests for Ping and validateConfiguration Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
134 lines
3.6 KiB
Go
134 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"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 TestExecuteCommand_EmptyCommand(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
api.On("GetUser", "user1").Return(&model.User{Locale: "en"}, nil)
|
|
|
|
p := &Plugin{}
|
|
p.API = api
|
|
resp, appErr := p.ExecuteCommand(&plugin.Context{}, &model.CommandArgs{Command: "", UserId: "user1"})
|
|
|
|
require.Nil(t, appErr)
|
|
require.NotNil(t, resp)
|
|
assert.Equal(t, model.CommandResponseTypeEphemeral, resp.ResponseType)
|
|
assert.Equal(t, "Empty command received.", resp.Text)
|
|
}
|
|
|
|
func TestExecuteCommand_UnknownCommand(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
api.On("GetUser", "user1").Return(&model.User{Locale: "es"}, nil)
|
|
|
|
p := &Plugin{}
|
|
p.API = api
|
|
resp, appErr := p.ExecuteCommand(&plugin.Context{}, &model.CommandArgs{Command: "/unknown", UserId: "user1"})
|
|
|
|
require.Nil(t, appErr)
|
|
require.NotNil(t, resp)
|
|
assert.Contains(t, resp.Text, "Comando desconocido")
|
|
}
|
|
|
|
func TestOnConfigurationChanged_NilClient(t *testing.T) {
|
|
p := &Plugin{}
|
|
p.shelfmarkClient = nil
|
|
|
|
// Should not panic when shelfmarkClient is nil.
|
|
assert.NotPanics(t, func() {
|
|
p.onConfigurationChanged()
|
|
})
|
|
}
|
|
|
|
func TestOnConfigurationChange_LogsInvalidConfig(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
api.On("LoadPluginConfiguration", &configuration{}).Return(nil)
|
|
api.On("LogWarn", "Plugin configuration is invalid", "error", "shelfmark server URL must be configured").Return()
|
|
|
|
p := &Plugin{}
|
|
p.API = api
|
|
|
|
err := p.OnConfigurationChange()
|
|
require.NoError(t, err)
|
|
api.AssertCalled(t, "LogWarn", "Plugin configuration is invalid", "error", "shelfmark server URL must be configured")
|
|
}
|
|
|
|
func newShelfmarkServer(t *testing.T) *httptest.Server {
|
|
t.Helper()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
require.NoError(t, json.NewEncoder(w).Encode(map[string]bool{"auth_required": false}))
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return srv
|
|
}
|
|
|
|
func TestValidateConfiguration_Valid(t *testing.T) {
|
|
srv := newShelfmarkServer(t)
|
|
|
|
api := &plugintest.API{}
|
|
api.On("GetChannel", "ch1").Return(&model.Channel{Id: "ch1"}, nil)
|
|
|
|
p := &Plugin{}
|
|
p.API = api
|
|
|
|
config := &configuration{
|
|
ShelfmarkHost: srv.URL,
|
|
ChannelID: "ch1",
|
|
}
|
|
err := p.validateConfiguration(config)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestValidateConfiguration_InvalidStruct(t *testing.T) {
|
|
p := &Plugin{}
|
|
|
|
config := &configuration{
|
|
ShelfmarkHost: "",
|
|
ChannelID: "ch1",
|
|
}
|
|
err := p.validateConfiguration(config)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "shelfmark server URL must be configured")
|
|
}
|
|
|
|
func TestValidateConfiguration_ChannelNotFound(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
api.On("GetChannel", "bad-channel").Return(nil, &model.AppError{Message: "not found"})
|
|
|
|
p := &Plugin{}
|
|
p.API = api
|
|
|
|
config := &configuration{
|
|
ShelfmarkHost: "http://localhost:9999",
|
|
ChannelID: "bad-channel",
|
|
}
|
|
err := p.validateConfiguration(config)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "The configured channel ID does not exist.")
|
|
}
|
|
|
|
func TestValidateConfiguration_ShelfmarkUnreachable(t *testing.T) {
|
|
api := &plugintest.API{}
|
|
api.On("GetChannel", "ch1").Return(&model.Channel{Id: "ch1"}, nil)
|
|
|
|
p := &Plugin{}
|
|
p.API = api
|
|
|
|
config := &configuration{
|
|
ShelfmarkHost: "http://127.0.0.1:1",
|
|
ChannelID: "ch1",
|
|
}
|
|
err := p.validateConfiguration(config)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "Cannot reach the Shelfmark server.")
|
|
}
|