2021-08-17 01:12:07 +00:00
|
|
|
package stacks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gofrs/uuid"
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
2021-09-27 00:52:50 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-08-17 01:12:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandler_webhookInvoke(t *testing.T) {
|
2021-09-27 00:52:50 +00:00
|
|
|
store, teardown := bolt.MustNewTestStore(true)
|
2021-08-17 01:12:07 +00:00
|
|
|
defer teardown()
|
|
|
|
|
|
|
|
webhookID := newGuidString(t)
|
|
|
|
store.StackService.CreateStack(&portainer.Stack{
|
|
|
|
AutoUpdate: &portainer.StackAutoUpdate{
|
|
|
|
Webhook: webhookID,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
h := NewHandler(nil)
|
|
|
|
h.DataStore = store
|
|
|
|
|
|
|
|
t.Run("invalid uuid results in http.StatusBadRequest", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
req := newRequest("notuuid")
|
|
|
|
h.Router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
|
|
})
|
|
|
|
t.Run("registered webhook ID in http.StatusNoContent", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
req := newRequest(webhookID)
|
|
|
|
h.Router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusNoContent, w.Code)
|
|
|
|
})
|
|
|
|
t.Run("unregistered webhook ID in http.StatusNotFound", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
req := newRequest(newGuidString(t))
|
|
|
|
h.Router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func newGuidString(t *testing.T) string {
|
|
|
|
uuid, err := uuid.NewV4()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
return uuid.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRequest(webhookID string) *http.Request {
|
|
|
|
return httptest.NewRequest(http.MethodPost, "/stacks/webhooks/"+webhookID, nil)
|
|
|
|
}
|