2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
2017-04-03 07:01:32 -04:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2017-10-23 02:39:51 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/model"
|
2017-04-03 07:01:32 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func newTestServer() (chan string, *httptest.Server) {
|
|
|
|
|
result := make(chan string, 100)
|
|
|
|
|
|
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
|
io.Copy(buf, r.Body)
|
|
|
|
|
|
|
|
|
|
result <- buf.String()
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
return result, server
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-02 01:36:54 -07:00
|
|
|
func TestPluginSetting(t *testing.T) {
|
2017-10-23 02:39:51 -07:00
|
|
|
settings := &model.PluginSettings{
|
|
|
|
|
Plugins: map[string]interface{}{
|
|
|
|
|
"test": map[string]string{
|
|
|
|
|
"foo": "bar",
|
|
|
|
|
},
|
2017-08-02 01:36:54 -07:00
|
|
|
},
|
|
|
|
|
}
|
2017-10-23 02:39:51 -07:00
|
|
|
assert.Equal(t, "bar", pluginSetting(settings, "test", "foo", "asd"))
|
|
|
|
|
assert.Equal(t, "asd", pluginSetting(settings, "test", "qwe", "asd"))
|
2017-08-02 01:36:54 -07:00
|
|
|
}
|
|
|
|
|
|
2017-12-07 17:03:11 -05:00
|
|
|
func TestPluginActivated(t *testing.T) {
|
|
|
|
|
states := map[string]*model.PluginState{
|
|
|
|
|
"foo": &model.PluginState{
|
|
|
|
|
Enable: true,
|
|
|
|
|
},
|
|
|
|
|
"bar": &model.PluginState{
|
|
|
|
|
Enable: false,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
assert.True(t, pluginActivated(states, "foo"))
|
|
|
|
|
assert.False(t, pluginActivated(states, "bar"))
|
|
|
|
|
assert.False(t, pluginActivated(states, "none"))
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-03 07:01:32 -04:00
|
|
|
func TestDiagnostics(t *testing.T) {
|
2017-09-12 09:19:52 -05:00
|
|
|
th := Setup().InitBasic()
|
2017-10-04 13:09:41 -07:00
|
|
|
defer th.TearDown()
|
2017-04-03 07:01:32 -04:00
|
|
|
|
|
|
|
|
if testing.Short() {
|
|
|
|
|
t.SkipNow()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, server := newTestServer()
|
|
|
|
|
defer server.Close()
|
|
|
|
|
|
2018-01-05 16:17:57 -06:00
|
|
|
diagnosticId := "i am not real"
|
|
|
|
|
th.App.SetDiagnosticId(diagnosticId)
|
|
|
|
|
th.App.initDiagnostics(server.URL)
|
2017-04-03 07:01:32 -04:00
|
|
|
|
|
|
|
|
// Should send a client identify message
|
|
|
|
|
select {
|
|
|
|
|
case identifyMessage := <-data:
|
|
|
|
|
t.Log("Got idmessage:\n" + identifyMessage)
|
2018-01-05 16:17:57 -06:00
|
|
|
if !strings.Contains(identifyMessage, diagnosticId) {
|
2017-04-03 07:01:32 -04:00
|
|
|
t.Fail()
|
|
|
|
|
}
|
|
|
|
|
case <-time.After(time.Second * 1):
|
2018-03-29 16:04:54 +02:00
|
|
|
t.Fatal("Did not receive ID message")
|
2017-04-03 07:01:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.Run("Send", func(t *testing.T) {
|
|
|
|
|
const TEST_VALUE = "stuff548959847"
|
2018-01-05 16:17:57 -06:00
|
|
|
th.App.SendDiagnostic("Testing Diagnostic", map[string]interface{}{
|
2017-04-03 07:01:32 -04:00
|
|
|
"hey": TEST_VALUE,
|
|
|
|
|
})
|
|
|
|
|
select {
|
|
|
|
|
case result := <-data:
|
|
|
|
|
t.Log("Got diagnostic:\n" + result)
|
|
|
|
|
if !strings.Contains(result, TEST_VALUE) {
|
|
|
|
|
t.Fail()
|
|
|
|
|
}
|
|
|
|
|
case <-time.After(time.Second * 1):
|
2018-03-29 16:04:54 +02:00
|
|
|
t.Fatal("Did not receive diagnostic")
|
2017-04-03 07:01:32 -04:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("SendDailyDiagnostics", func(t *testing.T) {
|
2017-09-12 09:19:52 -05:00
|
|
|
th.App.SendDailyDiagnostics()
|
2017-04-03 07:01:32 -04:00
|
|
|
|
|
|
|
|
info := ""
|
|
|
|
|
// Collect the info sent.
|
|
|
|
|
for {
|
|
|
|
|
done := false
|
|
|
|
|
select {
|
|
|
|
|
case result := <-data:
|
|
|
|
|
info += result
|
|
|
|
|
case <-time.After(time.Second * 1):
|
|
|
|
|
// Done recieving
|
|
|
|
|
done = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if done {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, item := range []string{
|
|
|
|
|
TRACK_CONFIG_SERVICE,
|
|
|
|
|
TRACK_CONFIG_TEAM,
|
|
|
|
|
TRACK_CONFIG_SERVICE,
|
|
|
|
|
TRACK_CONFIG_TEAM,
|
|
|
|
|
TRACK_CONFIG_SQL,
|
|
|
|
|
TRACK_CONFIG_LOG,
|
|
|
|
|
TRACK_CONFIG_FILE,
|
|
|
|
|
TRACK_CONFIG_RATE,
|
|
|
|
|
TRACK_CONFIG_EMAIL,
|
|
|
|
|
TRACK_CONFIG_PRIVACY,
|
|
|
|
|
TRACK_CONFIG_OAUTH,
|
|
|
|
|
TRACK_CONFIG_LDAP,
|
|
|
|
|
TRACK_CONFIG_COMPLIANCE,
|
|
|
|
|
TRACK_CONFIG_LOCALIZATION,
|
|
|
|
|
TRACK_CONFIG_SAML,
|
|
|
|
|
TRACK_CONFIG_PASSWORD,
|
|
|
|
|
TRACK_CONFIG_CLUSTER,
|
|
|
|
|
TRACK_CONFIG_METRICS,
|
|
|
|
|
TRACK_CONFIG_WEBRTC,
|
|
|
|
|
TRACK_CONFIG_SUPPORT,
|
|
|
|
|
TRACK_CONFIG_NATIVEAPP,
|
|
|
|
|
TRACK_CONFIG_ANALYTICS,
|
2017-08-02 01:36:54 -07:00
|
|
|
TRACK_CONFIG_PLUGIN,
|
2017-04-03 07:01:32 -04:00
|
|
|
TRACK_ACTIVITY,
|
|
|
|
|
TRACK_SERVER,
|
2017-11-30 09:07:04 -05:00
|
|
|
TRACK_CONFIG_MESSAGE_EXPORT,
|
2017-10-30 14:10:48 -04:00
|
|
|
TRACK_PLUGINS,
|
2017-04-03 07:01:32 -04:00
|
|
|
} {
|
|
|
|
|
if !strings.Contains(info, item) {
|
|
|
|
|
t.Fatal("Sent diagnostics missing item: " + item)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("SendDailyDiagnosticsDisabled", func(t *testing.T) {
|
2017-11-09 14:46:20 -06:00
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.LogSettings.EnableDiagnostics = false })
|
2017-04-03 07:01:32 -04:00
|
|
|
|
2017-09-12 09:19:52 -05:00
|
|
|
th.App.SendDailyDiagnostics()
|
2017-04-03 07:01:32 -04:00
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case <-data:
|
|
|
|
|
t.Fatal("Should not send diagnostics when they are disabled")
|
|
|
|
|
case <-time.After(time.Second * 1):
|
2018-03-29 16:04:54 +02:00
|
|
|
// Did not receive diagnostics
|
2017-04-03 07:01:32 -04:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|