github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+incompatible/api/admin_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api
     5  
     6  import (
     7  	"net/http"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/mattermost/mattermost-server/model"
    12  	"github.com/mattermost/mattermost-server/store"
    13  )
    14  
    15  func TestGetLogs(t *testing.T) {
    16  	th := Setup().InitSystemAdmin().InitBasic()
    17  	defer th.TearDown()
    18  
    19  	if _, err := th.BasicClient.GetLogs(); err == nil {
    20  		t.Fatal("Shouldn't have permissions")
    21  	}
    22  
    23  	if logs, err := th.SystemAdminClient.GetLogs(); err != nil {
    24  		t.Fatal(err)
    25  	} else if len(logs.Data.([]string)) <= 0 {
    26  		t.Fatal()
    27  	}
    28  }
    29  
    30  func TestGetClusterInfos(t *testing.T) {
    31  	if testing.Short() {
    32  		t.SkipNow()
    33  	}
    34  	th := Setup().InitSystemAdmin().InitBasic()
    35  	defer th.TearDown()
    36  
    37  	if _, err := th.BasicClient.GetClusterStatus(); err == nil {
    38  		t.Fatal("Shouldn't have permissions")
    39  	}
    40  
    41  	if _, err := th.SystemAdminClient.GetClusterStatus(); err != nil {
    42  		t.Fatal(err)
    43  	}
    44  }
    45  
    46  func TestGetAllAudits(t *testing.T) {
    47  	th := Setup().InitBasic().InitSystemAdmin()
    48  	defer th.TearDown()
    49  
    50  	if _, err := th.BasicClient.GetAllAudits(); err == nil {
    51  		t.Fatal("Shouldn't have permissions")
    52  	}
    53  
    54  	if audits, err := th.SystemAdminClient.GetAllAudits(); err != nil {
    55  		t.Fatal(err)
    56  	} else if len(audits.Data.(model.Audits)) <= 0 {
    57  		t.Fatal()
    58  	}
    59  }
    60  
    61  func TestGetConfig(t *testing.T) {
    62  	th := Setup().InitBasic().InitSystemAdmin()
    63  	defer th.TearDown()
    64  
    65  	if _, err := th.BasicClient.GetConfig(); err == nil {
    66  		t.Fatal("Shouldn't have permissions")
    67  	}
    68  
    69  	if result, err := th.SystemAdminClient.GetConfig(); err != nil {
    70  		t.Fatal(err)
    71  	} else {
    72  		cfg := result.Data.(*model.Config)
    73  
    74  		if len(cfg.TeamSettings.SiteName) == 0 {
    75  			t.Fatal()
    76  		}
    77  
    78  		if *cfg.LdapSettings.BindPassword != model.FAKE_SETTING && len(*cfg.LdapSettings.BindPassword) != 0 {
    79  			t.Fatal("did not sanitize properly")
    80  		}
    81  		if *cfg.FileSettings.PublicLinkSalt != model.FAKE_SETTING {
    82  			t.Fatal("did not sanitize properly")
    83  		}
    84  		if cfg.FileSettings.AmazonS3SecretAccessKey != model.FAKE_SETTING && len(cfg.FileSettings.AmazonS3SecretAccessKey) != 0 {
    85  			t.Fatal("did not sanitize properly")
    86  		}
    87  		if cfg.EmailSettings.InviteSalt != model.FAKE_SETTING {
    88  			t.Fatal("did not sanitize properly")
    89  		}
    90  		if cfg.EmailSettings.SMTPPassword != model.FAKE_SETTING && len(cfg.EmailSettings.SMTPPassword) != 0 {
    91  			t.Fatal("did not sanitize properly")
    92  		}
    93  		if cfg.GitLabSettings.Secret != model.FAKE_SETTING && len(cfg.GitLabSettings.Secret) != 0 {
    94  			t.Fatal("did not sanitize properly")
    95  		}
    96  		if *cfg.SqlSettings.DataSource != model.FAKE_SETTING {
    97  			t.Fatal("did not sanitize properly")
    98  		}
    99  		if cfg.SqlSettings.AtRestEncryptKey != model.FAKE_SETTING {
   100  			t.Fatal("did not sanitize properly")
   101  		}
   102  		if !strings.Contains(strings.Join(cfg.SqlSettings.DataSourceReplicas, " "), model.FAKE_SETTING) && len(cfg.SqlSettings.DataSourceReplicas) != 0 {
   103  			t.Fatal("did not sanitize properly")
   104  		}
   105  	}
   106  }
   107  
   108  func TestReloadConfig(t *testing.T) {
   109  	th := Setup().InitBasic().InitSystemAdmin()
   110  	defer th.TearDown()
   111  
   112  	if _, err := th.BasicClient.ReloadConfig(); err == nil {
   113  		t.Fatal("Shouldn't have permissions")
   114  	}
   115  
   116  	if _, err := th.SystemAdminClient.ReloadConfig(); err != nil {
   117  		t.Fatal(err)
   118  	}
   119  
   120  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.MaxUsersPerTeam = 50 })
   121  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = true })
   122  }
   123  
   124  func TestInvalidateAllCache(t *testing.T) {
   125  	th := Setup().InitBasic().InitSystemAdmin()
   126  	defer th.TearDown()
   127  
   128  	if _, err := th.BasicClient.InvalidateAllCaches(); err == nil {
   129  		t.Fatal("Shouldn't have permissions")
   130  	}
   131  
   132  	if _, err := th.SystemAdminClient.InvalidateAllCaches(); err != nil {
   133  		t.Fatal(err)
   134  	}
   135  }
   136  
   137  func TestSaveConfig(t *testing.T) {
   138  	th := Setup().InitBasic().InitSystemAdmin()
   139  	defer th.TearDown()
   140  
   141  	if _, err := th.BasicClient.SaveConfig(th.App.Config()); err == nil {
   142  		t.Fatal("Shouldn't have permissions")
   143  	}
   144  
   145  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = false })
   146  
   147  	if _, err := th.SystemAdminClient.SaveConfig(th.App.Config()); err != nil {
   148  		t.Fatal(err)
   149  	}
   150  
   151  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = true })
   152  }
   153  
   154  func TestRecycleDatabaseConnection(t *testing.T) {
   155  	th := Setup().InitBasic().InitSystemAdmin()
   156  	defer th.TearDown()
   157  
   158  	if _, err := th.BasicClient.RecycleDatabaseConnection(); err == nil {
   159  		t.Fatal("Shouldn't have permissions")
   160  	}
   161  
   162  	if _, err := th.SystemAdminClient.RecycleDatabaseConnection(); err != nil {
   163  		t.Fatal(err)
   164  	}
   165  }
   166  
   167  func TestEmailTest(t *testing.T) {
   168  	th := Setup().InitBasic().InitSystemAdmin()
   169  	defer th.TearDown()
   170  
   171  	SendEmailNotifications := th.App.Config().EmailSettings.SendEmailNotifications
   172  	SMTPServer := th.App.Config().EmailSettings.SMTPServer
   173  	SMTPPort := th.App.Config().EmailSettings.SMTPPort
   174  	FeedbackEmail := th.App.Config().EmailSettings.FeedbackEmail
   175  	defer func() {
   176  		th.App.UpdateConfig(func(cfg *model.Config) { cfg.EmailSettings.SendEmailNotifications = SendEmailNotifications })
   177  		th.App.UpdateConfig(func(cfg *model.Config) { cfg.EmailSettings.SMTPServer = SMTPServer })
   178  		th.App.UpdateConfig(func(cfg *model.Config) { cfg.EmailSettings.SMTPPort = SMTPPort })
   179  		th.App.UpdateConfig(func(cfg *model.Config) { cfg.EmailSettings.FeedbackEmail = FeedbackEmail })
   180  	}()
   181  
   182  	th.App.UpdateConfig(func(cfg *model.Config) { cfg.EmailSettings.SendEmailNotifications = false })
   183  	th.App.UpdateConfig(func(cfg *model.Config) { cfg.EmailSettings.SMTPServer = "" })
   184  	th.App.UpdateConfig(func(cfg *model.Config) { cfg.EmailSettings.SMTPPort = "" })
   185  	th.App.UpdateConfig(func(cfg *model.Config) { cfg.EmailSettings.FeedbackEmail = "" })
   186  
   187  	if _, err := th.BasicClient.TestEmail(th.App.Config()); err == nil {
   188  		t.Fatal("Shouldn't have permissions")
   189  	}
   190  
   191  	if _, err := th.SystemAdminClient.TestEmail(th.App.Config()); err == nil {
   192  		t.Fatal("should have errored")
   193  	} else {
   194  		if err.Id != "api.admin.test_email.missing_server" {
   195  			t.Fatal(err)
   196  		}
   197  	}
   198  }
   199  
   200  func TestLdapTest(t *testing.T) {
   201  	th := Setup().InitBasic().InitSystemAdmin()
   202  	defer th.TearDown()
   203  
   204  	if _, err := th.BasicClient.TestLdap(th.App.Config()); err == nil {
   205  		t.Fatal("Shouldn't have permissions")
   206  	}
   207  
   208  	if _, err := th.SystemAdminClient.TestLdap(th.App.Config()); err == nil {
   209  		t.Fatal("should have errored")
   210  	}
   211  }
   212  
   213  func TestGetTeamAnalyticsStandard(t *testing.T) {
   214  	th := Setup().InitBasic().InitSystemAdmin()
   215  	defer th.TearDown()
   216  
   217  	th.CreatePrivateChannel(th.BasicClient, th.BasicTeam)
   218  
   219  	if _, err := th.BasicClient.GetTeamAnalytics(th.BasicTeam.Id, "standard"); err == nil {
   220  		t.Fatal("Shouldn't have permissions")
   221  	}
   222  
   223  	maxUsersForStats := *th.App.Config().AnalyticsSettings.MaxUsersForStatistics
   224  	defer func() {
   225  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.AnalyticsSettings.MaxUsersForStatistics = maxUsersForStats })
   226  	}()
   227  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.AnalyticsSettings.MaxUsersForStatistics = 1000000 })
   228  
   229  	if result, err := th.SystemAdminClient.GetTeamAnalytics(th.BasicTeam.Id, "standard"); err != nil {
   230  		t.Fatal(err)
   231  	} else {
   232  		rows := result.Data.(model.AnalyticsRows)
   233  
   234  		if rows[0].Name != "channel_open_count" {
   235  			t.Log(rows.ToJson())
   236  			t.Fatal()
   237  		}
   238  
   239  		if rows[0].Value != 4 {
   240  			t.Log(rows.ToJson())
   241  			t.Fatal()
   242  		}
   243  
   244  		if rows[1].Name != "channel_private_count" {
   245  			t.Log(rows.ToJson())
   246  			t.Fatal()
   247  		}
   248  
   249  		if rows[1].Value != 1 {
   250  			t.Log(rows.ToJson())
   251  			t.Fatal()
   252  		}
   253  
   254  		if rows[2].Name != "post_count" {
   255  			t.Log(rows.ToJson())
   256  			t.Fatal()
   257  		}
   258  
   259  		if rows[2].Value != 9 {
   260  			t.Log(rows.ToJson())
   261  			t.Fatal()
   262  		}
   263  
   264  		if rows[3].Name != "unique_user_count" {
   265  			t.Log(rows.ToJson())
   266  			t.Fatal()
   267  		}
   268  
   269  		if rows[3].Value != 2 {
   270  			t.Log(rows.ToJson())
   271  			t.Fatal()
   272  		}
   273  
   274  		if rows[4].Name != "team_count" {
   275  			t.Log(rows.ToJson())
   276  			t.Fatal()
   277  		}
   278  
   279  		if rows[4].Value == 0 {
   280  			t.Log(rows.ToJson())
   281  			t.Fatal()
   282  		}
   283  	}
   284  
   285  	if result, err := th.SystemAdminClient.GetSystemAnalytics("standard"); err != nil {
   286  		t.Fatal(err)
   287  	} else {
   288  		rows := result.Data.(model.AnalyticsRows)
   289  
   290  		if rows[0].Name != "channel_open_count" {
   291  			t.Log(rows.ToJson())
   292  			t.Fatal()
   293  		}
   294  
   295  		if rows[0].Value < 3 {
   296  			t.Log(rows.ToJson())
   297  			t.Fatal()
   298  		}
   299  
   300  		if rows[1].Name != "channel_private_count" {
   301  			t.Log(rows.ToJson())
   302  			t.Fatal()
   303  		}
   304  
   305  		if rows[1].Value == 0 {
   306  			t.Log(rows.ToJson())
   307  			t.Fatal()
   308  		}
   309  
   310  		if rows[2].Name != "post_count" {
   311  			t.Log(rows.ToJson())
   312  			t.Fatal()
   313  		}
   314  
   315  		if rows[2].Value == 0 {
   316  			t.Log(rows.ToJson())
   317  			t.Fatal()
   318  		}
   319  
   320  		if rows[3].Name != "unique_user_count" {
   321  			t.Log(rows.ToJson())
   322  			t.Fatal()
   323  		}
   324  
   325  		if rows[3].Value == 0 {
   326  			t.Log(rows.ToJson())
   327  			t.Fatal()
   328  		}
   329  
   330  		if rows[4].Name != "team_count" {
   331  			t.Log(rows.ToJson())
   332  			t.Fatal()
   333  		}
   334  
   335  		if rows[4].Value == 0 {
   336  			t.Log(rows.ToJson())
   337  			t.Fatal()
   338  		}
   339  	}
   340  
   341  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.AnalyticsSettings.MaxUsersForStatistics = 1 })
   342  
   343  	if result, err := th.SystemAdminClient.GetSystemAnalytics("standard"); err != nil {
   344  		t.Fatal(err)
   345  	} else {
   346  		rows := result.Data.(model.AnalyticsRows)
   347  
   348  		if rows[2].Name != "post_count" {
   349  			t.Log(rows.ToJson())
   350  			t.Fatal()
   351  		}
   352  
   353  		if rows[2].Value != -1 {
   354  			t.Log(rows.ToJson())
   355  			t.Fatal()
   356  		}
   357  	}
   358  }
   359  
   360  func TestGetTeamAnalyticsExtra(t *testing.T) {
   361  	th := Setup().InitBasic().InitSystemAdmin()
   362  	defer th.TearDown()
   363  
   364  	th.CreatePost(th.BasicClient, th.BasicChannel)
   365  
   366  	if _, err := th.BasicClient.GetTeamAnalytics("", "extra_counts"); err == nil {
   367  		t.Fatal("Shouldn't have permissions")
   368  	}
   369  
   370  	maxUsersForStats := *th.App.Config().AnalyticsSettings.MaxUsersForStatistics
   371  	defer func() {
   372  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.AnalyticsSettings.MaxUsersForStatistics = maxUsersForStats })
   373  	}()
   374  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.AnalyticsSettings.MaxUsersForStatistics = 1000000 })
   375  
   376  	if result, err := th.SystemAdminClient.GetTeamAnalytics(th.BasicTeam.Id, "extra_counts"); err != nil {
   377  		t.Fatal(err)
   378  	} else {
   379  		rows := result.Data.(model.AnalyticsRows)
   380  
   381  		if rows[0].Name != "file_post_count" {
   382  			t.Log(rows.ToJson())
   383  			t.Fatal()
   384  		}
   385  
   386  		if rows[0].Value != 0 {
   387  			t.Log(rows.ToJson())
   388  			t.Fatal()
   389  		}
   390  
   391  		if rows[1].Name != "hashtag_post_count" {
   392  			t.Log(rows.ToJson())
   393  			t.Fatal()
   394  		}
   395  
   396  		if rows[1].Value != 0 {
   397  			t.Log(rows.ToJson())
   398  			t.Fatal()
   399  		}
   400  
   401  		if rows[2].Name != "incoming_webhook_count" {
   402  			t.Log(rows.ToJson())
   403  			t.Fatal()
   404  		}
   405  
   406  		if rows[2].Value != 0 {
   407  			t.Log(rows.ToJson())
   408  			t.Fatal()
   409  		}
   410  
   411  		if rows[3].Name != "outgoing_webhook_count" {
   412  			t.Log(rows.ToJson())
   413  			t.Fatal()
   414  		}
   415  
   416  		if rows[3].Value != 0 {
   417  			t.Log(rows.ToJson())
   418  			t.Fatal()
   419  		}
   420  
   421  		if rows[4].Name != "command_count" {
   422  			t.Log(rows.ToJson())
   423  			t.Fatal()
   424  		}
   425  
   426  		if rows[4].Value != 0 {
   427  			t.Log(rows.ToJson())
   428  			t.Fatal()
   429  		}
   430  
   431  		if rows[5].Name != "session_count" {
   432  			t.Log(rows.ToJson())
   433  			t.Fatal()
   434  		}
   435  
   436  		if rows[5].Value == 0 {
   437  			t.Log(rows.ToJson())
   438  			t.Fatal()
   439  		}
   440  	}
   441  
   442  	if result, err := th.SystemAdminClient.GetSystemAnalytics("extra_counts"); err != nil {
   443  		t.Fatal(err)
   444  	} else {
   445  		rows := result.Data.(model.AnalyticsRows)
   446  
   447  		if rows[0].Name != "file_post_count" {
   448  			t.Log(rows.ToJson())
   449  			t.Fatal()
   450  		}
   451  
   452  		if rows[1].Name != "hashtag_post_count" {
   453  			t.Log(rows.ToJson())
   454  			t.Fatal()
   455  		}
   456  
   457  		if rows[2].Name != "incoming_webhook_count" {
   458  			t.Log(rows.ToJson())
   459  			t.Fatal()
   460  		}
   461  
   462  		if rows[3].Name != "outgoing_webhook_count" {
   463  			t.Log(rows.ToJson())
   464  			t.Fatal()
   465  		}
   466  
   467  		if rows[4].Name != "command_count" {
   468  			t.Log(rows.ToJson())
   469  			t.Fatal()
   470  		}
   471  
   472  		if rows[5].Name != "session_count" {
   473  			t.Log(rows.ToJson())
   474  			t.Fatal()
   475  		}
   476  	}
   477  
   478  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.AnalyticsSettings.MaxUsersForStatistics = 1 })
   479  
   480  	if result, err := th.SystemAdminClient.GetSystemAnalytics("extra_counts"); err != nil {
   481  		t.Fatal(err)
   482  	} else {
   483  		rows := result.Data.(model.AnalyticsRows)
   484  
   485  		if rows[0].Value != -1 {
   486  			t.Log(rows.ToJson())
   487  			t.Fatal()
   488  		}
   489  
   490  		if rows[1].Value != -1 {
   491  			t.Log(rows.ToJson())
   492  			t.Fatal()
   493  		}
   494  	}
   495  }
   496  
   497  func TestAdminResetMfa(t *testing.T) {
   498  	th := Setup().InitBasic().InitSystemAdmin()
   499  	defer th.TearDown()
   500  
   501  	if _, err := th.BasicClient.AdminResetMfa("12345678901234567890123456"); err == nil {
   502  		t.Fatal("should have failed - not an admin")
   503  	}
   504  
   505  	if _, err := th.SystemAdminClient.AdminResetMfa(""); err == nil {
   506  		t.Fatal("should have failed - empty user id")
   507  	}
   508  
   509  	if _, err := th.SystemAdminClient.AdminResetMfa("12345678901234567890123456"); err == nil {
   510  		t.Fatal("should have failed - bad user id")
   511  	}
   512  
   513  	if _, err := th.SystemAdminClient.AdminResetMfa(th.BasicUser.Id); err == nil {
   514  		t.Fatal("should have failed - not licensed or configured")
   515  	}
   516  
   517  	// need to add more test cases when enterprise bits can be loaded into tests
   518  }
   519  
   520  func TestAdminResetPassword(t *testing.T) {
   521  	th := Setup().InitSystemAdmin()
   522  	defer th.TearDown()
   523  
   524  	Client := th.SystemAdminClient
   525  	team := th.SystemAdminTeam
   526  
   527  	user := &model.User{Email: strings.ToLower(model.NewId()) + "success+test@simulator.amazonses.com", Nickname: "Corey Hulen", Password: "passwd1"}
   528  	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
   529  	th.LinkUserToTeam(user, team)
   530  	store.Must(th.App.Srv.Store.User().VerifyEmail(user.Id))
   531  
   532  	if _, err := Client.AdminResetPassword("", "newpwd1"); err == nil {
   533  		t.Fatal("Should have errored - empty user id")
   534  	}
   535  
   536  	if _, err := Client.AdminResetPassword("123", "newpwd1"); err == nil {
   537  		t.Fatal("Should have errored - bad user id")
   538  	}
   539  
   540  	if _, err := Client.AdminResetPassword("12345678901234567890123456", "newpwd1"); err == nil {
   541  		t.Fatal("Should have errored - bad user id")
   542  	}
   543  
   544  	if _, err := Client.AdminResetPassword("12345678901234567890123456", "newp"); err == nil {
   545  		t.Fatal("Should have errored - password too short")
   546  	}
   547  
   548  	authData := model.NewId()
   549  	user2 := &model.User{Email: strings.ToLower(model.NewId()) + "success+test@simulator.amazonses.com", Nickname: "Corey Hulen", AuthData: &authData, AuthService: "random"}
   550  	user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
   551  	th.LinkUserToTeam(user2, team)
   552  	store.Must(th.App.Srv.Store.User().VerifyEmail(user2.Id))
   553  
   554  	if _, err := Client.AdminResetPassword(user.Id, "newpwd1"); err != nil {
   555  		t.Fatal(err)
   556  	}
   557  
   558  	Client.Logout()
   559  	Client.Must(Client.LoginById(user.Id, "newpwd1"))
   560  	Client.SetTeamId(team.Id)
   561  
   562  	if _, err := Client.AdminResetPassword(user.Id, "newpwd1"); err == nil {
   563  		t.Fatal("Should have errored - not sytem admin")
   564  	}
   565  }
   566  
   567  func TestAdminLdapSyncNow(t *testing.T) {
   568  	th := Setup().InitSystemAdmin()
   569  	defer th.TearDown()
   570  
   571  	Client := th.SystemAdminClient
   572  
   573  	if _, err := Client.LdapSyncNow(); err != nil {
   574  		t.Fatal("Returned Failure")
   575  	}
   576  }
   577  
   578  // Needs more work
   579  func TestGetRecentlyActiveUsers(t *testing.T) {
   580  	th := Setup().InitBasic()
   581  	defer th.TearDown()
   582  
   583  	if userMap, err := th.BasicClient.GetRecentlyActiveUsers(th.BasicTeam.Id); err != nil {
   584  		t.Fatal(err)
   585  	} else if len(userMap.Data.(map[string]*model.User)) >= 2 {
   586  		t.Fatal("should have been at least 2")
   587  	}
   588  }
   589  
   590  func TestDisableAPIv3(t *testing.T) {
   591  	th := Setup().InitBasic()
   592  	defer th.TearDown()
   593  
   594  	Client := th.BasicClient
   595  
   596  	enableAPIv3 := *th.App.Config().ServiceSettings.EnableAPIv3
   597  	defer func() {
   598  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableAPIv3 = enableAPIv3 })
   599  	}()
   600  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableAPIv3 = false })
   601  
   602  	_, err := Client.GetUser(th.BasicUser.Id, "")
   603  
   604  	if err.StatusCode != http.StatusNotImplemented {
   605  		t.Fatal("wrong error code")
   606  	}
   607  
   608  	if err.Id != "api.context.v3_disabled.app_error" {
   609  		t.Fatal("wrong error message")
   610  	}
   611  }