github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/app/diagnostics_test.go (about) 1 // Copyright (c) 2017-present Xenia, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "bytes" 8 "io" 9 "net/http" 10 "net/http/httptest" 11 "strings" 12 "testing" 13 "time" 14 15 "github.com/stretchr/testify/assert" 16 17 "github.com/xzl8028/xenia-server/model" 18 ) 19 20 func newTestServer() (chan string, *httptest.Server) { 21 result := make(chan string, 100) 22 23 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 24 buf := bytes.NewBuffer(nil) 25 io.Copy(buf, r.Body) 26 27 result <- buf.String() 28 })) 29 30 return result, server 31 } 32 33 func TestPluginSetting(t *testing.T) { 34 settings := &model.PluginSettings{ 35 Plugins: map[string]map[string]interface{}{ 36 "test": map[string]interface{}{ 37 "foo": "bar", 38 }, 39 }, 40 } 41 assert.Equal(t, "bar", pluginSetting(settings, "test", "foo", "asd")) 42 assert.Equal(t, "asd", pluginSetting(settings, "test", "qwe", "asd")) 43 } 44 45 func TestPluginActivated(t *testing.T) { 46 states := map[string]*model.PluginState{ 47 "foo": &model.PluginState{ 48 Enable: true, 49 }, 50 "bar": &model.PluginState{ 51 Enable: false, 52 }, 53 } 54 assert.True(t, pluginActivated(states, "foo")) 55 assert.False(t, pluginActivated(states, "bar")) 56 assert.False(t, pluginActivated(states, "none")) 57 } 58 59 func TestDiagnostics(t *testing.T) { 60 th := Setup(t).InitBasic() 61 defer th.TearDown() 62 63 if testing.Short() { 64 t.SkipNow() 65 } 66 67 data, server := newTestServer() 68 defer server.Close() 69 70 diagnosticId := "i am not real" 71 th.App.SetDiagnosticId(diagnosticId) 72 th.App.initDiagnostics(server.URL) 73 74 // Should send a client identify message 75 select { 76 case identifyMessage := <-data: 77 t.Log("Got idmessage:\n" + identifyMessage) 78 if !strings.Contains(identifyMessage, diagnosticId) { 79 t.Fail() 80 } 81 case <-time.After(time.Second * 1): 82 t.Fatal("Did not receive ID message") 83 } 84 85 t.Run("Send", func(t *testing.T) { 86 const TEST_VALUE = "stuff548959847" 87 th.App.SendDiagnostic("Testing Diagnostic", map[string]interface{}{ 88 "hey": TEST_VALUE, 89 }) 90 select { 91 case result := <-data: 92 t.Log("Got diagnostic:\n" + result) 93 if !strings.Contains(result, TEST_VALUE) { 94 t.Fail() 95 } 96 case <-time.After(time.Second * 1): 97 t.Fatal("Did not receive diagnostic") 98 } 99 }) 100 101 t.Run("SendDailyDiagnostics", func(t *testing.T) { 102 th.App.sendDailyDiagnostics(true) 103 104 info := "" 105 // Collect the info sent. 106 Loop: 107 for { 108 select { 109 case result := <-data: 110 info += result 111 case <-time.After(time.Second * 1): 112 break Loop 113 } 114 } 115 116 for _, item := range []string{ 117 TRACK_CONFIG_SERVICE, 118 TRACK_CONFIG_TEAM, 119 TRACK_CONFIG_SERVICE, 120 TRACK_CONFIG_TEAM, 121 TRACK_CONFIG_SQL, 122 TRACK_CONFIG_LOG, 123 TRACK_CONFIG_NOTIFICATION_LOG, 124 TRACK_CONFIG_FILE, 125 TRACK_CONFIG_RATE, 126 TRACK_CONFIG_EMAIL, 127 TRACK_CONFIG_PRIVACY, 128 TRACK_CONFIG_OAUTH, 129 TRACK_CONFIG_LDAP, 130 TRACK_CONFIG_COMPLIANCE, 131 TRACK_CONFIG_LOCALIZATION, 132 TRACK_CONFIG_SAML, 133 TRACK_CONFIG_PASSWORD, 134 TRACK_CONFIG_CLUSTER, 135 TRACK_CONFIG_METRICS, 136 TRACK_CONFIG_SUPPORT, 137 TRACK_CONFIG_NATIVEAPP, 138 TRACK_CONFIG_ANALYTICS, 139 TRACK_CONFIG_PLUGIN, 140 TRACK_ACTIVITY, 141 TRACK_SERVER, 142 TRACK_CONFIG_MESSAGE_EXPORT, 143 TRACK_PLUGINS, 144 } { 145 if !strings.Contains(info, item) { 146 t.Fatal("Sent diagnostics missing item: " + item) 147 } 148 } 149 }) 150 151 t.Run("SendDailyDiagnosticsNoSegmentKey", func(t *testing.T) { 152 th.App.SendDailyDiagnostics() 153 154 select { 155 case <-data: 156 t.Fatal("Should not send diagnostics when the segment key is not set") 157 case <-time.After(time.Second * 1): 158 // Did not receive diagnostics 159 } 160 }) 161 162 t.Run("SendDailyDiagnosticsDisabled", func(t *testing.T) { 163 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.LogSettings.EnableDiagnostics = false }) 164 165 th.App.sendDailyDiagnostics(true) 166 167 select { 168 case <-data: 169 t.Fatal("Should not send diagnostics when they are disabled") 170 case <-time.After(time.Second * 1): 171 // Did not receive diagnostics 172 } 173 }) 174 }