mirror of
https://github.com/ddworken/hishtory.git
synced 2025-06-25 22:42:04 +02:00
Test refactoring to remove a function that can be replaced with testify
This commit is contained in:
parent
7ad1e2fb03
commit
2e793b2c10
@ -14,6 +14,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/ddworken/hishtory/backend/server/internal/database"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/gorm"
|
||||
|
||||
@ -102,7 +103,7 @@ func TestESubmitThenQuery(t *testing.T) {
|
||||
require.Equal(t, 0, dbEntry.ReadCount)
|
||||
decEntry, err := data.DecryptHistoryEntry("key", *dbEntry)
|
||||
require.NoError(t, err)
|
||||
require.True(t, data.EntryEquals(decEntry, entry))
|
||||
require.Equal(t, decEntry, entry)
|
||||
|
||||
// Same for device id 2
|
||||
w = httptest.NewRecorder()
|
||||
@ -128,9 +129,7 @@ func TestESubmitThenQuery(t *testing.T) {
|
||||
}
|
||||
decEntry, err = data.DecryptHistoryEntry("key", *dbEntry)
|
||||
require.NoError(t, err)
|
||||
if !data.EntryEquals(decEntry, entry) {
|
||||
t.Fatalf("DB data is different than input! \ndb =%#v\ninput=%#v", *dbEntry, entry)
|
||||
}
|
||||
require.Equal(t, decEntry, entry)
|
||||
|
||||
// Bootstrap handler should return 2 entries, one for each device
|
||||
w = httptest.NewRecorder()
|
||||
@ -307,9 +306,7 @@ func TestDumpRequestAndResponse(t *testing.T) {
|
||||
}
|
||||
decEntry, err := data.DecryptHistoryEntry("dkey", *dbEntry)
|
||||
require.NoError(t, err)
|
||||
if !data.EntryEquals(decEntry, entry1Dec) && !data.EntryEquals(decEntry, entry2Dec) {
|
||||
t.Fatalf("DB data is different than input! \ndb =%#v\nentry1=%#v\nentry2=%#v", *dbEntry, entry1Dec, entry2Dec)
|
||||
}
|
||||
require.True(t, assert.ObjectsAreEqual(decEntry, entry1Dec) || assert.ObjectsAreEqual(decEntry, entry2Dec))
|
||||
}
|
||||
|
||||
// Assert that we aren't leaking connections
|
||||
@ -404,9 +401,7 @@ func TestDeletionRequests(t *testing.T) {
|
||||
}
|
||||
decEntry, err := data.DecryptHistoryEntry("dkey", *dbEntry)
|
||||
require.NoError(t, err)
|
||||
if !data.EntryEquals(decEntry, entry1) && !data.EntryEquals(decEntry, entry2) {
|
||||
t.Fatalf("DB data is different than input! \ndb =%#v\nentry1=%#v\nentry2=%#v", *dbEntry, entry1, entry2)
|
||||
}
|
||||
require.True(t, assert.ObjectsAreEqual(decEntry, entry1) || assert.ObjectsAreEqual(decEntry, entry2))
|
||||
}
|
||||
|
||||
// Submit a redact request for entry1
|
||||
@ -448,9 +443,7 @@ func TestDeletionRequests(t *testing.T) {
|
||||
}
|
||||
decEntry, err := data.DecryptHistoryEntry("dkey", *dbEntry)
|
||||
require.NoError(t, err)
|
||||
if !data.EntryEquals(decEntry, entry2) {
|
||||
t.Fatalf("DB data is different than input! \ndb =%#v\nentry=%#v", *dbEntry, entry2)
|
||||
}
|
||||
require.Equal(t, decEntry, entry2)
|
||||
|
||||
// Query for user 2
|
||||
w = httptest.NewRecorder()
|
||||
@ -476,9 +469,7 @@ func TestDeletionRequests(t *testing.T) {
|
||||
}
|
||||
decEntry, err = data.DecryptHistoryEntry("dOtherkey", *dbEntry)
|
||||
require.NoError(t, err)
|
||||
if !data.EntryEquals(decEntry, entry3) {
|
||||
t.Fatalf("DB data is different than input! \ndb =%#v\nentry=%#v", *dbEntry, entry3)
|
||||
}
|
||||
require.Equal(t, decEntry, entry3)
|
||||
|
||||
// Check that apiSubmit tells the client that there is a pending deletion request
|
||||
encEntry, err = data.EncryptHistoryEntry("dkey", entry2)
|
||||
|
@ -157,18 +157,6 @@ func DecryptHistoryEntry(userSecret string, entry shared.EncHistoryEntry) (Histo
|
||||
return decryptedEntry, nil
|
||||
}
|
||||
|
||||
func EntryEquals(entry1, entry2 HistoryEntry) bool {
|
||||
// TODO: Can we remove this function? Or at least move it to a test-only file?
|
||||
return entry1.LocalUsername == entry2.LocalUsername &&
|
||||
entry1.Hostname == entry2.Hostname &&
|
||||
entry1.Command == entry2.Command &&
|
||||
entry1.CurrentWorkingDirectory == entry2.CurrentWorkingDirectory &&
|
||||
entry1.HomeDirectory == entry2.HomeDirectory &&
|
||||
entry1.ExitCode == entry2.ExitCode &&
|
||||
entry1.StartTime.Format(time.RFC3339) == entry2.StartTime.Format(time.RFC3339) &&
|
||||
entry1.EndTime.Format(time.RFC3339) == entry2.EndTime.Format(time.RFC3339)
|
||||
}
|
||||
|
||||
func GetHishtoryPath() string {
|
||||
hishtoryPath := os.Getenv("HISHTORY_PATH")
|
||||
if hishtoryPath != "" {
|
||||
|
@ -82,9 +82,7 @@ func TestPersist(t *testing.T) {
|
||||
t.Fatalf("DB has %d entries, expected 1!", len(historyEntries))
|
||||
}
|
||||
dbEntry := historyEntries[0]
|
||||
if !data.EntryEquals(entry, *dbEntry) {
|
||||
t.Fatalf("DB data is different than input! \ndb =%#v \ninput=%#v", *dbEntry, entry)
|
||||
}
|
||||
require.Equal(t, entry, *dbEntry)
|
||||
}
|
||||
|
||||
func TestSearch(t *testing.T) {
|
||||
@ -105,12 +103,8 @@ func TestSearch(t *testing.T) {
|
||||
if len(results) != 2 {
|
||||
t.Fatalf("Search() returned %d results, expected 2, results=%#v", len(results), results)
|
||||
}
|
||||
if !data.EntryEquals(*results[0], entry2) {
|
||||
t.Fatalf("Search()[0]=%#v, expected: %#v", results[0], entry2)
|
||||
}
|
||||
if !data.EntryEquals(*results[1], entry1) {
|
||||
t.Fatalf("Search()[0]=%#v, expected: %#v", results[1], entry1)
|
||||
}
|
||||
require.Equal(t, entry2, *results[0])
|
||||
require.Equal(t, entry1, *results[1])
|
||||
|
||||
// Search but exclude bar
|
||||
results, err = Search(ctx, db, "ls -bar", 5)
|
||||
|
Loading…
x
Reference in New Issue
Block a user