github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/api4/system_test.go (about)

     1  package api4
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"os"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/mattermost/mattermost-server/mlog"
    12  	"github.com/mattermost/mattermost-server/model"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestGetPing(t *testing.T) {
    18  	th := Setup().InitBasic().InitSystemAdmin()
    19  	defer th.TearDown()
    20  	Client := th.Client
    21  
    22  	goRoutineHealthThreshold := *th.App.Config().ServiceSettings.GoroutineHealthThreshold
    23  	defer func() {
    24  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.GoroutineHealthThreshold = goRoutineHealthThreshold })
    25  	}()
    26  
    27  	status, resp := Client.GetPing()
    28  	CheckNoError(t, resp)
    29  	if status != "OK" {
    30  		t.Fatal("should return OK")
    31  	}
    32  
    33  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.GoroutineHealthThreshold = 10 })
    34  	status, resp = th.SystemAdminClient.GetPing()
    35  	CheckInternalErrorStatus(t, resp)
    36  	if status != "unhealthy" {
    37  		t.Fatal("should return unhealthy")
    38  	}
    39  }
    40  
    41  func TestGetConfig(t *testing.T) {
    42  	th := Setup().InitBasic().InitSystemAdmin()
    43  	defer th.TearDown()
    44  	Client := th.Client
    45  
    46  	_, resp := Client.GetConfig()
    47  	CheckForbiddenStatus(t, resp)
    48  
    49  	cfg, resp := th.SystemAdminClient.GetConfig()
    50  	CheckNoError(t, resp)
    51  
    52  	require.NotEqual(t, "", cfg.TeamSettings.SiteName)
    53  
    54  	if *cfg.LdapSettings.BindPassword != model.FAKE_SETTING && len(*cfg.LdapSettings.BindPassword) != 0 {
    55  		t.Fatal("did not sanitize properly")
    56  	}
    57  	if *cfg.FileSettings.PublicLinkSalt != model.FAKE_SETTING {
    58  		t.Fatal("did not sanitize properly")
    59  	}
    60  	if cfg.FileSettings.AmazonS3SecretAccessKey != model.FAKE_SETTING && len(cfg.FileSettings.AmazonS3SecretAccessKey) != 0 {
    61  		t.Fatal("did not sanitize properly")
    62  	}
    63  	if cfg.EmailSettings.InviteSalt != model.FAKE_SETTING {
    64  		t.Fatal("did not sanitize properly")
    65  	}
    66  	if cfg.EmailSettings.SMTPPassword != model.FAKE_SETTING && len(cfg.EmailSettings.SMTPPassword) != 0 {
    67  		t.Fatal("did not sanitize properly")
    68  	}
    69  	if cfg.GitLabSettings.Secret != model.FAKE_SETTING && len(cfg.GitLabSettings.Secret) != 0 {
    70  		t.Fatal("did not sanitize properly")
    71  	}
    72  	if *cfg.SqlSettings.DataSource != model.FAKE_SETTING {
    73  		t.Fatal("did not sanitize properly")
    74  	}
    75  	if cfg.SqlSettings.AtRestEncryptKey != model.FAKE_SETTING {
    76  		t.Fatal("did not sanitize properly")
    77  	}
    78  	if !strings.Contains(strings.Join(cfg.SqlSettings.DataSourceReplicas, " "), model.FAKE_SETTING) && len(cfg.SqlSettings.DataSourceReplicas) != 0 {
    79  		t.Fatal("did not sanitize properly")
    80  	}
    81  	if !strings.Contains(strings.Join(cfg.SqlSettings.DataSourceSearchReplicas, " "), model.FAKE_SETTING) && len(cfg.SqlSettings.DataSourceSearchReplicas) != 0 {
    82  		t.Fatal("did not sanitize properly")
    83  	}
    84  }
    85  
    86  func TestReloadConfig(t *testing.T) {
    87  	th := Setup().InitBasic().InitSystemAdmin()
    88  	defer th.TearDown()
    89  	Client := th.Client
    90  
    91  	flag, resp := Client.ReloadConfig()
    92  	CheckForbiddenStatus(t, resp)
    93  	if flag {
    94  		t.Fatal("should not Reload the config due no permission.")
    95  	}
    96  
    97  	flag, resp = th.SystemAdminClient.ReloadConfig()
    98  	CheckNoError(t, resp)
    99  	if !flag {
   100  		t.Fatal("should Reload the config")
   101  	}
   102  
   103  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.MaxUsersPerTeam = 50 })
   104  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = true })
   105  }
   106  
   107  func TestUpdateConfig(t *testing.T) {
   108  	th := Setup().InitBasic().InitSystemAdmin()
   109  	defer th.TearDown()
   110  	Client := th.Client
   111  
   112  	cfg, resp := th.SystemAdminClient.GetConfig()
   113  	CheckNoError(t, resp)
   114  
   115  	_, resp = Client.UpdateConfig(cfg)
   116  	CheckForbiddenStatus(t, resp)
   117  
   118  	SiteName := th.App.Config().TeamSettings.SiteName
   119  
   120  	cfg.TeamSettings.SiteName = "MyFancyName"
   121  	cfg, resp = th.SystemAdminClient.UpdateConfig(cfg)
   122  	CheckNoError(t, resp)
   123  
   124  	require.Equal(t, "MyFancyName", cfg.TeamSettings.SiteName, "It should update the SiteName")
   125  
   126  	//Revert the change
   127  	cfg.TeamSettings.SiteName = SiteName
   128  	cfg, resp = th.SystemAdminClient.UpdateConfig(cfg)
   129  	CheckNoError(t, resp)
   130  
   131  	require.Equal(t, SiteName, cfg.TeamSettings.SiteName, "It should update the SiteName")
   132  
   133  	t.Run("Should not be able to modify PluginSettings.EnableUploads", func(t *testing.T) {
   134  		oldEnableUploads := *th.App.GetConfig().PluginSettings.EnableUploads
   135  		*cfg.PluginSettings.EnableUploads = !oldEnableUploads
   136  
   137  		cfg, resp = th.SystemAdminClient.UpdateConfig(cfg)
   138  		CheckNoError(t, resp)
   139  		assert.Equal(t, oldEnableUploads, *cfg.PluginSettings.EnableUploads)
   140  		assert.Equal(t, oldEnableUploads, *th.App.GetConfig().PluginSettings.EnableUploads)
   141  
   142  		cfg.PluginSettings.EnableUploads = nil
   143  		cfg, resp = th.SystemAdminClient.UpdateConfig(cfg)
   144  		CheckNoError(t, resp)
   145  		assert.Equal(t, oldEnableUploads, *cfg.PluginSettings.EnableUploads)
   146  		assert.Equal(t, oldEnableUploads, *th.App.GetConfig().PluginSettings.EnableUploads)
   147  	})
   148  }
   149  
   150  func TestGetEnvironmentConfig(t *testing.T) {
   151  	os.Setenv("MM_SERVICESETTINGS_SITEURL", "http://example.mattermost.com")
   152  	os.Setenv("MM_SERVICESETTINGS_ENABLECUSTOMEMOJI", "true")
   153  	defer os.Unsetenv("MM_SERVICESETTINGS_SITEURL")
   154  
   155  	th := Setup().InitBasic().InitSystemAdmin()
   156  	defer th.TearDown()
   157  
   158  	t.Run("as system admin", func(t *testing.T) {
   159  		SystemAdminClient := th.SystemAdminClient
   160  
   161  		envConfig, resp := SystemAdminClient.GetEnvironmentConfig()
   162  		CheckNoError(t, resp)
   163  
   164  		if serviceSettings, ok := envConfig["ServiceSettings"]; !ok {
   165  			t.Fatal("should've returned ServiceSettings")
   166  		} else if serviceSettingsAsMap, ok := serviceSettings.(map[string]interface{}); !ok {
   167  			t.Fatal("should've returned ServiceSettings as a map")
   168  		} else {
   169  			if siteURL, ok := serviceSettingsAsMap["SiteURL"]; !ok {
   170  				t.Fatal("should've returned ServiceSettings.SiteURL")
   171  			} else if siteURLAsBool, ok := siteURL.(bool); !ok {
   172  				t.Fatal("should've returned ServiceSettings.SiteURL as a boolean")
   173  			} else if !siteURLAsBool {
   174  				t.Fatal("should've returned ServiceSettings.SiteURL as true")
   175  			}
   176  
   177  			if enableCustomEmoji, ok := serviceSettingsAsMap["EnableCustomEmoji"]; !ok {
   178  				t.Fatal("should've returned ServiceSettings.EnableCustomEmoji")
   179  			} else if enableCustomEmojiAsBool, ok := enableCustomEmoji.(bool); !ok {
   180  				t.Fatal("should've returned ServiceSettings.EnableCustomEmoji as a boolean")
   181  			} else if !enableCustomEmojiAsBool {
   182  				t.Fatal("should've returned ServiceSettings.EnableCustomEmoji as true")
   183  			}
   184  		}
   185  
   186  		if _, ok := envConfig["TeamSettings"]; ok {
   187  			t.Fatal("should not have returned TeamSettings")
   188  		}
   189  	})
   190  
   191  	t.Run("as team admin", func(t *testing.T) {
   192  		TeamAdminClient := th.CreateClient()
   193  		th.LoginTeamAdminWithClient(TeamAdminClient)
   194  
   195  		_, resp := TeamAdminClient.GetEnvironmentConfig()
   196  		CheckForbiddenStatus(t, resp)
   197  	})
   198  
   199  	t.Run("as regular user", func(t *testing.T) {
   200  		Client := th.Client
   201  
   202  		_, resp := Client.GetEnvironmentConfig()
   203  		CheckForbiddenStatus(t, resp)
   204  	})
   205  
   206  	t.Run("as not-regular user", func(t *testing.T) {
   207  		Client := th.CreateClient()
   208  
   209  		_, resp := Client.GetEnvironmentConfig()
   210  		CheckUnauthorizedStatus(t, resp)
   211  	})
   212  }
   213  
   214  func TestGetOldClientConfig(t *testing.T) {
   215  	th := Setup().InitBasic().InitSystemAdmin()
   216  	defer th.TearDown()
   217  
   218  	testKey := "supersecretkey"
   219  	th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.GoogleDeveloperKey = testKey })
   220  
   221  	t.Run("with session, without limited config", func(t *testing.T) {
   222  		th.App.UpdateConfig(func(cfg *model.Config) {
   223  			cfg.ServiceSettings.GoogleDeveloperKey = testKey
   224  			*cfg.ServiceSettings.ExperimentalLimitClientConfig = false
   225  		})
   226  
   227  		Client := th.Client
   228  
   229  		config, resp := Client.GetOldClientConfig("")
   230  		CheckNoError(t, resp)
   231  
   232  		if len(config["Version"]) == 0 {
   233  			t.Fatal("config not returned correctly")
   234  		}
   235  
   236  		if config["GoogleDeveloperKey"] != testKey {
   237  			t.Fatal("config missing developer key")
   238  		}
   239  	})
   240  
   241  	t.Run("without session, without limited config", func(t *testing.T) {
   242  		th.App.UpdateConfig(func(cfg *model.Config) {
   243  			cfg.ServiceSettings.GoogleDeveloperKey = testKey
   244  			*cfg.ServiceSettings.ExperimentalLimitClientConfig = false
   245  		})
   246  
   247  		Client := th.CreateClient()
   248  
   249  		config, resp := Client.GetOldClientConfig("")
   250  		CheckNoError(t, resp)
   251  
   252  		if len(config["Version"]) == 0 {
   253  			t.Fatal("config not returned correctly")
   254  		}
   255  
   256  		if config["GoogleDeveloperKey"] != testKey {
   257  			t.Fatal("config missing developer key")
   258  		}
   259  	})
   260  
   261  	t.Run("with session, with limited config", func(t *testing.T) {
   262  		th.App.UpdateConfig(func(cfg *model.Config) {
   263  			cfg.ServiceSettings.GoogleDeveloperKey = testKey
   264  			*cfg.ServiceSettings.ExperimentalLimitClientConfig = true
   265  		})
   266  
   267  		Client := th.Client
   268  
   269  		config, resp := Client.GetOldClientConfig("")
   270  		CheckNoError(t, resp)
   271  
   272  		if len(config["Version"]) == 0 {
   273  			t.Fatal("config not returned correctly")
   274  		}
   275  
   276  		if config["GoogleDeveloperKey"] != testKey {
   277  			t.Fatal("config missing developer key")
   278  		}
   279  	})
   280  
   281  	t.Run("without session, without limited config", func(t *testing.T) {
   282  		th.App.UpdateConfig(func(cfg *model.Config) {
   283  			cfg.ServiceSettings.GoogleDeveloperKey = testKey
   284  			*cfg.ServiceSettings.ExperimentalLimitClientConfig = true
   285  		})
   286  
   287  		Client := th.CreateClient()
   288  
   289  		config, resp := Client.GetOldClientConfig("")
   290  		CheckNoError(t, resp)
   291  
   292  		if len(config["Version"]) == 0 {
   293  			t.Fatal("config not returned correctly")
   294  		}
   295  
   296  		if _, ok := config["GoogleDeveloperKey"]; ok {
   297  			t.Fatal("config should be missing developer key")
   298  		}
   299  	})
   300  
   301  	t.Run("missing format", func(t *testing.T) {
   302  		Client := th.Client
   303  
   304  		if _, err := Client.DoApiGet("/config/client", ""); err == nil || err.StatusCode != http.StatusNotImplemented {
   305  			t.Fatal("should have errored with 501")
   306  		}
   307  	})
   308  
   309  	t.Run("invalid format", func(t *testing.T) {
   310  		Client := th.Client
   311  
   312  		if _, err := Client.DoApiGet("/config/client?format=junk", ""); err == nil || err.StatusCode != http.StatusBadRequest {
   313  			t.Fatal("should have errored with 400")
   314  		}
   315  	})
   316  }
   317  
   318  func TestGetOldClientLicense(t *testing.T) {
   319  	th := Setup().InitBasic().InitSystemAdmin()
   320  	defer th.TearDown()
   321  	Client := th.Client
   322  
   323  	license, resp := Client.GetOldClientLicense("")
   324  	CheckNoError(t, resp)
   325  
   326  	if len(license["IsLicensed"]) == 0 {
   327  		t.Fatal("license not returned correctly")
   328  	}
   329  
   330  	Client.Logout()
   331  
   332  	_, resp = Client.GetOldClientLicense("")
   333  	CheckNoError(t, resp)
   334  
   335  	if _, err := Client.DoApiGet("/license/client", ""); err == nil || err.StatusCode != http.StatusNotImplemented {
   336  		t.Fatal("should have errored with 501")
   337  	}
   338  
   339  	if _, err := Client.DoApiGet("/license/client?format=junk", ""); err == nil || err.StatusCode != http.StatusBadRequest {
   340  		t.Fatal("should have errored with 400")
   341  	}
   342  
   343  	license, resp = th.SystemAdminClient.GetOldClientLicense("")
   344  	CheckNoError(t, resp)
   345  
   346  	if len(license["IsLicensed"]) == 0 {
   347  		t.Fatal("license not returned correctly")
   348  	}
   349  }
   350  
   351  func TestGetAudits(t *testing.T) {
   352  	th := Setup().InitBasic().InitSystemAdmin()
   353  	defer th.TearDown()
   354  	Client := th.Client
   355  
   356  	audits, resp := th.SystemAdminClient.GetAudits(0, 100, "")
   357  	CheckNoError(t, resp)
   358  
   359  	if len(audits) == 0 {
   360  		t.Fatal("should not be empty")
   361  	}
   362  
   363  	audits, resp = th.SystemAdminClient.GetAudits(0, 1, "")
   364  	CheckNoError(t, resp)
   365  
   366  	if len(audits) != 1 {
   367  		t.Fatal("should only be 1")
   368  	}
   369  
   370  	audits, resp = th.SystemAdminClient.GetAudits(1, 1, "")
   371  	CheckNoError(t, resp)
   372  
   373  	if len(audits) != 1 {
   374  		t.Fatal("should only be 1")
   375  	}
   376  
   377  	_, resp = th.SystemAdminClient.GetAudits(-1, -1, "")
   378  	CheckNoError(t, resp)
   379  
   380  	_, resp = Client.GetAudits(0, 100, "")
   381  	CheckForbiddenStatus(t, resp)
   382  
   383  	Client.Logout()
   384  	_, resp = Client.GetAudits(0, 100, "")
   385  	CheckUnauthorizedStatus(t, resp)
   386  }
   387  
   388  func TestEmailTest(t *testing.T) {
   389  	th := Setup().InitBasic().InitSystemAdmin()
   390  	defer th.TearDown()
   391  	Client := th.Client
   392  
   393  	config := model.Config{
   394  		EmailSettings: model.EmailSettings{
   395  			SMTPServer: "",
   396  			SMTPPort:   "",
   397  		},
   398  	}
   399  
   400  	_, resp := Client.TestEmail(&config)
   401  	CheckForbiddenStatus(t, resp)
   402  
   403  	_, resp = th.SystemAdminClient.TestEmail(&config)
   404  	CheckErrorMessage(t, resp, "api.admin.test_email.missing_server")
   405  	CheckBadRequestStatus(t, resp)
   406  
   407  	inbucket_host := os.Getenv("CI_HOST")
   408  	if inbucket_host == "" {
   409  		inbucket_host = "dockerhost"
   410  	}
   411  
   412  	inbucket_port := os.Getenv("CI_INBUCKET_PORT")
   413  	if inbucket_port == "" {
   414  		inbucket_port = "9000"
   415  	}
   416  
   417  	config.EmailSettings.SMTPServer = inbucket_host
   418  	config.EmailSettings.SMTPPort = inbucket_port
   419  	_, resp = th.SystemAdminClient.TestEmail(&config)
   420  	CheckOKStatus(t, resp)
   421  }
   422  
   423  func TestDatabaseRecycle(t *testing.T) {
   424  	th := Setup().InitBasic().InitSystemAdmin()
   425  	defer th.TearDown()
   426  	Client := th.Client
   427  
   428  	_, resp := Client.DatabaseRecycle()
   429  	CheckForbiddenStatus(t, resp)
   430  
   431  	_, resp = th.SystemAdminClient.DatabaseRecycle()
   432  	CheckNoError(t, resp)
   433  }
   434  
   435  func TestInvalidateCaches(t *testing.T) {
   436  	th := Setup().InitBasic().InitSystemAdmin()
   437  	defer th.TearDown()
   438  	Client := th.Client
   439  
   440  	flag, resp := Client.InvalidateCaches()
   441  	CheckForbiddenStatus(t, resp)
   442  	if flag {
   443  		t.Fatal("should not clean the cache due no permission.")
   444  	}
   445  
   446  	flag, resp = th.SystemAdminClient.InvalidateCaches()
   447  	CheckNoError(t, resp)
   448  	if !flag {
   449  		t.Fatal("should clean the cache")
   450  	}
   451  }
   452  
   453  func TestGetLogs(t *testing.T) {
   454  	th := Setup().InitBasic().InitSystemAdmin()
   455  	defer th.TearDown()
   456  	Client := th.Client
   457  
   458  	for i := 0; i < 20; i++ {
   459  		mlog.Info(fmt.Sprint(i))
   460  	}
   461  
   462  	logs, resp := th.SystemAdminClient.GetLogs(0, 10)
   463  	CheckNoError(t, resp)
   464  
   465  	if len(logs) != 10 {
   466  		t.Log(len(logs))
   467  		t.Fatal("wrong length")
   468  	}
   469  
   470  	logs, resp = th.SystemAdminClient.GetLogs(1, 10)
   471  	CheckNoError(t, resp)
   472  
   473  	if len(logs) != 10 {
   474  		t.Log(len(logs))
   475  		t.Fatal("wrong length")
   476  	}
   477  
   478  	logs, resp = th.SystemAdminClient.GetLogs(-1, -1)
   479  	CheckNoError(t, resp)
   480  
   481  	if len(logs) == 0 {
   482  		t.Fatal("should not be empty")
   483  	}
   484  
   485  	_, resp = Client.GetLogs(0, 10)
   486  	CheckForbiddenStatus(t, resp)
   487  
   488  	Client.Logout()
   489  	_, resp = Client.GetLogs(0, 10)
   490  	CheckUnauthorizedStatus(t, resp)
   491  }
   492  
   493  func TestPostLog(t *testing.T) {
   494  	th := Setup().InitBasic().InitSystemAdmin()
   495  	defer th.TearDown()
   496  	Client := th.Client
   497  
   498  	enableDev := *th.App.Config().ServiceSettings.EnableDeveloper
   499  	defer func() {
   500  		*th.App.Config().ServiceSettings.EnableDeveloper = enableDev
   501  	}()
   502  	*th.App.Config().ServiceSettings.EnableDeveloper = true
   503  
   504  	message := make(map[string]string)
   505  	message["level"] = "ERROR"
   506  	message["message"] = "this is a test"
   507  
   508  	_, resp := Client.PostLog(message)
   509  	CheckNoError(t, resp)
   510  
   511  	Client.Logout()
   512  
   513  	_, resp = Client.PostLog(message)
   514  	CheckNoError(t, resp)
   515  
   516  	*th.App.Config().ServiceSettings.EnableDeveloper = false
   517  
   518  	_, resp = Client.PostLog(message)
   519  	CheckForbiddenStatus(t, resp)
   520  
   521  	logMessage, resp := th.SystemAdminClient.PostLog(message)
   522  	CheckNoError(t, resp)
   523  	if len(logMessage) == 0 {
   524  		t.Fatal("should return the log message")
   525  	}
   526  }
   527  
   528  func TestUploadLicenseFile(t *testing.T) {
   529  	th := Setup().InitBasic().InitSystemAdmin()
   530  	defer th.TearDown()
   531  	Client := th.Client
   532  
   533  	ok, resp := Client.UploadLicenseFile([]byte{})
   534  	CheckForbiddenStatus(t, resp)
   535  	if ok {
   536  		t.Fatal("should fail")
   537  	}
   538  
   539  	ok, resp = th.SystemAdminClient.UploadLicenseFile([]byte{})
   540  	CheckBadRequestStatus(t, resp)
   541  	if ok {
   542  		t.Fatal("should fail")
   543  	}
   544  }
   545  
   546  func TestRemoveLicenseFile(t *testing.T) {
   547  	th := Setup().InitBasic().InitSystemAdmin()
   548  	defer th.TearDown()
   549  	Client := th.Client
   550  
   551  	ok, resp := Client.RemoveLicenseFile()
   552  	CheckForbiddenStatus(t, resp)
   553  	if ok {
   554  		t.Fatal("should fail")
   555  	}
   556  
   557  	ok, resp = th.SystemAdminClient.RemoveLicenseFile()
   558  	CheckNoError(t, resp)
   559  	if !ok {
   560  		t.Fatal("should pass")
   561  	}
   562  }
   563  
   564  func TestGetAnalyticsOld(t *testing.T) {
   565  	th := Setup().InitBasic().InitSystemAdmin()
   566  	defer th.TearDown()
   567  	Client := th.Client
   568  
   569  	rows, resp := Client.GetAnalyticsOld("", "")
   570  	CheckForbiddenStatus(t, resp)
   571  	if rows != nil {
   572  		t.Fatal("should be nil")
   573  	}
   574  
   575  	rows, resp = th.SystemAdminClient.GetAnalyticsOld("", "")
   576  	CheckNoError(t, resp)
   577  
   578  	found := false
   579  	found2 := false
   580  	for _, row := range rows {
   581  		if row.Name == "unique_user_count" {
   582  			found = true
   583  		} else if row.Name == "inactive_user_count" {
   584  			found2 = true
   585  			assert.True(t, row.Value >= 0)
   586  		}
   587  	}
   588  
   589  	assert.True(t, found, "should return unique user count")
   590  	assert.True(t, found2, "should return inactive user count")
   591  
   592  	_, resp = th.SystemAdminClient.GetAnalyticsOld("post_counts_day", "")
   593  	CheckNoError(t, resp)
   594  
   595  	_, resp = th.SystemAdminClient.GetAnalyticsOld("user_counts_with_posts_day", "")
   596  	CheckNoError(t, resp)
   597  
   598  	_, resp = th.SystemAdminClient.GetAnalyticsOld("extra_counts", "")
   599  	CheckNoError(t, resp)
   600  
   601  	rows, resp = th.SystemAdminClient.GetAnalyticsOld("", th.BasicTeam.Id)
   602  	CheckNoError(t, resp)
   603  
   604  	for _, row := range rows {
   605  		if row.Name == "inactive_user_count" {
   606  			assert.Equal(t, float64(-1), row.Value, "inactive user count should be -1 when team specified")
   607  		}
   608  	}
   609  
   610  	rows2, resp2 := th.SystemAdminClient.GetAnalyticsOld("standard", "")
   611  	CheckNoError(t, resp2)
   612  	assert.Equal(t, "total_websocket_connections", rows2[5].Name)
   613  	assert.Equal(t, float64(0), rows2[5].Value)
   614  
   615  	WebSocketClient, err := th.CreateWebSocketClient()
   616  	if err != nil {
   617  		t.Fatal(err)
   618  	}
   619  
   620  	rows2, resp2 = th.SystemAdminClient.GetAnalyticsOld("standard", "")
   621  	CheckNoError(t, resp2)
   622  	assert.Equal(t, "total_websocket_connections", rows2[5].Name)
   623  	assert.Equal(t, float64(1), rows2[5].Value)
   624  
   625  	WebSocketClient.Close()
   626  
   627  	rows2, resp2 = th.SystemAdminClient.GetAnalyticsOld("standard", "")
   628  	CheckNoError(t, resp2)
   629  	assert.Equal(t, "total_websocket_connections", rows2[5].Name)
   630  	assert.Equal(t, float64(0), rows2[5].Value)
   631  
   632  	Client.Logout()
   633  	_, resp = Client.GetAnalyticsOld("", th.BasicTeam.Id)
   634  	CheckUnauthorizedStatus(t, resp)
   635  }
   636  
   637  func TestS3TestConnection(t *testing.T) {
   638  	th := Setup().InitBasic().InitSystemAdmin()
   639  	defer th.TearDown()
   640  	Client := th.Client
   641  
   642  	s3Host := os.Getenv("CI_HOST")
   643  	if s3Host == "" {
   644  		s3Host = "dockerhost"
   645  	}
   646  
   647  	s3Port := os.Getenv("CI_MINIO_PORT")
   648  	if s3Port == "" {
   649  		s3Port = "9001"
   650  	}
   651  
   652  	s3Endpoint := fmt.Sprintf("%s:%s", s3Host, s3Port)
   653  	config := model.Config{
   654  		FileSettings: model.FileSettings{
   655  			DriverName:              model.NewString(model.IMAGE_DRIVER_S3),
   656  			AmazonS3AccessKeyId:     model.MINIO_ACCESS_KEY,
   657  			AmazonS3SecretAccessKey: model.MINIO_SECRET_KEY,
   658  			AmazonS3Bucket:          "",
   659  			AmazonS3Endpoint:        s3Endpoint,
   660  			AmazonS3SSL:             model.NewBool(false),
   661  		},
   662  	}
   663  
   664  	_, resp := Client.TestS3Connection(&config)
   665  	CheckForbiddenStatus(t, resp)
   666  
   667  	_, resp = th.SystemAdminClient.TestS3Connection(&config)
   668  	CheckBadRequestStatus(t, resp)
   669  	if resp.Error.Message != "S3 Bucket is required" {
   670  		t.Fatal("should return error - missing s3 bucket")
   671  	}
   672  
   673  	config.FileSettings.AmazonS3Bucket = model.MINIO_BUCKET
   674  	config.FileSettings.AmazonS3Region = "us-east-1"
   675  	_, resp = th.SystemAdminClient.TestS3Connection(&config)
   676  	CheckOKStatus(t, resp)
   677  
   678  	config.FileSettings.AmazonS3Region = ""
   679  	_, resp = th.SystemAdminClient.TestS3Connection(&config)
   680  	CheckOKStatus(t, resp)
   681  
   682  	config.FileSettings.AmazonS3Bucket = "Wrong_bucket"
   683  	_, resp = th.SystemAdminClient.TestS3Connection(&config)
   684  	CheckInternalErrorStatus(t, resp)
   685  	assert.Equal(t, "Unable to create bucket.", resp.Error.Message)
   686  
   687  	config.FileSettings.AmazonS3Bucket = "shouldcreatenewbucket"
   688  	_, resp = th.SystemAdminClient.TestS3Connection(&config)
   689  	CheckOKStatus(t, resp)
   690  
   691  }
   692  
   693  func TestSupportedTimezones(t *testing.T) {
   694  	th := Setup().InitBasic()
   695  	defer th.TearDown()
   696  	Client := th.Client
   697  
   698  	supportedTimezonesFromConfig := th.App.Timezones()
   699  	supportedTimezones, resp := Client.GetSupportedTimezone()
   700  
   701  	CheckNoError(t, resp)
   702  	assert.Equal(t, supportedTimezonesFromConfig, supportedTimezones)
   703  }
   704  
   705  func TestRedirectLocation(t *testing.T) {
   706  	expected := "https://mattermost.com/wp-content/themes/mattermostv2/img/logo-light.svg"
   707  
   708  	testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
   709  		res.Header().Set("Location", expected)
   710  		res.WriteHeader(http.StatusFound)
   711  		res.Write([]byte("body"))
   712  	}))
   713  	defer func() { testServer.Close() }()
   714  
   715  	mockBitlyLink := testServer.URL
   716  
   717  	th := Setup().InitBasic().InitSystemAdmin()
   718  	defer th.TearDown()
   719  	Client := th.Client
   720  	enableLinkPreviews := *th.App.Config().ServiceSettings.EnableLinkPreviews
   721  	defer func() {
   722  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableLinkPreviews = enableLinkPreviews })
   723  	}()
   724  
   725  	*th.App.Config().ServiceSettings.EnableLinkPreviews = true
   726  
   727  	_, resp := th.SystemAdminClient.GetRedirectLocation("https://mattermost.com/", "")
   728  	CheckNoError(t, resp)
   729  
   730  	_, resp = th.SystemAdminClient.GetRedirectLocation("", "")
   731  	CheckBadRequestStatus(t, resp)
   732  
   733  	actual, resp := th.SystemAdminClient.GetRedirectLocation(mockBitlyLink, "")
   734  	CheckNoError(t, resp)
   735  	if actual != expected {
   736  		t.Errorf("Expected %v but got %v.", expected, actual)
   737  	}
   738  
   739  	*th.App.Config().ServiceSettings.EnableLinkPreviews = false
   740  	actual, resp = th.SystemAdminClient.GetRedirectLocation("https://mattermost.com/", "")
   741  	CheckNoError(t, resp)
   742  	assert.Equal(t, actual, "")
   743  
   744  	actual, resp = th.SystemAdminClient.GetRedirectLocation("", "")
   745  	CheckNoError(t, resp)
   746  	assert.Equal(t, actual, "")
   747  
   748  	actual, resp = th.SystemAdminClient.GetRedirectLocation(mockBitlyLink, "")
   749  	CheckNoError(t, resp)
   750  	assert.Equal(t, actual, "")
   751  
   752  	Client.Logout()
   753  	_, resp = Client.GetRedirectLocation("", "")
   754  	CheckUnauthorizedStatus(t, resp)
   755  }