Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1a48f0541a |
@@ -48,13 +48,7 @@ func TestPreloadedModelsMarshalWithoutTheirParent(t *testing.T) {
|
|||||||
|
|
||||||
assert.Contains(t, encoded, entrypointID)
|
assert.Contains(t, encoded, entrypointID)
|
||||||
assert.Contains(t, encoded, targetID)
|
assert.Contains(t, encoded, targetID)
|
||||||
|
assert.NotContains(t, encoded, `"webhook":`)
|
||||||
// Each child holds the parent's id as its webhookId, so the parent
|
|
||||||
// is looked for by its own id field.
|
|
||||||
parentIDField := `"id":"` + stored.ID + `"`
|
|
||||||
|
|
||||||
assert.NotContains(t, marshalModel(t, webhook.Entrypoints[0]), parentIDField)
|
|
||||||
assert.NotContains(t, marshalModel(t, webhook.Targets[0]), parentIDField)
|
|
||||||
|
|
||||||
var target database.Target
|
var target database.Target
|
||||||
|
|
||||||
@@ -67,7 +61,7 @@ func TestPreloadedModelsMarshalWithoutTheirParent(t *testing.T) {
|
|||||||
encoded = marshalModel(t, target)
|
encoded = marshalModel(t, target)
|
||||||
|
|
||||||
assert.Contains(t, encoded, stored.ID)
|
assert.Contains(t, encoded, stored.ID)
|
||||||
assert.NotContains(t, encoded, parentIDField)
|
assert.NotContains(t, encoded, `"webhook":`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestModelsMarshalWithoutTheirParent covers the other references to a
|
// TestModelsMarshalWithoutTheirParent covers the other references to a
|
||||||
|
|||||||
@@ -1911,13 +1911,9 @@ func (h *Handlers) HandleTargetToggle() http.HandlerFunc {
|
|||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only the active column: saving the whole row would
|
tgt.Active = !tgt.Active
|
||||||
// write back the name and settings read above over an
|
|
||||||
// edit saved since.
|
|
||||||
active := !tgt.Active
|
|
||||||
|
|
||||||
return active, h.db.DB().Model(&tgt).
|
return tgt.Active, h.db.DB().Save(&tgt).Error
|
||||||
Update("active", active).Error
|
|
||||||
},
|
},
|
||||||
"failed to toggle target",
|
"failed to toggle target",
|
||||||
targetActivated, targetDeactivated,
|
targetActivated, targetDeactivated,
|
||||||
|
|||||||
@@ -1,66 +0,0 @@
|
|||||||
package handlers_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestHandleTargetToggle_DoesNotUndoAnEdit proves that a toggle which
|
|
||||||
// loaded the target before an edit of it was saved does not write the
|
|
||||||
// old name and settings back over the edit. The edit is submitted from
|
|
||||||
// a callback on the toggle's own read of the target, so it is saved
|
|
||||||
// after that read and before the toggle writes.
|
|
||||||
func TestHandleTargetToggle_DoesNotUndoAnEdit(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
env := setupSourceTest(t)
|
|
||||||
wh, tgt := seedHTTPTarget(t, env, "", "")
|
|
||||||
require.True(t, tgt.Active)
|
|
||||||
|
|
||||||
var (
|
|
||||||
edited bool
|
|
||||||
editCode int
|
|
||||||
)
|
|
||||||
|
|
||||||
require.NoError(t, env.db.DB().Callback().Query().
|
|
||||||
After("gorm:query").
|
|
||||||
Register("test:edit_after_toggle_read", func(tx *gorm.DB) {
|
|
||||||
// The edit reads the target too; only the toggle's read,
|
|
||||||
// the first, submits it.
|
|
||||||
if tx.Statement.Table != "targets" || edited {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
edited = true
|
|
||||||
editCode = submitTargetEdit(
|
|
||||||
env, wh.ID, tgt.ID,
|
|
||||||
editForm(editReplacedURL, "", ""),
|
|
||||||
).Code
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
||||||
req := postRequest(
|
|
||||||
"/hook/"+wh.ID+"/targets/"+tgt.ID+"/toggle",
|
|
||||||
env.cookies,
|
|
||||||
map[string]string{paramSourceID: wh.ID, paramTargetID: tgt.ID},
|
|
||||||
)
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
|
|
||||||
env.handlers.HandleTargetToggle().ServeHTTP(w, req)
|
|
||||||
|
|
||||||
require.Equal(t, http.StatusSeeOther, w.Code)
|
|
||||||
require.Equal(t, http.StatusSeeOther, editCode)
|
|
||||||
|
|
||||||
stored := storedTarget(t, env, tgt.ID)
|
|
||||||
assert.False(t, stored.Active)
|
|
||||||
assert.Equal(t, "edited-name", stored.Name)
|
|
||||||
assert.Equal(t, 5, stored.MaxRetries)
|
|
||||||
assert.Equal(
|
|
||||||
t, editReplacedURL, storedHTTPConfig(t, env, tgt.ID).URL,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/sessions"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"sneak.berlin/go/webhooker/internal/config"
|
"sneak.berlin/go/webhooker/internal/config"
|
||||||
@@ -77,7 +78,14 @@ func newTestSessionManager(
|
|||||||
key[i] = byte(i)
|
key[i] = byte(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
store := session.NewStore(key)
|
store := sessions.NewCookieStore(key)
|
||||||
|
store.Options = &sessions.Options{
|
||||||
|
Path: "/",
|
||||||
|
MaxAge: 86400 * 7,
|
||||||
|
HttpOnly: true,
|
||||||
|
Secure: false,
|
||||||
|
SameSite: http.SameSiteLaxMode,
|
||||||
|
}
|
||||||
|
|
||||||
var now func() time.Time
|
var now func() time.Time
|
||||||
|
|
||||||
@@ -923,7 +931,8 @@ func metricsAuthMiddleware(
|
|||||||
}
|
}
|
||||||
|
|
||||||
key := make([]byte, testKeySize)
|
key := make([]byte, testKeySize)
|
||||||
store := session.NewStore(key)
|
store := sessions.NewCookieStore(key)
|
||||||
|
store.Options = &sessions.Options{Path: "/", MaxAge: 86400}
|
||||||
|
|
||||||
sessManager := session.NewForTest(store, cfg, log, key, nil)
|
sessManager := session.NewForTest(store, cfg, log, key, nil)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package session
|
||||||
|
|
||||||
|
import "github.com/gorilla/sessions"
|
||||||
|
|
||||||
|
// NewStore exposes the production cookie-store constructor so tests
|
||||||
|
// exercise the store the application actually runs with, rather than a
|
||||||
|
// lookalike assembled in the test.
|
||||||
|
func NewStore(key []byte) *sessions.CookieStore {
|
||||||
|
return newStore(key)
|
||||||
|
}
|
||||||
@@ -8,13 +8,6 @@ import (
|
|||||||
"sneak.berlin/go/webhooker/internal/config"
|
"sneak.berlin/go/webhooker/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewStore exposes the production cookie-store constructor so tests
|
|
||||||
// exercise the store the application actually runs with, rather than a
|
|
||||||
// lookalike assembled in the test.
|
|
||||||
func NewStore(key []byte) *sessions.CookieStore {
|
|
||||||
return newStore(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewForTest creates a Session with a pre-configured cookie store for use
|
// NewForTest creates a Session with a pre-configured cookie store for use
|
||||||
// in tests. This bypasses the fx lifecycle and database dependency, allowing
|
// in tests. This bypasses the fx lifecycle and database dependency, allowing
|
||||||
// middleware and handler tests to use real session functionality. The key
|
// middleware and handler tests to use real session functionality. The key
|
||||||
|
|||||||
Reference in New Issue
Block a user