mirror of
https://github.com/portainer/portainer.git
synced 2025-07-24 07:49:41 +02:00
feat(unit-testing): add a mock for the RequestBouncer EE-5610 (#9089)
This commit is contained in:
parent
933e764a13
commit
f7dd73b0f7
42 changed files with 147 additions and 126 deletions
|
@ -18,7 +18,8 @@ import (
|
|||
"github.com/portainer/portainer/api/crypto"
|
||||
"github.com/portainer/portainer/api/demo"
|
||||
"github.com/portainer/portainer/api/http/offlinegate"
|
||||
i "github.com/portainer/portainer/api/internal/testhelpers"
|
||||
"github.com/portainer/portainer/api/internal/testhelpers"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -48,7 +49,14 @@ func Test_backupHandlerWithoutPassword_shouldCreateATarballArchive(t *testing.T)
|
|||
gate := offlinegate.NewOfflineGate()
|
||||
adminMonitor := adminmonitor.New(time.Hour, nil, context.Background())
|
||||
|
||||
handlerErr := NewHandler(nil, i.NewDatastore(), gate, "./test_assets/handler_test", func() {}, adminMonitor, &demo.Service{}).backup(w, r)
|
||||
handlerErr := NewHandler(
|
||||
testhelpers.NewTestRequestBouncer(),
|
||||
testhelpers.NewDatastore(),
|
||||
gate,
|
||||
"./test_assets/handler_test",
|
||||
func() {},
|
||||
adminMonitor,
|
||||
&demo.Service{}).backup(w, r)
|
||||
assert.Nil(t, handlerErr, "Handler should not fail")
|
||||
|
||||
response := w.Result()
|
||||
|
@ -85,7 +93,14 @@ func Test_backupHandlerWithPassword_shouldCreateEncryptedATarballArchive(t *test
|
|||
gate := offlinegate.NewOfflineGate()
|
||||
adminMonitor := adminmonitor.New(time.Hour, nil, nil)
|
||||
|
||||
handlerErr := NewHandler(nil, i.NewDatastore(), gate, "./test_assets/handler_test", func() {}, adminMonitor, &demo.Service{}).backup(w, r)
|
||||
handlerErr := NewHandler(
|
||||
testhelpers.NewTestRequestBouncer(),
|
||||
testhelpers.NewDatastore(),
|
||||
gate,
|
||||
"./test_assets/handler_test",
|
||||
func() {},
|
||||
adminMonitor,
|
||||
&demo.Service{}).backup(w, r)
|
||||
assert.Nil(t, handlerErr, "Handler should not fail")
|
||||
|
||||
response := w.Result()
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
httperror "github.com/portainer/libhttp/error"
|
||||
"github.com/portainer/portainer/api/adminmonitor"
|
||||
"github.com/portainer/portainer/api/dataservices"
|
||||
|
@ -12,12 +11,14 @@ import (
|
|||
"github.com/portainer/portainer/api/http/middlewares"
|
||||
"github.com/portainer/portainer/api/http/offlinegate"
|
||||
"github.com/portainer/portainer/api/http/security"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// Handler is an http handler responsible for backup and restore portainer state
|
||||
type Handler struct {
|
||||
*mux.Router
|
||||
bouncer *security.RequestBouncer
|
||||
bouncer security.BouncerService
|
||||
dataStore dataservices.DataStore
|
||||
gate *offlinegate.OfflineGate
|
||||
filestorePath string
|
||||
|
@ -27,7 +28,7 @@ type Handler struct {
|
|||
|
||||
// NewHandler creates an new instance of backup handler
|
||||
func NewHandler(
|
||||
bouncer *security.RequestBouncer,
|
||||
bouncer security.BouncerService,
|
||||
dataStore dataservices.DataStore,
|
||||
gate *offlinegate.OfflineGate,
|
||||
filestorePath string,
|
||||
|
|
|
@ -16,7 +16,8 @@ import (
|
|||
"github.com/portainer/portainer/api/adminmonitor"
|
||||
"github.com/portainer/portainer/api/demo"
|
||||
"github.com/portainer/portainer/api/http/offlinegate"
|
||||
i "github.com/portainer/portainer/api/internal/testhelpers"
|
||||
"github.com/portainer/portainer/api/internal/testhelpers"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -49,10 +50,21 @@ func Test_restoreArchive_usingCombinationOfPasswords(t *testing.T) {
|
|||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
datastore := i.NewDatastore(i.WithUsers([]portainer.User{}), i.WithEdgeJobs([]portainer.EdgeJob{}))
|
||||
datastore := testhelpers.NewDatastore(
|
||||
testhelpers.WithUsers([]portainer.User{}),
|
||||
testhelpers.WithEdgeJobs([]portainer.EdgeJob{}),
|
||||
)
|
||||
adminMonitor := adminmonitor.New(time.Hour, datastore, context.Background())
|
||||
|
||||
h := NewHandler(nil, datastore, offlinegate.NewOfflineGate(), "./test_assets/handler_test", func() {}, adminMonitor, &demo.Service{})
|
||||
h := NewHandler(
|
||||
testhelpers.NewTestRequestBouncer(),
|
||||
datastore,
|
||||
offlinegate.NewOfflineGate(),
|
||||
"./test_assets/handler_test",
|
||||
func() {},
|
||||
adminMonitor,
|
||||
&demo.Service{},
|
||||
)
|
||||
|
||||
//backup
|
||||
archive := backup(t, h, test.backupPassword)
|
||||
|
@ -72,10 +84,20 @@ func Test_restoreArchive_shouldFailIfSystemWasAlreadyInitialized(t *testing.T) {
|
|||
admin := portainer.User{
|
||||
Role: portainer.AdministratorRole,
|
||||
}
|
||||
datastore := i.NewDatastore(i.WithUsers([]portainer.User{admin}), i.WithEdgeJobs([]portainer.EdgeJob{}))
|
||||
datastore := testhelpers.NewDatastore(
|
||||
testhelpers.WithUsers([]portainer.User{admin}),
|
||||
testhelpers.WithEdgeJobs([]portainer.EdgeJob{}),
|
||||
)
|
||||
adminMonitor := adminmonitor.New(time.Hour, datastore, context.Background())
|
||||
|
||||
h := NewHandler(nil, datastore, offlinegate.NewOfflineGate(), "./test_assets/handler_test", func() {}, adminMonitor, &demo.Service{})
|
||||
h := NewHandler(testhelpers.NewTestRequestBouncer(),
|
||||
datastore,
|
||||
offlinegate.NewOfflineGate(),
|
||||
"./test_assets/handler_test",
|
||||
func() {},
|
||||
adminMonitor,
|
||||
&demo.Service{},
|
||||
)
|
||||
|
||||
//backup
|
||||
archive := backup(t, h, "password")
|
||||
|
@ -106,11 +128,13 @@ func backup(t *testing.T, h *Handler, password string) []byte {
|
|||
|
||||
func prepareMultipartRequest(password string, file []byte) (*http.Request, error) {
|
||||
var body bytes.Buffer
|
||||
|
||||
w := multipart.NewWriter(&body)
|
||||
err := w.WriteField("password", password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fw, err := w.CreateFormFile("file", "filename")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue