Skip to content
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

MM-23227: preserve disable across config changes #93

Merged
merged 4 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ require (
github.com/mattermost/mattermost-plugin-api v0.0.9
github.com/mattermost/mattermost-server/v5 v5.3.2-0.20200313113657-e2883bfe5f37
github.com/pkg/errors v0.9.1
github.com/reflog/struct2interface v0.3.0 // indirect
github.com/stretchr/testify v1.5.1
)
11 changes: 8 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfV
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand Down Expand Up @@ -226,6 +225,8 @@ github.com/mattermost/ldap v0.0.0-20191128190019-9f62ba4b8d4d h1:2DV7VIlEv6J5R5o
github.com/mattermost/ldap v0.0.0-20191128190019-9f62ba4b8d4d/go.mod h1:HLbgMEI5K131jpxGazJ97AxfPDt31osq36YS1oxFQPQ=
github.com/mattermost/mattermost-plugin-api v0.0.9 h1:9RhlOLbO37PtIjErKJYU9nQe4STJDiffnAZk2uWDqQs=
github.com/mattermost/mattermost-plugin-api v0.0.9/go.mod h1:EKzltYWI4VZVjnB8fKLG/ZRoo9UuzWkc/txhN35Wij4=
github.com/mattermost/mattermost-server/v5 v5.3.2-0.20200214230800-bd9d674006b3 h1:st2Zm/4v7tPQwykfJICNM3AZ0t5E0uyFhGm9WOnox/Q=
github.com/mattermost/mattermost-server/v5 v5.3.2-0.20200214230800-bd9d674006b3/go.mod h1:2NWWV01o7ebeG511GQ31t0x+sZxXxS8fmHohAtTd+WU=
github.com/mattermost/mattermost-server/v5 v5.3.2-0.20200313113657-e2883bfe5f37 h1:roF2kU96CwIfAeEronp/Xss51aiQBIZdmnxLyjN5iT4=
github.com/mattermost/mattermost-server/v5 v5.3.2-0.20200313113657-e2883bfe5f37/go.mod h1:XdS3HDp3QsDhXF18PP6Ff69x16wFRzOCy1AwMu/GRuQ=
github.com/mattermost/rsc v0.0.0-20160330161541-bbaefb05eaa0/go.mod h1:nV5bfVpT//+B1RPD2JvRnxbkLmJEYXmRaaVl15fsXjs=
Expand Down Expand Up @@ -324,6 +325,7 @@ github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/reflog/struct2interface v0.3.0/go.mod h1:Hj4XSqbzQyLswqmKfmGqzOlh4xCRPSl27779XT9TPN4=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
Expand Down Expand Up @@ -360,7 +362,6 @@ github.com/shurcooL/sanitized_anchor_name v0.0.0-20170918181015-86672fcb3f95/go.
github.com/shurcooL/users v0.0.0-20180125191416-49c67e49c537/go.mod h1:QJTqeLYEDaXHZDBsXlPCDqdhQuJkuw4NOtaxYe3xii4=
github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5kWdCj2z2KEozexVbfEZIWiTjhE0+UjmZgPqehw=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/assertions v1.0.0/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM=
Expand Down Expand Up @@ -431,6 +432,8 @@ golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200117160349-530e935923ad h1:Jh8cai0fqIK+f6nG0UgPW5wFk8wmiMhM3AyciDBdtQg=
golang.org/x/crypto v0.0.0-20200117160349-530e935923ad/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975 h1:/Tl7pH94bvbAAHBdZJT947M/+gp0+CqQXDtMRC0fseo=
golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -532,7 +535,9 @@ golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200213050514-49b8ac185c84 h1:0QCtZnPx0LFDcPMUX7Qg328Twbm3c/Jx1d0XT/x9jcg=
golang.org/x/tools v0.0.0-20200206050830-dd0d5d485177/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200211205636-11eff242d136 h1:yFWeDNMOyrJIQNtXrNR5smCrv+Y4IN6Ul42TzAXxd9k=
golang.org/x/tools v0.0.0-20200211205636-11eff242d136/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200213050514-49b8ac185c84/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
4 changes: 2 additions & 2 deletions server/command_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (p *Plugin) executeCommandHooks(args *model.CommandArgs) *model.CommandResp
}
}

configuration.disabled = false
p.setEnabled(true)
p.emitStatusChange()

return &model.CommandResponse{
Expand All @@ -181,7 +181,7 @@ func (p *Plugin) executeCommandHooks(args *model.CommandArgs) *model.CommandResp
}
}

configuration.disabled = true
p.setEnabled(false)
p.emitStatusChange()

