package handlers_test import ( "os" "path/filepath" "strings" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "sneak.berlin/go/webhooker/internal/database" "sneak.berlin/go/webhooker/internal/delivery" "sneak.berlin/go/webhooker/internal/handlers" "sneak.berlin/go/webhooker/internal/session" ) // TestHandleSourceDetail_ShowsArchiveFile proves a database target's // row names its archive file and says "not created yet" before the // first write, adds the file's size and last write once it has one row, // and says "not created yet" again once the file has been moved away. // A target of another type shows no archive file. func TestHandleSourceDetail_ShowsArchiveFile(t *testing.T) { t.Parallel() var ( h *handlers.Handlers sess *session.Session db *database.Database dbMgr *database.WebhookDBManager ) app := newTestApp(t, &h, &sess, &db, &dbMgr) app.RequireStart() t.Cleanup(app.RequireStop) wh := seedWebhook(t, db) archive := seedTarget(t, db, wh.ID, database.TargetTypeDatabase) seedTarget(t, db, wh.ID, database.TargetTypeLog) path := delivery.ArchivePath(dbMgr, wh, archive) body := renderSourceDetailPage(t, h, sess, wh.ID) assert.Equal(t, 1, strings.Count(body, "Archive File:")) assert.Contains(t, body, filepath.Base(path)) assert.Contains(t, body, "not created yet") assert.NotContains(t, body, "Archive Size:") seedArchive(t, path, 1, 100) file, err := os.Stat(path) require.NoError(t, err) body = renderSourceDetailPage(t, h, sess, wh.ID) assert.Contains(t, body, filepath.Base(path)) assert.NotContains(t, body, "not created yet") assert.Regexp(t, `Archive Size:\s*[1-9][0-9.]* [kM]?B`, body, ) assert.Contains(t, body, `title="`+file.ModTime().UTC().Format(time.DateTime)+` UTC"`, ) require.NoError(t, os.Rename(path, filepath.Join(t.TempDir(), "moved.db"))) body = renderSourceDetailPage(t, h, sess, wh.ID) assert.Contains(t, body, filepath.Base(path)) assert.Contains(t, body, "not created yet") assert.NotContains(t, body, "Archive Size:") }