Files
webhooker/internal/handlers/delivery_attempts_test.go
T
clawbot e67ac700c4
check / check (push) Successful in 3m21s
Say what a database or log target's attempt did, without a status (closes #388)
A database or log target sends no HTTP request, yet each of its attempts
read "success  Status: — (no response)", which for an http target means
a failed connection. The shared attempt template now reads a successful
database attempt as "archived" and a successful log attempt as "written
to the log", and shows no status for either, in the event log and on the
event's page. http and slack attempts are unchanged. One test per target
type checks both pages.

Model: opus-5-5
2026-10-03 00:14:26 +00:00

80 lines
2.0 KiB
Go

package handlers_test
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm/clause"
"sneak.berlin/go/webhooker/internal/database"
)
// TestDeliveryAttempts_ReadInTheTargetTypesOwnTerms proves, on the
// event's page and in the event log, that an http or slack attempt
// shows its status as before, while a database or log attempt, which
// sends no HTTP request, says what it did and shows no status.
func TestDeliveryAttempts_ReadInTheTargetTypesOwnTerms(t *testing.T) {
t.Parallel()
cases := []struct {
targetType database.TargetType
success bool
statusCode int
outcome string
status string // "" when the attempt must show no status
}{
{
database.TargetTypeHTTP, false, 0,
"failure", "Status: — (no response)",
},
{
database.TargetTypeSlack, true, http.StatusOK,
"success", "Status: 200",
},
{database.TargetTypeDatabase, true, 0, "archived", ""},
{database.TargetTypeLog, true, 0, "written to the log", ""},
}
for _, tc := range cases {
t.Run(string(tc.targetType), func(t *testing.T) {
t.Parallel()
f := newRecentEventsFixture(t)
target := seedTarget(t, f.db, f.webhook.ID, tc.targetType)
event := f.event(t, contentTypeJSON, "{}", time.Now())
dlv := f.delivery(
t, event, target.ID, database.DeliveryStatusDelivered,
)
require.NoError(t, f.webhookDB.Omit(clause.Associations).Create(
&database.DeliveryResult{
DeliveryID: dlv.ID,
AttemptNum: 1,
Success: tc.success,
StatusCode: tc.statusCode,
},
).Error)
w := serveEventPage(t, f.h, f.sess, f.webhook.ID, event.ID)
require.Equal(t, http.StatusOK, w.Code)
pages := []string{
w.Body.String(),
renderSourceLogsPage(t, f.h, f.sess, f.webhook.ID),
}
for _, page := range pages {
assert.Contains(t, page, ">"+tc.outcome+"</span>")
if tc.status == "" {
assert.NotContains(t, page, "Status:")
} else {
assert.Contains(t, page, tc.status)
}
}
})
}
}