return &model.CommandResponse{
Expand Down
2 changes: 1 addition & 1 deletion server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (p *Plugin) diffConfiguration(newConfiguration *configuration) {
// This demo implementation ensures the configured demo user and channel are created for use
// by the plugin.
func (p *Plugin) OnConfigurationChange() error {
var configuration = new(configuration)
configuration := p.getConfiguration().Clone()
var err error

// Load the public configuration fields from the Mattermost server configuration.
Expand Down
40 changes: 20 additions & 20 deletions server/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,16 @@ func TestOnConfigurationChange(t *testing.T) {
demoChannelIds := map[string]string{
teamId: channelId,
}
var apiConfiguration = new(configuration)

for name, test := range map[string]struct {
SetupAPI func() *plugintest.API
SetupHelpers func() *plugintest.Helpers
preConfiguration *configuration
ShouldError bool
SetupAPI func() *plugintest.API
SetupHelpers func() *plugintest.Helpers
ShouldError bool
}{
"same configuration": {
SetupAPI: func() *plugintest.API {
api := &plugintest.API{}
api.On("LoadPluginConfiguration", apiConfiguration).Return(nil)
api.On("LoadPluginConfiguration", mock.Anything).Return(nil)
api.On("GetTeams").Return([]*model.Team{&model.Team{Id: teamId}}, nil)
api.On("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil)
api.On("CreateTeamMember", teamId, "").Return(&model.TeamMember{}, nil)
Expand All @@ -88,13 +86,15 @@ func TestOnConfigurationChange(t *testing.T) {
helpers.On("EnsureBot", mock.AnythingOfType("*model.Bot"), mock.AnythingOfType("plugin.EnsureBotOption")).Return(model.NewId(), nil)
return helpers
},
preConfiguration: apiConfiguration,
ShouldError: false,
ShouldError: false,
},
"different configuration": {
SetupAPI: func() *plugintest.API {
api := &plugintest.API{}
api.On("LoadPluginConfiguration", apiConfiguration).Return(nil)
api.On("LoadPluginConfiguration", mock.Anything).Return(nil).Run(func(args mock.Arguments) {
apiConfiguration := args.Get(0).(*configuration)
apiConfiguration.EnableMentionUser = true
})
api.On("GetTeams").Return([]*model.Team{&model.Team{Id: teamId}}, nil)
api.On("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil)
api.On("CreateTeamMember", teamId, "").Return(&model.TeamMember{}, nil)
Expand All @@ -112,13 +112,15 @@ func TestOnConfigurationChange(t *testing.T) {
helpers.On("EnsureBot", mock.AnythingOfType("*model.Bot"), mock.AnythingOfType("plugin.EnsureBotOption")).Return(model.NewId(), nil)
return helpers
},
preConfiguration: &configuration{EnableMentionUser: true},
ShouldError: false,
ShouldError: false,
},
"failure to ensure bot": {
SetupAPI: func() *plugintest.API {
api := &plugintest.API{}
api.On("LoadPluginConfiguration", apiConfiguration).Return(nil)
api.On("LoadPluginConfiguration", mock.Anything).Return(nil).Run(func(args mock.Arguments) {
apiConfiguration := args.Get(0).(*configuration)
apiConfiguration.EnableMentionUser = true
})
api.On("GetTeams").Return([]*model.Team{&model.Team{Id: teamId}}, nil)
api.On("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil)
api.On("CreateTeamMember", teamId, "").Return(&model.TeamMember{}, nil)
Expand All @@ -130,13 +132,15 @@ func TestOnConfigurationChange(t *testing.T) {
helpers.On("EnsureBot", mock.AnythingOfType("*model.Bot"), mock.AnythingOfType("plugin.EnsureBotOption")).Return("", errors.New("some error"))
return helpers
},
preConfiguration: &configuration{EnableMentionUser: true},
ShouldError: true,
ShouldError: true,
},
"bot icon exists": {
SetupAPI: func() *plugintest.API {
api := &plugintest.API{}
api.On("LoadPluginConfiguration", apiConfiguration).Return(nil)
api.On("LoadPluginConfiguration", mock.Anything).Return(nil).Run(func(args mock.Arguments) {
apiConfiguration := args.Get(0).(*configuration)
apiConfiguration.EnableMentionUser = true
})
api.On("GetTeams").Return([]*model.Team{&model.Team{Id: teamId}}, nil)
api.On("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil)
api.On("CreateTeamMember", teamId, "").Return(&model.TeamMember{}, nil)
Expand All @@ -154,8 +158,7 @@ func TestOnConfigurationChange(t *testing.T) {
helpers.On("EnsureBot", mock.AnythingOfType("*model.Bot"), mock.AnythingOfType("plugin.EnsureBotOption")).Return(model.NewId(), nil)
return helpers
},
preConfiguration: &configuration{EnableMentionUser: true},
ShouldError: false,
ShouldError: false,
},
} {
t.Run(name, func(t *testing.T) {
Expand All @@ -172,9 +175,6 @@ func TestOnConfigurationChange(t *testing.T) {
p.SetAPI(api)
p.SetHelpers(helpers)

// The configuration set here allows us to test calling the
// "OnConfigurationChange" hook from multiple states
p.setConfiguration(test.preConfiguration)
err := p.OnConfigurationChange()

if test.ShouldError {
Expand Down