github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/api4/group_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"net/http"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  
    16  	"github.com/masterhung0112/hk_server/v5/model"
    17  )
    18  
    19  func TestGetGroup(t *testing.T) {
    20  	th := Setup(t)
    21  	defer th.TearDown()
    22  
    23  	id := model.NewId()
    24  	g, err := th.App.CreateGroup(&model.Group{
    25  		DisplayName: "dn_" + id,
    26  		Name:        model.NewString("name" + id),
    27  		Source:      model.GroupSourceLdap,
    28  		Description: "description_" + id,
    29  		RemoteId:    model.NewId(),
    30  	})
    31  	assert.Nil(t, err)
    32  
    33  	_, response := th.Client.GetGroup(g.Id, "")
    34  	CheckNotImplementedStatus(t, response)
    35  
    36  	_, response = th.SystemAdminClient.GetGroup(g.Id, "")
    37  	CheckNotImplementedStatus(t, response)
    38  
    39  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
    40  
    41  	group, response := th.SystemAdminClient.GetGroup(g.Id, "")
    42  	CheckNoError(t, response)
    43  
    44  	assert.Equal(t, g.DisplayName, group.DisplayName)
    45  	assert.Equal(t, g.Name, group.Name)
    46  	assert.Equal(t, g.Source, group.Source)
    47  	assert.Equal(t, g.Description, group.Description)
    48  	assert.Equal(t, g.RemoteId, group.RemoteId)
    49  	assert.Equal(t, g.CreateAt, group.CreateAt)
    50  	assert.Equal(t, g.UpdateAt, group.UpdateAt)
    51  	assert.Equal(t, g.DeleteAt, group.DeleteAt)
    52  
    53  	_, response = th.SystemAdminClient.GetGroup(model.NewId(), "")
    54  	CheckNotFoundStatus(t, response)
    55  
    56  	_, response = th.SystemAdminClient.GetGroup("12345", "")
    57  	CheckBadRequestStatus(t, response)
    58  
    59  	th.SystemAdminClient.Logout()
    60  	_, response = th.SystemAdminClient.GetGroup(group.Id, "")
    61  	CheckUnauthorizedStatus(t, response)
    62  }
    63  
    64  func TestPatchGroup(t *testing.T) {
    65  	th := Setup(t)
    66  	defer th.TearDown()
    67  
    68  	id := model.NewId()
    69  	g, err := th.App.CreateGroup(&model.Group{
    70  		DisplayName: "dn_" + id,
    71  		Name:        model.NewString("name" + id),
    72  		Source:      model.GroupSourceLdap,
    73  		Description: "description_" + id,
    74  		RemoteId:    model.NewId(),
    75  	})
    76  	assert.Nil(t, err)
    77  
    78  	updateFmt := "%s_updated"
    79  
    80  	newName := fmt.Sprintf(updateFmt, *g.Name)
    81  	newDisplayName := fmt.Sprintf(updateFmt, g.DisplayName)
    82  	newDescription := fmt.Sprintf(updateFmt, g.Description)
    83  
    84  	gp := &model.GroupPatch{
    85  		Name:        &newName,
    86  		DisplayName: &newDisplayName,
    87  		Description: &newDescription,
    88  	}
    89  
    90  	_, response := th.Client.PatchGroup(g.Id, gp)
    91  	CheckNotImplementedStatus(t, response)
    92  
    93  	_, response = th.SystemAdminClient.PatchGroup(g.Id, gp)
    94  	CheckNotImplementedStatus(t, response)
    95  
    96  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
    97  
    98  	group2, response := th.SystemAdminClient.PatchGroup(g.Id, gp)
    99  	CheckOKStatus(t, response)
   100  
   101  	group, response := th.SystemAdminClient.GetGroup(g.Id, "")
   102  	CheckNoError(t, response)
   103  
   104  	assert.Equal(t, *gp.DisplayName, group.DisplayName)
   105  	assert.Equal(t, *gp.DisplayName, group2.DisplayName)
   106  	assert.Equal(t, *gp.Name, *group.Name)
   107  	assert.Equal(t, *gp.Name, *group2.Name)
   108  	assert.Equal(t, *gp.Description, group.Description)
   109  	assert.Equal(t, *gp.Description, group2.Description)
   110  
   111  	assert.Equal(t, group2.UpdateAt, group.UpdateAt)
   112  
   113  	assert.Equal(t, g.Source, group.Source)
   114  	assert.Equal(t, g.Source, group2.Source)
   115  	assert.Equal(t, g.RemoteId, group.RemoteId)
   116  	assert.Equal(t, g.RemoteId, group2.RemoteId)
   117  	assert.Equal(t, g.CreateAt, group.CreateAt)
   118  	assert.Equal(t, g.CreateAt, group2.CreateAt)
   119  	assert.Equal(t, g.DeleteAt, group.DeleteAt)
   120  	assert.Equal(t, g.DeleteAt, group2.DeleteAt)
   121  
   122  	_, response = th.SystemAdminClient.PatchGroup(model.NewId(), gp)
   123  	CheckNotFoundStatus(t, response)
   124  
   125  	th.SystemAdminClient.Logout()
   126  	_, response = th.SystemAdminClient.PatchGroup(group.Id, gp)
   127  	CheckUnauthorizedStatus(t, response)
   128  }
   129  
   130  func TestLinkGroupTeam(t *testing.T) {
   131  	th := Setup(t).InitBasic()
   132  	defer th.TearDown()
   133  
   134  	id := model.NewId()
   135  	g, err := th.App.CreateGroup(&model.Group{
   136  		DisplayName: "dn_" + id,
   137  		Name:        model.NewString("name" + id),
   138  		Source:      model.GroupSourceLdap,
   139  		Description: "description_" + id,
   140  		RemoteId:    model.NewId(),
   141  	})
   142  	assert.Nil(t, err)
   143  
   144  	patch := &model.GroupSyncablePatch{
   145  		AutoAdd: model.NewBool(true),
   146  	}
   147  
   148  	_, response := th.Client.LinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   149  	CheckNotImplementedStatus(t, response)
   150  
   151  	_, response = th.SystemAdminClient.LinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   152  	CheckNotImplementedStatus(t, response)
   153  
   154  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   155  
   156  	_, response = th.Client.LinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   157  	assert.NotNil(t, response.Error)
   158  
   159  	th.UpdateUserToTeamAdmin(th.BasicUser, th.BasicTeam)
   160  	th.Client.Logout()
   161  	th.Client.Login(th.BasicUser.Email, th.BasicUser.Password)
   162  
   163  	groupTeam, response := th.Client.LinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   164  	assert.Equal(t, http.StatusCreated, response.StatusCode)
   165  	assert.NotNil(t, groupTeam)
   166  }
   167  
   168  func TestLinkGroupChannel(t *testing.T) {
   169  	th := Setup(t).InitBasic()
   170  	defer th.TearDown()
   171  
   172  	id := model.NewId()
   173  	g, err := th.App.CreateGroup(&model.Group{
   174  		DisplayName: "dn_" + id,
   175  		Name:        model.NewString("name" + id),
   176  		Source:      model.GroupSourceLdap,
   177  		Description: "description_" + id,
   178  		RemoteId:    model.NewId(),
   179  	})
   180  	assert.Nil(t, err)
   181  
   182  	patch := &model.GroupSyncablePatch{
   183  		AutoAdd: model.NewBool(true),
   184  	}
   185  
   186  	_, response := th.Client.LinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   187  	CheckNotImplementedStatus(t, response)
   188  
   189  	_, response = th.SystemAdminClient.LinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   190  	CheckNotImplementedStatus(t, response)
   191  
   192  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   193  
   194  	groupTeam, response := th.Client.LinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   195  	assert.Equal(t, http.StatusCreated, response.StatusCode)
   196  	assert.Equal(t, th.BasicChannel.TeamId, groupTeam.TeamID)
   197  	assert.NotNil(t, groupTeam)
   198  
   199  	_, response = th.SystemAdminClient.UpdateChannelRoles(th.BasicChannel.Id, th.BasicUser.Id, "")
   200  	require.Nil(t, response.Error)
   201  	th.Client.Logout()
   202  	th.Client.Login(th.BasicUser.Email, th.BasicUser.Password)
   203  
   204  	_, response = th.Client.LinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   205  	assert.NotNil(t, response.Error)
   206  }
   207  
   208  func TestUnlinkGroupTeam(t *testing.T) {
   209  	th := Setup(t).InitBasic()
   210  	defer th.TearDown()
   211  
   212  	id := model.NewId()
   213  	g, err := th.App.CreateGroup(&model.Group{
   214  		DisplayName: "dn_" + id,
   215  		Name:        model.NewString("name" + id),
   216  		Source:      model.GroupSourceLdap,
   217  		Description: "description_" + id,
   218  		RemoteId:    model.NewId(),
   219  	})
   220  	assert.Nil(t, err)
   221  
   222  	patch := &model.GroupSyncablePatch{
   223  		AutoAdd: model.NewBool(true),
   224  	}
   225  
   226  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   227  
   228  	_, response := th.SystemAdminClient.LinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   229  	assert.Equal(t, http.StatusCreated, response.StatusCode)
   230  
   231  	th.App.Srv().SetLicense(nil)
   232  
   233  	response = th.Client.UnlinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam)
   234  	CheckNotImplementedStatus(t, response)
   235  
   236  	response = th.SystemAdminClient.UnlinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam)
   237  	CheckNotImplementedStatus(t, response)
   238  
   239  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   240  
   241  	response = th.Client.UnlinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam)
   242  	assert.NotNil(t, response.Error)
   243  	time.Sleep(2 * time.Second) // A hack to let "go c.App.SyncRolesAndMembership" finish before moving on.
   244  	th.UpdateUserToTeamAdmin(th.BasicUser, th.BasicTeam)
   245  	ok, response := th.Client.Logout()
   246  	assert.True(t, ok)
   247  	CheckOKStatus(t, response)
   248  	_, response = th.Client.Login(th.BasicUser.Email, th.BasicUser.Password)
   249  	CheckOKStatus(t, response)
   250  
   251  	response = th.Client.UnlinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam)
   252  	CheckOKStatus(t, response)
   253  }
   254  
   255  func TestUnlinkGroupChannel(t *testing.T) {
   256  	th := Setup(t).InitBasic()
   257  	defer th.TearDown()
   258  
   259  	id := model.NewId()
   260  	g, err := th.App.CreateGroup(&model.Group{
   261  		DisplayName: "dn_" + id,
   262  		Name:        model.NewString("name" + id),
   263  		Source:      model.GroupSourceLdap,
   264  		Description: "description_" + id,
   265  		RemoteId:    model.NewId(),
   266  	})
   267  	assert.Nil(t, err)
   268  
   269  	patch := &model.GroupSyncablePatch{
   270  		AutoAdd: model.NewBool(true),
   271  	}
   272  
   273  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   274  
   275  	_, response := th.SystemAdminClient.LinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   276  	assert.Equal(t, http.StatusCreated, response.StatusCode)
   277  
   278  	th.App.Srv().SetLicense(nil)
   279  
   280  	response = th.Client.UnlinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel)
   281  	CheckNotImplementedStatus(t, response)
   282  
   283  	response = th.SystemAdminClient.UnlinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel)
   284  	CheckNotImplementedStatus(t, response)
   285  
   286  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   287  
   288  	_, response = th.SystemAdminClient.UpdateChannelRoles(th.BasicChannel.Id, th.BasicUser.Id, "")
   289  	require.Nil(t, response.Error)
   290  	th.Client.Logout()
   291  	th.Client.Login(th.BasicUser.Email, th.BasicUser.Password)
   292  
   293  	response = th.Client.UnlinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel)
   294  	assert.NotNil(t, response.Error)
   295  
   296  	_, response = th.SystemAdminClient.UpdateChannelRoles(th.BasicChannel.Id, th.BasicUser.Id, "channel_admin channel_user")
   297  	require.Nil(t, response.Error)
   298  	th.Client.Logout()
   299  	th.Client.Login(th.BasicUser.Email, th.BasicUser.Password)
   300  
   301  	response = th.Client.UnlinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel)
   302  	assert.Nil(t, response.Error)
   303  }
   304  
   305  func TestGetGroupTeam(t *testing.T) {
   306  	th := Setup(t).InitBasic()
   307  	defer th.TearDown()
   308  
   309  	id := model.NewId()
   310  	g, err := th.App.CreateGroup(&model.Group{
   311  		DisplayName: "dn_" + id,
   312  		Name:        model.NewString("name" + id),
   313  		Source:      model.GroupSourceLdap,
   314  		Description: "description_" + id,
   315  		RemoteId:    model.NewId(),
   316  	})
   317  	assert.Nil(t, err)
   318  
   319  	_, response := th.Client.GetGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, "")
   320  	CheckNotImplementedStatus(t, response)
   321  
   322  	_, response = th.SystemAdminClient.GetGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, "")
   323  	CheckNotImplementedStatus(t, response)
   324  
   325  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   326  
   327  	patch := &model.GroupSyncablePatch{
   328  		AutoAdd: model.NewBool(true),
   329  	}
   330  
   331  	_, response = th.SystemAdminClient.LinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   332  	assert.Equal(t, http.StatusCreated, response.StatusCode)
   333  
   334  	groupSyncable, response := th.SystemAdminClient.GetGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, "")
   335  	CheckOKStatus(t, response)
   336  	assert.NotNil(t, groupSyncable)
   337  
   338  	assert.Equal(t, g.Id, groupSyncable.GroupId)
   339  	assert.Equal(t, th.BasicTeam.Id, groupSyncable.SyncableId)
   340  	assert.Equal(t, *patch.AutoAdd, groupSyncable.AutoAdd)
   341  
   342  	_, response = th.SystemAdminClient.GetGroupSyncable(model.NewId(), th.BasicTeam.Id, model.GroupSyncableTypeTeam, "")
   343  	CheckNotFoundStatus(t, response)
   344  
   345  	_, response = th.SystemAdminClient.GetGroupSyncable(g.Id, model.NewId(), model.GroupSyncableTypeTeam, "")
   346  	CheckNotFoundStatus(t, response)
   347  
   348  	_, response = th.SystemAdminClient.GetGroupSyncable("asdfasdfe3", th.BasicTeam.Id, model.GroupSyncableTypeTeam, "")
   349  	CheckBadRequestStatus(t, response)
   350  
   351  	_, response = th.SystemAdminClient.GetGroupSyncable(g.Id, "asdfasdfe3", model.GroupSyncableTypeTeam, "")
   352  	CheckBadRequestStatus(t, response)
   353  
   354  	th.SystemAdminClient.Logout()
   355  	_, response = th.SystemAdminClient.GetGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, "")
   356  	CheckUnauthorizedStatus(t, response)
   357  }
   358  
   359  func TestGetGroupChannel(t *testing.T) {
   360  	th := Setup(t).InitBasic()
   361  	defer th.TearDown()
   362  
   363  	id := model.NewId()
   364  	g, err := th.App.CreateGroup(&model.Group{
   365  		DisplayName: "dn_" + id,
   366  		Name:        model.NewString("name" + id),
   367  		Source:      model.GroupSourceLdap,
   368  		Description: "description_" + id,
   369  		RemoteId:    model.NewId(),
   370  	})
   371  	assert.Nil(t, err)
   372  
   373  	_, response := th.Client.GetGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, "")
   374  	CheckNotImplementedStatus(t, response)
   375  
   376  	_, response = th.SystemAdminClient.GetGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, "")
   377  	CheckNotImplementedStatus(t, response)
   378  
   379  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   380  
   381  	patch := &model.GroupSyncablePatch{
   382  		AutoAdd: model.NewBool(true),
   383  	}
   384  
   385  	_, response = th.SystemAdminClient.LinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   386  	assert.Equal(t, http.StatusCreated, response.StatusCode)
   387  
   388  	groupSyncable, response := th.SystemAdminClient.GetGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, "")
   389  	CheckOKStatus(t, response)
   390  	assert.NotNil(t, groupSyncable)
   391  
   392  	assert.Equal(t, g.Id, groupSyncable.GroupId)
   393  	assert.Equal(t, th.BasicChannel.Id, groupSyncable.SyncableId)
   394  	assert.Equal(t, *patch.AutoAdd, groupSyncable.AutoAdd)
   395  
   396  	_, response = th.SystemAdminClient.GetGroupSyncable(model.NewId(), th.BasicChannel.Id, model.GroupSyncableTypeChannel, "")
   397  	CheckNotFoundStatus(t, response)
   398  
   399  	_, response = th.SystemAdminClient.GetGroupSyncable(g.Id, model.NewId(), model.GroupSyncableTypeChannel, "")
   400  	CheckNotFoundStatus(t, response)
   401  
   402  	_, response = th.SystemAdminClient.GetGroupSyncable("asdfasdfe3", th.BasicChannel.Id, model.GroupSyncableTypeChannel, "")
   403  	CheckBadRequestStatus(t, response)
   404  
   405  	_, response = th.SystemAdminClient.GetGroupSyncable(g.Id, "asdfasdfe3", model.GroupSyncableTypeChannel, "")
   406  	CheckBadRequestStatus(t, response)
   407  
   408  	th.SystemAdminClient.Logout()
   409  	_, response = th.SystemAdminClient.GetGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, "")
   410  	CheckUnauthorizedStatus(t, response)
   411  }
   412  
   413  func TestGetGroupTeams(t *testing.T) {
   414  	th := Setup(t)
   415  	defer th.TearDown()
   416  
   417  	id := model.NewId()
   418  	g, err := th.App.CreateGroup(&model.Group{
   419  		DisplayName: "dn_" + id,
   420  		Name:        model.NewString("name" + id),
   421  		Source:      model.GroupSourceLdap,
   422  		Description: "description_" + id,
   423  		RemoteId:    model.NewId(),
   424  	})
   425  	assert.Nil(t, err)
   426  
   427  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   428  
   429  	patch := &model.GroupSyncablePatch{
   430  		AutoAdd: model.NewBool(true),
   431  	}
   432  
   433  	for i := 0; i < 10; i++ {
   434  		team := th.CreateTeam()
   435  		_, response := th.SystemAdminClient.LinkGroupSyncable(g.Id, team.Id, model.GroupSyncableTypeTeam, patch)
   436  		assert.Equal(t, http.StatusCreated, response.StatusCode)
   437  	}
   438  
   439  	th.App.Srv().SetLicense(nil)
   440  
   441  	_, response := th.Client.GetGroupSyncables(g.Id, model.GroupSyncableTypeTeam, "")
   442  	CheckNotImplementedStatus(t, response)
   443  
   444  	_, response = th.SystemAdminClient.GetGroupSyncables(g.Id, model.GroupSyncableTypeTeam, "")
   445  	CheckNotImplementedStatus(t, response)
   446  
   447  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   448  
   449  	_, response = th.Client.GetGroupSyncables(g.Id, model.GroupSyncableTypeTeam, "")
   450  	assert.Equal(t, http.StatusForbidden, response.StatusCode)
   451  
   452  	groupSyncables, response := th.SystemAdminClient.GetGroupSyncables(g.Id, model.GroupSyncableTypeTeam, "")
   453  	CheckOKStatus(t, response)
   454  
   455  	assert.Len(t, groupSyncables, 10)
   456  
   457  	th.SystemAdminClient.Logout()
   458  	_, response = th.SystemAdminClient.GetGroupSyncables(g.Id, model.GroupSyncableTypeTeam, "")
   459  	CheckUnauthorizedStatus(t, response)
   460  }
   461  
   462  func TestGetGroupChannels(t *testing.T) {
   463  	th := Setup(t).InitBasic()
   464  	defer th.TearDown()
   465  
   466  	id := model.NewId()
   467  	g, err := th.App.CreateGroup(&model.Group{
   468  		DisplayName: "dn_" + id,
   469  		Name:        model.NewString("name" + id),
   470  		Source:      model.GroupSourceLdap,
   471  		Description: "description_" + id,
   472  		RemoteId:    model.NewId(),
   473  	})
   474  	assert.Nil(t, err)
   475  
   476  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   477  
   478  	patch := &model.GroupSyncablePatch{
   479  		AutoAdd: model.NewBool(true),
   480  	}
   481  
   482  	for i := 0; i < 10; i++ {
   483  		channel := th.CreatePublicChannel()
   484  		_, response := th.SystemAdminClient.LinkGroupSyncable(g.Id, channel.Id, model.GroupSyncableTypeChannel, patch)
   485  		assert.Equal(t, http.StatusCreated, response.StatusCode)
   486  	}
   487  
   488  	th.App.Srv().SetLicense(nil)
   489  
   490  	_, response := th.Client.GetGroupSyncables(g.Id, model.GroupSyncableTypeChannel, "")
   491  	CheckNotImplementedStatus(t, response)
   492  
   493  	_, response = th.SystemAdminClient.GetGroupSyncables(g.Id, model.GroupSyncableTypeChannel, "")
   494  	CheckNotImplementedStatus(t, response)
   495  
   496  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   497  
   498  	_, response = th.Client.GetGroupSyncables(g.Id, model.GroupSyncableTypeChannel, "")
   499  	assert.Equal(t, http.StatusForbidden, response.StatusCode)
   500  
   501  	groupSyncables, response := th.SystemAdminClient.GetGroupSyncables(g.Id, model.GroupSyncableTypeChannel, "")
   502  	CheckOKStatus(t, response)
   503  
   504  	assert.Len(t, groupSyncables, 10)
   505  
   506  	th.SystemAdminClient.Logout()
   507  	_, response = th.SystemAdminClient.GetGroupSyncables(g.Id, model.GroupSyncableTypeChannel, "")
   508  	CheckUnauthorizedStatus(t, response)
   509  }
   510  
   511  func TestPatchGroupTeam(t *testing.T) {
   512  	th := Setup(t).InitBasic()
   513  	defer th.TearDown()
   514  
   515  	id := model.NewId()
   516  	g, err := th.App.CreateGroup(&model.Group{
   517  		DisplayName: "dn_" + id,
   518  		Name:        model.NewString("name" + id),
   519  		Source:      model.GroupSourceLdap,
   520  		Description: "description_" + id,
   521  		RemoteId:    model.NewId(),
   522  	})
   523  	assert.Nil(t, err)
   524  
   525  	patch := &model.GroupSyncablePatch{
   526  		AutoAdd: model.NewBool(true),
   527  	}
   528  
   529  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   530  
   531  	groupSyncable, response := th.SystemAdminClient.LinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   532  	assert.Equal(t, http.StatusCreated, response.StatusCode)
   533  	assert.NotNil(t, groupSyncable)
   534  	assert.True(t, groupSyncable.AutoAdd)
   535  
   536  	_, response = th.Client.PatchGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   537  	assert.Equal(t, http.StatusForbidden, response.StatusCode)
   538  
   539  	th.App.Srv().SetLicense(nil)
   540  
   541  	_, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   542  	CheckNotImplementedStatus(t, response)
   543  
   544  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   545  
   546  	patch.AutoAdd = model.NewBool(false)
   547  	groupSyncable, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   548  	CheckOKStatus(t, response)
   549  	assert.False(t, groupSyncable.AutoAdd)
   550  
   551  	assert.Equal(t, g.Id, groupSyncable.GroupId)
   552  	assert.Equal(t, th.BasicTeam.Id, groupSyncable.SyncableId)
   553  	assert.Equal(t, model.GroupSyncableTypeTeam, groupSyncable.Type)
   554  
   555  	patch.AutoAdd = model.NewBool(true)
   556  	groupSyncable, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   557  	CheckOKStatus(t, response)
   558  
   559  	_, response = th.SystemAdminClient.PatchGroupSyncable(model.NewId(), th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   560  	CheckNotFoundStatus(t, response)
   561  
   562  	_, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, model.NewId(), model.GroupSyncableTypeTeam, patch)
   563  	CheckNotFoundStatus(t, response)
   564  
   565  	_, response = th.SystemAdminClient.PatchGroupSyncable("abc", th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   566  	CheckBadRequestStatus(t, response)
   567  
   568  	_, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, "abc", model.GroupSyncableTypeTeam, patch)
   569  	CheckBadRequestStatus(t, response)
   570  
   571  	th.SystemAdminClient.Logout()
   572  	_, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   573  	CheckUnauthorizedStatus(t, response)
   574  }
   575  
   576  func TestPatchGroupChannel(t *testing.T) {
   577  	th := Setup(t).InitBasic()
   578  	defer th.TearDown()
   579  
   580  	id := model.NewId()
   581  	g, err := th.App.CreateGroup(&model.Group{
   582  		DisplayName: "dn_" + id,
   583  		Name:        model.NewString("name" + id),
   584  		Source:      model.GroupSourceLdap,
   585  		Description: "description_" + id,
   586  		RemoteId:    model.NewId(),
   587  	})
   588  	assert.Nil(t, err)
   589  
   590  	patch := &model.GroupSyncablePatch{
   591  		AutoAdd: model.NewBool(true),
   592  	}
   593  
   594  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   595  
   596  	groupSyncable, response := th.SystemAdminClient.LinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   597  	assert.Equal(t, http.StatusCreated, response.StatusCode)
   598  	assert.NotNil(t, groupSyncable)
   599  	assert.True(t, groupSyncable.AutoAdd)
   600  
   601  	role, err := th.App.GetRoleByName(context.Background(), "channel_user")
   602  	require.Nil(t, err)
   603  	originalPermissions := role.Permissions
   604  	_, err = th.App.PatchRole(role, &model.RolePatch{Permissions: &[]string{}})
   605  	require.Nil(t, err)
   606  
   607  	_, response = th.Client.PatchGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   608  	assert.Equal(t, http.StatusForbidden, response.StatusCode)
   609  
   610  	_, err = th.App.PatchRole(role, &model.RolePatch{Permissions: &originalPermissions})
   611  	require.Nil(t, err)
   612  
   613  	th.App.Srv().SetLicense(nil)
   614  
   615  	_, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   616  	CheckNotImplementedStatus(t, response)
   617  
   618  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   619  
   620  	patch.AutoAdd = model.NewBool(false)
   621  	groupSyncable, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   622  	CheckOKStatus(t, response)
   623  	assert.False(t, groupSyncable.AutoAdd)
   624  
   625  	assert.Equal(t, g.Id, groupSyncable.GroupId)
   626  	assert.Equal(t, th.BasicChannel.Id, groupSyncable.SyncableId)
   627  	assert.Equal(t, th.BasicChannel.TeamId, groupSyncable.TeamID)
   628  	assert.Equal(t, model.GroupSyncableTypeChannel, groupSyncable.Type)
   629  
   630  	patch.AutoAdd = model.NewBool(true)
   631  	groupSyncable, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   632  	CheckOKStatus(t, response)
   633  
   634  	_, response = th.SystemAdminClient.PatchGroupSyncable(model.NewId(), th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   635  	CheckNotFoundStatus(t, response)
   636  
   637  	_, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, model.NewId(), model.GroupSyncableTypeChannel, patch)
   638  	CheckNotFoundStatus(t, response)
   639  
   640  	_, response = th.SystemAdminClient.PatchGroupSyncable("abc", th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   641  	CheckBadRequestStatus(t, response)
   642  
   643  	_, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, "abc", model.GroupSyncableTypeChannel, patch)
   644  	CheckBadRequestStatus(t, response)
   645  
   646  	th.SystemAdminClient.Logout()
   647  	_, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   648  	CheckUnauthorizedStatus(t, response)
   649  }
   650  
   651  func TestGetGroupsByChannel(t *testing.T) {
   652  	th := Setup(t).InitBasic()
   653  	defer th.TearDown()
   654  
   655  	id := model.NewId()
   656  	group, err := th.App.CreateGroup(&model.Group{
   657  		DisplayName: "dn_" + id,
   658  		Name:        model.NewString("name" + id),
   659  		Source:      model.GroupSourceLdap,
   660  		Description: "description_" + id,
   661  		RemoteId:    model.NewId(),
   662  	})
   663  	assert.Nil(t, err)
   664  
   665  	groupSyncable, err := th.App.UpsertGroupSyncable(&model.GroupSyncable{
   666  		AutoAdd:    true,
   667  		SyncableId: th.BasicChannel.Id,
   668  		Type:       model.GroupSyncableTypeChannel,
   669  		GroupId:    group.Id,
   670  	})
   671  	assert.Nil(t, err)
   672  
   673  	opts := model.GroupSearchOpts{
   674  		PageOpts: &model.PageOpts{
   675  			Page:    0,
   676  			PerPage: 60,
   677  		},
   678  	}
   679  
   680  	th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
   681  		_, _, response := client.GetGroupsByChannel("asdfasdf", opts)
   682  		CheckBadRequestStatus(t, response)
   683  	})
   684  
   685  	th.App.Srv().SetLicense(nil)
   686  
   687  	th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
   688  		_, _, response := client.GetGroupsByChannel(th.BasicChannel.Id, opts)
   689  		CheckNotImplementedStatus(t, response)
   690  	})
   691  
   692  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   693  
   694  	privateChannel := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE)
   695  
   696  	_, _, response := th.Client.GetGroupsByChannel(privateChannel.Id, opts)
   697  	CheckForbiddenStatus(t, response)
   698  
   699  	th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
   700  		groups, _, response := client.GetGroupsByChannel(th.BasicChannel.Id, opts)
   701  		assert.Nil(t, response.Error)
   702  		assert.ElementsMatch(t, []*model.GroupWithSchemeAdmin{{Group: *group, SchemeAdmin: model.NewBool(false)}}, groups)
   703  		require.NotNil(t, groups[0].SchemeAdmin)
   704  		require.False(t, *groups[0].SchemeAdmin)
   705  	})
   706  
   707  	// set syncable to true
   708  	groupSyncable.SchemeAdmin = true
   709  	_, err = th.App.UpdateGroupSyncable(groupSyncable)
   710  	require.Nil(t, err)
   711  
   712  	th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
   713  		groups, _, response := client.GetGroupsByChannel(th.BasicChannel.Id, opts)
   714  		assert.Nil(t, response.Error)
   715  		// ensure that SchemeAdmin field is updated
   716  		assert.ElementsMatch(t, []*model.GroupWithSchemeAdmin{{Group: *group, SchemeAdmin: model.NewBool(true)}}, groups)
   717  		require.NotNil(t, groups[0].SchemeAdmin)
   718  		require.True(t, *groups[0].SchemeAdmin)
   719  
   720  		groups, _, response = client.GetGroupsByChannel(model.NewId(), opts)
   721  		assert.Equal(t, "app.channel.get.existing.app_error", response.Error.Id)
   722  		assert.Empty(t, groups)
   723  	})
   724  }
   725  
   726  func TestGetGroupsAssociatedToChannelsByTeam(t *testing.T) {
   727  	th := Setup(t).InitBasic()
   728  	defer th.TearDown()
   729  
   730  	id := model.NewId()
   731  	group, err := th.App.CreateGroup(&model.Group{
   732  		DisplayName: "dn_" + id,
   733  		Name:        model.NewString("name" + id),
   734  		Source:      model.GroupSourceLdap,
   735  		Description: "description_" + id,
   736  		RemoteId:    model.NewId(),
   737  	})
   738  	assert.Nil(t, err)
   739  
   740  	groupSyncable, err := th.App.UpsertGroupSyncable(&model.GroupSyncable{
   741  		AutoAdd:    true,
   742  		SyncableId: th.BasicChannel.Id,
   743  		Type:       model.GroupSyncableTypeChannel,
   744  		GroupId:    group.Id,
   745  	})
   746  	assert.Nil(t, err)
   747  
   748  	opts := model.GroupSearchOpts{
   749  		PageOpts: &model.PageOpts{
   750  			Page:    0,
   751  			PerPage: 60,
   752  		},
   753  	}
   754  
   755  	_, response := th.SystemAdminClient.GetGroupsAssociatedToChannelsByTeam("asdfasdf", opts)
   756  	CheckBadRequestStatus(t, response)
   757  
   758  	th.App.Srv().SetLicense(nil)
   759  
   760  	_, response = th.SystemAdminClient.GetGroupsAssociatedToChannelsByTeam(th.BasicTeam.Id, opts)
   761  	CheckNotImplementedStatus(t, response)
   762  
   763  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   764  
   765  	groups, response := th.SystemAdminClient.GetGroupsAssociatedToChannelsByTeam(th.BasicTeam.Id, opts)
   766  	assert.Nil(t, response.Error)
   767  
   768  	assert.Equal(t, map[string][]*model.GroupWithSchemeAdmin{
   769  		th.BasicChannel.Id: {
   770  			{Group: *group, SchemeAdmin: model.NewBool(false)},
   771  		},
   772  	}, groups)
   773  
   774  	require.NotNil(t, groups[th.BasicChannel.Id][0].SchemeAdmin)
   775  	require.False(t, *groups[th.BasicChannel.Id][0].SchemeAdmin)
   776  
   777  	// set syncable to true
   778  	groupSyncable.SchemeAdmin = true
   779  	_, err = th.App.UpdateGroupSyncable(groupSyncable)
   780  	require.Nil(t, err)
   781  
   782  	// ensure that SchemeAdmin field is updated
   783  	groups, response = th.SystemAdminClient.GetGroupsAssociatedToChannelsByTeam(th.BasicTeam.Id, opts)
   784  	assert.Nil(t, response.Error)
   785  
   786  	assert.Equal(t, map[string][]*model.GroupWithSchemeAdmin{
   787  		th.BasicChannel.Id: {
   788  			{Group: *group, SchemeAdmin: model.NewBool(true)},
   789  		},
   790  	}, groups)
   791  
   792  	require.NotNil(t, groups[th.BasicChannel.Id][0].SchemeAdmin)
   793  	require.True(t, *groups[th.BasicChannel.Id][0].SchemeAdmin)
   794  
   795  	groups, response = th.SystemAdminClient.GetGroupsAssociatedToChannelsByTeam(model.NewId(), opts)
   796  	assert.Nil(t, response.Error)
   797  	assert.Empty(t, groups)
   798  }
   799  
   800  func TestGetGroupsByTeam(t *testing.T) {
   801  	th := Setup(t).InitBasic()
   802  	defer th.TearDown()
   803  
   804  	id := model.NewId()
   805  	group, err := th.App.CreateGroup(&model.Group{
   806  		DisplayName: "dn_" + id,
   807  		Name:        model.NewString("name" + id),
   808  		Source:      model.GroupSourceLdap,
   809  		Description: "description_" + id,
   810  		RemoteId:    model.NewId(),
   811  	})
   812  	assert.Nil(t, err)
   813  
   814  	groupSyncable, err := th.App.UpsertGroupSyncable(&model.GroupSyncable{
   815  		AutoAdd:    true,
   816  		SyncableId: th.BasicTeam.Id,
   817  		Type:       model.GroupSyncableTypeTeam,
   818  		GroupId:    group.Id,
   819  	})
   820  	assert.Nil(t, err)
   821  
   822  	opts := model.GroupSearchOpts{
   823  		PageOpts: &model.PageOpts{
   824  			Page:    0,
   825  			PerPage: 60,
   826  		},
   827  	}
   828  
   829  	th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
   830  		_, _, response := client.GetGroupsByTeam("asdfasdf", opts)
   831  		CheckBadRequestStatus(t, response)
   832  	})
   833  
   834  	th.App.Srv().SetLicense(nil)
   835  
   836  	th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
   837  		_, _, response := client.GetGroupsByTeam(th.BasicTeam.Id, opts)
   838  		CheckNotImplementedStatus(t, response)
   839  	})
   840  
   841  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   842  
   843  	th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
   844  		groups, _, response := client.GetGroupsByTeam(th.BasicTeam.Id, opts)
   845  		assert.Nil(t, response.Error)
   846  		assert.ElementsMatch(t, []*model.GroupWithSchemeAdmin{{Group: *group, SchemeAdmin: model.NewBool(false)}}, groups)
   847  		require.NotNil(t, groups[0].SchemeAdmin)
   848  		require.False(t, *groups[0].SchemeAdmin)
   849  	})
   850  
   851  	// set syncable to true
   852  	groupSyncable.SchemeAdmin = true
   853  	_, err = th.App.UpdateGroupSyncable(groupSyncable)
   854  	require.Nil(t, err)
   855  
   856  	th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
   857  		groups, _, response := client.GetGroupsByTeam(th.BasicTeam.Id, opts)
   858  		assert.Nil(t, response.Error)
   859  		// ensure that SchemeAdmin field is updated
   860  		assert.ElementsMatch(t, []*model.GroupWithSchemeAdmin{{Group: *group, SchemeAdmin: model.NewBool(true)}}, groups)
   861  		require.NotNil(t, groups[0].SchemeAdmin)
   862  		require.True(t, *groups[0].SchemeAdmin)
   863  
   864  		groups, _, response = client.GetGroupsByTeam(model.NewId(), opts)
   865  		assert.Nil(t, response.Error)
   866  		assert.Empty(t, groups)
   867  	})
   868  }
   869  
   870  func TestGetGroups(t *testing.T) {
   871  	th := Setup(t).InitBasic()
   872  	defer th.TearDown()
   873  
   874  	// make sure "createdDate" for next group is after one created in InitBasic()
   875  	time.Sleep(2 * time.Millisecond)
   876  	id := model.NewId()
   877  	group, err := th.App.CreateGroup(&model.Group{
   878  		DisplayName: "dn-foo_" + id,
   879  		Name:        model.NewString("name" + id),
   880  		Source:      model.GroupSourceLdap,
   881  		Description: "description_" + id,
   882  		RemoteId:    model.NewId(),
   883  	})
   884  	assert.Nil(t, err)
   885  	start := group.UpdateAt - 1
   886  
   887  	opts := model.GroupSearchOpts{
   888  		PageOpts: &model.PageOpts{
   889  			Page:    0,
   890  			PerPage: 60,
   891  		},
   892  	}
   893  
   894  	th.App.Srv().SetLicense(nil)
   895  
   896  	_, response := th.SystemAdminClient.GetGroups(opts)
   897  	CheckNotImplementedStatus(t, response)
   898  
   899  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
   900  
   901  	_, response = th.SystemAdminClient.GetGroups(opts)
   902  	require.Nil(t, response.Error)
   903  
   904  	_, response = th.SystemAdminClient.UpdateChannelRoles(th.BasicChannel.Id, th.BasicUser.Id, "")
   905  	require.Nil(t, response.Error)
   906  
   907  	opts.NotAssociatedToChannel = th.BasicChannel.Id
   908  
   909  	_, response = th.SystemAdminClient.UpdateChannelRoles(th.BasicChannel.Id, th.BasicUser.Id, "channel_user channel_admin")
   910  	require.Nil(t, response.Error)
   911  
   912  	groups, response := th.SystemAdminClient.GetGroups(opts)
   913  	assert.Nil(t, response.Error)
   914  	assert.ElementsMatch(t, []*model.Group{group, th.Group}, groups)
   915  	assert.Nil(t, groups[0].MemberCount)
   916  
   917  	opts.IncludeMemberCount = true
   918  	groups, _ = th.SystemAdminClient.GetGroups(opts)
   919  	assert.NotNil(t, groups[0].MemberCount)
   920  	opts.IncludeMemberCount = false
   921  
   922  	opts.Q = "-fOo"
   923  	groups, _ = th.SystemAdminClient.GetGroups(opts)
   924  	assert.Len(t, groups, 1)
   925  	opts.Q = ""
   926  
   927  	_, response = th.SystemAdminClient.UpdateTeamMemberRoles(th.BasicTeam.Id, th.BasicUser.Id, "")
   928  	require.Nil(t, response.Error)
   929  
   930  	opts.NotAssociatedToTeam = th.BasicTeam.Id
   931  
   932  	_, response = th.SystemAdminClient.UpdateTeamMemberRoles(th.BasicTeam.Id, th.BasicUser.Id, "team_user team_admin")
   933  	require.Nil(t, response.Error)
   934  
   935  	_, response = th.Client.GetGroups(opts)
   936  	assert.Nil(t, response.Error)
   937  
   938  	// test "since", should only return group created in this test, not th.Group
   939  	opts.Since = start
   940  	groups, response = th.Client.GetGroups(opts)
   941  	assert.Nil(t, response.Error)
   942  	assert.Len(t, groups, 1)
   943  	// test correct group returned
   944  	assert.Equal(t, groups[0].Id, group.Id)
   945  
   946  	// delete group, should still return
   947  	th.App.DeleteGroup(group.Id)
   948  	groups, response = th.Client.GetGroups(opts)
   949  	assert.Nil(t, response.Error)
   950  	assert.Len(t, groups, 1)
   951  	assert.Equal(t, groups[0].Id, group.Id)
   952  
   953  	// test with current since value, return none
   954  	opts.Since = model.GetMillis()
   955  	groups, response = th.Client.GetGroups(opts)
   956  	assert.Nil(t, response.Error)
   957  	assert.Empty(t, groups)
   958  
   959  	// make sure delete group is not returned without Since
   960  	opts.Since = 0
   961  	groups, response = th.Client.GetGroups(opts)
   962  	assert.Nil(t, response.Error)
   963  	//'Normal getGroups should not return delete groups
   964  	assert.Len(t, groups, 1)
   965  	// make sure it returned th.Group,not group
   966  	assert.Equal(t, groups[0].Id, th.Group.Id)
   967  }
   968  
   969  func TestGetGroupsByUserId(t *testing.T) {
   970  	th := Setup(t).InitBasic()
   971  	defer th.TearDown()
   972  
   973  	id := model.NewId()
   974  	group1, err := th.App.CreateGroup(&model.Group{
   975  		DisplayName: "dn-foo_" + id,
   976  		Name:        model.NewString("name" + id),
   977  		Source:      model.GroupSourceLdap,
   978  		Description: "description_" + id,
   979  		RemoteId:    model.NewId(),
   980  	})
   981  	assert.Nil(t, err)
   982  
   983  	user1, err := th.App.CreateUser(th.Context, &model.User{Email: th.GenerateTestEmail(), Nickname: "test user1", Password: "test-password-1", Username: "test-user-1", Roles: model.SYSTEM_USER_ROLE_ID})
   984  	assert.Nil(t, err)
   985  	user1.Password = "test-password-1"
   986  	_, err = th.App.UpsertGroupMember(group1.Id, user1.Id)
   987  	assert.Nil(t, err)
   988  
   989  	id = model.NewId()
   990  	group2, err := th.App.CreateGroup(&model.Group{
   991  		DisplayName: "dn-foo_" + id,
   992  		Name:        model.NewString("name" + id),
   993  		Source:      model.GroupSourceLdap,
   994  		Description: "description_" + id,
   995  		RemoteId:    model.NewId(),
   996  	})
   997  	assert.Nil(t, err)
   998  
   999  	_, err = th.App.UpsertGroupMember(group2.Id, user1.Id)
  1000  	assert.Nil(t, err)
  1001  
  1002  	th.App.Srv().SetLicense(nil)
  1003  	_, response := th.SystemAdminClient.GetGroupsByUserId(user1.Id)
  1004  	CheckNotImplementedStatus(t, response)
  1005  
  1006  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
  1007  	_, response = th.SystemAdminClient.GetGroupsByUserId("")
  1008  	CheckBadRequestStatus(t, response)
  1009  
  1010  	_, response = th.SystemAdminClient.GetGroupsByUserId("notvaliduserid")
  1011  	CheckBadRequestStatus(t, response)
  1012  
  1013  	groups, response := th.SystemAdminClient.GetGroupsByUserId(user1.Id)
  1014  	require.Nil(t, response.Error)
  1015  	assert.ElementsMatch(t, []*model.Group{group1, group2}, groups)
  1016  
  1017  	// test permissions
  1018  	th.Client.Logout()
  1019  	th.Client.Login(th.BasicUser.Email, th.BasicUser.Password)
  1020  	_, response = th.Client.GetGroupsByUserId(user1.Id)
  1021  	CheckForbiddenStatus(t, response)
  1022  
  1023  	th.Client.Logout()
  1024  	th.Client.Login(user1.Email, user1.Password)
  1025  	groups, response = th.Client.GetGroupsByUserId(user1.Id)
  1026  	require.Nil(t, response.Error)
  1027  	assert.ElementsMatch(t, []*model.Group{group1, group2}, groups)
  1028  
  1029  }
  1030  
  1031  func TestGetGroupStats(t *testing.T) {
  1032  	th := Setup(t).InitBasic()
  1033  	defer th.TearDown()
  1034  
  1035  	id := model.NewId()
  1036  	group, err := th.App.CreateGroup(&model.Group{
  1037  		DisplayName: "dn-foo_" + id,
  1038  		Name:        model.NewString("name" + id),
  1039  		Source:      model.GroupSourceLdap,
  1040  		Description: "description_" + id,
  1041  		RemoteId:    model.NewId(),
  1042  	})
  1043  	assert.Nil(t, err)
  1044  
  1045  	var response *model.Response
  1046  	var stats *model.GroupStats
  1047  
  1048  	t.Run("Requires ldap license", func(t *testing.T) {
  1049  		_, response = th.SystemAdminClient.GetGroupStats(group.Id)
  1050  		CheckNotImplementedStatus(t, response)
  1051  	})
  1052  
  1053  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
  1054  
  1055  	t.Run("Requires manage system permission to access group stats", func(t *testing.T) {
  1056  		th.Client.Login(th.BasicUser.Email, th.BasicUser.Password)
  1057  		_, response = th.Client.GetGroupStats(group.Id)
  1058  		CheckForbiddenStatus(t, response)
  1059  	})
  1060  
  1061  	t.Run("Returns stats for a group with no members", func(t *testing.T) {
  1062  		stats, _ = th.SystemAdminClient.GetGroupStats(group.Id)
  1063  		assert.Equal(t, stats.GroupID, group.Id)
  1064  		assert.Equal(t, stats.TotalMemberCount, int64(0))
  1065  	})
  1066  
  1067  	user1, err := th.App.CreateUser(th.Context, &model.User{Email: th.GenerateTestEmail(), Nickname: "test user1", Password: "test-password-1", Username: "test-user-1", Roles: model.SYSTEM_USER_ROLE_ID})
  1068  	assert.Nil(t, err)
  1069  	_, err = th.App.UpsertGroupMember(group.Id, user1.Id)
  1070  	assert.Nil(t, err)
  1071  
  1072  	t.Run("Returns stats for a group with members", func(t *testing.T) {
  1073  		stats, _ = th.SystemAdminClient.GetGroupStats(group.Id)
  1074  		assert.Equal(t, stats.GroupID, group.Id)
  1075  		assert.Equal(t, stats.TotalMemberCount, int64(1))
  1076  	})
  1077  }
  1078  
  1079  func TestGetGroupsGroupConstrainedParentTeam(t *testing.T) {
  1080  	th := Setup(t)
  1081  	defer th.TearDown()
  1082  
  1083  	th.App.Srv().SetLicense(model.NewTestLicense("ldap"))
  1084  
  1085  	var groups []*model.Group
  1086  	for i := 0; i < 4; i++ {
  1087  		id := model.NewId()
  1088  		group, err := th.App.CreateGroup(&model.Group{
  1089  			DisplayName: fmt.Sprintf("dn-foo_%d", i),
  1090  			Name:        model.NewString("name" + id),
  1091  			Source:      model.GroupSourceLdap,
  1092  			Description: "description_" + id,
  1093  			RemoteId:    model.NewId(),
  1094  		})
  1095  		require.Nil(t, err)
  1096  		groups = append(groups, group)
  1097  	}
  1098  
  1099  	team := th.CreateTeam()
  1100  
  1101  	id := model.NewId()
  1102  	channel := &model.Channel{
  1103  		DisplayName:      "dn_" + id,
  1104  		Name:             "name" + id,
  1105  		Type:             model.CHANNEL_PRIVATE,
  1106  		TeamId:           team.Id,
  1107  		GroupConstrained: model.NewBool(true),
  1108  	}
  1109  	channel, err := th.App.CreateChannel(th.Context, channel, false)
  1110  	require.Nil(t, err)
  1111  
  1112  	// normal result of groups are returned if the team is not group-constrained
  1113  	apiGroups, response := th.SystemAdminClient.GetGroups(model.GroupSearchOpts{NotAssociatedToChannel: channel.Id})
  1114  	require.Nil(t, response.Error)
  1115  	require.Contains(t, apiGroups, groups[0])
  1116  	require.Contains(t, apiGroups, groups[1])
  1117  	require.Contains(t, apiGroups, groups[2])
  1118  
  1119  	team.GroupConstrained = model.NewBool(true)
  1120  	team, err = th.App.UpdateTeam(team)
  1121  	require.Nil(t, err)
  1122  
  1123  	// team is group-constrained but has no associated groups
  1124  	apiGroups, response = th.SystemAdminClient.GetGroups(model.GroupSearchOpts{NotAssociatedToChannel: channel.Id, FilterParentTeamPermitted: true})
  1125  	require.Nil(t, response.Error)
  1126  	require.Len(t, apiGroups, 0)
  1127  
  1128  	for _, group := range []*model.Group{groups[0], groups[2], groups[3]} {
  1129  		_, err = th.App.UpsertGroupSyncable(model.NewGroupTeam(group.Id, team.Id, false))
  1130  		require.Nil(t, err)
  1131  	}
  1132  
  1133  	// set of the teams groups are returned
  1134  	apiGroups, response = th.SystemAdminClient.GetGroups(model.GroupSearchOpts{NotAssociatedToChannel: channel.Id, FilterParentTeamPermitted: true})
  1135  	require.Nil(t, response.Error)
  1136  	require.Contains(t, apiGroups, groups[0])
  1137  	require.NotContains(t, apiGroups, groups[1])
  1138  	require.Contains(t, apiGroups, groups[2])
  1139  
  1140  	// paged results function as expected
  1141  	apiGroups, response = th.SystemAdminClient.GetGroups(model.GroupSearchOpts{NotAssociatedToChannel: channel.Id, FilterParentTeamPermitted: true, PageOpts: &model.PageOpts{PerPage: 2, Page: 0}})
  1142  	require.Nil(t, response.Error)
  1143  	require.Len(t, apiGroups, 2)
  1144  	require.Equal(t, apiGroups[0].Id, groups[0].Id)
  1145  	require.Equal(t, apiGroups[1].Id, groups[2].Id)
  1146  
  1147  	apiGroups, response = th.SystemAdminClient.GetGroups(model.GroupSearchOpts{NotAssociatedToChannel: channel.Id, FilterParentTeamPermitted: true, PageOpts: &model.PageOpts{PerPage: 2, Page: 1}})
  1148  	require.Nil(t, response.Error)
  1149  	require.Len(t, apiGroups, 1)
  1150  	require.Equal(t, apiGroups[0].Id, groups[3].Id)
  1151  
  1152  	_, err = th.App.UpsertGroupSyncable(model.NewGroupChannel(groups[0].Id, channel.Id, false))
  1153  	require.Nil(t, err)
  1154  
  1155  	// as usual it doesn't return groups already associated to the channel
  1156  	apiGroups, response = th.SystemAdminClient.GetGroups(model.GroupSearchOpts{NotAssociatedToChannel: channel.Id})
  1157  	require.Nil(t, response.Error)
  1158  	require.NotContains(t, apiGroups, groups[0])
  1159  	require.Contains(t, apiGroups, groups[2])
  1160  }