mirror of https://github.com/portainer/portainer
				
				
				
			
		
			
				
	
	
		
			63 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
			
		
		
	
	
			63 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
| package stacks
 | |
| 
 | |
| import (
 | |
| 	"net/http"
 | |
| 	"net/http/httptest"
 | |
| 	"testing"
 | |
| 
 | |
| 	portainer "github.com/portainer/portainer/api"
 | |
| 	"github.com/portainer/portainer/api/datastore"
 | |
| 	"github.com/portainer/portainer/api/internal/testhelpers"
 | |
| 
 | |
| 	"github.com/gofrs/uuid"
 | |
| 	"github.com/stretchr/testify/assert"
 | |
| 	"github.com/stretchr/testify/require"
 | |
| )
 | |
| 
 | |
| func TestHandler_webhookInvoke(t *testing.T) {
 | |
| 	_, store := datastore.MustNewTestStore(t, true, true)
 | |
| 
 | |
| 	webhookID := newGuidString(t)
 | |
| 	store.StackService.Create(&portainer.Stack{
 | |
| 		ID: 1,
 | |
| 		AutoUpdate: &portainer.AutoUpdateSettings{
 | |
| 			Webhook: webhookID,
 | |
| 		},
 | |
| 	})
 | |
| 
 | |
| 	h := NewHandler(testhelpers.NewTestRequestBouncer())
 | |
| 	h.DataStore = store
 | |
| 
 | |
| 	t.Run("invalid uuid results in http.StatusBadRequest", func(t *testing.T) {
 | |
| 		w := httptest.NewRecorder()
 | |
| 		req := newRequest("notuuid")
 | |
| 		h.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.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.ServeHTTP(w, req)
 | |
| 		assert.Equal(t, http.StatusNotFound, w.Code)
 | |
| 	})
 | |
| }
 | |
| 
 | |
| func newGuidString(t *testing.T) string {
 | |
| 	uuid, err := uuid.NewV4()
 | |
| 	require.NoError(t, err)
 | |
| 
 | |
| 	return uuid.String()
 | |
| }
 | |
| 
 | |
| func newRequest(webhookID string) *http.Request {
 | |
| 	return httptest.NewRequest(http.MethodPost, "/stacks/webhooks/"+webhookID, nil)
 | |
| }
 |