Test refactoring to remove a function that can be replaced with testify

This commit is contained in:
David Dworken 2023-10-01 18:21:11 -07:00
parent 7ad1e2fb03
commit 2e793b2c10
No known key found for this signature in database
3 changed files with 10 additions and 37 deletions

View File

@ -14,6 +14,7 @@ import (
"time" "time"
"github.com/ddworken/hishtory/backend/server/internal/database" "github.com/ddworken/hishtory/backend/server/internal/database"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gorm.io/gorm" "gorm.io/gorm"
@ -102,7 +103,7 @@ func TestESubmitThenQuery(t *testing.T) {
require.Equal(t, 0, dbEntry.ReadCount) require.Equal(t, 0, dbEntry.ReadCount)
decEntry, err := data.DecryptHistoryEntry("key", *dbEntry) decEntry, err := data.DecryptHistoryEntry("key", *dbEntry)
require.NoError(t, err) require.NoError(t, err)
require.True(t, data.EntryEquals(decEntry, entry)) require.Equal(t, decEntry, entry)
// Same for device id 2 // Same for device id 2
w = httptest.NewRecorder() w = httptest.NewRecorder()
@ -128,9 +129,7 @@ func TestESubmitThenQuery(t *testing.T) {
} }
decEntry, err = data.DecryptHistoryEntry("key", *dbEntry) decEntry, err = data.DecryptHistoryEntry("key", *dbEntry)
require.NoError(t, err) require.NoError(t, err)
if !data.EntryEquals(decEntry, entry) { require.Equal(t, decEntry, entry)
t.Fatalf("DB data is different than input! \ndb =%#v\ninput=%#v", *dbEntry, entry)
}
// Bootstrap handler should return 2 entries, one for each device // Bootstrap handler should return 2 entries, one for each device
w = httptest.NewRecorder() w = httptest.NewRecorder()
@ -307,9 +306,7 @@ func TestDumpRequestAndResponse(t *testing.T) {
} }
decEntry, err := data.DecryptHistoryEntry("dkey", *dbEntry) decEntry, err := data.DecryptHistoryEntry("dkey", *dbEntry)
require.NoError(t, err) require.NoError(t, err)
if !data.EntryEquals(decEntry, entry1Dec) && !data.EntryEquals(decEntry, entry2Dec) { require.True(t, assert.ObjectsAreEqual(decEntry, entry1Dec) || assert.ObjectsAreEqual(decEntry, entry2Dec))
t.Fatalf("DB data is different than input! \ndb =%#v\nentry1=%#v\nentry2=%#v", *dbEntry, entry1Dec, entry2Dec)
}
} }
// Assert that we aren't leaking connections // Assert that we aren't leaking connections
@ -404,9 +401,7 @@ func TestDeletionRequests(t *testing.T) {
} }
decEntry, err := data.DecryptHistoryEntry("dkey", *dbEntry) decEntry, err := data.DecryptHistoryEntry("dkey", *dbEntry)
require.NoError(t, err) require.NoError(t, err)
if !data.EntryEquals(decEntry, entry1) && !data.EntryEquals(decEntry, entry2) { require.True(t, assert.ObjectsAreEqual(decEntry, entry1) || assert.ObjectsAreEqual(decEntry, entry2))
t.Fatalf("DB data is different than input! \ndb =%#v\nentry1=%#v\nentry2=%#v", *dbEntry, entry1, entry2)
}
} }
// Submit a redact request for entry1 // Submit a redact request for entry1
@ -448,9 +443,7 @@ func TestDeletionRequests(t *testing.T) {
} }
decEntry, err := data.DecryptHistoryEntry("dkey", *dbEntry) decEntry, err := data.DecryptHistoryEntry("dkey", *dbEntry)
require.NoError(t, err) require.NoError(t, err)
if !data.EntryEquals(decEntry, entry2) { require.Equal(t, decEntry, entry2)
t.Fatalf("DB data is different than input! \ndb =%#v\nentry=%#v", *dbEntry, entry2)
}
// Query for user 2 // Query for user 2
w = httptest.NewRecorder() w = httptest.NewRecorder()
@ -476,9 +469,7 @@ func TestDeletionRequests(t *testing.T) {
} }
decEntry, err = data.DecryptHistoryEntry("dOtherkey", *dbEntry) decEntry, err = data.DecryptHistoryEntry("dOtherkey", *dbEntry)
require.NoError(t, err) require.NoError(t, err)
if !data.EntryEquals(decEntry, entry3) { require.Equal(t, decEntry, entry3)
t.Fatalf("DB data is different than input! \ndb =%#v\nentry=%#v", *dbEntry, entry3)
}
// Check that apiSubmit tells the client that there is a pending deletion request // Check that apiSubmit tells the client that there is a pending deletion request
encEntry, err = data.EncryptHistoryEntry("dkey", entry2) encEntry, err = data.EncryptHistoryEntry("dkey", entry2)

View File

@ -157,18 +157,6 @@ func DecryptHistoryEntry(userSecret string, entry shared.EncHistoryEntry) (Histo
return decryptedEntry, nil 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 { func GetHishtoryPath() string {
hishtoryPath := os.Getenv("HISHTORY_PATH") hishtoryPath := os.Getenv("HISHTORY_PATH")
if hishtoryPath != "" { if hishtoryPath != "" {

View File

@ -82,9 +82,7 @@ func TestPersist(t *testing.T) {
t.Fatalf("DB has %d entries, expected 1!", len(historyEntries)) t.Fatalf("DB has %d entries, expected 1!", len(historyEntries))
} }
dbEntry := historyEntries[0] dbEntry := historyEntries[0]
if !data.EntryEquals(entry, *dbEntry) { require.Equal(t, entry, *dbEntry)
t.Fatalf("DB data is different than input! \ndb =%#v \ninput=%#v", *dbEntry, entry)
}
} }
func TestSearch(t *testing.T) { func TestSearch(t *testing.T) {
@ -105,12 +103,8 @@ func TestSearch(t *testing.T) {
if len(results) != 2 { if len(results) != 2 {
t.Fatalf("Search() returned %d results, expected 2, results=%#v", len(results), results) t.Fatalf("Search() returned %d results, expected 2, results=%#v", len(results), results)
} }
if !data.EntryEquals(*results[0], entry2) { require.Equal(t, entry2, *results[0])
t.Fatalf("Search()[0]=%#v, expected: %#v", results[0], entry2) require.Equal(t, entry1, *results[1])
}
if !data.EntryEquals(*results[1], entry1) {
t.Fatalf("Search()[0]=%#v, expected: %#v", results[1], entry1)
}
// Search but exclude bar // Search but exclude bar
results, err = Search(ctx, db, "ls -bar", 5) results, err = Search(ctx, db, "ls -bar", 5)