mirror of
https://github.com/mattermost/mattermost.git
synced 2026-02-03 20:40:00 -05:00
It was a good decision in hindsight to keep the public module as 0.x because this would have been a breaking change again. https://mattermost.atlassian.net/browse/MM-53032 ```release-note Changed the Go module path from github.com/mattermost/mattermost-server/server/v8 to github.com/mattermost/mattermost/server/v8. For the public facing module, it's path is also changed from github.com/mattermost/mattermost-server/server/public to github.com/mattermost/mattermost/server/public ```
58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package plugintest_test
|
|
|
|
import (
|
|
"fmt"
|
|
io "io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/plugin"
|
|
"github.com/mattermost/mattermost/server/public/plugin/plugintest"
|
|
)
|
|
|
|
type HelloUserPlugin struct {
|
|
plugin.MattermostPlugin
|
|
}
|
|
|
|
func (p *HelloUserPlugin) ServeHTTP(context *plugin.Context, w http.ResponseWriter, r *http.Request) {
|
|
userID := r.Header.Get("Mattermost-User-Id")
|
|
user, err := p.API.GetUser(userID)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
p.API.LogError(err.Error())
|
|
return
|
|
}
|
|
|
|
fmt.Fprintf(w, "Welcome back, %s!", user.Username)
|
|
}
|
|
|
|
func Example() {
|
|
t := &testing.T{}
|
|
user := &model.User{
|
|
Id: model.NewId(),
|
|
Username: "billybob",
|
|
}
|
|
|
|
api := &plugintest.API{}
|
|
api.On("GetUser", user.Id).Return(user, nil)
|
|
defer api.AssertExpectations(t)
|
|
|
|
p := &HelloUserPlugin{}
|
|
p.SetAPI(api)
|
|
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
r.Header.Add("Mattermost-User-Id", user.Id)
|
|
p.ServeHTTP(&plugin.Context{}, w, r)
|
|
body, err := io.ReadAll(w.Result().Body)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "Welcome back, billybob!", string(body))
|
|
}
|