github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/storetest/compliance_store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/mattermost/mattermost-server/model"
    11  	"github.com/mattermost/mattermost-server/store"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestComplianceStore(t *testing.T, ss store.Store) {
    17  	t.Run("", func(t *testing.T) { testComplianceStore(t, ss) })
    18  	t.Run("ComplianceExport", func(t *testing.T) { testComplianceExport(t, ss) })
    19  	t.Run("ComplianceExportDirectMessages", func(t *testing.T) { testComplianceExportDirectMessages(t, ss) })
    20  	t.Run("MessageExportPublicChannel", func(t *testing.T) { testMessageExportPublicChannel(t, ss) })
    21  	t.Run("MessageExportPrivateChannel", func(t *testing.T) { testMessageExportPrivateChannel(t, ss) })
    22  	t.Run("MessageExportDirectMessageChannel", func(t *testing.T) { testMessageExportDirectMessageChannel(t, ss) })
    23  	t.Run("MessageExportGroupMessageChannel", func(t *testing.T) { testMessageExportGroupMessageChannel(t, ss) })
    24  }
    25  
    26  func testComplianceStore(t *testing.T, ss store.Store) {
    27  	compliance1 := &model.Compliance{Desc: "Audit for federal subpoena case #22443", UserId: model.NewId(), Status: model.COMPLIANCE_STATUS_FAILED, StartAt: model.GetMillis() - 1, EndAt: model.GetMillis() + 1, Type: model.COMPLIANCE_TYPE_ADHOC}
    28  	store.Must(ss.Compliance().Save(compliance1))
    29  	time.Sleep(100 * time.Millisecond)
    30  
    31  	compliance2 := &model.Compliance{Desc: "Audit for federal subpoena case #11458", UserId: model.NewId(), Status: model.COMPLIANCE_STATUS_RUNNING, StartAt: model.GetMillis() - 1, EndAt: model.GetMillis() + 1, Type: model.COMPLIANCE_TYPE_ADHOC}
    32  	store.Must(ss.Compliance().Save(compliance2))
    33  	time.Sleep(100 * time.Millisecond)
    34  
    35  	c := ss.Compliance().GetAll(0, 1000)
    36  	result := <-c
    37  	compliances := result.Data.(model.Compliances)
    38  
    39  	require.Equal(t, model.COMPLIANCE_STATUS_RUNNING, compliances[0].Status)
    40  	require.Equal(t, compliance2.Id, compliances[0].Id)
    41  
    42  	compliance2.Status = model.COMPLIANCE_STATUS_FAILED
    43  	store.Must(ss.Compliance().Update(compliance2))
    44  
    45  	c = ss.Compliance().GetAll(0, 1000)
    46  	result = <-c
    47  	compliances = result.Data.(model.Compliances)
    48  
    49  	require.Equal(t, model.COMPLIANCE_STATUS_FAILED, compliances[0].Status)
    50  	require.Equal(t, compliance2.Id, compliances[0].Id)
    51  
    52  	c = ss.Compliance().GetAll(0, 1)
    53  	result = <-c
    54  	compliances = result.Data.(model.Compliances)
    55  
    56  	require.Len(t, compliances, 1)
    57  
    58  	c = ss.Compliance().GetAll(1, 1)
    59  	result = <-c
    60  	compliances = result.Data.(model.Compliances)
    61  
    62  	if len(compliances) != 1 {
    63  		t.Fatal("should only have returned 1")
    64  	}
    65  
    66  	rc2 := (<-ss.Compliance().Get(compliance2.Id)).Data.(*model.Compliance)
    67  	require.Equal(t, compliance2.Status, rc2.Status)
    68  }
    69  
    70  func testComplianceExport(t *testing.T, ss store.Store) {
    71  	time.Sleep(100 * time.Millisecond)
    72  
    73  	t1 := &model.Team{}
    74  	t1.DisplayName = "DisplayName"
    75  	t1.Name = "zz" + model.NewId() + "b"
    76  	t1.Email = MakeEmail()
    77  	t1.Type = model.TEAM_OPEN
    78  	t1 = store.Must(ss.Team().Save(t1)).(*model.Team)
    79  
    80  	u1 := &model.User{}
    81  	u1.Email = MakeEmail()
    82  	u1.Username = model.NewId()
    83  	u1 = store.Must(ss.User().Save(u1)).(*model.User)
    84  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: t1.Id, UserId: u1.Id}, -1))
    85  
    86  	u2 := &model.User{}
    87  	u2.Email = MakeEmail()
    88  	u2.Username = model.NewId()
    89  	u2 = store.Must(ss.User().Save(u2)).(*model.User)
    90  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: t1.Id, UserId: u2.Id}, -1))
    91  
    92  	c1 := &model.Channel{}
    93  	c1.TeamId = t1.Id
    94  	c1.DisplayName = "Channel2"
    95  	c1.Name = "zz" + model.NewId() + "b"
    96  	c1.Type = model.CHANNEL_OPEN
    97  	c1 = store.Must(ss.Channel().Save(c1, -1)).(*model.Channel)
    98  
    99  	o1 := &model.Post{}
   100  	o1.ChannelId = c1.Id
   101  	o1.UserId = u1.Id
   102  	o1.CreateAt = model.GetMillis()
   103  	o1.Message = "zz" + model.NewId() + "b"
   104  	o1 = store.Must(ss.Post().Save(o1)).(*model.Post)
   105  
   106  	o1a := &model.Post{}
   107  	o1a.ChannelId = c1.Id
   108  	o1a.UserId = u1.Id
   109  	o1a.CreateAt = o1.CreateAt + 10
   110  	o1a.Message = "zz" + model.NewId() + "b"
   111  	_ = store.Must(ss.Post().Save(o1a)).(*model.Post)
   112  
   113  	o2 := &model.Post{}
   114  	o2.ChannelId = c1.Id
   115  	o2.UserId = u1.Id
   116  	o2.CreateAt = o1.CreateAt + 20
   117  	o2.Message = "zz" + model.NewId() + "b"
   118  	_ = store.Must(ss.Post().Save(o2)).(*model.Post)
   119  
   120  	o2a := &model.Post{}
   121  	o2a.ChannelId = c1.Id
   122  	o2a.UserId = u2.Id
   123  	o2a.CreateAt = o1.CreateAt + 30
   124  	o2a.Message = "zz" + model.NewId() + "b"
   125  	o2a = store.Must(ss.Post().Save(o2a)).(*model.Post)
   126  
   127  	time.Sleep(100 * time.Millisecond)
   128  
   129  	cr1 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1}
   130  	if r1 := <-ss.Compliance().ComplianceExport(cr1); r1.Err != nil {
   131  		t.Fatal(r1.Err)
   132  	} else {
   133  		cposts := r1.Data.([]*model.CompliancePost)
   134  
   135  		if len(cposts) != 4 {
   136  			t.Fatal("return wrong results length")
   137  		}
   138  
   139  		if cposts[0].PostId != o1.Id {
   140  			t.Fatal("Wrong sort")
   141  		}
   142  
   143  		if cposts[3].PostId != o2a.Id {
   144  			t.Fatal("Wrong sort")
   145  		}
   146  	}
   147  
   148  	cr2 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1, Emails: u2.Email}
   149  	if r1 := <-ss.Compliance().ComplianceExport(cr2); r1.Err != nil {
   150  		t.Fatal(r1.Err)
   151  	} else {
   152  		cposts := r1.Data.([]*model.CompliancePost)
   153  
   154  		if len(cposts) != 1 {
   155  			t.Fatal("return wrong results length")
   156  		}
   157  
   158  		if cposts[0].PostId != o2a.Id {
   159  			t.Fatal("Wrong sort")
   160  		}
   161  	}
   162  
   163  	cr3 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1, Emails: u2.Email + ", " + u1.Email}
   164  	if r1 := <-ss.Compliance().ComplianceExport(cr3); r1.Err != nil {
   165  		t.Fatal(r1.Err)
   166  	} else {
   167  		cposts := r1.Data.([]*model.CompliancePost)
   168  
   169  		if len(cposts) != 4 {
   170  			t.Fatal("return wrong results length")
   171  		}
   172  
   173  		if cposts[0].PostId != o1.Id {
   174  			t.Fatal("Wrong sort")
   175  		}
   176  
   177  		if cposts[3].PostId != o2a.Id {
   178  			t.Fatal("Wrong sort")
   179  		}
   180  	}
   181  
   182  	cr4 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1, Keywords: o2a.Message}
   183  	if r1 := <-ss.Compliance().ComplianceExport(cr4); r1.Err != nil {
   184  		t.Fatal(r1.Err)
   185  	} else {
   186  		cposts := r1.Data.([]*model.CompliancePost)
   187  
   188  		if len(cposts) != 1 {
   189  			t.Fatal("return wrong results length")
   190  		}
   191  
   192  		if cposts[0].PostId != o2a.Id {
   193  			t.Fatal("Wrong sort")
   194  		}
   195  	}
   196  
   197  	cr5 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1, Keywords: o2a.Message + " " + o1.Message}
   198  	if r1 := <-ss.Compliance().ComplianceExport(cr5); r1.Err != nil {
   199  		t.Fatal(r1.Err)
   200  	} else {
   201  		cposts := r1.Data.([]*model.CompliancePost)
   202  
   203  		if len(cposts) != 2 {
   204  			t.Fatal("return wrong results length")
   205  		}
   206  
   207  		if cposts[0].PostId != o1.Id {
   208  			t.Fatal("Wrong sort")
   209  		}
   210  	}
   211  
   212  	cr6 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1, Emails: u2.Email + ", " + u1.Email, Keywords: o2a.Message + " " + o1.Message}
   213  	if r1 := <-ss.Compliance().ComplianceExport(cr6); r1.Err != nil {
   214  		t.Fatal(r1.Err)
   215  	} else {
   216  		cposts := r1.Data.([]*model.CompliancePost)
   217  
   218  		if len(cposts) != 2 {
   219  			t.Fatal("return wrong results length")
   220  		}
   221  
   222  		if cposts[0].PostId != o1.Id {
   223  			t.Fatal("Wrong sort")
   224  		}
   225  
   226  		if cposts[1].PostId != o2a.Id {
   227  			t.Fatal("Wrong sort")
   228  		}
   229  	}
   230  }
   231  
   232  func testComplianceExportDirectMessages(t *testing.T, ss store.Store) {
   233  	time.Sleep(100 * time.Millisecond)
   234  
   235  	t1 := &model.Team{}
   236  	t1.DisplayName = "DisplayName"
   237  	t1.Name = "zz" + model.NewId() + "b"
   238  	t1.Email = MakeEmail()
   239  	t1.Type = model.TEAM_OPEN
   240  	t1 = store.Must(ss.Team().Save(t1)).(*model.Team)
   241  
   242  	u1 := &model.User{}
   243  	u1.Email = MakeEmail()
   244  	u1.Username = model.NewId()
   245  	u1 = store.Must(ss.User().Save(u1)).(*model.User)
   246  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: t1.Id, UserId: u1.Id}, -1))
   247  
   248  	u2 := &model.User{}
   249  	u2.Email = MakeEmail()
   250  	u2.Username = model.NewId()
   251  	u2 = store.Must(ss.User().Save(u2)).(*model.User)
   252  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: t1.Id, UserId: u2.Id}, -1))
   253  
   254  	c1 := &model.Channel{}
   255  	c1.TeamId = t1.Id
   256  	c1.DisplayName = "Channel2"
   257  	c1.Name = "zz" + model.NewId() + "b"
   258  	c1.Type = model.CHANNEL_OPEN
   259  	c1 = store.Must(ss.Channel().Save(c1, -1)).(*model.Channel)
   260  
   261  	cDM := store.Must(ss.Channel().CreateDirectChannel(u1.Id, u2.Id)).(*model.Channel)
   262  
   263  	o1 := &model.Post{}
   264  	o1.ChannelId = c1.Id
   265  	o1.UserId = u1.Id
   266  	o1.CreateAt = model.GetMillis()
   267  	o1.Message = "zz" + model.NewId() + "b"
   268  	o1 = store.Must(ss.Post().Save(o1)).(*model.Post)
   269  
   270  	o1a := &model.Post{}
   271  	o1a.ChannelId = c1.Id
   272  	o1a.UserId = u1.Id
   273  	o1a.CreateAt = o1.CreateAt + 10
   274  	o1a.Message = "zz" + model.NewId() + "b"
   275  	_ = store.Must(ss.Post().Save(o1a)).(*model.Post)
   276  
   277  	o2 := &model.Post{}
   278  	o2.ChannelId = c1.Id
   279  	o2.UserId = u1.Id
   280  	o2.CreateAt = o1.CreateAt + 20
   281  	o2.Message = "zz" + model.NewId() + "b"
   282  	_ = store.Must(ss.Post().Save(o2)).(*model.Post)
   283  
   284  	o2a := &model.Post{}
   285  	o2a.ChannelId = c1.Id
   286  	o2a.UserId = u2.Id
   287  	o2a.CreateAt = o1.CreateAt + 30
   288  	o2a.Message = "zz" + model.NewId() + "b"
   289  	_ = store.Must(ss.Post().Save(o2a)).(*model.Post)
   290  
   291  	o3 := &model.Post{}
   292  	o3.ChannelId = cDM.Id
   293  	o3.UserId = u1.Id
   294  	o3.CreateAt = o1.CreateAt + 40
   295  	o3.Message = "zz" + model.NewId() + "b"
   296  	o3 = store.Must(ss.Post().Save(o3)).(*model.Post)
   297  
   298  	time.Sleep(100 * time.Millisecond)
   299  
   300  	cr1 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o3.CreateAt + 1, Emails: u1.Email}
   301  	if r1 := <-ss.Compliance().ComplianceExport(cr1); r1.Err != nil {
   302  		t.Fatal(r1.Err)
   303  	} else {
   304  		cposts := r1.Data.([]*model.CompliancePost)
   305  
   306  		if len(cposts) != 4 {
   307  			t.Fatal("return wrong results length")
   308  		}
   309  
   310  		if cposts[0].PostId != o1.Id {
   311  			t.Fatal("Wrong sort")
   312  		}
   313  
   314  		if cposts[len(cposts)-1].PostId != o3.Id {
   315  			t.Fatal("Wrong sort")
   316  		}
   317  	}
   318  }
   319  
   320  func testMessageExportPublicChannel(t *testing.T, ss store.Store) {
   321  	// get the starting number of message export entries
   322  	startTime := model.GetMillis()
   323  	var numMessageExports = 0
   324  	if r1 := <-ss.Compliance().MessageExport(startTime-10, 10); r1.Err != nil {
   325  		t.Fatal(r1.Err)
   326  	} else {
   327  		messages := r1.Data.([]*model.MessageExport)
   328  		numMessageExports = len(messages)
   329  	}
   330  
   331  	// need a team
   332  	team := &model.Team{
   333  		DisplayName: "DisplayName",
   334  		Name:        "zz" + model.NewId() + "b",
   335  		Email:       MakeEmail(),
   336  		Type:        model.TEAM_OPEN,
   337  	}
   338  	team = store.Must(ss.Team().Save(team)).(*model.Team)
   339  
   340  	// and two users that are a part of that team
   341  	user1 := &model.User{
   342  		Email:    MakeEmail(),
   343  		Username: model.NewId(),
   344  	}
   345  	user1 = store.Must(ss.User().Save(user1)).(*model.User)
   346  	store.Must(ss.Team().SaveMember(&model.TeamMember{
   347  		TeamId: team.Id,
   348  		UserId: user1.Id,
   349  	}, -1))
   350  
   351  	user2 := &model.User{
   352  		Email:    MakeEmail(),
   353  		Username: model.NewId(),
   354  	}
   355  	user2 = store.Must(ss.User().Save(user2)).(*model.User)
   356  	store.Must(ss.Team().SaveMember(&model.TeamMember{
   357  		TeamId: team.Id,
   358  		UserId: user2.Id,
   359  	}, -1))
   360  
   361  	// need a public channel
   362  	channel := &model.Channel{
   363  		TeamId:      team.Id,
   364  		Name:        model.NewId(),
   365  		DisplayName: "Public Channel",
   366  		Type:        model.CHANNEL_OPEN,
   367  	}
   368  	channel = store.Must(ss.Channel().Save(channel, -1)).(*model.Channel)
   369  
   370  	// user1 posts twice in the public channel
   371  	post1 := &model.Post{
   372  		ChannelId: channel.Id,
   373  		UserId:    user1.Id,
   374  		CreateAt:  startTime,
   375  		Message:   "zz" + model.NewId() + "a",
   376  	}
   377  	post1 = store.Must(ss.Post().Save(post1)).(*model.Post)
   378  
   379  	post2 := &model.Post{
   380  		ChannelId: channel.Id,
   381  		UserId:    user1.Id,
   382  		CreateAt:  startTime + 10,
   383  		Message:   "zz" + model.NewId() + "b",
   384  	}
   385  	post2 = store.Must(ss.Post().Save(post2)).(*model.Post)
   386  
   387  	// fetch the message exports for both posts that user1 sent
   388  	messageExportMap := map[string]model.MessageExport{}
   389  	if r1 := <-ss.Compliance().MessageExport(startTime-10, 10); r1.Err != nil {
   390  		t.Fatal(r1.Err)
   391  	} else {
   392  		messages := r1.Data.([]*model.MessageExport)
   393  		assert.Equal(t, numMessageExports+2, len(messages))
   394  
   395  		for _, v := range messages {
   396  			messageExportMap[*v.PostId] = *v
   397  		}
   398  	}
   399  
   400  	// post1 was made by user1 in channel1 and team1
   401  	assert.Equal(t, post1.Id, *messageExportMap[post1.Id].PostId)
   402  	assert.Equal(t, post1.CreateAt, *messageExportMap[post1.Id].PostCreateAt)
   403  	assert.Equal(t, post1.Message, *messageExportMap[post1.Id].PostMessage)
   404  	assert.Equal(t, channel.Id, *messageExportMap[post1.Id].ChannelId)
   405  	assert.Equal(t, channel.DisplayName, *messageExportMap[post1.Id].ChannelDisplayName)
   406  	assert.Equal(t, user1.Id, *messageExportMap[post1.Id].UserId)
   407  	assert.Equal(t, user1.Email, *messageExportMap[post1.Id].UserEmail)
   408  	assert.Equal(t, user1.Username, *messageExportMap[post1.Id].Username)
   409  
   410  	// post2 was made by user1 in channel1 and team1
   411  	assert.Equal(t, post2.Id, *messageExportMap[post2.Id].PostId)
   412  	assert.Equal(t, post2.CreateAt, *messageExportMap[post2.Id].PostCreateAt)
   413  	assert.Equal(t, post2.Message, *messageExportMap[post2.Id].PostMessage)
   414  	assert.Equal(t, channel.Id, *messageExportMap[post2.Id].ChannelId)
   415  	assert.Equal(t, channel.DisplayName, *messageExportMap[post2.Id].ChannelDisplayName)
   416  	assert.Equal(t, user1.Id, *messageExportMap[post2.Id].UserId)
   417  	assert.Equal(t, user1.Email, *messageExportMap[post2.Id].UserEmail)
   418  	assert.Equal(t, user1.Username, *messageExportMap[post2.Id].Username)
   419  }
   420  
   421  func testMessageExportPrivateChannel(t *testing.T, ss store.Store) {
   422  	// get the starting number of message export entries
   423  	startTime := model.GetMillis()
   424  	var numMessageExports = 0
   425  	if r1 := <-ss.Compliance().MessageExport(startTime-10, 10); r1.Err != nil {
   426  		t.Fatal(r1.Err)
   427  	} else {
   428  		messages := r1.Data.([]*model.MessageExport)
   429  		numMessageExports = len(messages)
   430  	}
   431  
   432  	// need a team
   433  	team := &model.Team{
   434  		DisplayName: "DisplayName",
   435  		Name:        "zz" + model.NewId() + "b",
   436  		Email:       MakeEmail(),
   437  		Type:        model.TEAM_OPEN,
   438  	}
   439  	team = store.Must(ss.Team().Save(team)).(*model.Team)
   440  
   441  	// and two users that are a part of that team
   442  	user1 := &model.User{
   443  		Email:    MakeEmail(),
   444  		Username: model.NewId(),
   445  	}
   446  	user1 = store.Must(ss.User().Save(user1)).(*model.User)
   447  	store.Must(ss.Team().SaveMember(&model.TeamMember{
   448  		TeamId: team.Id,
   449  		UserId: user1.Id,
   450  	}, -1))
   451  
   452  	user2 := &model.User{
   453  		Email:    MakeEmail(),
   454  		Username: model.NewId(),
   455  	}
   456  	user2 = store.Must(ss.User().Save(user2)).(*model.User)
   457  	store.Must(ss.Team().SaveMember(&model.TeamMember{
   458  		TeamId: team.Id,
   459  		UserId: user2.Id,
   460  	}, -1))
   461  
   462  	// need a private channel
   463  	channel := &model.Channel{
   464  		TeamId:      team.Id,
   465  		Name:        model.NewId(),
   466  		DisplayName: "Private Channel",
   467  		Type:        model.CHANNEL_PRIVATE,
   468  	}
   469  	channel = store.Must(ss.Channel().Save(channel, -1)).(*model.Channel)
   470  
   471  	// user1 posts twice in the private channel
   472  	post1 := &model.Post{
   473  		ChannelId: channel.Id,
   474  		UserId:    user1.Id,
   475  		CreateAt:  startTime,
   476  		Message:   "zz" + model.NewId() + "a",
   477  	}
   478  	post1 = store.Must(ss.Post().Save(post1)).(*model.Post)
   479  
   480  	post2 := &model.Post{
   481  		ChannelId: channel.Id,
   482  		UserId:    user1.Id,
   483  		CreateAt:  startTime + 10,
   484  		Message:   "zz" + model.NewId() + "b",
   485  	}
   486  	post2 = store.Must(ss.Post().Save(post2)).(*model.Post)
   487  
   488  	// fetch the message exports for both posts that user1 sent
   489  	messageExportMap := map[string]model.MessageExport{}
   490  	if r1 := <-ss.Compliance().MessageExport(startTime-10, 10); r1.Err != nil {
   491  		t.Fatal(r1.Err)
   492  	} else {
   493  		messages := r1.Data.([]*model.MessageExport)
   494  		assert.Equal(t, numMessageExports+2, len(messages))
   495  
   496  		for _, v := range messages {
   497  			messageExportMap[*v.PostId] = *v
   498  		}
   499  	}
   500  
   501  	// post1 was made by user1 in channel1 and team1
   502  	assert.Equal(t, post1.Id, *messageExportMap[post1.Id].PostId)
   503  	assert.Equal(t, post1.CreateAt, *messageExportMap[post1.Id].PostCreateAt)
   504  	assert.Equal(t, post1.Message, *messageExportMap[post1.Id].PostMessage)
   505  	assert.Equal(t, channel.Id, *messageExportMap[post1.Id].ChannelId)
   506  	assert.Equal(t, channel.DisplayName, *messageExportMap[post1.Id].ChannelDisplayName)
   507  	assert.Equal(t, channel.Type, *messageExportMap[post1.Id].ChannelType)
   508  	assert.Equal(t, user1.Id, *messageExportMap[post1.Id].UserId)
   509  	assert.Equal(t, user1.Email, *messageExportMap[post1.Id].UserEmail)
   510  	assert.Equal(t, user1.Username, *messageExportMap[post1.Id].Username)
   511  
   512  	// post2 was made by user1 in channel1 and team1
   513  	assert.Equal(t, post2.Id, *messageExportMap[post2.Id].PostId)
   514  	assert.Equal(t, post2.CreateAt, *messageExportMap[post2.Id].PostCreateAt)
   515  	assert.Equal(t, post2.Message, *messageExportMap[post2.Id].PostMessage)
   516  	assert.Equal(t, channel.Id, *messageExportMap[post2.Id].ChannelId)
   517  	assert.Equal(t, channel.DisplayName, *messageExportMap[post2.Id].ChannelDisplayName)
   518  	assert.Equal(t, channel.Type, *messageExportMap[post2.Id].ChannelType)
   519  	assert.Equal(t, user1.Id, *messageExportMap[post2.Id].UserId)
   520  	assert.Equal(t, user1.Email, *messageExportMap[post2.Id].UserEmail)
   521  	assert.Equal(t, user1.Username, *messageExportMap[post2.Id].Username)
   522  }
   523  
   524  func testMessageExportDirectMessageChannel(t *testing.T, ss store.Store) {
   525  	// get the starting number of message export entries
   526  	startTime := model.GetMillis()
   527  	var numMessageExports = 0
   528  	if r1 := <-ss.Compliance().MessageExport(startTime-10, 10); r1.Err != nil {
   529  		t.Fatal(r1.Err)
   530  	} else {
   531  		messages := r1.Data.([]*model.MessageExport)
   532  		numMessageExports = len(messages)
   533  	}
   534  
   535  	// need a team
   536  	team := &model.Team{
   537  		DisplayName: "DisplayName",
   538  		Name:        "zz" + model.NewId() + "b",
   539  		Email:       MakeEmail(),
   540  		Type:        model.TEAM_OPEN,
   541  	}
   542  	team = store.Must(ss.Team().Save(team)).(*model.Team)
   543  
   544  	// and two users that are a part of that team
   545  	user1 := &model.User{
   546  		Email:    MakeEmail(),
   547  		Username: model.NewId(),
   548  	}
   549  	user1 = store.Must(ss.User().Save(user1)).(*model.User)
   550  	store.Must(ss.Team().SaveMember(&model.TeamMember{
   551  		TeamId: team.Id,
   552  		UserId: user1.Id,
   553  	}, -1))
   554  
   555  	user2 := &model.User{
   556  		Email:    MakeEmail(),
   557  		Username: model.NewId(),
   558  	}
   559  	user2 = store.Must(ss.User().Save(user2)).(*model.User)
   560  	store.Must(ss.Team().SaveMember(&model.TeamMember{
   561  		TeamId: team.Id,
   562  		UserId: user2.Id,
   563  	}, -1))
   564  
   565  	// as well as a DM channel between those users
   566  	directMessageChannel := store.Must(ss.Channel().CreateDirectChannel(user1.Id, user2.Id)).(*model.Channel)
   567  
   568  	// user1 also sends a DM to user2
   569  	post := &model.Post{
   570  		ChannelId: directMessageChannel.Id,
   571  		UserId:    user1.Id,
   572  		CreateAt:  startTime + 20,
   573  		Message:   "zz" + model.NewId() + "c",
   574  	}
   575  	post = store.Must(ss.Post().Save(post)).(*model.Post)
   576  
   577  	// fetch the message export for the post that user1 sent
   578  	messageExportMap := map[string]model.MessageExport{}
   579  	if r1 := <-ss.Compliance().MessageExport(startTime-10, 10); r1.Err != nil {
   580  		t.Fatal(r1.Err)
   581  	} else {
   582  		messages := r1.Data.([]*model.MessageExport)
   583  		assert.Equal(t, numMessageExports+1, len(messages))
   584  
   585  		for _, v := range messages {
   586  			messageExportMap[*v.PostId] = *v
   587  		}
   588  	}
   589  
   590  	// post is a DM between user1 and user2
   591  	// there is no channel display name for direct messages, so we sub in the string "Direct Message" instead
   592  	assert.Equal(t, post.Id, *messageExportMap[post.Id].PostId)
   593  	assert.Equal(t, post.CreateAt, *messageExportMap[post.Id].PostCreateAt)
   594  	assert.Equal(t, post.Message, *messageExportMap[post.Id].PostMessage)
   595  	assert.Equal(t, directMessageChannel.Id, *messageExportMap[post.Id].ChannelId)
   596  	assert.Equal(t, "Direct Message", *messageExportMap[post.Id].ChannelDisplayName)
   597  	assert.Equal(t, user1.Id, *messageExportMap[post.Id].UserId)
   598  	assert.Equal(t, user1.Email, *messageExportMap[post.Id].UserEmail)
   599  	assert.Equal(t, user1.Username, *messageExportMap[post.Id].Username)
   600  }
   601  
   602  func testMessageExportGroupMessageChannel(t *testing.T, ss store.Store) {
   603  	// get the starting number of message export entries
   604  	startTime := model.GetMillis()
   605  	var numMessageExports = 0
   606  	if r1 := <-ss.Compliance().MessageExport(startTime-10, 10); r1.Err != nil {
   607  		t.Fatal(r1.Err)
   608  	} else {
   609  		messages := r1.Data.([]*model.MessageExport)
   610  		numMessageExports = len(messages)
   611  	}
   612  
   613  	// need a team
   614  	team := &model.Team{
   615  		DisplayName: "DisplayName",
   616  		Name:        "zz" + model.NewId() + "b",
   617  		Email:       MakeEmail(),
   618  		Type:        model.TEAM_OPEN,
   619  	}
   620  	team = store.Must(ss.Team().Save(team)).(*model.Team)
   621  
   622  	// and three users that are a part of that team
   623  	user1 := &model.User{
   624  		Email:    MakeEmail(),
   625  		Username: model.NewId(),
   626  	}
   627  	user1 = store.Must(ss.User().Save(user1)).(*model.User)
   628  	store.Must(ss.Team().SaveMember(&model.TeamMember{
   629  		TeamId: team.Id,
   630  		UserId: user1.Id,
   631  	}, -1))
   632  
   633  	user2 := &model.User{
   634  		Email:    MakeEmail(),
   635  		Username: model.NewId(),
   636  	}
   637  	user2 = store.Must(ss.User().Save(user2)).(*model.User)
   638  	store.Must(ss.Team().SaveMember(&model.TeamMember{
   639  		TeamId: team.Id,
   640  		UserId: user2.Id,
   641  	}, -1))
   642  
   643  	user3 := &model.User{
   644  		Email:    MakeEmail(),
   645  		Username: model.NewId(),
   646  	}
   647  	user3 = store.Must(ss.User().Save(user3)).(*model.User)
   648  	store.Must(ss.Team().SaveMember(&model.TeamMember{
   649  		TeamId: team.Id,
   650  		UserId: user3.Id,
   651  	}, -1))
   652  
   653  	// can't create a group channel directly, because importing app creates an import cycle, so we have to fake it
   654  	groupMessageChannel := &model.Channel{
   655  		TeamId: team.Id,
   656  		Name:   model.NewId(),
   657  		Type:   model.CHANNEL_GROUP,
   658  	}
   659  	groupMessageChannel = store.Must(ss.Channel().Save(groupMessageChannel, -1)).(*model.Channel)
   660  
   661  	// user1 posts in the GM
   662  	post := &model.Post{
   663  		ChannelId: groupMessageChannel.Id,
   664  		UserId:    user1.Id,
   665  		CreateAt:  startTime + 20,
   666  		Message:   "zz" + model.NewId() + "c",
   667  	}
   668  	post = store.Must(ss.Post().Save(post)).(*model.Post)
   669  
   670  	// fetch the message export for the post that user1 sent
   671  	messageExportMap := map[string]model.MessageExport{}
   672  	if r1 := <-ss.Compliance().MessageExport(startTime-10, 10); r1.Err != nil {
   673  		t.Fatal(r1.Err)
   674  	} else {
   675  		messages := r1.Data.([]*model.MessageExport)
   676  		assert.Equal(t, numMessageExports+1, len(messages))
   677  
   678  		for _, v := range messages {
   679  			messageExportMap[*v.PostId] = *v
   680  		}
   681  	}
   682  
   683  	// post is a DM between user1 and user2
   684  	// there is no channel display name for direct messages, so we sub in the string "Direct Message" instead
   685  	assert.Equal(t, post.Id, *messageExportMap[post.Id].PostId)
   686  	assert.Equal(t, post.CreateAt, *messageExportMap[post.Id].PostCreateAt)
   687  	assert.Equal(t, post.Message, *messageExportMap[post.Id].PostMessage)
   688  	assert.Equal(t, groupMessageChannel.Id, *messageExportMap[post.Id].ChannelId)
   689  	assert.Equal(t, "Group Message", *messageExportMap[post.Id].ChannelDisplayName)
   690  	assert.Equal(t, user1.Id, *messageExportMap[post.Id].UserId)
   691  	assert.Equal(t, user1.Email, *messageExportMap[post.Id].UserEmail)
   692  	assert.Equal(t, user1.Username, *messageExportMap[post.Id].Username)
   693  }