github.com/wgh-/mattermost-server@v4.8.0-rc2+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  )
    14  
    15  func TestComplianceStore(t *testing.T, ss store.Store) {
    16  	t.Run("", func(t *testing.T) { testComplianceStore(t, ss) })
    17  	t.Run("ComplianceExport", func(t *testing.T) { testComplianceExport(t, ss) })
    18  	t.Run("ComplianceExportDirectMessages", func(t *testing.T) { testComplianceExportDirectMessages(t, ss) })
    19  	t.Run("MessageExport", func(t *testing.T) { testComplianceMessageExport(t, ss) })
    20  }
    21  
    22  func testComplianceStore(t *testing.T, ss store.Store) {
    23  	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}
    24  	store.Must(ss.Compliance().Save(compliance1))
    25  	time.Sleep(100 * time.Millisecond)
    26  
    27  	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}
    28  	store.Must(ss.Compliance().Save(compliance2))
    29  	time.Sleep(100 * time.Millisecond)
    30  
    31  	c := ss.Compliance().GetAll(0, 1000)
    32  	result := <-c
    33  	compliances := result.Data.(model.Compliances)
    34  
    35  	if compliances[0].Status != model.COMPLIANCE_STATUS_RUNNING && compliance2.Id != compliances[0].Id {
    36  		t.Fatal()
    37  	}
    38  
    39  	compliance2.Status = model.COMPLIANCE_STATUS_FAILED
    40  	store.Must(ss.Compliance().Update(compliance2))
    41  
    42  	c = ss.Compliance().GetAll(0, 1000)
    43  	result = <-c
    44  	compliances = result.Data.(model.Compliances)
    45  
    46  	if compliances[0].Status != model.COMPLIANCE_STATUS_FAILED && compliance2.Id != compliances[0].Id {
    47  		t.Fatal()
    48  	}
    49  
    50  	c = ss.Compliance().GetAll(0, 1)
    51  	result = <-c
    52  	compliances = result.Data.(model.Compliances)
    53  
    54  	if len(compliances) != 1 {
    55  		t.Fatal("should only have returned 1")
    56  	}
    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  	if rc2.Status != compliance2.Status {
    68  		t.Fatal()
    69  	}
    70  }
    71  
    72  func testComplianceExport(t *testing.T, ss store.Store) {
    73  	time.Sleep(100 * time.Millisecond)
    74  
    75  	t1 := &model.Team{}
    76  	t1.DisplayName = "DisplayName"
    77  	t1.Name = "zz" + model.NewId() + "b"
    78  	t1.Email = model.NewId() + "@nowhere.com"
    79  	t1.Type = model.TEAM_OPEN
    80  	t1 = store.Must(ss.Team().Save(t1)).(*model.Team)
    81  
    82  	u1 := &model.User{}
    83  	u1.Email = model.NewId()
    84  	u1.Username = model.NewId()
    85  	u1 = store.Must(ss.User().Save(u1)).(*model.User)
    86  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: t1.Id, UserId: u1.Id}, -1))
    87  
    88  	u2 := &model.User{}
    89  	u2.Email = model.NewId()
    90  	u2.Username = model.NewId()
    91  	u2 = store.Must(ss.User().Save(u2)).(*model.User)
    92  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: t1.Id, UserId: u2.Id}, -1))
    93  
    94  	c1 := &model.Channel{}
    95  	c1.TeamId = t1.Id
    96  	c1.DisplayName = "Channel2"
    97  	c1.Name = "zz" + model.NewId() + "b"
    98  	c1.Type = model.CHANNEL_OPEN
    99  	c1 = store.Must(ss.Channel().Save(c1, -1)).(*model.Channel)
   100  
   101  	o1 := &model.Post{}
   102  	o1.ChannelId = c1.Id
   103  	o1.UserId = u1.Id
   104  	o1.CreateAt = model.GetMillis()
   105  	o1.Message = "zz" + model.NewId() + "b"
   106  	o1 = store.Must(ss.Post().Save(o1)).(*model.Post)
   107  
   108  	o1a := &model.Post{}
   109  	o1a.ChannelId = c1.Id
   110  	o1a.UserId = u1.Id
   111  	o1a.CreateAt = o1.CreateAt + 10
   112  	o1a.Message = "zz" + model.NewId() + "b"
   113  	o1a = store.Must(ss.Post().Save(o1a)).(*model.Post)
   114  
   115  	o2 := &model.Post{}
   116  	o2.ChannelId = c1.Id
   117  	o2.UserId = u1.Id
   118  	o2.CreateAt = o1.CreateAt + 20
   119  	o2.Message = "zz" + model.NewId() + "b"
   120  	o2 = store.Must(ss.Post().Save(o2)).(*model.Post)
   121  
   122  	o2a := &model.Post{}
   123  	o2a.ChannelId = c1.Id
   124  	o2a.UserId = u2.Id
   125  	o2a.CreateAt = o1.CreateAt + 30
   126  	o2a.Message = "zz" + model.NewId() + "b"
   127  	o2a = store.Must(ss.Post().Save(o2a)).(*model.Post)
   128  
   129  	time.Sleep(100 * time.Millisecond)
   130  
   131  	cr1 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1}
   132  	if r1 := <-ss.Compliance().ComplianceExport(cr1); r1.Err != nil {
   133  		t.Fatal(r1.Err)
   134  	} else {
   135  		cposts := r1.Data.([]*model.CompliancePost)
   136  
   137  		if len(cposts) != 4 {
   138  			t.Fatal("return wrong results length")
   139  		}
   140  
   141  		if cposts[0].PostId != o1.Id {
   142  			t.Fatal("Wrong sort")
   143  		}
   144  
   145  		if cposts[3].PostId != o2a.Id {
   146  			t.Fatal("Wrong sort")
   147  		}
   148  	}
   149  
   150  	cr2 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1, Emails: u2.Email}
   151  	if r1 := <-ss.Compliance().ComplianceExport(cr2); r1.Err != nil {
   152  		t.Fatal(r1.Err)
   153  	} else {
   154  		cposts := r1.Data.([]*model.CompliancePost)
   155  
   156  		if len(cposts) != 1 {
   157  			t.Fatal("return wrong results length")
   158  		}
   159  
   160  		if cposts[0].PostId != o2a.Id {
   161  			t.Fatal("Wrong sort")
   162  		}
   163  	}
   164  
   165  	cr3 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1, Emails: u2.Email + ", " + u1.Email}
   166  	if r1 := <-ss.Compliance().ComplianceExport(cr3); r1.Err != nil {
   167  		t.Fatal(r1.Err)
   168  	} else {
   169  		cposts := r1.Data.([]*model.CompliancePost)
   170  
   171  		if len(cposts) != 4 {
   172  			t.Fatal("return wrong results length")
   173  		}
   174  
   175  		if cposts[0].PostId != o1.Id {
   176  			t.Fatal("Wrong sort")
   177  		}
   178  
   179  		if cposts[3].PostId != o2a.Id {
   180  			t.Fatal("Wrong sort")
   181  		}
   182  	}
   183  
   184  	cr4 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1, Keywords: o2a.Message}
   185  	if r1 := <-ss.Compliance().ComplianceExport(cr4); r1.Err != nil {
   186  		t.Fatal(r1.Err)
   187  	} else {
   188  		cposts := r1.Data.([]*model.CompliancePost)
   189  
   190  		if len(cposts) != 1 {
   191  			t.Fatal("return wrong results length")
   192  		}
   193  
   194  		if cposts[0].PostId != o2a.Id {
   195  			t.Fatal("Wrong sort")
   196  		}
   197  	}
   198  
   199  	cr5 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o2a.CreateAt + 1, Keywords: o2a.Message + " " + o1.Message}
   200  	if r1 := <-ss.Compliance().ComplianceExport(cr5); r1.Err != nil {
   201  		t.Fatal(r1.Err)
   202  	} else {
   203  		cposts := r1.Data.([]*model.CompliancePost)
   204  
   205  		if len(cposts) != 2 {
   206  			t.Fatal("return wrong results length")
   207  		}
   208  
   209  		if cposts[0].PostId != o1.Id {
   210  			t.Fatal("Wrong sort")
   211  		}
   212  	}
   213  
   214  	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}
   215  	if r1 := <-ss.Compliance().ComplianceExport(cr6); r1.Err != nil {
   216  		t.Fatal(r1.Err)
   217  	} else {
   218  		cposts := r1.Data.([]*model.CompliancePost)
   219  
   220  		if len(cposts) != 2 {
   221  			t.Fatal("return wrong results length")
   222  		}
   223  
   224  		if cposts[0].PostId != o1.Id {
   225  			t.Fatal("Wrong sort")
   226  		}
   227  
   228  		if cposts[1].PostId != o2a.Id {
   229  			t.Fatal("Wrong sort")
   230  		}
   231  	}
   232  }
   233  
   234  func testComplianceExportDirectMessages(t *testing.T, ss store.Store) {
   235  	time.Sleep(100 * time.Millisecond)
   236  
   237  	t1 := &model.Team{}
   238  	t1.DisplayName = "DisplayName"
   239  	t1.Name = "zz" + model.NewId() + "b"
   240  	t1.Email = model.NewId() + "@nowhere.com"
   241  	t1.Type = model.TEAM_OPEN
   242  	t1 = store.Must(ss.Team().Save(t1)).(*model.Team)
   243  
   244  	u1 := &model.User{}
   245  	u1.Email = model.NewId()
   246  	u1.Username = model.NewId()
   247  	u1 = store.Must(ss.User().Save(u1)).(*model.User)
   248  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: t1.Id, UserId: u1.Id}, -1))
   249  
   250  	u2 := &model.User{}
   251  	u2.Email = model.NewId()
   252  	u2.Username = model.NewId()
   253  	u2 = store.Must(ss.User().Save(u2)).(*model.User)
   254  	store.Must(ss.Team().SaveMember(&model.TeamMember{TeamId: t1.Id, UserId: u2.Id}, -1))
   255  
   256  	c1 := &model.Channel{}
   257  	c1.TeamId = t1.Id
   258  	c1.DisplayName = "Channel2"
   259  	c1.Name = "zz" + model.NewId() + "b"
   260  	c1.Type = model.CHANNEL_OPEN
   261  	c1 = store.Must(ss.Channel().Save(c1, -1)).(*model.Channel)
   262  
   263  	cDM := store.Must(ss.Channel().CreateDirectChannel(u1.Id, u2.Id)).(*model.Channel)
   264  
   265  	o1 := &model.Post{}
   266  	o1.ChannelId = c1.Id
   267  	o1.UserId = u1.Id
   268  	o1.CreateAt = model.GetMillis()
   269  	o1.Message = "zz" + model.NewId() + "b"
   270  	o1 = store.Must(ss.Post().Save(o1)).(*model.Post)
   271  
   272  	o1a := &model.Post{}
   273  	o1a.ChannelId = c1.Id
   274  	o1a.UserId = u1.Id
   275  	o1a.CreateAt = o1.CreateAt + 10
   276  	o1a.Message = "zz" + model.NewId() + "b"
   277  	o1a = store.Must(ss.Post().Save(o1a)).(*model.Post)
   278  
   279  	o2 := &model.Post{}
   280  	o2.ChannelId = c1.Id
   281  	o2.UserId = u1.Id
   282  	o2.CreateAt = o1.CreateAt + 20
   283  	o2.Message = "zz" + model.NewId() + "b"
   284  	o2 = store.Must(ss.Post().Save(o2)).(*model.Post)
   285  
   286  	o2a := &model.Post{}
   287  	o2a.ChannelId = c1.Id
   288  	o2a.UserId = u2.Id
   289  	o2a.CreateAt = o1.CreateAt + 30
   290  	o2a.Message = "zz" + model.NewId() + "b"
   291  	o2a = store.Must(ss.Post().Save(o2a)).(*model.Post)
   292  
   293  	o3 := &model.Post{}
   294  	o3.ChannelId = cDM.Id
   295  	o3.UserId = u1.Id
   296  	o3.CreateAt = o1.CreateAt + 40
   297  	o3.Message = "zz" + model.NewId() + "b"
   298  	o3 = store.Must(ss.Post().Save(o3)).(*model.Post)
   299  
   300  	time.Sleep(100 * time.Millisecond)
   301  
   302  	cr1 := &model.Compliance{Desc: "test" + model.NewId(), StartAt: o1.CreateAt - 1, EndAt: o3.CreateAt + 1, Emails: u1.Email}
   303  	if r1 := <-ss.Compliance().ComplianceExport(cr1); r1.Err != nil {
   304  		t.Fatal(r1.Err)
   305  	} else {
   306  		cposts := r1.Data.([]*model.CompliancePost)
   307  
   308  		if len(cposts) != 4 {
   309  			t.Fatal("return wrong results length")
   310  		}
   311  
   312  		if cposts[0].PostId != o1.Id {
   313  			t.Fatal("Wrong sort")
   314  		}
   315  
   316  		if cposts[len(cposts)-1].PostId != o3.Id {
   317  			t.Fatal("Wrong sort")
   318  		}
   319  	}
   320  }
   321  
   322  func testComplianceMessageExport(t *testing.T, ss store.Store) {
   323  	// get the starting number of message export entries
   324  	startTime := model.GetMillis()
   325  	var numMessageExports = 0
   326  	if r1 := <-ss.Compliance().MessageExport(startTime-10, 10); r1.Err != nil {
   327  		t.Fatal(r1.Err)
   328  	} else {
   329  		messages := r1.Data.([]*model.MessageExport)
   330  		numMessageExports = len(messages)
   331  	}
   332  
   333  	// need a team
   334  	team := &model.Team{
   335  		DisplayName: "DisplayName",
   336  		Name:        "zz" + model.NewId() + "b",
   337  		Email:       model.NewId() + "@nowhere.com",
   338  		Type:        model.TEAM_OPEN,
   339  	}
   340  	team = store.Must(ss.Team().Save(team)).(*model.Team)
   341  
   342  	// and two users that are a part of that team
   343  	user1 := &model.User{
   344  		Email:    model.NewId(),
   345  		Username: model.NewId(),
   346  	}
   347  	user1 = store.Must(ss.User().Save(user1)).(*model.User)
   348  	store.Must(ss.Team().SaveMember(&model.TeamMember{
   349  		TeamId: team.Id,
   350  		UserId: user1.Id,
   351  	}, -1))
   352  
   353  	user2 := &model.User{
   354  		Email:    model.NewId(),
   355  		Username: model.NewId(),
   356  	}
   357  	user2 = store.Must(ss.User().Save(user2)).(*model.User)
   358  	store.Must(ss.Team().SaveMember(&model.TeamMember{
   359  		TeamId: team.Id,
   360  		UserId: user2.Id,
   361  	}, -1))
   362  
   363  	// need a public channel as well as a DM channel between the two users
   364  	channel := &model.Channel{
   365  		TeamId:      team.Id,
   366  		Name:        model.NewId(),
   367  		DisplayName: "Channel2",
   368  		Type:        model.CHANNEL_OPEN,
   369  	}
   370  	channel = store.Must(ss.Channel().Save(channel, -1)).(*model.Channel)
   371  	directMessageChannel := store.Must(ss.Channel().CreateDirectChannel(user1.Id, user2.Id)).(*model.Channel)
   372  
   373  	// user1 posts twice in the public channel
   374  	post1 := &model.Post{
   375  		ChannelId: channel.Id,
   376  		UserId:    user1.Id,
   377  		CreateAt:  startTime,
   378  		Message:   "zz" + model.NewId() + "a",
   379  	}
   380  	post1 = store.Must(ss.Post().Save(post1)).(*model.Post)
   381  
   382  	post2 := &model.Post{
   383  		ChannelId: channel.Id,
   384  		UserId:    user1.Id,
   385  		CreateAt:  startTime + 10,
   386  		Message:   "zz" + model.NewId() + "b",
   387  	}
   388  	post2 = store.Must(ss.Post().Save(post2)).(*model.Post)
   389  
   390  	// user1 also sends a DM to user2
   391  	post3 := &model.Post{
   392  		ChannelId: directMessageChannel.Id,
   393  		UserId:    user1.Id,
   394  		CreateAt:  startTime + 20,
   395  		Message:   "zz" + model.NewId() + "c",
   396  	}
   397  	post3 = store.Must(ss.Post().Save(post3)).(*model.Post)
   398  
   399  	// fetch the message exports for all three posts that user1 sent
   400  	messageExportMap := map[string]model.MessageExport{}
   401  	if r1 := <-ss.Compliance().MessageExport(startTime-10, 10); r1.Err != nil {
   402  		t.Fatal(r1.Err)
   403  	} else {
   404  		messages := r1.Data.([]*model.MessageExport)
   405  		assert.Equal(t, numMessageExports+3, len(messages))
   406  
   407  		for _, v := range messages {
   408  			messageExportMap[*v.PostId] = *v
   409  		}
   410  	}
   411  
   412  	// post1 was made by user1 in channel1 and team1
   413  	assert.Equal(t, post1.Id, *messageExportMap[post1.Id].PostId)
   414  	assert.Equal(t, post1.CreateAt, *messageExportMap[post1.Id].PostCreateAt)
   415  	assert.Equal(t, post1.Message, *messageExportMap[post1.Id].PostMessage)
   416  	assert.Equal(t, channel.Id, *messageExportMap[post1.Id].ChannelId)
   417  	assert.Equal(t, channel.DisplayName, *messageExportMap[post1.Id].ChannelDisplayName)
   418  	assert.Equal(t, user1.Id, *messageExportMap[post1.Id].UserId)
   419  	assert.Equal(t, user1.Email, *messageExportMap[post1.Id].UserEmail)
   420  	assert.Equal(t, user1.Username, *messageExportMap[post1.Id].Username)
   421  
   422  	// post2 was made by user1 in channel1 and team1
   423  	assert.Equal(t, post2.Id, *messageExportMap[post2.Id].PostId)
   424  	assert.Equal(t, post2.CreateAt, *messageExportMap[post2.Id].PostCreateAt)
   425  	assert.Equal(t, post2.Message, *messageExportMap[post2.Id].PostMessage)
   426  	assert.Equal(t, channel.Id, *messageExportMap[post2.Id].ChannelId)
   427  	assert.Equal(t, channel.DisplayName, *messageExportMap[post2.Id].ChannelDisplayName)
   428  	assert.Equal(t, user1.Id, *messageExportMap[post2.Id].UserId)
   429  	assert.Equal(t, user1.Email, *messageExportMap[post2.Id].UserEmail)
   430  	assert.Equal(t, user1.Username, *messageExportMap[post2.Id].Username)
   431  
   432  	// post3 is a DM between user1 and user2
   433  	assert.Equal(t, post3.Id, *messageExportMap[post3.Id].PostId)
   434  	assert.Equal(t, post3.CreateAt, *messageExportMap[post3.Id].PostCreateAt)
   435  	assert.Equal(t, post3.Message, *messageExportMap[post3.Id].PostMessage)
   436  	assert.Equal(t, directMessageChannel.Id, *messageExportMap[post3.Id].ChannelId)
   437  	assert.Equal(t, user1.Id, *messageExportMap[post3.Id].UserId)
   438  	assert.Equal(t, user1.Email, *messageExportMap[post3.Id].UserEmail)
   439  	assert.Equal(t, user1.Username, *messageExportMap[post3.Id].Username)
   440  }