check / check (push) Waiting to run
Target.MaxRetries (max_retries) becomes Target.MaxAttempts (max_attempts), and the form field, parser and ceiling are renamed to match. The column is NOT NULL and a check constraint refuses a value below 1, so every target, whatever its type, is stored with at least 1. 1 is a single attempt with no circuit breaker; above 1 retries behind the breaker up to that many attempts. Both target forms refuse 0 and less; a blank field on the add target form gives 1, and on the edit form keeps the stored value. The target page shows the plain number, and the README's two paragraphs on 0 against 1 become one sentence. Model: opus-5-5
71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
package server_test
|
|
|
|
import (
|
|
"compress/gzip"
|
|
"encoding/json"
|
|
"net/http"
|
|
"regexp"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/gorm/clause"
|
|
"sneak.berlin/go/webhooker/internal/database"
|
|
)
|
|
|
|
// TestHook_DownloadArchive follows the Download link the webhook page
|
|
// shows for a database target, and only for it, and gets the archive
|
|
// as a gzipped JSON file. Signed out, the link leads to the login page.
|
|
func TestHook_DownloadArchive(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
env := newTestEnv(t)
|
|
|
|
userID, _ := env.seedUser(t, "archivist", "somepassword")
|
|
cookies := env.authCookies(t, userID, "archivist")
|
|
wh := env.seedWebhook(t, userID)
|
|
env.seedTarget(t, wh.ID)
|
|
|
|
archive := &database.Target{
|
|
WebhookID: wh.ID,
|
|
Name: "kept",
|
|
Type: database.TargetTypeDatabase,
|
|
Active: true,
|
|
MaxAttempts: 1,
|
|
}
|
|
require.NoError(t,
|
|
env.db.DB().Omit(clause.Associations).Create(archive).Error,
|
|
)
|
|
|
|
page := env.get("/hook/"+wh.ID, cookies)
|
|
require.Equal(t, http.StatusOK, page.Code)
|
|
|
|
links := regexp.MustCompile(
|
|
`href="(/hook/[^/"]+/targets/[^/"]+/download)"`,
|
|
).FindAllStringSubmatch(page.Body.String(), -1)
|
|
require.Len(t, links, 1, "only the database target has a Download")
|
|
|
|
link := links[0][1]
|
|
assert.Equal(t,
|
|
"/hook/"+wh.ID+"/targets/"+archive.ID+"/download", link,
|
|
)
|
|
|
|
w := env.get(link, cookies)
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
assert.Equal(t, "application/gzip", w.Header().Get("Content-Type"))
|
|
|
|
zr, err := gzip.NewReader(w.Body)
|
|
require.NoError(t, err)
|
|
|
|
var got map[string]json.RawMessage
|
|
|
|
require.NoError(t, json.NewDecoder(zr).Decode(&got))
|
|
assert.JSONEq(t,
|
|
`{"id":"`+wh.ID+`","name":"routed"}`, string(got["webhook"]),
|
|
)
|
|
|
|
w = env.get(link, nil)
|
|
assert.Equal(t, http.StatusSeeOther, w.Code)
|
|
assert.Contains(t, w.Header().Get("Location"), "/pages/login")
|
|
}
|