github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/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/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/mattermost/mattermost-server/v5/model"
    14  	"github.com/mattermost/mattermost-server/v5/store"
    15  )
    16  
    17  func TestAuditStore(t *testing.T, ss store.Store) {
    18  	t.Run("", func(t *testing.T) { testAuditStore(t, ss) })
    19  }
    20  
    21  func testAuditStore(t *testing.T, ss store.Store) {
    22  	audit := &model.Audit{UserId: model.NewId(), IpAddress: "ipaddress", Action: "Action"}
    23  	require.NoError(t, ss.Audit().Save(audit))
    24  	time.Sleep(100 * time.Millisecond)
    25  	require.NoError(t, ss.Audit().Save(audit))
    26  	time.Sleep(100 * time.Millisecond)
    27  	require.NoError(t, ss.Audit().Save(audit))
    28  	time.Sleep(100 * time.Millisecond)
    29  	audit.ExtraInfo = "extra"
    30  	time.Sleep(100 * time.Millisecond)
    31  	require.NoError(t, ss.Audit().Save(audit))
    32  
    33  	time.Sleep(100 * time.Millisecond)
    34  
    35  	audits, err := ss.Audit().Get(audit.UserId, 0, 100)
    36  	require.NoError(t, err)
    37  
    38  	assert.Len(t, audits, 4)
    39  
    40  	assert.Equal(t, "extra", audits[0].ExtraInfo)
    41  
    42  	audits, err = ss.Audit().Get("missing", 0, 100)
    43  	require.NoError(t, err)
    44  	assert.Empty(t, audits)
    45  
    46  	audits, err = ss.Audit().Get("", 0, 100)
    47  	require.NoError(t, err)
    48  	require.Len(t, audits, 4, "Failed to save and retrieve 4 audit logs")
    49  
    50  	require.NoError(t, ss.Audit().PermanentDeleteByUser(audit.UserId))
    51  }