github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/storetest/audit_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  )
    13  
    14  func TestAuditStore(t *testing.T, ss store.Store) {
    15  	t.Run("", func(t *testing.T) { testAuditStore(t, ss) })
    16  	t.Run("PermanentDeleteBatch", func(t *testing.T) { testAuditStorePermanentDeleteBatch(t, ss) })
    17  }
    18  
    19  func testAuditStore(t *testing.T, ss store.Store) {
    20  	audit := &model.Audit{UserId: model.NewId(), IpAddress: "ipaddress", Action: "Action"}
    21  	store.Must(ss.Audit().Save(audit))
    22  	time.Sleep(100 * time.Millisecond)
    23  	store.Must(ss.Audit().Save(audit))
    24  	time.Sleep(100 * time.Millisecond)
    25  	store.Must(ss.Audit().Save(audit))
    26  	time.Sleep(100 * time.Millisecond)
    27  	audit.ExtraInfo = "extra"
    28  	time.Sleep(100 * time.Millisecond)
    29  	store.Must(ss.Audit().Save(audit))
    30  
    31  	time.Sleep(100 * time.Millisecond)
    32  
    33  	c := ss.Audit().Get(audit.UserId, 0, 100)
    34  	result := <-c
    35  	audits := result.Data.(model.Audits)
    36  
    37  	if len(audits) != 4 {
    38  		t.Fatal("Failed to save and retrieve 4 audit logs")
    39  	}
    40  
    41  	if audits[0].ExtraInfo != "extra" {
    42  		t.Fatal("Failed to save property for extra info")
    43  	}
    44  
    45  	c = ss.Audit().Get("missing", 0, 100)
    46  	result = <-c
    47  	audits = result.Data.(model.Audits)
    48  
    49  	if len(audits) != 0 {
    50  		t.Fatal("Should have returned empty because user_id is missing")
    51  	}
    52  
    53  	c = ss.Audit().Get("", 0, 100)
    54  	result = <-c
    55  	audits = result.Data.(model.Audits)
    56  
    57  	if len(audits) < 4 {
    58  		t.Fatal("Failed to save and retrieve 4 audit logs")
    59  	}
    60  
    61  	if r2 := <-ss.Audit().PermanentDeleteByUser(audit.UserId); r2.Err != nil {
    62  		t.Fatal(r2.Err)
    63  	}
    64  }
    65  
    66  func testAuditStorePermanentDeleteBatch(t *testing.T, ss store.Store) {
    67  	a1 := &model.Audit{UserId: model.NewId(), IpAddress: "ipaddress", Action: "Action"}
    68  	store.Must(ss.Audit().Save(a1))
    69  	time.Sleep(10 * time.Millisecond)
    70  	a2 := &model.Audit{UserId: a1.UserId, IpAddress: "ipaddress", Action: "Action"}
    71  	store.Must(ss.Audit().Save(a2))
    72  	time.Sleep(10 * time.Millisecond)
    73  	cutoff := model.GetMillis()
    74  	time.Sleep(10 * time.Millisecond)
    75  	a3 := &model.Audit{UserId: a1.UserId, IpAddress: "ipaddress", Action: "Action"}
    76  	store.Must(ss.Audit().Save(a3))
    77  
    78  	if r := <-ss.Audit().Get(a1.UserId, 0, 100); len(r.Data.(model.Audits)) != 3 {
    79  		t.Fatal("Expected 3 audits. Got ", len(r.Data.(model.Audits)))
    80  	}
    81  
    82  	store.Must(ss.Audit().PermanentDeleteBatch(cutoff, 1000000))
    83  
    84  	if r := <-ss.Audit().Get(a1.UserId, 0, 100); len(r.Data.(model.Audits)) != 1 {
    85  		t.Fatal("Expected 1 audit. Got ", len(r.Data.(model.Audits)))
    86  	}
    87  
    88  	if r2 := <-ss.Audit().PermanentDeleteByUser(a1.UserId); r2.Err != nil {
    89  		t.Fatal(r2.Err)
    90  	}
    91  }