github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/api4/group_test.go (about)

     1  // Copyright (c) 2018-present Xenia, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  
    15  	"github.com/xzl8028/xenia-server/model"
    16  )
    17  
    18  func TestGetGroup(t *testing.T) {
    19  	th := Setup().InitBasic()
    20  	defer th.TearDown()
    21  
    22  	id := model.NewId()
    23  	g, err := th.App.CreateGroup(&model.Group{
    24  		DisplayName: "dn_" + id,
    25  		Name:        "name" + id,
    26  		Source:      model.GroupSourceLdap,
    27  		Description: "description_" + id,
    28  		RemoteId:    model.NewId(),
    29  	})
    30  	assert.Nil(t, err)
    31  
    32  	_, response := th.Client.GetGroup(g.Id, "")
    33  	CheckNotImplementedStatus(t, response)
    34  
    35  	_, response = th.SystemAdminClient.GetGroup(g.Id, "")
    36  	CheckNotImplementedStatus(t, response)
    37  
    38  	th.App.SetLicense(model.NewTestLicense("ldap"))
    39  
    40  	group, response := th.SystemAdminClient.GetGroup(g.Id, "")
    41  	CheckNoError(t, response)
    42  
    43  	assert.Equal(t, g.DisplayName, group.DisplayName)
    44  	assert.Equal(t, g.Name, group.Name)
    45  	assert.Equal(t, g.Source, group.Source)
    46  	assert.Equal(t, g.Description, group.Description)
    47  	assert.Equal(t, g.RemoteId, group.RemoteId)
    48  	assert.Equal(t, g.CreateAt, group.CreateAt)
    49  	assert.Equal(t, g.UpdateAt, group.UpdateAt)
    50  	assert.Equal(t, g.DeleteAt, group.DeleteAt)
    51  
    52  	_, response = th.SystemAdminClient.GetGroup(model.NewId(), "")
    53  	CheckNotFoundStatus(t, response)
    54  
    55  	_, response = th.SystemAdminClient.GetGroup("12345", "")
    56  	CheckBadRequestStatus(t, response)
    57  
    58  	th.SystemAdminClient.Logout()
    59  	_, response = th.SystemAdminClient.GetGroup(group.Id, "")
    60  	CheckUnauthorizedStatus(t, response)
    61  }
    62  
    63  func TestPatchGroup(t *testing.T) {
    64  	th := Setup().InitBasic()
    65  	defer th.TearDown()
    66  
    67  	id := model.NewId()
    68  	g, err := th.App.CreateGroup(&model.Group{
    69  		DisplayName: "dn_" + id,
    70  		Name:        "name" + id,
    71  		Source:      model.GroupSourceLdap,
    72  		Description: "description_" + id,
    73  		RemoteId:    model.NewId(),
    74  	})
    75  	assert.Nil(t, err)
    76  
    77  	updateFmt := "%s_updated"
    78  
    79  	newName := fmt.Sprintf(updateFmt, g.Name)
    80  	newDisplayName := fmt.Sprintf(updateFmt, g.DisplayName)
    81  	newDescription := fmt.Sprintf(updateFmt, g.Description)
    82  
    83  	gp := &model.GroupPatch{
    84  		Name:        &newName,
    85  		DisplayName: &newDisplayName,
    86  		Description: &newDescription,
    87  	}
    88  
    89  	_, response := th.Client.PatchGroup(g.Id, gp)
    90  	CheckNotImplementedStatus(t, response)
    91  
    92  	_, response = th.SystemAdminClient.PatchGroup(g.Id, gp)
    93  	CheckNotImplementedStatus(t, response)
    94  
    95  	th.App.SetLicense(model.NewTestLicense("ldap"))
    96  
    97  	group2, response := th.SystemAdminClient.PatchGroup(g.Id, gp)
    98  	CheckOKStatus(t, response)
    99  
   100  	group, response := th.SystemAdminClient.GetGroup(g.Id, "")
   101  	CheckNoError(t, response)
   102  
   103  	assert.Equal(t, *gp.DisplayName, group.DisplayName)
   104  	assert.Equal(t, *gp.DisplayName, group2.DisplayName)
   105  	assert.Equal(t, *gp.Name, group.Name)
   106  	assert.Equal(t, *gp.Name, group2.Name)
   107  	assert.Equal(t, *gp.Description, group.Description)
   108  	assert.Equal(t, *gp.Description, group2.Description)
   109  
   110  	assert.Equal(t, group2.UpdateAt, group.UpdateAt)
   111  
   112  	assert.Equal(t, g.Source, group.Source)
   113  	assert.Equal(t, g.Source, group2.Source)
   114  	assert.Equal(t, g.RemoteId, group.RemoteId)
   115  	assert.Equal(t, g.RemoteId, group2.RemoteId)
   116  	assert.Equal(t, g.CreateAt, group.CreateAt)
   117  	assert.Equal(t, g.CreateAt, group2.CreateAt)
   118  	assert.Equal(t, g.DeleteAt, group.DeleteAt)
   119  	assert.Equal(t, g.DeleteAt, group2.DeleteAt)
   120  
   121  	_, response = th.SystemAdminClient.PatchGroup(model.NewId(), gp)
   122  	CheckNotFoundStatus(t, response)
   123  
   124  	th.SystemAdminClient.Logout()
   125  	_, response = th.SystemAdminClient.PatchGroup(group.Id, gp)
   126  	CheckUnauthorizedStatus(t, response)
   127  }
   128  
   129  func TestLinkGroupTeam(t *testing.T) {
   130  	th := Setup().InitBasic()
   131  	defer th.TearDown()
   132  
   133  	id := model.NewId()
   134  	g, err := th.App.CreateGroup(&model.Group{
   135  		DisplayName: "dn_" + id,
   136  		Name:        "name" + id,
   137  		Source:      model.GroupSourceLdap,
   138  		Description: "description_" + id,
   139  		RemoteId:    model.NewId(),
   140  	})
   141  	assert.Nil(t, err)
   142  
   143  	patch := &model.GroupSyncablePatch{
   144  		AutoAdd: model.NewBool(true),
   145  	}
   146  
   147  	_, response := th.Client.LinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   148  	CheckNotImplementedStatus(t, response)
   149  
   150  	_, response = th.SystemAdminClient.LinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   151  	CheckNotImplementedStatus(t, response)
   152  
   153  	th.App.SetLicense(model.NewTestLicense("ldap"))
   154  
   155  	_, response = th.Client.LinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   156  	assert.NotNil(t, response.Error)
   157  
   158  	th.UpdateUserToTeamAdmin(th.BasicUser, th.BasicTeam)
   159  	th.Client.Logout()
   160  	th.Client.Login(th.BasicUser.Email, th.BasicUser.Password)
   161  
   162  	groupTeam, response := th.Client.LinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   163  	assert.Equal(t, http.StatusCreated, response.StatusCode)
   164  	assert.NotNil(t, groupTeam)
   165  }
   166  
   167  func TestLinkGroupChannel(t *testing.T) {
   168  	th := Setup().InitBasic()
   169  	defer th.TearDown()
   170  
   171  	id := model.NewId()
   172  	g, err := th.App.CreateGroup(&model.Group{
   173  		DisplayName: "dn_" + id,
   174  		Name:        "name" + id,
   175  		Source:      model.GroupSourceLdap,
   176  		Description: "description_" + id,
   177  		RemoteId:    model.NewId(),
   178  	})
   179  	assert.Nil(t, err)
   180  
   181  	patch := &model.GroupSyncablePatch{
   182  		AutoAdd: model.NewBool(true),
   183  	}
   184  
   185  	_, response := th.Client.LinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   186  	CheckNotImplementedStatus(t, response)
   187  
   188  	_, response = th.SystemAdminClient.LinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   189  	CheckNotImplementedStatus(t, response)
   190  
   191  	th.App.SetLicense(model.NewTestLicense("ldap"))
   192  
   193  	groupTeam, response := th.Client.LinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   194  	assert.Equal(t, http.StatusCreated, response.StatusCode)
   195  	assert.NotNil(t, groupTeam)
   196  
   197  	_, response = th.SystemAdminClient.UpdateChannelRoles(th.BasicChannel.Id, th.BasicUser.Id, "")
   198  	require.Nil(t, response.Error)
   199  	th.Client.Logout()
   200  	th.Client.Login(th.BasicUser.Email, th.BasicUser.Password)
   201  
   202  	_, response = th.Client.LinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   203  	assert.NotNil(t, response.Error)
   204  }
   205  
   206  func TestUnlinkGroupTeam(t *testing.T) {
   207  	th := Setup().InitBasic()
   208  	defer th.TearDown()
   209  
   210  	id := model.NewId()
   211  	g, err := th.App.CreateGroup(&model.Group{
   212  		DisplayName: "dn_" + id,
   213  		Name:        "name" + id,
   214  		Source:      model.GroupSourceLdap,
   215  		Description: "description_" + id,
   216  		RemoteId:    model.NewId(),
   217  	})
   218  	assert.Nil(t, err)
   219  
   220  	patch := &model.GroupSyncablePatch{
   221  		AutoAdd: model.NewBool(true),
   222  	}
   223  
   224  	th.App.SetLicense(model.NewTestLicense("ldap"))
   225  
   226  	_, response := th.SystemAdminClient.LinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   227  	assert.Equal(t, http.StatusCreated, response.StatusCode)
   228  
   229  	th.App.SetLicense(nil)
   230  
   231  	response = th.Client.UnlinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam)
   232  	CheckNotImplementedStatus(t, response)
   233  
   234  	response = th.SystemAdminClient.UnlinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam)
   235  	CheckNotImplementedStatus(t, response)
   236  
   237  	th.App.SetLicense(model.NewTestLicense("ldap"))
   238  
   239  	response = th.Client.UnlinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam)
   240  	assert.NotNil(t, response.Error)
   241  
   242  	th.UpdateUserToTeamAdmin(th.BasicUser, th.BasicTeam)
   243  	th.Client.Logout()
   244  	th.Client.Login(th.BasicUser.Email, th.BasicUser.Password)
   245  
   246  	response = th.Client.UnlinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam)
   247  	CheckOKStatus(t, response)
   248  }
   249  
   250  func TestUnlinkGroupChannel(t *testing.T) {
   251  	th := Setup().InitBasic()
   252  	defer th.TearDown()
   253  
   254  	id := model.NewId()
   255  	g, err := th.App.CreateGroup(&model.Group{
   256  		DisplayName: "dn_" + id,
   257  		Name:        "name" + id,
   258  		Source:      model.GroupSourceLdap,
   259  		Description: "description_" + id,
   260  		RemoteId:    model.NewId(),
   261  	})
   262  	assert.Nil(t, err)
   263  
   264  	patch := &model.GroupSyncablePatch{
   265  		AutoAdd: model.NewBool(true),
   266  	}
   267  
   268  	th.App.SetLicense(model.NewTestLicense("ldap"))
   269  
   270  	_, response := th.SystemAdminClient.LinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   271  	assert.Equal(t, http.StatusCreated, response.StatusCode)
   272  
   273  	th.App.SetLicense(nil)
   274  
   275  	response = th.Client.UnlinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel)
   276  	CheckNotImplementedStatus(t, response)
   277  
   278  	response = th.SystemAdminClient.UnlinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel)
   279  	CheckNotImplementedStatus(t, response)
   280  
   281  	th.App.SetLicense(model.NewTestLicense("ldap"))
   282  
   283  	_, response = th.SystemAdminClient.UpdateChannelRoles(th.BasicChannel.Id, th.BasicUser.Id, "")
   284  	require.Nil(t, response.Error)
   285  	th.Client.Logout()
   286  	th.Client.Login(th.BasicUser.Email, th.BasicUser.Password)
   287  
   288  	response = th.Client.UnlinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel)
   289  	assert.NotNil(t, response.Error)
   290  
   291  	_, response = th.SystemAdminClient.UpdateChannelRoles(th.BasicChannel.Id, th.BasicUser.Id, "channel_admin channel_user")
   292  	require.Nil(t, response.Error)
   293  	th.Client.Logout()
   294  	th.Client.Login(th.BasicUser.Email, th.BasicUser.Password)
   295  
   296  	response = th.Client.UnlinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel)
   297  	assert.Nil(t, response.Error)
   298  }
   299  
   300  func TestGetGroupTeam(t *testing.T) {
   301  	th := Setup().InitBasic()
   302  	defer th.TearDown()
   303  
   304  	id := model.NewId()
   305  	g, err := th.App.CreateGroup(&model.Group{
   306  		DisplayName: "dn_" + id,
   307  		Name:        "name" + id,
   308  		Source:      model.GroupSourceLdap,
   309  		Description: "description_" + id,
   310  		RemoteId:    model.NewId(),
   311  	})
   312  	assert.Nil(t, err)
   313  
   314  	_, response := th.Client.GetGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, "")
   315  	CheckNotImplementedStatus(t, response)
   316  
   317  	_, response = th.SystemAdminClient.GetGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, "")
   318  	CheckNotImplementedStatus(t, response)
   319  
   320  	th.App.SetLicense(model.NewTestLicense("ldap"))
   321  
   322  	patch := &model.GroupSyncablePatch{
   323  		AutoAdd: model.NewBool(true),
   324  	}
   325  
   326  	_, response = th.SystemAdminClient.LinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   327  	assert.Equal(t, http.StatusCreated, response.StatusCode)
   328  
   329  	groupSyncable, response := th.SystemAdminClient.GetGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, "")
   330  	CheckOKStatus(t, response)
   331  	assert.NotNil(t, groupSyncable)
   332  
   333  	assert.Equal(t, g.Id, groupSyncable.GroupId)
   334  	assert.Equal(t, th.BasicTeam.Id, groupSyncable.SyncableId)
   335  	assert.Equal(t, *patch.AutoAdd, groupSyncable.AutoAdd)
   336  
   337  	_, response = th.SystemAdminClient.GetGroupSyncable(model.NewId(), th.BasicTeam.Id, model.GroupSyncableTypeTeam, "")
   338  	CheckNotFoundStatus(t, response)
   339  
   340  	_, response = th.SystemAdminClient.GetGroupSyncable(g.Id, model.NewId(), model.GroupSyncableTypeTeam, "")
   341  	CheckNotFoundStatus(t, response)
   342  
   343  	_, response = th.SystemAdminClient.GetGroupSyncable("asdfasdfe3", th.BasicTeam.Id, model.GroupSyncableTypeTeam, "")
   344  	CheckBadRequestStatus(t, response)
   345  
   346  	_, response = th.SystemAdminClient.GetGroupSyncable(g.Id, "asdfasdfe3", model.GroupSyncableTypeTeam, "")
   347  	CheckBadRequestStatus(t, response)
   348  
   349  	th.SystemAdminClient.Logout()
   350  	_, response = th.SystemAdminClient.GetGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, "")
   351  	CheckUnauthorizedStatus(t, response)
   352  }
   353  
   354  func TestGetGroupChannel(t *testing.T) {
   355  	th := Setup().InitBasic()
   356  	defer th.TearDown()
   357  
   358  	id := model.NewId()
   359  	g, err := th.App.CreateGroup(&model.Group{
   360  		DisplayName: "dn_" + id,
   361  		Name:        "name" + id,
   362  		Source:      model.GroupSourceLdap,
   363  		Description: "description_" + id,
   364  		RemoteId:    model.NewId(),
   365  	})
   366  	assert.Nil(t, err)
   367  
   368  	_, response := th.Client.GetGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, "")
   369  	CheckNotImplementedStatus(t, response)
   370  
   371  	_, response = th.SystemAdminClient.GetGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, "")
   372  	CheckNotImplementedStatus(t, response)
   373  
   374  	th.App.SetLicense(model.NewTestLicense("ldap"))
   375  
   376  	patch := &model.GroupSyncablePatch{
   377  		AutoAdd: model.NewBool(true),
   378  	}
   379  
   380  	_, response = th.SystemAdminClient.LinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   381  	assert.Equal(t, http.StatusCreated, response.StatusCode)
   382  
   383  	groupSyncable, response := th.SystemAdminClient.GetGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, "")
   384  	CheckOKStatus(t, response)
   385  	assert.NotNil(t, groupSyncable)
   386  
   387  	assert.Equal(t, g.Id, groupSyncable.GroupId)
   388  	assert.Equal(t, th.BasicChannel.Id, groupSyncable.SyncableId)
   389  	assert.Equal(t, *patch.AutoAdd, groupSyncable.AutoAdd)
   390  
   391  	_, response = th.SystemAdminClient.GetGroupSyncable(model.NewId(), th.BasicChannel.Id, model.GroupSyncableTypeChannel, "")
   392  	CheckNotFoundStatus(t, response)
   393  
   394  	_, response = th.SystemAdminClient.GetGroupSyncable(g.Id, model.NewId(), model.GroupSyncableTypeChannel, "")
   395  	CheckNotFoundStatus(t, response)
   396  
   397  	_, response = th.SystemAdminClient.GetGroupSyncable("asdfasdfe3", th.BasicChannel.Id, model.GroupSyncableTypeChannel, "")
   398  	CheckBadRequestStatus(t, response)
   399  
   400  	_, response = th.SystemAdminClient.GetGroupSyncable(g.Id, "asdfasdfe3", model.GroupSyncableTypeChannel, "")
   401  	CheckBadRequestStatus(t, response)
   402  
   403  	th.SystemAdminClient.Logout()
   404  	_, response = th.SystemAdminClient.GetGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, "")
   405  	CheckUnauthorizedStatus(t, response)
   406  }
   407  
   408  func TestGetGroupTeams(t *testing.T) {
   409  	th := Setup().InitBasic()
   410  	defer th.TearDown()
   411  
   412  	id := model.NewId()
   413  	g, err := th.App.CreateGroup(&model.Group{
   414  		DisplayName: "dn_" + id,
   415  		Name:        "name" + id,
   416  		Source:      model.GroupSourceLdap,
   417  		Description: "description_" + id,
   418  		RemoteId:    model.NewId(),
   419  	})
   420  	assert.Nil(t, err)
   421  
   422  	th.App.SetLicense(model.NewTestLicense("ldap"))
   423  
   424  	patch := &model.GroupSyncablePatch{
   425  		AutoAdd: model.NewBool(true),
   426  	}
   427  
   428  	for i := 0; i < 10; i++ {
   429  		team := th.CreateTeam()
   430  		_, response := th.SystemAdminClient.LinkGroupSyncable(g.Id, team.Id, model.GroupSyncableTypeTeam, patch)
   431  		assert.Equal(t, http.StatusCreated, response.StatusCode)
   432  	}
   433  
   434  	th.App.SetLicense(nil)
   435  
   436  	_, response := th.Client.GetGroupSyncables(g.Id, model.GroupSyncableTypeTeam, "")
   437  	CheckNotImplementedStatus(t, response)
   438  
   439  	_, response = th.SystemAdminClient.GetGroupSyncables(g.Id, model.GroupSyncableTypeTeam, "")
   440  	CheckNotImplementedStatus(t, response)
   441  
   442  	th.App.SetLicense(model.NewTestLicense("ldap"))
   443  
   444  	_, response = th.Client.GetGroupSyncables(g.Id, model.GroupSyncableTypeTeam, "")
   445  	assert.Equal(t, http.StatusForbidden, response.StatusCode)
   446  
   447  	groupSyncables, response := th.SystemAdminClient.GetGroupSyncables(g.Id, model.GroupSyncableTypeTeam, "")
   448  	CheckOKStatus(t, response)
   449  
   450  	assert.Len(t, groupSyncables, 10)
   451  
   452  	th.SystemAdminClient.Logout()
   453  	_, response = th.SystemAdminClient.GetGroupSyncables(g.Id, model.GroupSyncableTypeTeam, "")
   454  	CheckUnauthorizedStatus(t, response)
   455  }
   456  
   457  func TestGetGroupChannels(t *testing.T) {
   458  	th := Setup().InitBasic()
   459  	defer th.TearDown()
   460  
   461  	id := model.NewId()
   462  	g, err := th.App.CreateGroup(&model.Group{
   463  		DisplayName: "dn_" + id,
   464  		Name:        "name" + id,
   465  		Source:      model.GroupSourceLdap,
   466  		Description: "description_" + id,
   467  		RemoteId:    model.NewId(),
   468  	})
   469  	assert.Nil(t, err)
   470  
   471  	th.App.SetLicense(model.NewTestLicense("ldap"))
   472  
   473  	patch := &model.GroupSyncablePatch{
   474  		AutoAdd: model.NewBool(true),
   475  	}
   476  
   477  	for i := 0; i < 10; i++ {
   478  		channel := th.CreatePublicChannel()
   479  		_, response := th.SystemAdminClient.LinkGroupSyncable(g.Id, channel.Id, model.GroupSyncableTypeChannel, patch)
   480  		assert.Equal(t, http.StatusCreated, response.StatusCode)
   481  	}
   482  
   483  	th.App.SetLicense(nil)
   484  
   485  	_, response := th.Client.GetGroupSyncables(g.Id, model.GroupSyncableTypeChannel, "")
   486  	CheckNotImplementedStatus(t, response)
   487  
   488  	_, response = th.SystemAdminClient.GetGroupSyncables(g.Id, model.GroupSyncableTypeChannel, "")
   489  	CheckNotImplementedStatus(t, response)
   490  
   491  	th.App.SetLicense(model.NewTestLicense("ldap"))
   492  
   493  	_, response = th.Client.GetGroupSyncables(g.Id, model.GroupSyncableTypeChannel, "")
   494  	assert.Equal(t, http.StatusForbidden, response.StatusCode)
   495  
   496  	groupSyncables, response := th.SystemAdminClient.GetGroupSyncables(g.Id, model.GroupSyncableTypeChannel, "")
   497  	CheckOKStatus(t, response)
   498  
   499  	assert.Len(t, groupSyncables, 10)
   500  
   501  	th.SystemAdminClient.Logout()
   502  	_, response = th.SystemAdminClient.GetGroupSyncables(g.Id, model.GroupSyncableTypeChannel, "")
   503  	CheckUnauthorizedStatus(t, response)
   504  }
   505  
   506  func TestPatchGroupTeam(t *testing.T) {
   507  	th := Setup().InitBasic()
   508  	defer th.TearDown()
   509  
   510  	id := model.NewId()
   511  	g, err := th.App.CreateGroup(&model.Group{
   512  		DisplayName: "dn_" + id,
   513  		Name:        "name" + id,
   514  		Source:      model.GroupSourceLdap,
   515  		Description: "description_" + id,
   516  		RemoteId:    model.NewId(),
   517  	})
   518  	assert.Nil(t, err)
   519  
   520  	patch := &model.GroupSyncablePatch{
   521  		AutoAdd: model.NewBool(true),
   522  	}
   523  
   524  	th.App.SetLicense(model.NewTestLicense("ldap"))
   525  
   526  	groupSyncable, response := th.SystemAdminClient.LinkGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   527  	assert.Equal(t, http.StatusCreated, response.StatusCode)
   528  	assert.NotNil(t, groupSyncable)
   529  	assert.True(t, groupSyncable.AutoAdd)
   530  
   531  	_, response = th.Client.PatchGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   532  	assert.Equal(t, http.StatusForbidden, response.StatusCode)
   533  
   534  	th.App.SetLicense(nil)
   535  
   536  	_, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   537  	CheckNotImplementedStatus(t, response)
   538  
   539  	th.App.SetLicense(model.NewTestLicense("ldap"))
   540  
   541  	patch.AutoAdd = model.NewBool(false)
   542  	groupSyncable, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   543  	CheckOKStatus(t, response)
   544  	assert.False(t, groupSyncable.AutoAdd)
   545  
   546  	assert.Equal(t, g.Id, groupSyncable.GroupId)
   547  	assert.Equal(t, th.BasicTeam.Id, groupSyncable.SyncableId)
   548  	assert.Equal(t, model.GroupSyncableTypeTeam, groupSyncable.Type)
   549  
   550  	patch.AutoAdd = model.NewBool(true)
   551  	groupSyncable, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   552  	CheckOKStatus(t, response)
   553  
   554  	_, response = th.SystemAdminClient.PatchGroupSyncable(model.NewId(), th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   555  	CheckNotFoundStatus(t, response)
   556  
   557  	_, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, model.NewId(), model.GroupSyncableTypeTeam, patch)
   558  	CheckNotFoundStatus(t, response)
   559  
   560  	_, response = th.SystemAdminClient.PatchGroupSyncable("abc", th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   561  	CheckBadRequestStatus(t, response)
   562  
   563  	_, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, "abc", model.GroupSyncableTypeTeam, patch)
   564  	CheckBadRequestStatus(t, response)
   565  
   566  	th.SystemAdminClient.Logout()
   567  	_, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, th.BasicTeam.Id, model.GroupSyncableTypeTeam, patch)
   568  	CheckUnauthorizedStatus(t, response)
   569  }
   570  
   571  func TestPatchGroupChannel(t *testing.T) {
   572  	th := Setup().InitBasic()
   573  	defer th.TearDown()
   574  
   575  	id := model.NewId()
   576  	g, err := th.App.CreateGroup(&model.Group{
   577  		DisplayName: "dn_" + id,
   578  		Name:        "name" + id,
   579  		Source:      model.GroupSourceLdap,
   580  		Description: "description_" + id,
   581  		RemoteId:    model.NewId(),
   582  	})
   583  	assert.Nil(t, err)
   584  
   585  	patch := &model.GroupSyncablePatch{
   586  		AutoAdd: model.NewBool(true),
   587  	}
   588  
   589  	th.App.SetLicense(model.NewTestLicense("ldap"))
   590  
   591  	groupSyncable, response := th.SystemAdminClient.LinkGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   592  	assert.Equal(t, http.StatusCreated, response.StatusCode)
   593  	assert.NotNil(t, groupSyncable)
   594  	assert.True(t, groupSyncable.AutoAdd)
   595  
   596  	_, response = th.Client.PatchGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   597  	assert.Equal(t, http.StatusForbidden, response.StatusCode)
   598  
   599  	th.App.SetLicense(nil)
   600  
   601  	_, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   602  	CheckNotImplementedStatus(t, response)
   603  
   604  	th.App.SetLicense(model.NewTestLicense("ldap"))
   605  
   606  	patch.AutoAdd = model.NewBool(false)
   607  	groupSyncable, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   608  	CheckOKStatus(t, response)
   609  	assert.False(t, groupSyncable.AutoAdd)
   610  
   611  	assert.Equal(t, g.Id, groupSyncable.GroupId)
   612  	assert.Equal(t, th.BasicChannel.Id, groupSyncable.SyncableId)
   613  	assert.Equal(t, model.GroupSyncableTypeChannel, groupSyncable.Type)
   614  
   615  	patch.AutoAdd = model.NewBool(true)
   616  	groupSyncable, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   617  	CheckOKStatus(t, response)
   618  
   619  	_, response = th.SystemAdminClient.PatchGroupSyncable(model.NewId(), th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   620  	CheckNotFoundStatus(t, response)
   621  
   622  	_, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, model.NewId(), model.GroupSyncableTypeChannel, patch)
   623  	CheckNotFoundStatus(t, response)
   624  
   625  	_, response = th.SystemAdminClient.PatchGroupSyncable("abc", th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   626  	CheckBadRequestStatus(t, response)
   627  
   628  	_, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, "abc", model.GroupSyncableTypeChannel, patch)
   629  	CheckBadRequestStatus(t, response)
   630  
   631  	th.SystemAdminClient.Logout()
   632  	_, response = th.SystemAdminClient.PatchGroupSyncable(g.Id, th.BasicChannel.Id, model.GroupSyncableTypeChannel, patch)
   633  	CheckUnauthorizedStatus(t, response)
   634  }
   635  
   636  func TestGetGroupsByChannel(t *testing.T) {
   637  	th := Setup().InitBasic()
   638  	defer th.TearDown()
   639  
   640  	id := model.NewId()
   641  	group, err := th.App.CreateGroup(&model.Group{
   642  		DisplayName: "dn_" + id,
   643  		Name:        "name" + id,
   644  		Source:      model.GroupSourceLdap,
   645  		Description: "description_" + id,
   646  		RemoteId:    model.NewId(),
   647  	})
   648  	assert.Nil(t, err)
   649  
   650  	_, err = th.App.CreateGroupSyncable(&model.GroupSyncable{
   651  		AutoAdd:    true,
   652  		SyncableId: th.BasicChannel.Id,
   653  		Type:       model.GroupSyncableTypeChannel,
   654  		GroupId:    group.Id,
   655  	})
   656  	assert.Nil(t, err)
   657  
   658  	opts := model.GroupSearchOpts{
   659  		PageOpts: &model.PageOpts{
   660  			Page:    0,
   661  			PerPage: 60,
   662  		},
   663  	}
   664  
   665  	_, _, response := th.SystemAdminClient.GetGroupsByChannel("asdfasdf", opts)
   666  	CheckBadRequestStatus(t, response)
   667  
   668  	th.App.SetLicense(nil)
   669  
   670  	_, _, response = th.SystemAdminClient.GetGroupsByChannel(th.BasicChannel.Id, opts)
   671  	CheckNotImplementedStatus(t, response)
   672  
   673  	th.App.SetLicense(model.NewTestLicense("ldap"))
   674  
   675  	privateChannel := th.CreateChannelWithClient(th.SystemAdminClient, model.CHANNEL_PRIVATE)
   676  
   677  	_, _, response = th.Client.GetGroupsByChannel(privateChannel.Id, opts)
   678  	CheckForbiddenStatus(t, response)
   679  
   680  	groups, _, response := th.SystemAdminClient.GetGroupsByChannel(th.BasicChannel.Id, opts)
   681  	assert.Nil(t, response.Error)
   682  	assert.ElementsMatch(t, []*model.Group{group}, groups)
   683  
   684  	groups, _, response = th.SystemAdminClient.GetGroupsByChannel(model.NewId(), opts)
   685  	assert.Equal(t, "store.sql_channel.get.existing.app_error", response.Error.Id)
   686  	assert.Empty(t, groups)
   687  }
   688  
   689  func TestGetGroupsByTeam(t *testing.T) {
   690  	th := Setup().InitBasic()
   691  	defer th.TearDown()
   692  
   693  	id := model.NewId()
   694  	group, err := th.App.CreateGroup(&model.Group{
   695  		DisplayName: "dn_" + id,
   696  		Name:        "name" + id,
   697  		Source:      model.GroupSourceLdap,
   698  		Description: "description_" + id,
   699  		RemoteId:    model.NewId(),
   700  	})
   701  	assert.Nil(t, err)
   702  
   703  	_, err = th.App.CreateGroupSyncable(&model.GroupSyncable{
   704  		AutoAdd:    true,
   705  		SyncableId: th.BasicTeam.Id,
   706  		Type:       model.GroupSyncableTypeTeam,
   707  		GroupId:    group.Id,
   708  	})
   709  	assert.Nil(t, err)
   710  
   711  	opts := model.GroupSearchOpts{
   712  		PageOpts: &model.PageOpts{
   713  			Page:    0,
   714  			PerPage: 60,
   715  		},
   716  	}
   717  
   718  	_, _, response := th.SystemAdminClient.GetGroupsByTeam("asdfasdf", opts)
   719  	CheckBadRequestStatus(t, response)
   720  
   721  	th.App.SetLicense(nil)
   722  
   723  	_, _, response = th.SystemAdminClient.GetGroupsByTeam(th.BasicTeam.Id, opts)
   724  	CheckNotImplementedStatus(t, response)
   725  
   726  	th.App.SetLicense(model.NewTestLicense("ldap"))
   727  
   728  	_, _, response = th.Client.GetGroupsByTeam(th.BasicTeam.Id, opts)
   729  	CheckForbiddenStatus(t, response)
   730  
   731  	groups, _, response := th.SystemAdminClient.GetGroupsByTeam(th.BasicTeam.Id, opts)
   732  	assert.Nil(t, response.Error)
   733  	assert.ElementsMatch(t, []*model.Group{group}, groups)
   734  
   735  	groups, _, response = th.SystemAdminClient.GetGroupsByTeam(model.NewId(), opts)
   736  	assert.Nil(t, response.Error)
   737  	assert.Empty(t, groups)
   738  }
   739  
   740  func TestGetGroups(t *testing.T) {
   741  	th := Setup().InitBasic()
   742  	defer th.TearDown()
   743  
   744  	id := model.NewId()
   745  	group, err := th.App.CreateGroup(&model.Group{
   746  		DisplayName: "dn-foo_" + id,
   747  		Name:        "name" + id,
   748  		Source:      model.GroupSourceLdap,
   749  		Description: "description_" + id,
   750  		RemoteId:    model.NewId(),
   751  	})
   752  	assert.Nil(t, err)
   753  
   754  	opts := model.GroupSearchOpts{
   755  		PageOpts: &model.PageOpts{
   756  			Page:    0,
   757  			PerPage: 60,
   758  		},
   759  	}
   760  
   761  	th.App.SetLicense(nil)
   762  
   763  	_, response := th.SystemAdminClient.GetGroups(opts)
   764  	CheckNotImplementedStatus(t, response)
   765  
   766  	th.App.SetLicense(model.NewTestLicense("ldap"))
   767  
   768  	groups, response := th.SystemAdminClient.GetGroups(opts)
   769  	assert.Nil(t, response.Error)
   770  	assert.ElementsMatch(t, []*model.Group{group, th.Group}, groups)
   771  	assert.Nil(t, groups[0].MemberCount)
   772  
   773  	opts.IncludeMemberCount = true
   774  	groups, _ = th.SystemAdminClient.GetGroups(opts)
   775  	assert.NotNil(t, groups[0].MemberCount)
   776  	opts.IncludeMemberCount = false
   777  
   778  	opts.Q = "-fOo"
   779  	groups, _ = th.SystemAdminClient.GetGroups(opts)
   780  	assert.Len(t, groups, 1)
   781  	opts.Q = ""
   782  
   783  	_, response = th.SystemAdminClient.UpdateTeamMemberRoles(th.BasicTeam.Id, th.BasicUser.Id, "")
   784  	require.Nil(t, response.Error)
   785  
   786  	opts.NotAssociatedToTeam = th.BasicTeam.Id
   787  	_, response = th.Client.GetGroups(opts)
   788  	CheckForbiddenStatus(t, response)
   789  
   790  	_, response = th.SystemAdminClient.UpdateTeamMemberRoles(th.BasicTeam.Id, th.BasicUser.Id, "team_user")
   791  	require.Nil(t, response.Error)
   792  
   793  	_, response = th.Client.GetGroups(opts)
   794  	assert.Nil(t, response.Error)
   795  }