github.com/teloshs/mattermost-server@v5.11.1+incompatible/store/storetest/group_supplier.go (about)

     1  // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/mattermost/mattermost-server/model"
    11  	"github.com/mattermost/mattermost-server/store"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestGroupStore(t *testing.T, ss store.Store) {
    16  	t.Run("Create", func(t *testing.T) { testGroupStoreCreate(t, ss) })
    17  	t.Run("Get", func(t *testing.T) { testGroupStoreGet(t, ss) })
    18  	t.Run("GetByRemoteID", func(t *testing.T) { testGroupStoreGetByRemoteID(t, ss) })
    19  	t.Run("GetAllBySource", func(t *testing.T) { testGroupStoreGetAllByType(t, ss) })
    20  	t.Run("Update", func(t *testing.T) { testGroupStoreUpdate(t, ss) })
    21  	t.Run("Delete", func(t *testing.T) { testGroupStoreDelete(t, ss) })
    22  
    23  	t.Run("GetMemberUsers", func(t *testing.T) { testGroupGetMemberUsers(t, ss) })
    24  	t.Run("GetMemberUsersPage", func(t *testing.T) { testGroupGetMemberUsersPage(t, ss) })
    25  	t.Run("CreateOrRestoreMember", func(t *testing.T) { testGroupCreateOrRestoreMember(t, ss) })
    26  	t.Run("DeleteMember", func(t *testing.T) { testGroupDeleteMember(t, ss) })
    27  
    28  	t.Run("CreateGroupSyncable", func(t *testing.T) { testCreateGroupSyncable(t, ss) })
    29  	t.Run("GetGroupSyncable", func(t *testing.T) { testGetGroupSyncable(t, ss) })
    30  	t.Run("GetAllGroupSyncablesByGroupId", func(t *testing.T) { testGetAllGroupSyncablesByGroup(t, ss) })
    31  	t.Run("UpdateGroupSyncable", func(t *testing.T) { testUpdateGroupSyncable(t, ss) })
    32  	t.Run("DeleteGroupSyncable", func(t *testing.T) { testDeleteGroupSyncable(t, ss) })
    33  
    34  	t.Run("PendingAutoAddTeamMembers", func(t *testing.T) { testPendingAutoAddTeamMembers(t, ss) })
    35  	t.Run("PendingAutoAddChannelMembers", func(t *testing.T) { testPendingAutoAddChannelMembers(t, ss) })
    36  }
    37  
    38  func testGroupStoreCreate(t *testing.T, ss store.Store) {
    39  	// Save a new group
    40  	g1 := &model.Group{
    41  		Name:        model.NewId(),
    42  		DisplayName: model.NewId(),
    43  		Source:      model.GroupSourceLdap,
    44  		Description: model.NewId(),
    45  		RemoteId:    model.NewId(),
    46  	}
    47  
    48  	// Happy path
    49  	res1 := <-ss.Group().Create(g1)
    50  	assert.Nil(t, res1.Err)
    51  	d1 := res1.Data.(*model.Group)
    52  	assert.Len(t, d1.Id, 26)
    53  	assert.Equal(t, g1.Name, d1.Name)
    54  	assert.Equal(t, g1.DisplayName, d1.DisplayName)
    55  	assert.Equal(t, g1.Description, d1.Description)
    56  	assert.Equal(t, g1.RemoteId, d1.RemoteId)
    57  	assert.NotZero(t, d1.CreateAt)
    58  	assert.NotZero(t, d1.UpdateAt)
    59  	assert.Zero(t, d1.DeleteAt)
    60  
    61  	// Requires name and display name
    62  	g2 := &model.Group{
    63  		Name:        "",
    64  		DisplayName: model.NewId(),
    65  		Source:      model.GroupSourceLdap,
    66  		RemoteId:    model.NewId(),
    67  	}
    68  	res2 := <-ss.Group().Create(g2)
    69  	assert.Nil(t, res2.Data)
    70  	assert.NotNil(t, res2.Err)
    71  	assert.Equal(t, res2.Err.Id, "model.group.name.app_error")
    72  
    73  	g2.Name = model.NewId()
    74  	g2.DisplayName = ""
    75  	res3 := <-ss.Group().Create(g2)
    76  	assert.Nil(t, res3.Data)
    77  	assert.NotNil(t, res3.Err)
    78  	assert.Equal(t, res3.Err.Id, "model.group.display_name.app_error")
    79  
    80  	// Won't accept a duplicate name
    81  	g4 := &model.Group{
    82  		Name:        model.NewId(),
    83  		DisplayName: model.NewId(),
    84  		Source:      model.GroupSourceLdap,
    85  		RemoteId:    model.NewId(),
    86  	}
    87  	res5 := <-ss.Group().Create(g4)
    88  	assert.Nil(t, res5.Err)
    89  	g4b := &model.Group{
    90  		Name:        g4.Name,
    91  		DisplayName: model.NewId(),
    92  		Source:      model.GroupSourceLdap,
    93  		RemoteId:    model.NewId(),
    94  	}
    95  	res5b := <-ss.Group().Create(g4b)
    96  	assert.Nil(t, res5b.Data)
    97  	assert.Equal(t, res5b.Err.Id, "store.sql_group.unique_constraint")
    98  
    99  	// Fields cannot be greater than max values
   100  	g5 := &model.Group{
   101  		Name:        strings.Repeat("x", model.GroupNameMaxLength),
   102  		DisplayName: strings.Repeat("x", model.GroupDisplayNameMaxLength),
   103  		Description: strings.Repeat("x", model.GroupDescriptionMaxLength),
   104  		Source:      model.GroupSourceLdap,
   105  		RemoteId:    model.NewId(),
   106  	}
   107  	assert.Nil(t, g5.IsValidForCreate())
   108  
   109  	g5.Name = g5.Name + "x"
   110  	assert.Equal(t, g5.IsValidForCreate().Id, "model.group.name.app_error")
   111  	g5.Name = model.NewId()
   112  	assert.Nil(t, g5.IsValidForCreate())
   113  
   114  	g5.DisplayName = g5.DisplayName + "x"
   115  	assert.Equal(t, g5.IsValidForCreate().Id, "model.group.display_name.app_error")
   116  	g5.DisplayName = model.NewId()
   117  	assert.Nil(t, g5.IsValidForCreate())
   118  
   119  	g5.Description = g5.Description + "x"
   120  	assert.Equal(t, g5.IsValidForCreate().Id, "model.group.description.app_error")
   121  	g5.Description = model.NewId()
   122  	assert.Nil(t, g5.IsValidForCreate())
   123  
   124  	// Must use a valid type
   125  	g6 := &model.Group{
   126  		Name:        model.NewId(),
   127  		DisplayName: model.NewId(),
   128  		Description: model.NewId(),
   129  		Source:      model.GroupSource("fake"),
   130  		RemoteId:    model.NewId(),
   131  	}
   132  	assert.Equal(t, g6.IsValidForCreate().Id, "model.group.source.app_error")
   133  }
   134  
   135  func testGroupStoreGet(t *testing.T, ss store.Store) {
   136  	// Create a group
   137  	g1 := &model.Group{
   138  		Name:        model.NewId(),
   139  		DisplayName: model.NewId(),
   140  		Description: model.NewId(),
   141  		Source:      model.GroupSourceLdap,
   142  		RemoteId:    model.NewId(),
   143  	}
   144  	res1 := <-ss.Group().Create(g1)
   145  	assert.Nil(t, res1.Err)
   146  	d1 := res1.Data.(*model.Group)
   147  	assert.Len(t, d1.Id, 26)
   148  
   149  	// Get the group
   150  	res2 := <-ss.Group().Get(d1.Id)
   151  	assert.Nil(t, res2.Err)
   152  	d2 := res2.Data.(*model.Group)
   153  	assert.Equal(t, d1.Id, d2.Id)
   154  	assert.Equal(t, d1.Name, d2.Name)
   155  	assert.Equal(t, d1.DisplayName, d2.DisplayName)
   156  	assert.Equal(t, d1.Description, d2.Description)
   157  	assert.Equal(t, d1.RemoteId, d2.RemoteId)
   158  	assert.Equal(t, d1.CreateAt, d2.CreateAt)
   159  	assert.Equal(t, d1.UpdateAt, d2.UpdateAt)
   160  	assert.Equal(t, d1.DeleteAt, d2.DeleteAt)
   161  
   162  	// Get an invalid group
   163  	res3 := <-ss.Group().Get(model.NewId())
   164  	assert.NotNil(t, res3.Err)
   165  	assert.Equal(t, res3.Err.Id, "store.sql_group.no_rows")
   166  }
   167  
   168  func testGroupStoreGetByRemoteID(t *testing.T, ss store.Store) {
   169  	// Create a group
   170  	g1 := &model.Group{
   171  		Name:        model.NewId(),
   172  		DisplayName: model.NewId(),
   173  		Description: model.NewId(),
   174  		Source:      model.GroupSourceLdap,
   175  		RemoteId:    model.NewId(),
   176  	}
   177  	res1 := <-ss.Group().Create(g1)
   178  	assert.Nil(t, res1.Err)
   179  	d1 := res1.Data.(*model.Group)
   180  	assert.Len(t, d1.Id, 26)
   181  
   182  	// Get the group
   183  	res2 := <-ss.Group().GetByRemoteID(d1.RemoteId, model.GroupSourceLdap)
   184  	assert.Nil(t, res2.Err)
   185  	d2 := res2.Data.(*model.Group)
   186  	assert.Equal(t, d1.Id, d2.Id)
   187  	assert.Equal(t, d1.Name, d2.Name)
   188  	assert.Equal(t, d1.DisplayName, d2.DisplayName)
   189  	assert.Equal(t, d1.Description, d2.Description)
   190  	assert.Equal(t, d1.RemoteId, d2.RemoteId)
   191  	assert.Equal(t, d1.CreateAt, d2.CreateAt)
   192  	assert.Equal(t, d1.UpdateAt, d2.UpdateAt)
   193  	assert.Equal(t, d1.DeleteAt, d2.DeleteAt)
   194  
   195  	// Get an invalid group
   196  	res3 := <-ss.Group().GetByRemoteID(model.NewId(), model.GroupSource("fake"))
   197  	assert.NotNil(t, res3.Err)
   198  	assert.Equal(t, res3.Err.Id, "store.sql_group.no_rows")
   199  }
   200  
   201  func testGroupStoreGetAllByType(t *testing.T, ss store.Store) {
   202  	numGroups := 10
   203  
   204  	groups := []*model.Group{}
   205  
   206  	// Create groups
   207  	for i := 0; i < numGroups; i++ {
   208  		g := &model.Group{
   209  			Name:        model.NewId(),
   210  			DisplayName: model.NewId(),
   211  			Description: model.NewId(),
   212  			Source:      model.GroupSourceLdap,
   213  			RemoteId:    model.NewId(),
   214  		}
   215  		groups = append(groups, g)
   216  		res := <-ss.Group().Create(g)
   217  		assert.Nil(t, res.Err)
   218  	}
   219  
   220  	// Returns all the groups
   221  	res1 := <-ss.Group().GetAllBySource(model.GroupSourceLdap)
   222  	d1 := res1.Data.([]*model.Group)
   223  	assert.Condition(t, func() bool { return len(d1) >= numGroups })
   224  	for _, expectedGroup := range groups {
   225  		present := false
   226  		for _, dbGroup := range d1 {
   227  			if dbGroup.Id == expectedGroup.Id {
   228  				present = true
   229  				break
   230  			}
   231  		}
   232  		assert.True(t, present)
   233  	}
   234  }
   235  
   236  func testGroupStoreUpdate(t *testing.T, ss store.Store) {
   237  	// Save a new group
   238  	g1 := &model.Group{
   239  		Name:        "g1-test",
   240  		DisplayName: model.NewId(),
   241  		Source:      model.GroupSourceLdap,
   242  		Description: model.NewId(),
   243  		RemoteId:    model.NewId(),
   244  	}
   245  
   246  	// Create a group
   247  	res := <-ss.Group().Create(g1)
   248  	assert.Nil(t, res.Err)
   249  	d1 := res.Data.(*model.Group)
   250  
   251  	// Update happy path
   252  	g1Update := &model.Group{}
   253  	*g1Update = *g1
   254  	g1Update.Name = model.NewId()
   255  	g1Update.DisplayName = model.NewId()
   256  	g1Update.Description = model.NewId()
   257  	g1Update.RemoteId = model.NewId()
   258  
   259  	res2 := <-ss.Group().Update(g1Update)
   260  	assert.Nil(t, res2.Err)
   261  	ud1 := res2.Data.(*model.Group)
   262  	// Not changed...
   263  	assert.Equal(t, d1.Id, ud1.Id)
   264  	assert.Equal(t, d1.CreateAt, ud1.CreateAt)
   265  	assert.Equal(t, d1.Source, ud1.Source)
   266  	// Still zero...
   267  	assert.Zero(t, ud1.DeleteAt)
   268  	// Updated...
   269  	assert.Equal(t, g1Update.Name, ud1.Name)
   270  	assert.Equal(t, g1Update.DisplayName, ud1.DisplayName)
   271  	assert.Equal(t, g1Update.Description, ud1.Description)
   272  	assert.Equal(t, g1Update.RemoteId, ud1.RemoteId)
   273  
   274  	// Requires name and display name
   275  	res3 := <-ss.Group().Update(&model.Group{
   276  		Id:          d1.Id,
   277  		Name:        "",
   278  		DisplayName: model.NewId(),
   279  		Source:      model.GroupSourceLdap,
   280  		RemoteId:    model.NewId(),
   281  		Description: model.NewId(),
   282  	})
   283  	assert.Nil(t, res3.Data)
   284  	assert.NotNil(t, res3.Err)
   285  	assert.Equal(t, res3.Err.Id, "model.group.name.app_error")
   286  
   287  	res4 := <-ss.Group().Update(&model.Group{
   288  		Id:          d1.Id,
   289  		Name:        model.NewId(),
   290  		DisplayName: "",
   291  		Source:      model.GroupSourceLdap,
   292  		RemoteId:    model.NewId(),
   293  	})
   294  	assert.Nil(t, res4.Data)
   295  	assert.NotNil(t, res4.Err)
   296  	assert.Equal(t, res4.Err.Id, "model.group.display_name.app_error")
   297  
   298  	// Create another Group
   299  	g2 := &model.Group{
   300  		Name:        model.NewId(),
   301  		DisplayName: model.NewId(),
   302  		Source:      model.GroupSourceLdap,
   303  		Description: model.NewId(),
   304  		RemoteId:    model.NewId(),
   305  	}
   306  	res5 := <-ss.Group().Create(g2)
   307  	assert.Nil(t, res5.Err)
   308  	d2 := res5.Data.(*model.Group)
   309  
   310  	// Can't update the name to be a duplicate of an existing group's name
   311  	res6 := <-ss.Group().Update(&model.Group{
   312  		Id:          d2.Id,
   313  		Name:        g1Update.Name,
   314  		DisplayName: model.NewId(),
   315  		Source:      model.GroupSourceLdap,
   316  		Description: model.NewId(),
   317  		RemoteId:    model.NewId(),
   318  	})
   319  	assert.Equal(t, res6.Err.Id, "store.update_error")
   320  
   321  	// Cannot update CreateAt
   322  	someVal := model.GetMillis()
   323  	d1.CreateAt = someVal
   324  	res7 := <-ss.Group().Update(d1)
   325  	d3 := res7.Data.(*model.Group)
   326  	assert.NotEqual(t, someVal, d3.CreateAt)
   327  
   328  	// Cannot update DeleteAt to non-zero
   329  	d1.DeleteAt = 1
   330  	res9 := <-ss.Group().Update(d1)
   331  	assert.Equal(t, "model.group.delete_at.app_error", res9.Err.Id)
   332  
   333  	//...except for 0 for DeleteAt
   334  	d1.DeleteAt = 0
   335  	res8 := <-ss.Group().Update(d1)
   336  	assert.Nil(t, res8.Err)
   337  	d4 := res8.Data.(*model.Group)
   338  	assert.Zero(t, d4.DeleteAt)
   339  }
   340  
   341  func testGroupStoreDelete(t *testing.T, ss store.Store) {
   342  	// Save a group
   343  	g1 := &model.Group{
   344  		Name:        model.NewId(),
   345  		DisplayName: model.NewId(),
   346  		Description: model.NewId(),
   347  		Source:      model.GroupSourceLdap,
   348  		RemoteId:    model.NewId(),
   349  	}
   350  
   351  	res1 := <-ss.Group().Create(g1)
   352  	assert.Nil(t, res1.Err)
   353  	d1 := res1.Data.(*model.Group)
   354  	assert.Len(t, d1.Id, 26)
   355  
   356  	// Check the group is retrievable
   357  	res2 := <-ss.Group().Get(d1.Id)
   358  	assert.Nil(t, res2.Err)
   359  
   360  	// Get the before count
   361  	res7 := <-ss.Group().GetAllBySource(model.GroupSourceLdap)
   362  	d7 := res7.Data.([]*model.Group)
   363  	beforeCount := len(d7)
   364  
   365  	// Delete the group
   366  	res3 := <-ss.Group().Delete(d1.Id)
   367  	assert.Nil(t, res3.Err)
   368  
   369  	// Check the group is deleted
   370  	res4 := <-ss.Group().Get(d1.Id)
   371  	d4 := res4.Data.(*model.Group)
   372  	assert.NotZero(t, d4.DeleteAt)
   373  
   374  	// Check the after count
   375  	res5 := <-ss.Group().GetAllBySource(model.GroupSourceLdap)
   376  	d5 := res5.Data.([]*model.Group)
   377  	afterCount := len(d5)
   378  	assert.Condition(t, func() bool { return beforeCount == afterCount+1 })
   379  
   380  	// Try and delete a nonexistent group
   381  	res6 := <-ss.Group().Delete(model.NewId())
   382  	assert.NotNil(t, res6.Err)
   383  	assert.Equal(t, res6.Err.Id, "store.sql_group.no_rows")
   384  
   385  	// Cannot delete again
   386  	res8 := <-ss.Group().Delete(d1.Id)
   387  	assert.Equal(t, res8.Err.Id, "store.sql_group.no_rows")
   388  }
   389  
   390  func testGroupGetMemberUsers(t *testing.T, ss store.Store) {
   391  	// Save a group
   392  	g1 := &model.Group{
   393  		Name:        model.NewId(),
   394  		DisplayName: model.NewId(),
   395  		Description: model.NewId(),
   396  		Source:      model.GroupSourceLdap,
   397  		RemoteId:    model.NewId(),
   398  	}
   399  	res := <-ss.Group().Create(g1)
   400  	assert.Nil(t, res.Err)
   401  	group := res.Data.(*model.Group)
   402  
   403  	u1 := &model.User{
   404  		Email:    MakeEmail(),
   405  		Username: model.NewId(),
   406  	}
   407  	res = <-ss.User().Save(u1)
   408  	assert.Nil(t, res.Err)
   409  	user1 := res.Data.(*model.User)
   410  
   411  	res = <-ss.Group().CreateOrRestoreMember(group.Id, user1.Id)
   412  	assert.Nil(t, res.Err)
   413  
   414  	u2 := &model.User{
   415  		Email:    MakeEmail(),
   416  		Username: model.NewId(),
   417  	}
   418  	res = <-ss.User().Save(u2)
   419  	assert.Nil(t, res.Err)
   420  	user2 := res.Data.(*model.User)
   421  
   422  	res = <-ss.Group().CreateOrRestoreMember(group.Id, user2.Id)
   423  	assert.Nil(t, res.Err)
   424  
   425  	// Check returns members
   426  	res = <-ss.Group().GetMemberUsers(group.Id)
   427  	assert.Nil(t, res.Err)
   428  	groupMembers := res.Data.([]*model.User)
   429  	assert.Equal(t, 2, len(groupMembers))
   430  
   431  	// Check madeup id
   432  	res = <-ss.Group().GetMemberUsers(model.NewId())
   433  	assert.Equal(t, 0, len(res.Data.([]*model.User)))
   434  
   435  	// Delete a member
   436  	<-ss.Group().DeleteMember(group.Id, user1.Id)
   437  
   438  	// Should not return deleted members
   439  	res = <-ss.Group().GetMemberUsers(group.Id)
   440  	groupMembers = res.Data.([]*model.User)
   441  	assert.Equal(t, 1, len(groupMembers))
   442  }
   443  
   444  func testGroupGetMemberUsersPage(t *testing.T, ss store.Store) {
   445  	// Save a group
   446  	g1 := &model.Group{
   447  		Name:        model.NewId(),
   448  		DisplayName: model.NewId(),
   449  		Description: model.NewId(),
   450  		Source:      model.GroupSourceLdap,
   451  		RemoteId:    model.NewId(),
   452  	}
   453  	res := <-ss.Group().Create(g1)
   454  	assert.Nil(t, res.Err)
   455  	group := res.Data.(*model.Group)
   456  
   457  	u1 := &model.User{
   458  		Email:    MakeEmail(),
   459  		Username: model.NewId(),
   460  	}
   461  	res = <-ss.User().Save(u1)
   462  	assert.Nil(t, res.Err)
   463  	user1 := res.Data.(*model.User)
   464  
   465  	res = <-ss.Group().CreateOrRestoreMember(group.Id, user1.Id)
   466  	assert.Nil(t, res.Err)
   467  
   468  	u2 := &model.User{
   469  		Email:    MakeEmail(),
   470  		Username: model.NewId(),
   471  	}
   472  	res = <-ss.User().Save(u2)
   473  	assert.Nil(t, res.Err)
   474  	user2 := res.Data.(*model.User)
   475  
   476  	res = <-ss.Group().CreateOrRestoreMember(group.Id, user2.Id)
   477  	assert.Nil(t, res.Err)
   478  
   479  	// Check returns members
   480  	res = <-ss.Group().GetMemberUsersPage(group.Id, 0, 100)
   481  	assert.Nil(t, res.Err)
   482  	groupMembers := res.Data.([]*model.User)
   483  	assert.Equal(t, 2, len(groupMembers))
   484  
   485  	// Check page 1
   486  	res = <-ss.Group().GetMemberUsersPage(group.Id, 0, 1)
   487  	assert.Nil(t, res.Err)
   488  	groupMembers = res.Data.([]*model.User)
   489  	assert.Equal(t, 1, len(groupMembers))
   490  	assert.Equal(t, user2.Id, groupMembers[0].Id)
   491  
   492  	// Check page 2
   493  	res = <-ss.Group().GetMemberUsersPage(group.Id, 1, 1)
   494  	assert.Nil(t, res.Err)
   495  	groupMembers = res.Data.([]*model.User)
   496  	assert.Equal(t, 1, len(groupMembers))
   497  	assert.Equal(t, user1.Id, groupMembers[0].Id)
   498  
   499  	// Check madeup id
   500  	res = <-ss.Group().GetMemberUsersPage(model.NewId(), 0, 100)
   501  	assert.Equal(t, 0, len(res.Data.([]*model.User)))
   502  
   503  	// Delete a member
   504  	<-ss.Group().DeleteMember(group.Id, user1.Id)
   505  
   506  	// Should not return deleted members
   507  	res = <-ss.Group().GetMemberUsersPage(group.Id, 0, 100)
   508  	groupMembers = res.Data.([]*model.User)
   509  	assert.Equal(t, 1, len(groupMembers))
   510  }
   511  
   512  func testGroupCreateOrRestoreMember(t *testing.T, ss store.Store) {
   513  	// Create group
   514  	g1 := &model.Group{
   515  		Name:        model.NewId(),
   516  		DisplayName: model.NewId(),
   517  		Source:      model.GroupSourceLdap,
   518  		RemoteId:    model.NewId(),
   519  	}
   520  	res1 := <-ss.Group().Create(g1)
   521  	assert.Nil(t, res1.Err)
   522  	group := res1.Data.(*model.Group)
   523  
   524  	// Create user
   525  	u1 := &model.User{
   526  		Email:    MakeEmail(),
   527  		Username: model.NewId(),
   528  	}
   529  	res2 := <-ss.User().Save(u1)
   530  	assert.Nil(t, res2.Err)
   531  	user := res2.Data.(*model.User)
   532  
   533  	// Happy path
   534  	res3 := <-ss.Group().CreateOrRestoreMember(group.Id, user.Id)
   535  	assert.Nil(t, res3.Err)
   536  	d2 := res3.Data.(*model.GroupMember)
   537  	assert.Equal(t, d2.GroupId, group.Id)
   538  	assert.Equal(t, d2.UserId, user.Id)
   539  	assert.NotZero(t, d2.CreateAt)
   540  	assert.Zero(t, d2.DeleteAt)
   541  
   542  	// Duplicate composite key (GroupId, UserId)
   543  	res4 := <-ss.Group().CreateOrRestoreMember(group.Id, user.Id)
   544  	assert.Equal(t, res4.Err.Id, "store.sql_group.uniqueness_error")
   545  
   546  	// Invalid GroupId
   547  	res6 := <-ss.Group().CreateOrRestoreMember(model.NewId(), user.Id)
   548  	assert.Equal(t, res6.Err.Id, "store.insert_error")
   549  
   550  	// Restores a deleted member
   551  	res := <-ss.Group().CreateOrRestoreMember(group.Id, user.Id)
   552  	assert.NotNil(t, res.Err)
   553  
   554  	res = <-ss.Group().DeleteMember(group.Id, user.Id)
   555  	assert.Nil(t, res.Err)
   556  
   557  	res = <-ss.Group().GetMemberUsers(group.Id)
   558  	beforeRestoreCount := len(res.Data.([]*model.User))
   559  
   560  	res = <-ss.Group().CreateOrRestoreMember(group.Id, user.Id)
   561  	assert.Nil(t, res.Err)
   562  
   563  	res = <-ss.Group().GetMemberUsers(group.Id)
   564  	afterRestoreCount := len(res.Data.([]*model.User))
   565  
   566  	assert.Equal(t, beforeRestoreCount+1, afterRestoreCount)
   567  }
   568  
   569  func testGroupDeleteMember(t *testing.T, ss store.Store) {
   570  	// Create group
   571  	g1 := &model.Group{
   572  		Name:        model.NewId(),
   573  		DisplayName: model.NewId(),
   574  		Source:      model.GroupSourceLdap,
   575  		RemoteId:    model.NewId(),
   576  	}
   577  	res1 := <-ss.Group().Create(g1)
   578  	assert.Nil(t, res1.Err)
   579  	group := res1.Data.(*model.Group)
   580  
   581  	// Create user
   582  	u1 := &model.User{
   583  		Email:    MakeEmail(),
   584  		Username: model.NewId(),
   585  	}
   586  	res2 := <-ss.User().Save(u1)
   587  	assert.Nil(t, res2.Err)
   588  	user := res2.Data.(*model.User)
   589  
   590  	// Create member
   591  	res3 := <-ss.Group().CreateOrRestoreMember(group.Id, user.Id)
   592  	assert.Nil(t, res3.Err)
   593  	d1 := res3.Data.(*model.GroupMember)
   594  
   595  	// Happy path
   596  	res4 := <-ss.Group().DeleteMember(group.Id, user.Id)
   597  	assert.Nil(t, res4.Err)
   598  	d2 := res4.Data.(*model.GroupMember)
   599  	assert.Equal(t, d2.GroupId, group.Id)
   600  	assert.Equal(t, d2.UserId, user.Id)
   601  	assert.Equal(t, d2.CreateAt, d1.CreateAt)
   602  	assert.NotZero(t, d2.DeleteAt)
   603  
   604  	// Delete an already deleted member
   605  	res5 := <-ss.Group().DeleteMember(group.Id, user.Id)
   606  	assert.Equal(t, res5.Err.Id, "store.sql_group.no_rows")
   607  
   608  	// Delete with non-existent User
   609  	res8 := <-ss.Group().DeleteMember(group.Id, model.NewId())
   610  	assert.Equal(t, res8.Err.Id, "store.sql_group.no_rows")
   611  
   612  	// Delete non-existent Group
   613  	res9 := <-ss.Group().DeleteMember(model.NewId(), group.Id)
   614  	assert.Equal(t, res9.Err.Id, "store.sql_group.no_rows")
   615  }
   616  
   617  func testCreateGroupSyncable(t *testing.T, ss store.Store) {
   618  	// Invalid GroupID
   619  	res2 := <-ss.Group().CreateGroupSyncable(&model.GroupSyncable{
   620  		GroupId:    "x",
   621  		SyncableId: string(model.NewId()),
   622  		Type:       model.GroupSyncableTypeTeam,
   623  	})
   624  	assert.Equal(t, res2.Err.Id, "model.group_syncable.group_id.app_error")
   625  
   626  	// Create Group
   627  	g1 := &model.Group{
   628  		Name:        model.NewId(),
   629  		DisplayName: model.NewId(),
   630  		Source:      model.GroupSourceLdap,
   631  		RemoteId:    model.NewId(),
   632  	}
   633  	res4 := <-ss.Group().Create(g1)
   634  	assert.Nil(t, res4.Err)
   635  	group := res4.Data.(*model.Group)
   636  
   637  	// Create Team
   638  	t1 := &model.Team{
   639  		DisplayName:     "Name",
   640  		Description:     "Some description",
   641  		CompanyName:     "Some company name",
   642  		AllowOpenInvite: false,
   643  		InviteId:        "inviteid0",
   644  		Name:            "z-z-" + model.NewId() + "a",
   645  		Email:           "success+" + model.NewId() + "@simulator.amazonses.com",
   646  		Type:            model.TEAM_OPEN,
   647  	}
   648  	res5 := <-ss.Team().Save(t1)
   649  	assert.Nil(t, res5.Err)
   650  	team := res5.Data.(*model.Team)
   651  
   652  	// New GroupSyncable, happy path
   653  	gt1 := &model.GroupSyncable{
   654  		GroupId:    group.Id,
   655  		AutoAdd:    false,
   656  		SyncableId: string(team.Id),
   657  		Type:       model.GroupSyncableTypeTeam,
   658  	}
   659  	res6 := <-ss.Group().CreateGroupSyncable(gt1)
   660  	assert.Nil(t, res6.Err)
   661  	d1 := res6.Data.(*model.GroupSyncable)
   662  	assert.Equal(t, gt1.SyncableId, d1.SyncableId)
   663  	assert.Equal(t, gt1.GroupId, d1.GroupId)
   664  	assert.Equal(t, gt1.AutoAdd, d1.AutoAdd)
   665  	assert.NotZero(t, d1.CreateAt)
   666  	assert.Zero(t, d1.DeleteAt)
   667  }
   668  
   669  func testGetGroupSyncable(t *testing.T, ss store.Store) {
   670  	// Create a group
   671  	g1 := &model.Group{
   672  		Name:        model.NewId(),
   673  		DisplayName: model.NewId(),
   674  		Description: model.NewId(),
   675  		Source:      model.GroupSourceLdap,
   676  		RemoteId:    model.NewId(),
   677  	}
   678  	res1 := <-ss.Group().Create(g1)
   679  	assert.Nil(t, res1.Err)
   680  	group := res1.Data.(*model.Group)
   681  
   682  	// Create Team
   683  	t1 := &model.Team{
   684  		DisplayName:     "Name",
   685  		Description:     "Some description",
   686  		CompanyName:     "Some company name",
   687  		AllowOpenInvite: false,
   688  		InviteId:        "inviteid0",
   689  		Name:            "z-z-" + model.NewId() + "a",
   690  		Email:           "success+" + model.NewId() + "@simulator.amazonses.com",
   691  		Type:            model.TEAM_OPEN,
   692  	}
   693  	res2 := <-ss.Team().Save(t1)
   694  	assert.Nil(t, res2.Err)
   695  	team := res2.Data.(*model.Team)
   696  
   697  	// Create GroupSyncable
   698  	gt1 := &model.GroupSyncable{
   699  		GroupId:    group.Id,
   700  		AutoAdd:    false,
   701  		SyncableId: string(team.Id),
   702  		Type:       model.GroupSyncableTypeTeam,
   703  	}
   704  	res3 := <-ss.Group().CreateGroupSyncable(gt1)
   705  	assert.Nil(t, res3.Err)
   706  	groupTeam := res3.Data.(*model.GroupSyncable)
   707  
   708  	// Get GroupSyncable
   709  	res4 := <-ss.Group().GetGroupSyncable(groupTeam.GroupId, groupTeam.SyncableId, model.GroupSyncableTypeTeam)
   710  	assert.Nil(t, res4.Err)
   711  	dgt := res4.Data.(*model.GroupSyncable)
   712  	assert.Equal(t, gt1.GroupId, dgt.GroupId)
   713  	assert.Equal(t, gt1.SyncableId, dgt.SyncableId)
   714  	assert.Equal(t, gt1.AutoAdd, dgt.AutoAdd)
   715  	assert.NotZero(t, gt1.CreateAt)
   716  	assert.NotZero(t, gt1.UpdateAt)
   717  	assert.Zero(t, gt1.DeleteAt)
   718  }
   719  
   720  func testGetAllGroupSyncablesByGroup(t *testing.T, ss store.Store) {
   721  	numGroupSyncables := 10
   722  
   723  	// Create group
   724  	g := &model.Group{
   725  		Name:        model.NewId(),
   726  		DisplayName: model.NewId(),
   727  		Description: model.NewId(),
   728  		Source:      model.GroupSourceLdap,
   729  		RemoteId:    model.NewId(),
   730  	}
   731  	res1 := <-ss.Group().Create(g)
   732  	assert.Nil(t, res1.Err)
   733  	group := res1.Data.(*model.Group)
   734  
   735  	groupTeams := []*model.GroupSyncable{}
   736  
   737  	// Create groupTeams
   738  	for i := 0; i < numGroupSyncables; i++ {
   739  		// Create Team
   740  		t1 := &model.Team{
   741  			DisplayName:     "Name",
   742  			Description:     "Some description",
   743  			CompanyName:     "Some company name",
   744  			AllowOpenInvite: false,
   745  			InviteId:        "inviteid0",
   746  			Name:            "z-z-" + model.NewId() + "a",
   747  			Email:           "success+" + model.NewId() + "@simulator.amazonses.com",
   748  			Type:            model.TEAM_OPEN,
   749  		}
   750  		res2 := <-ss.Team().Save(t1)
   751  		assert.Nil(t, res2.Err)
   752  		team := res2.Data.(*model.Team)
   753  
   754  		// create groupteam
   755  		res3 := <-ss.Group().CreateGroupSyncable(&model.GroupSyncable{
   756  			GroupId:    group.Id,
   757  			SyncableId: string(team.Id),
   758  			Type:       model.GroupSyncableTypeTeam,
   759  		})
   760  		assert.Nil(t, res3.Err)
   761  		groupTeam := res3.Data.(*model.GroupSyncable)
   762  		groupTeams = append(groupTeams, groupTeam)
   763  	}
   764  
   765  	// Returns all the group teams
   766  	res4 := <-ss.Group().GetAllGroupSyncablesByGroupId(group.Id, model.GroupSyncableTypeTeam)
   767  	d1 := res4.Data.([]*model.GroupSyncable)
   768  	assert.Condition(t, func() bool { return len(d1) >= numGroupSyncables })
   769  	for _, expectedGroupTeam := range groupTeams {
   770  		present := false
   771  		for _, dbGroupTeam := range d1 {
   772  			if dbGroupTeam.GroupId == expectedGroupTeam.GroupId && dbGroupTeam.SyncableId == expectedGroupTeam.SyncableId {
   773  				present = true
   774  				break
   775  			}
   776  		}
   777  		assert.True(t, present)
   778  	}
   779  }
   780  
   781  func testUpdateGroupSyncable(t *testing.T, ss store.Store) {
   782  	// Create Group
   783  	g1 := &model.Group{
   784  		Name:        model.NewId(),
   785  		DisplayName: model.NewId(),
   786  		Source:      model.GroupSourceLdap,
   787  		RemoteId:    model.NewId(),
   788  	}
   789  	res4 := <-ss.Group().Create(g1)
   790  	assert.Nil(t, res4.Err)
   791  	group := res4.Data.(*model.Group)
   792  
   793  	// Create Team
   794  	t1 := &model.Team{
   795  		DisplayName:     "Name",
   796  		Description:     "Some description",
   797  		CompanyName:     "Some company name",
   798  		AllowOpenInvite: false,
   799  		InviteId:        "inviteid0",
   800  		Name:            "z-z-" + model.NewId() + "a",
   801  		Email:           "success+" + model.NewId() + "@simulator.amazonses.com",
   802  		Type:            model.TEAM_OPEN,
   803  	}
   804  	res5 := <-ss.Team().Save(t1)
   805  	assert.Nil(t, res5.Err)
   806  	team := res5.Data.(*model.Team)
   807  
   808  	// New GroupSyncable, happy path
   809  	gt1 := &model.GroupSyncable{
   810  		GroupId:    group.Id,
   811  		AutoAdd:    false,
   812  		SyncableId: string(team.Id),
   813  		Type:       model.GroupSyncableTypeTeam,
   814  	}
   815  	res6 := <-ss.Group().CreateGroupSyncable(gt1)
   816  	assert.Nil(t, res6.Err)
   817  	d1 := res6.Data.(*model.GroupSyncable)
   818  
   819  	// Update existing group team
   820  	gt1.AutoAdd = true
   821  	res7 := <-ss.Group().UpdateGroupSyncable(gt1)
   822  	assert.Nil(t, res7.Err)
   823  	d2 := res7.Data.(*model.GroupSyncable)
   824  	assert.True(t, d2.AutoAdd)
   825  
   826  	// Non-existent Group
   827  	gt2 := &model.GroupSyncable{
   828  		GroupId:    model.NewId(),
   829  		AutoAdd:    false,
   830  		SyncableId: string(team.Id),
   831  		Type:       model.GroupSyncableTypeTeam,
   832  	}
   833  	res9 := <-ss.Group().UpdateGroupSyncable(gt2)
   834  	assert.Equal(t, res9.Err.Id, "store.sql_group.no_rows")
   835  
   836  	// Non-existent Team
   837  	gt3 := &model.GroupSyncable{
   838  		GroupId:    group.Id,
   839  		AutoAdd:    false,
   840  		SyncableId: string(model.NewId()),
   841  		Type:       model.GroupSyncableTypeTeam,
   842  	}
   843  	res10 := <-ss.Group().UpdateGroupSyncable(gt3)
   844  	assert.Equal(t, res10.Err.Id, "store.sql_group.no_rows")
   845  
   846  	// Cannot update CreateAt or DeleteAt
   847  	origCreateAt := d1.CreateAt
   848  	d1.CreateAt = model.GetMillis()
   849  	d1.AutoAdd = true
   850  	res11 := <-ss.Group().UpdateGroupSyncable(d1)
   851  	assert.Nil(t, res11.Err)
   852  	d3 := res11.Data.(*model.GroupSyncable)
   853  	assert.Equal(t, origCreateAt, d3.CreateAt)
   854  
   855  	// Cannot update DeleteAt to arbitrary value
   856  	d1.DeleteAt = 1
   857  	res12 := <-ss.Group().UpdateGroupSyncable(d1)
   858  	assert.Equal(t, "model.group.delete_at.app_error", res12.Err.Id)
   859  
   860  	// Can update DeleteAt to 0
   861  	d1.DeleteAt = 0
   862  	res13 := <-ss.Group().UpdateGroupSyncable(d1)
   863  	assert.Nil(t, res13.Err)
   864  	d4 := res13.Data.(*model.GroupSyncable)
   865  	assert.Zero(t, d4.DeleteAt)
   866  }
   867  
   868  func testDeleteGroupSyncable(t *testing.T, ss store.Store) {
   869  	// Create Group
   870  	g1 := &model.Group{
   871  		Name:        model.NewId(),
   872  		DisplayName: model.NewId(),
   873  		Source:      model.GroupSourceLdap,
   874  		RemoteId:    model.NewId(),
   875  	}
   876  	res1 := <-ss.Group().Create(g1)
   877  	assert.Nil(t, res1.Err)
   878  	group := res1.Data.(*model.Group)
   879  
   880  	// Create Team
   881  	t1 := &model.Team{
   882  		DisplayName:     "Name",
   883  		Description:     "Some description",
   884  		CompanyName:     "Some company name",
   885  		AllowOpenInvite: false,
   886  		InviteId:        "inviteid0",
   887  		Name:            "z-z-" + model.NewId() + "a",
   888  		Email:           "success+" + model.NewId() + "@simulator.amazonses.com",
   889  		Type:            model.TEAM_OPEN,
   890  	}
   891  	res2 := <-ss.Team().Save(t1)
   892  	assert.Nil(t, res2.Err)
   893  	team := res2.Data.(*model.Team)
   894  
   895  	// Create GroupSyncable
   896  	gt1 := &model.GroupSyncable{
   897  		GroupId:    group.Id,
   898  		AutoAdd:    false,
   899  		SyncableId: string(team.Id),
   900  		Type:       model.GroupSyncableTypeTeam,
   901  	}
   902  	res7 := <-ss.Group().CreateGroupSyncable(gt1)
   903  	assert.Nil(t, res7.Err)
   904  	groupTeam := res7.Data.(*model.GroupSyncable)
   905  
   906  	// Non-existent Group
   907  	res5 := <-ss.Group().DeleteGroupSyncable(model.NewId(), groupTeam.SyncableId, model.GroupSyncableTypeTeam)
   908  	assert.Equal(t, res5.Err.Id, "store.sql_group.no_rows")
   909  
   910  	// Non-existent Team
   911  	res6 := <-ss.Group().DeleteGroupSyncable(groupTeam.GroupId, string(model.NewId()), model.GroupSyncableTypeTeam)
   912  	assert.Equal(t, res6.Err.Id, "store.sql_group.no_rows")
   913  
   914  	// Happy path...
   915  	res8 := <-ss.Group().DeleteGroupSyncable(groupTeam.GroupId, groupTeam.SyncableId, model.GroupSyncableTypeTeam)
   916  	assert.Nil(t, res8.Err)
   917  	d1 := res8.Data.(*model.GroupSyncable)
   918  	assert.NotZero(t, d1.DeleteAt)
   919  	assert.Equal(t, d1.GroupId, groupTeam.GroupId)
   920  	assert.Equal(t, d1.SyncableId, groupTeam.SyncableId)
   921  	assert.Equal(t, d1.AutoAdd, groupTeam.AutoAdd)
   922  	assert.Equal(t, d1.CreateAt, groupTeam.CreateAt)
   923  	assert.Condition(t, func() bool { return d1.UpdateAt > groupTeam.UpdateAt })
   924  
   925  	// Record already deleted
   926  	res9 := <-ss.Group().DeleteGroupSyncable(d1.GroupId, d1.SyncableId, d1.Type)
   927  	assert.NotNil(t, res9.Err)
   928  	assert.Equal(t, res9.Err.Id, "store.sql_group.group_syncable_already_deleted")
   929  }
   930  
   931  func testPendingAutoAddTeamMembers(t *testing.T, ss store.Store) {
   932  	// Create Group
   933  	res := <-ss.Group().Create(&model.Group{
   934  		Name:        model.NewId(),
   935  		DisplayName: "PendingAutoAddTeamMembers Test Group",
   936  		RemoteId:    model.NewId(),
   937  		Source:      model.GroupSourceLdap,
   938  	})
   939  	assert.Nil(t, res.Err)
   940  	group := res.Data.(*model.Group)
   941  
   942  	// Create User
   943  	user := &model.User{
   944  		Email:    MakeEmail(),
   945  		Username: model.NewId(),
   946  	}
   947  	res = <-ss.User().Save(user)
   948  	assert.Nil(t, res.Err)
   949  	user = res.Data.(*model.User)
   950  
   951  	// Create GroupMember
   952  	res = <-ss.Group().CreateOrRestoreMember(group.Id, user.Id)
   953  	assert.Nil(t, res.Err)
   954  
   955  	// Create Team
   956  	team := &model.Team{
   957  		DisplayName:     "Name",
   958  		Description:     "Some description",
   959  		CompanyName:     "Some company name",
   960  		AllowOpenInvite: false,
   961  		InviteId:        "inviteid0",
   962  		Name:            "z-z-" + model.NewId() + "a",
   963  		Email:           "success+" + model.NewId() + "@simulator.amazonses.com",
   964  		Type:            model.TEAM_OPEN,
   965  	}
   966  	res = <-ss.Team().Save(team)
   967  	assert.Nil(t, res.Err)
   968  	team = res.Data.(*model.Team)
   969  
   970  	// Create GroupTeam
   971  	res = <-ss.Group().CreateGroupSyncable(&model.GroupSyncable{
   972  		AutoAdd:    true,
   973  		SyncableId: team.Id,
   974  		Type:       model.GroupSyncableTypeTeam,
   975  		GroupId:    group.Id,
   976  	})
   977  	assert.Nil(t, res.Err)
   978  	syncable := res.Data.(*model.GroupSyncable)
   979  
   980  	// Time before syncable was created
   981  	res = <-ss.Group().PendingAutoAddTeamMembers(syncable.CreateAt - 1)
   982  	assert.Nil(t, res.Err)
   983  	userTeamIDs := res.Data.([]*model.UserTeamIDPair)
   984  	assert.Len(t, userTeamIDs, 1)
   985  	assert.Equal(t, user.Id, userTeamIDs[0].UserID)
   986  	assert.Equal(t, team.Id, userTeamIDs[0].TeamID)
   987  
   988  	// Time after syncable was created
   989  	res = <-ss.Group().PendingAutoAddTeamMembers(syncable.CreateAt + 1)
   990  	assert.Nil(t, res.Err)
   991  	assert.Len(t, res.Data, 0)
   992  
   993  	// Delete and restore GroupMember should return result
   994  	res = <-ss.Group().DeleteMember(group.Id, user.Id)
   995  	assert.Nil(t, res.Err)
   996  	res = <-ss.Group().CreateOrRestoreMember(group.Id, user.Id)
   997  	assert.Nil(t, res.Err)
   998  	res = <-ss.Group().PendingAutoAddTeamMembers(syncable.CreateAt + 1)
   999  	assert.Nil(t, res.Err)
  1000  	assert.Len(t, res.Data, 1)
  1001  
  1002  	pristineSyncable := *syncable
  1003  
  1004  	res = <-ss.Group().UpdateGroupSyncable(syncable)
  1005  	assert.Nil(t, res.Err)
  1006  
  1007  	// Time before syncable was updated
  1008  	res = <-ss.Group().PendingAutoAddTeamMembers(syncable.UpdateAt - 1)
  1009  	assert.Nil(t, res.Err)
  1010  	userTeamIDs = res.Data.([]*model.UserTeamIDPair)
  1011  	assert.Len(t, userTeamIDs, 1)
  1012  	assert.Equal(t, user.Id, userTeamIDs[0].UserID)
  1013  	assert.Equal(t, team.Id, userTeamIDs[0].TeamID)
  1014  
  1015  	// Time after syncable was updated
  1016  	res = <-ss.Group().PendingAutoAddTeamMembers(syncable.UpdateAt + 1)
  1017  	assert.Nil(t, res.Err)
  1018  	assert.Len(t, res.Data, 0)
  1019  
  1020  	// Only includes if auto-add
  1021  	syncable.AutoAdd = false
  1022  	res = <-ss.Group().UpdateGroupSyncable(syncable)
  1023  	assert.Nil(t, res.Err)
  1024  	res = <-ss.Group().PendingAutoAddTeamMembers(0)
  1025  	assert.Nil(t, res.Err)
  1026  	assert.Len(t, res.Data, 0)
  1027  
  1028  	// reset state of syncable and verify
  1029  	res = <-ss.Group().UpdateGroupSyncable(&pristineSyncable)
  1030  	assert.Nil(t, res.Err)
  1031  	res = <-ss.Group().PendingAutoAddTeamMembers(0)
  1032  	assert.Nil(t, res.Err)
  1033  	assert.Len(t, res.Data, 1)
  1034  
  1035  	// No result if Group deleted
  1036  	res = <-ss.Group().Delete(group.Id)
  1037  	assert.Nil(t, res.Err)
  1038  	res = <-ss.Group().PendingAutoAddTeamMembers(0)
  1039  	assert.Nil(t, res.Err)
  1040  	assert.Len(t, res.Data, 0)
  1041  
  1042  	// reset state of group and verify
  1043  	group.DeleteAt = 0
  1044  	res = <-ss.Group().Update(group)
  1045  	res = <-ss.Group().PendingAutoAddTeamMembers(0)
  1046  	assert.Nil(t, res.Err)
  1047  	assert.Len(t, res.Data, 1)
  1048  
  1049  	// No result if Team deleted
  1050  	team.DeleteAt = model.GetMillis()
  1051  	res = <-ss.Team().Update(team)
  1052  	assert.Nil(t, res.Err)
  1053  	res = <-ss.Group().PendingAutoAddTeamMembers(0)
  1054  	assert.Nil(t, res.Err)
  1055  	assert.Len(t, res.Data, 0)
  1056  
  1057  	// reset state of team and verify
  1058  	team.DeleteAt = 0
  1059  	res = <-ss.Team().Update(team)
  1060  	assert.Nil(t, res.Err)
  1061  	res = <-ss.Group().PendingAutoAddTeamMembers(0)
  1062  	assert.Nil(t, res.Err)
  1063  	assert.Len(t, res.Data, 1)
  1064  
  1065  	// No result if GroupTeam deleted
  1066  	res = <-ss.Group().DeleteGroupSyncable(group.Id, team.Id, model.GroupSyncableTypeTeam)
  1067  	assert.Nil(t, res.Err)
  1068  	res = <-ss.Group().PendingAutoAddTeamMembers(0)
  1069  	assert.Nil(t, res.Err)
  1070  	assert.Len(t, res.Data, 0)
  1071  
  1072  	// reset GroupTeam and verify
  1073  	res = <-ss.Group().UpdateGroupSyncable(&pristineSyncable)
  1074  	assert.Nil(t, res.Err)
  1075  	res = <-ss.Group().PendingAutoAddTeamMembers(0)
  1076  	assert.Nil(t, res.Err)
  1077  	assert.Len(t, res.Data, 1)
  1078  
  1079  	// No result if GroupMember deleted
  1080  	res = <-ss.Group().DeleteMember(group.Id, user.Id)
  1081  	assert.Nil(t, res.Err)
  1082  	res = <-ss.Group().PendingAutoAddTeamMembers(0)
  1083  	assert.Nil(t, res.Err)
  1084  	assert.Len(t, res.Data, 0)
  1085  
  1086  	// restore group member and verify
  1087  	res = <-ss.Group().CreateOrRestoreMember(group.Id, user.Id)
  1088  	res = <-ss.Group().PendingAutoAddTeamMembers(0)
  1089  	assert.Nil(t, res.Err)
  1090  	assert.Len(t, res.Data, 1)
  1091  
  1092  	// adding team membership stops returning result
  1093  	res = <-ss.Team().SaveMember(&model.TeamMember{
  1094  		TeamId: team.Id,
  1095  		UserId: user.Id,
  1096  	}, 999)
  1097  	assert.Nil(t, res.Err)
  1098  	res = <-ss.Group().PendingAutoAddTeamMembers(0)
  1099  	assert.Nil(t, res.Err)
  1100  	assert.Len(t, res.Data, 0)
  1101  }
  1102  
  1103  func testPendingAutoAddChannelMembers(t *testing.T, ss store.Store) {
  1104  	// Create Group
  1105  	res := <-ss.Group().Create(&model.Group{
  1106  		Name:        model.NewId(),
  1107  		DisplayName: "PendingAutoAddChannelMembers Test Group",
  1108  		RemoteId:    model.NewId(),
  1109  		Source:      model.GroupSourceLdap,
  1110  	})
  1111  	assert.Nil(t, res.Err)
  1112  	group := res.Data.(*model.Group)
  1113  
  1114  	// Create User
  1115  	user := &model.User{
  1116  		Email:    MakeEmail(),
  1117  		Username: model.NewId(),
  1118  	}
  1119  	res = <-ss.User().Save(user)
  1120  	assert.Nil(t, res.Err)
  1121  	user = res.Data.(*model.User)
  1122  
  1123  	// Create GroupMember
  1124  	res = <-ss.Group().CreateOrRestoreMember(group.Id, user.Id)
  1125  	assert.Nil(t, res.Err)
  1126  
  1127  	// Create Channel
  1128  	channel := &model.Channel{
  1129  		TeamId:      model.NewId(),
  1130  		DisplayName: "A Name",
  1131  		Name:        model.NewId(),
  1132  		Type:        model.CHANNEL_OPEN, // Query does not look at type so this shouldn't matter.
  1133  	}
  1134  	res = <-ss.Channel().Save(channel, 9999)
  1135  	assert.Nil(t, res.Err)
  1136  	channel = res.Data.(*model.Channel)
  1137  
  1138  	// Create GroupChannel
  1139  	res = <-ss.Group().CreateGroupSyncable(&model.GroupSyncable{
  1140  		AutoAdd:    true,
  1141  		SyncableId: channel.Id,
  1142  		Type:       model.GroupSyncableTypeChannel,
  1143  		GroupId:    group.Id,
  1144  	})
  1145  	assert.Nil(t, res.Err)
  1146  	syncable := res.Data.(*model.GroupSyncable)
  1147  
  1148  	// Time before syncable was created
  1149  	res = <-ss.Group().PendingAutoAddChannelMembers(syncable.CreateAt - 1)
  1150  	assert.Nil(t, res.Err)
  1151  	userChannelIDs := res.Data.([]*model.UserChannelIDPair)
  1152  	assert.Len(t, userChannelIDs, 1)
  1153  	assert.Equal(t, user.Id, userChannelIDs[0].UserID)
  1154  	assert.Equal(t, channel.Id, userChannelIDs[0].ChannelID)
  1155  
  1156  	// Time after syncable was created
  1157  	res = <-ss.Group().PendingAutoAddChannelMembers(syncable.CreateAt + 1)
  1158  	assert.Nil(t, res.Err)
  1159  	assert.Len(t, res.Data, 0)
  1160  
  1161  	// Delete and restore GroupMember should return result
  1162  	res = <-ss.Group().DeleteMember(group.Id, user.Id)
  1163  	assert.Nil(t, res.Err)
  1164  	res = <-ss.Group().CreateOrRestoreMember(group.Id, user.Id)
  1165  	assert.Nil(t, res.Err)
  1166  	res = <-ss.Group().PendingAutoAddChannelMembers(syncable.CreateAt + 1)
  1167  	assert.Nil(t, res.Err)
  1168  	assert.Len(t, res.Data, 1)
  1169  
  1170  	pristineSyncable := *syncable
  1171  
  1172  	res = <-ss.Group().UpdateGroupSyncable(syncable)
  1173  	assert.Nil(t, res.Err)
  1174  
  1175  	// Time before syncable was updated
  1176  	res = <-ss.Group().PendingAutoAddChannelMembers(syncable.UpdateAt - 1)
  1177  	assert.Nil(t, res.Err)
  1178  	userChannelIDs = res.Data.([]*model.UserChannelIDPair)
  1179  	assert.Len(t, userChannelIDs, 1)
  1180  	assert.Equal(t, user.Id, userChannelIDs[0].UserID)
  1181  	assert.Equal(t, channel.Id, userChannelIDs[0].ChannelID)
  1182  
  1183  	// Time after syncable was updated
  1184  	res = <-ss.Group().PendingAutoAddChannelMembers(syncable.UpdateAt + 1)
  1185  	assert.Nil(t, res.Err)
  1186  	assert.Len(t, res.Data, 0)
  1187  
  1188  	// Only includes if auto-add
  1189  	syncable.AutoAdd = false
  1190  	res = <-ss.Group().UpdateGroupSyncable(syncable)
  1191  	assert.Nil(t, res.Err)
  1192  	res = <-ss.Group().PendingAutoAddChannelMembers(0)
  1193  	assert.Nil(t, res.Err)
  1194  	assert.Len(t, res.Data, 0)
  1195  
  1196  	// reset state of syncable and verify
  1197  	res = <-ss.Group().UpdateGroupSyncable(&pristineSyncable)
  1198  	assert.Nil(t, res.Err)
  1199  	res = <-ss.Group().PendingAutoAddChannelMembers(0)
  1200  	assert.Nil(t, res.Err)
  1201  	assert.Len(t, res.Data, 1)
  1202  
  1203  	// No result if Group deleted
  1204  	res = <-ss.Group().Delete(group.Id)
  1205  	assert.Nil(t, res.Err)
  1206  	res = <-ss.Group().PendingAutoAddChannelMembers(0)
  1207  	assert.Nil(t, res.Err)
  1208  	assert.Len(t, res.Data, 0)
  1209  
  1210  	// reset state of group and verify
  1211  	group.DeleteAt = 0
  1212  	res = <-ss.Group().Update(group)
  1213  	res = <-ss.Group().PendingAutoAddChannelMembers(0)
  1214  	assert.Nil(t, res.Err)
  1215  	assert.Len(t, res.Data, 1)
  1216  
  1217  	// No result if Channel deleted
  1218  	res = <-ss.Channel().Delete(channel.Id, model.GetMillis())
  1219  	assert.Nil(t, res.Err)
  1220  	res = <-ss.Group().PendingAutoAddChannelMembers(0)
  1221  	assert.Nil(t, res.Err)
  1222  	assert.Len(t, res.Data, 0)
  1223  
  1224  	// reset state of channel and verify
  1225  	channel.DeleteAt = 0
  1226  	res = <-ss.Channel().Update(channel)
  1227  	assert.Nil(t, res.Err)
  1228  	res = <-ss.Group().PendingAutoAddChannelMembers(0)
  1229  	assert.Nil(t, res.Err)
  1230  	assert.Len(t, res.Data, 1)
  1231  
  1232  	// No result if GroupChannel deleted
  1233  	res = <-ss.Group().DeleteGroupSyncable(group.Id, channel.Id, model.GroupSyncableTypeChannel)
  1234  	assert.Nil(t, res.Err)
  1235  	res = <-ss.Group().PendingAutoAddChannelMembers(0)
  1236  	assert.Nil(t, res.Err)
  1237  	assert.Len(t, res.Data, 0)
  1238  
  1239  	// reset GroupChannel and verify
  1240  	res = <-ss.Group().UpdateGroupSyncable(&pristineSyncable)
  1241  	assert.Nil(t, res.Err)
  1242  	res = <-ss.Group().PendingAutoAddChannelMembers(0)
  1243  	assert.Nil(t, res.Err)
  1244  	assert.Len(t, res.Data, 1)
  1245  
  1246  	// No result if GroupMember deleted
  1247  	res = <-ss.Group().DeleteMember(group.Id, user.Id)
  1248  	assert.Nil(t, res.Err)
  1249  	res = <-ss.Group().PendingAutoAddChannelMembers(0)
  1250  	assert.Nil(t, res.Err)
  1251  	assert.Len(t, res.Data, 0)
  1252  
  1253  	// restore group member and verify
  1254  	res = <-ss.Group().CreateOrRestoreMember(group.Id, user.Id)
  1255  	assert.Nil(t, res.Err)
  1256  	res = <-ss.Group().PendingAutoAddChannelMembers(0)
  1257  	assert.Nil(t, res.Err)
  1258  	assert.Len(t, res.Data, 1)
  1259  
  1260  	// Adding Channel (ChannelMemberHistory) should stop returning result
  1261  	res = <-ss.ChannelMemberHistory().LogJoinEvent(user.Id, channel.Id, model.GetMillis())
  1262  	assert.Nil(t, res.Err)
  1263  	res = <-ss.Group().PendingAutoAddChannelMembers(0)
  1264  	assert.Nil(t, res.Err)
  1265  	assert.Len(t, res.Data, 0)
  1266  
  1267  	// Leaving Channel (ChannelMemberHistory) should still not return result
  1268  	res = <-ss.ChannelMemberHistory().LogLeaveEvent(user.Id, channel.Id, model.GetMillis())
  1269  	assert.Nil(t, res.Err)
  1270  	res = <-ss.Group().PendingAutoAddChannelMembers(0)
  1271  	assert.Nil(t, res.Err)
  1272  	assert.Len(t, res.Data, 0)
  1273  
  1274  	// Purging ChannelMemberHistory re-returns the result
  1275  	res = <-ss.ChannelMemberHistory().PermanentDeleteBatch(model.GetMillis()+1, 100)
  1276  	assert.Nil(t, res.Err)
  1277  	res = <-ss.Group().PendingAutoAddChannelMembers(0)
  1278  	assert.Nil(t, res.Err)
  1279  	assert.Len(t, res.Data, 1)
  1280  }