-
Notifications
You must be signed in to change notification settings - Fork 108
Add channel settings tab demo #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nickmisasi
wants to merge
8
commits into
master
Choose a base branch
from
channel-settings-pluggable
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+870
−22
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b82d856
Add channel settings tab smoke test and fix GetTeams resilience
nickmisasi 47d11b0
Rename channel settings tab from 'Smoke Test' to 'Demo Plugin'
nickmisasi 2b819ee
Channel settings tab: host save bar API and fa-plug sidebar icon
nickmisasi 0728245
Use plugin public icon for channel settings tab (match user settings)
nickmisasi 0db03b5
feat: rewrite channel settings to schema-based webapp API (#212)
nickmisasi 35de514
Revert OnActivate GetTeams failure to fatal error
nickmisasi e2208af
Merge remote-tracking branch 'origin/master' into channel-settings-pl…
nickmisasi 4e870d1
Drop host-injected theme/webSocketClient from custom channel settings…
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
|
|
||
| "github.com/gorilla/mux" | ||
| "github.com/mattermost/mattermost/server/public/model" | ||
| ) | ||
|
|
||
| // KV keys are kept well under the 50-char limit (channel IDs are 26 chars). | ||
| func csSchemaKey(channelID string) string { return "cs_schema_" + channelID } | ||
| func csCustomKey(channelID string) string { return "cs_custom_" + channelID } | ||
|
|
||
| // customState is the typed payload for the fully-custom channel settings tab. | ||
| type customState struct { | ||
| Note string `json:"note"` | ||
| PinGreeting bool `json:"pinGreeting"` | ||
| } | ||
|
|
||
| // requireUser enforces that the request is from an authenticated user. It | ||
| // returns false if it already wrote an error response. | ||
| func (p *Plugin) requireUser(w http.ResponseWriter, r *http.Request) bool { | ||
| if r.Header.Get("Mattermost-User-ID") == "" { | ||
| http.Error(w, "Not authorized", http.StatusUnauthorized) | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| // requireChannelReader enforces auth and read access to the channel for read | ||
| // paths. It returns the authenticated user ID, or false if it already wrote an | ||
| // error response. | ||
| func (p *Plugin) requireChannelReader(w http.ResponseWriter, r *http.Request, channelID string) (string, bool) { | ||
| if !p.requireUser(w, r) { | ||
| return "", false | ||
| } | ||
| userID := r.Header.Get("Mattermost-User-ID") | ||
|
|
||
| if !p.API.HasPermissionToChannel(userID, channelID, model.PermissionReadChannel) { | ||
| http.Error(w, "Forbidden", http.StatusForbidden) | ||
| return "", false | ||
| } | ||
|
|
||
| return userID, true | ||
| } | ||
|
|
||
| // requireChannelManager enforces auth and channel-properties permission for | ||
| // write paths. It returns the authenticated user ID, or false if it already | ||
| // wrote an error response. | ||
| func (p *Plugin) requireChannelManager(w http.ResponseWriter, r *http.Request, channelID string) (string, bool) { | ||
| if !p.requireUser(w, r) { | ||
| return "", false | ||
| } | ||
| userID := r.Header.Get("Mattermost-User-ID") | ||
|
|
||
| channel, appErr := p.API.GetChannel(channelID) | ||
| if appErr != nil { | ||
| p.API.LogError("Failed to get channel for channel settings", "channel_id", channelID, "err", appErr.Error()) | ||
| http.Error(w, "Internal server error", http.StatusInternalServerError) | ||
| return "", false | ||
| } | ||
|
|
||
| permission := model.PermissionManagePublicChannelProperties | ||
| if channel.Type == model.ChannelTypePrivate { | ||
| permission = model.PermissionManagePrivateChannelProperties | ||
| } | ||
|
|
||
| if !p.API.HasPermissionToChannel(userID, channelID, permission) { | ||
| http.Error(w, "Forbidden", http.StatusForbidden) | ||
| return "", false | ||
| } | ||
|
|
||
| return userID, true | ||
| } | ||
|
|
||
| func (p *Plugin) handleGetChannelSettingsSchema(w http.ResponseWriter, r *http.Request) { | ||
| channelID := mux.Vars(r)["channel_id"] | ||
| if _, ok := p.requireChannelReader(w, r, channelID); !ok { | ||
| return | ||
| } | ||
|
|
||
| values := map[string]string{} | ||
| if err := p.client.KV.Get(csSchemaKey(channelID), &values); err != nil { | ||
| p.API.LogError("Failed to get channel settings schema", "channel_id", channelID, "err", err.Error()) | ||
| http.Error(w, "Internal server error", http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
nickmisasi marked this conversation as resolved.
|
||
| if values == nil { | ||
| values = map[string]string{} | ||
| } | ||
|
|
||
| p.writeJSON(w, values) | ||
| } | ||
|
|
||
| func (p *Plugin) handleSaveChannelSettingsSchema(w http.ResponseWriter, r *http.Request) { | ||
| channelID := mux.Vars(r)["channel_id"] | ||
| if _, ok := p.requireChannelManager(w, r, channelID); !ok { | ||
| return | ||
| } | ||
|
|
||
| var values map[string]string | ||
| if err := json.NewDecoder(r.Body).Decode(&values); err != nil { | ||
| http.Error(w, "Invalid request body", http.StatusBadRequest) | ||
| return | ||
| } | ||
| defer r.Body.Close() | ||
|
|
||
| if _, err := p.client.KV.Set(csSchemaKey(channelID), values); err != nil { | ||
| p.API.LogError("Failed to save channel settings schema", "channel_id", channelID, "err", err.Error()) | ||
| http.Error(w, "Internal server error", http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| p.writeJSON(w, values) | ||
| } | ||
|
|
||
| func (p *Plugin) handleGetChannelSettingsCustom(w http.ResponseWriter, r *http.Request) { | ||
| channelID := mux.Vars(r)["channel_id"] | ||
| if _, ok := p.requireChannelReader(w, r, channelID); !ok { | ||
| return | ||
| } | ||
|
|
||
| var state customState | ||
| if err := p.client.KV.Get(csCustomKey(channelID), &state); err != nil { | ||
| p.API.LogError("Failed to get channel settings custom state", "channel_id", channelID, "err", err.Error()) | ||
| http.Error(w, "Internal server error", http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| p.writeJSON(w, state) | ||
| } | ||
|
|
||
| func (p *Plugin) handleSaveChannelSettingsCustom(w http.ResponseWriter, r *http.Request) { | ||
| channelID := mux.Vars(r)["channel_id"] | ||
| if _, ok := p.requireChannelManager(w, r, channelID); !ok { | ||
| return | ||
| } | ||
|
|
||
| var state customState | ||
| if err := json.NewDecoder(r.Body).Decode(&state); err != nil { | ||
| http.Error(w, "Invalid request body", http.StatusBadRequest) | ||
| return | ||
| } | ||
| defer r.Body.Close() | ||
|
|
||
| if _, err := p.client.KV.Set(csCustomKey(channelID), state); err != nil { | ||
| p.API.LogError("Failed to save channel settings custom state", "channel_id", channelID, "err", err.Error()) | ||
| http.Error(w, "Internal server error", http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| p.writeJSON(w, state) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/mattermost/mattermost/server/public/model" | ||
| "github.com/mattermost/mattermost/server/public/plugin/plugintest" | ||
| "github.com/mattermost/mattermost/server/public/pluginapi" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| const testChannelID = "channelid1234567890123456a" | ||
|
|
||
| func newChannelSettingsPlugin(api *plugintest.API) *Plugin { | ||
| p := &Plugin{} | ||
| p.SetAPI(api) | ||
| p.client = pluginapi.NewClient(api, nil) | ||
| p.initializeAPI() | ||
| return p | ||
| } | ||
|
|
||
| func TestChannelSettingsSchemaRoundTrip(t *testing.T) { | ||
| api := &plugintest.API{} | ||
| defer api.AssertExpectations(t) | ||
|
|
||
| store := map[string][]byte{} | ||
| api.On("KVSetWithOptions", "cs_schema_"+testChannelID, mock.Anything, mock.Anything).Return(true, nil).Run(func(args mock.Arguments) { | ||
| store[args.String(0)] = args.Get(1).([]byte) | ||
| }) | ||
| api.On("KVGet", "cs_schema_"+testChannelID).Return(func(key string) []byte { return store[key] }, nil) | ||
| api.On("GetChannel", testChannelID).Return(&model.Channel{Id: testChannelID, Type: model.ChannelTypeOpen}, nil) | ||
| api.On("HasPermissionToChannel", "user1", testChannelID, model.PermissionManagePublicChannelProperties).Return(true) | ||
| api.On("HasPermissionToChannel", "user1", testChannelID, model.PermissionReadChannel).Return(true) | ||
|
|
||
| p := newChannelSettingsPlugin(api) | ||
|
|
||
| w := httptest.NewRecorder() | ||
| r := httptest.NewRequest(http.MethodPost, "/channel_settings/"+testChannelID+"/schema", strings.NewReader(`{"postPrefixStyle":"bold"}`)) | ||
| r.Header.Set("Mattermost-User-ID", "user1") | ||
| p.ServeHTTP(nil, w, r) | ||
| require.Equal(t, http.StatusOK, w.Result().StatusCode) | ||
|
|
||
| w = httptest.NewRecorder() | ||
| r = httptest.NewRequest(http.MethodGet, "/channel_settings/"+testChannelID+"/schema", nil) | ||
| r.Header.Set("Mattermost-User-ID", "user1") | ||
| p.ServeHTTP(nil, w, r) | ||
| require.Equal(t, http.StatusOK, w.Result().StatusCode) | ||
| require.JSONEq(t, `{"postPrefixStyle":"bold"}`, w.Body.String()) | ||
| } | ||
|
|
||
| func TestChannelSettingsCustomRoundTrip(t *testing.T) { | ||
| api := &plugintest.API{} | ||
| defer api.AssertExpectations(t) | ||
|
|
||
| store := map[string][]byte{} | ||
| api.On("KVSetWithOptions", "cs_custom_"+testChannelID, mock.Anything, mock.Anything).Return(true, nil).Run(func(args mock.Arguments) { | ||
| store[args.String(0)] = args.Get(1).([]byte) | ||
| }) | ||
| api.On("KVGet", "cs_custom_"+testChannelID).Return(func(key string) []byte { return store[key] }, nil) | ||
| api.On("GetChannel", testChannelID).Return(&model.Channel{Id: testChannelID, Type: model.ChannelTypeOpen}, nil) | ||
| api.On("HasPermissionToChannel", "user1", testChannelID, model.PermissionManagePublicChannelProperties).Return(true) | ||
| api.On("HasPermissionToChannel", "user1", testChannelID, model.PermissionReadChannel).Return(true) | ||
|
|
||
| p := newChannelSettingsPlugin(api) | ||
|
|
||
| // GET before any save returns the zero-valued custom state. | ||
| w := httptest.NewRecorder() | ||
| r := httptest.NewRequest(http.MethodGet, "/channel_settings/"+testChannelID+"/custom", nil) | ||
| r.Header.Set("Mattermost-User-ID", "user1") | ||
| p.ServeHTTP(nil, w, r) | ||
| require.Equal(t, http.StatusOK, w.Result().StatusCode) | ||
| require.JSONEq(t, `{"note":"","pinGreeting":false}`, w.Body.String()) | ||
|
|
||
| w = httptest.NewRecorder() | ||
| r = httptest.NewRequest(http.MethodPost, "/channel_settings/"+testChannelID+"/custom", strings.NewReader(`{"note":"hello","pinGreeting":true}`)) | ||
| r.Header.Set("Mattermost-User-ID", "user1") | ||
| p.ServeHTTP(nil, w, r) | ||
| require.Equal(t, http.StatusOK, w.Result().StatusCode) | ||
|
|
||
| w = httptest.NewRecorder() | ||
| r = httptest.NewRequest(http.MethodGet, "/channel_settings/"+testChannelID+"/custom", nil) | ||
| r.Header.Set("Mattermost-User-ID", "user1") | ||
| p.ServeHTTP(nil, w, r) | ||
| require.Equal(t, http.StatusOK, w.Result().StatusCode) | ||
| require.JSONEq(t, `{"note":"hello","pinGreeting":true}`, w.Body.String()) | ||
| } | ||
|
|
||
| func TestChannelSettingsReadAuthorization(t *testing.T) { | ||
| t.Run("get without auth is unauthorized", func(t *testing.T) { | ||
| api := &plugintest.API{} | ||
| p := newChannelSettingsPlugin(api) | ||
|
|
||
| w := httptest.NewRecorder() | ||
| r := httptest.NewRequest(http.MethodGet, "/channel_settings/"+testChannelID+"/schema", nil) | ||
| p.ServeHTTP(nil, w, r) | ||
| require.Equal(t, http.StatusUnauthorized, w.Result().StatusCode) | ||
| }) | ||
|
|
||
| t.Run("get without read permission is forbidden", func(t *testing.T) { | ||
| api := &plugintest.API{} | ||
| defer api.AssertExpectations(t) | ||
| api.On("HasPermissionToChannel", "user1", testChannelID, model.PermissionReadChannel).Return(false) | ||
|
|
||
| p := newChannelSettingsPlugin(api) | ||
|
|
||
| w := httptest.NewRecorder() | ||
| r := httptest.NewRequest(http.MethodGet, "/channel_settings/"+testChannelID+"/custom", nil) | ||
| r.Header.Set("Mattermost-User-ID", "user1") | ||
| p.ServeHTTP(nil, w, r) | ||
| require.Equal(t, http.StatusForbidden, w.Result().StatusCode) | ||
| }) | ||
| } | ||
|
|
||
| func TestChannelSettingsMalformedBody(t *testing.T) { | ||
| api := &plugintest.API{} | ||
| defer api.AssertExpectations(t) | ||
| api.On("GetChannel", testChannelID).Return(&model.Channel{Id: testChannelID, Type: model.ChannelTypeOpen}, nil) | ||
| api.On("HasPermissionToChannel", "user1", testChannelID, model.PermissionManagePublicChannelProperties).Return(true) | ||
|
|
||
| p := newChannelSettingsPlugin(api) | ||
|
|
||
| w := httptest.NewRecorder() | ||
| r := httptest.NewRequest(http.MethodPost, "/channel_settings/"+testChannelID+"/custom", strings.NewReader(`{not json`)) | ||
| r.Header.Set("Mattermost-User-ID", "user1") | ||
| p.ServeHTTP(nil, w, r) | ||
| require.Equal(t, http.StatusBadRequest, w.Result().StatusCode) | ||
| } | ||
|
|
||
| func TestChannelSettingsAuthAndPermission(t *testing.T) { | ||
| t.Run("save without auth is unauthorized", func(t *testing.T) { | ||
| api := &plugintest.API{} | ||
| p := newChannelSettingsPlugin(api) | ||
|
|
||
| w := httptest.NewRecorder() | ||
| r := httptest.NewRequest(http.MethodPost, "/channel_settings/"+testChannelID+"/schema", strings.NewReader(`{}`)) | ||
| p.ServeHTTP(nil, w, r) | ||
| require.Equal(t, http.StatusUnauthorized, w.Result().StatusCode) | ||
| }) | ||
|
|
||
| t.Run("save without permission is forbidden", func(t *testing.T) { | ||
| api := &plugintest.API{} | ||
| defer api.AssertExpectations(t) | ||
| api.On("GetChannel", testChannelID).Return(&model.Channel{Id: testChannelID, Type: model.ChannelTypePrivate}, nil) | ||
| api.On("HasPermissionToChannel", "user1", testChannelID, model.PermissionManagePrivateChannelProperties).Return(false) | ||
|
|
||
| p := newChannelSettingsPlugin(api) | ||
|
|
||
| w := httptest.NewRecorder() | ||
| r := httptest.NewRequest(http.MethodPost, "/channel_settings/"+testChannelID+"/custom", strings.NewReader(`{"note":"x"}`)) | ||
| r.Header.Set("Mattermost-User-ID", "user1") | ||
| p.ServeHTTP(nil, w, r) | ||
| require.Equal(t, http.StatusForbidden, w.Result().StatusCode) | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's strange that
GetTeamsfails. I wonder if we should still fail loudly as that makes things easier for QA to debug.