feat(webhooks): align account payloads
This commit is contained in:
@@ -2,6 +2,8 @@ package v1
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -31,7 +33,7 @@ func (s *WebhookSubscriptionHandlerTestSuite) SetupSuite() {
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
s.Require().NoError(db.AutoMigrate(&model.Account{}, &model.WebhookSubscription{}))
|
||||
s.Require().NoError(db.AutoMigrate(&model.Account{}, &model.Inbox{}, &model.WebhookSubscription{}))
|
||||
s.db = db
|
||||
|
||||
repo := repository.NewWebhookSubscriptionRepo(db)
|
||||
@@ -55,21 +57,98 @@ func TestWebhookSubscriptionHandlerSuite(t *testing.T) {
|
||||
|
||||
func (s *WebhookSubscriptionHandlerTestSuite) TestList_Success() {
|
||||
r := gin.New()
|
||||
r.GET("/api/v1/accounts/:account_id/webhooks/:webhook_id/subscriptions", s.handler.List)
|
||||
r.GET("/api/v1/accounts/:account_id/webhooks", s.handler.List)
|
||||
|
||||
_, err := s.handler.webhookSubscriptionService.CreateWebhook(context.Background(), s.account.ID, service.WebhookSubscriptionMutation{
|
||||
Name: "List hook",
|
||||
URL: "https://example.com/list-hook",
|
||||
Subscriptions: []string{"message_created"},
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/webhooks/1/subscriptions", s.account.ID), nil)
|
||||
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/webhooks", s.account.ID), nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
var body map[string]any
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &body))
|
||||
payload := body["payload"].(map[string]any)
|
||||
webhooks := payload["webhooks"].([]any)
|
||||
s.NotEmpty(webhooks)
|
||||
}
|
||||
|
||||
func (s *WebhookSubscriptionHandlerTestSuite) TestCreate_Success_ChatwootPayload() {
|
||||
r := gin.New()
|
||||
r.POST("/api/v1/accounts/:account_id/webhooks", s.handler.Create)
|
||||
|
||||
body := `{"webhook":{"name":"Created hook","url":"https://example.com/created-hook","subscriptions":["conversation_created","message_created"]}}`
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/webhooks", s.account.ID), bytes.NewBufferString(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
var parsed map[string]any
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &parsed))
|
||||
webhook := parsed["payload"].(map[string]any)["webhook"].(map[string]any)
|
||||
s.Equal("Created hook", webhook["name"])
|
||||
s.Equal("https://example.com/created-hook", webhook["url"])
|
||||
s.NotEmpty(webhook["secret"])
|
||||
s.Equal([]any{"conversation_created", "message_created"}, webhook["subscriptions"])
|
||||
}
|
||||
|
||||
func (s *WebhookSubscriptionHandlerTestSuite) TestUpdate_Success_ChatwootPayload() {
|
||||
created, err := s.handler.webhookSubscriptionService.CreateWebhook(context.Background(), s.account.ID, service.WebhookSubscriptionMutation{
|
||||
Name: "Before",
|
||||
URL: "https://example.com/update-before",
|
||||
Subscriptions: []string{"message_created"},
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
|
||||
r := gin.New()
|
||||
r.PATCH("/api/v1/accounts/:account_id/webhooks/:webhook_id", s.handler.Update)
|
||||
|
||||
body := `{"webhook":{"name":"After","url":"https://example.com/update-after","subscriptions":["contact_created"]}}`
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("PATCH", fmt.Sprintf("/api/v1/accounts/%d/webhooks/%d", s.account.ID, created.ID), bytes.NewBufferString(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
var parsed map[string]any
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &parsed))
|
||||
webhook := parsed["payload"].(map[string]any)["webhook"].(map[string]any)
|
||||
s.Equal("After", webhook["name"])
|
||||
s.Equal("https://example.com/update-after", webhook["url"])
|
||||
s.Equal([]any{"contact_created"}, webhook["subscriptions"])
|
||||
}
|
||||
|
||||
func (s *WebhookSubscriptionHandlerTestSuite) TestDelete_Success_ReturnsEmptyOK() {
|
||||
created, err := s.handler.webhookSubscriptionService.CreateWebhook(context.Background(), s.account.ID, service.WebhookSubscriptionMutation{
|
||||
Name: "Delete",
|
||||
URL: "https://example.com/delete-hook",
|
||||
Subscriptions: []string{"message_created"},
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
|
||||
r := gin.New()
|
||||
r.DELETE("/api/v1/accounts/:account_id/webhooks/:webhook_id", s.handler.Delete)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/webhooks/%d", s.account.ID, created.ID), nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
s.Empty(w.Body.String())
|
||||
}
|
||||
|
||||
func (s *WebhookSubscriptionHandlerTestSuite) TestCreate_BadRequest_EmptyBody() {
|
||||
r := gin.New()
|
||||
r.POST("/api/v1/accounts/:account_id/webhooks/:webhook_id/subscriptions", s.handler.Create)
|
||||
r.POST("/api/v1/accounts/:account_id/webhooks", s.handler.Create)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/webhooks/1/subscriptions", s.account.ID), nil)
|
||||
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/webhooks", s.account.ID), nil)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
@@ -78,22 +157,21 @@ func (s *WebhookSubscriptionHandlerTestSuite) TestCreate_BadRequest_EmptyBody()
|
||||
|
||||
func (s *WebhookSubscriptionHandlerTestSuite) TestGet_BadRequest_InvalidID() {
|
||||
r := gin.New()
|
||||
r.GET("/api/v1/accounts/:account_id/webhooks/:webhook_id/subscriptions/:webhook_id", s.handler.Get)
|
||||
r.GET("/api/v1/accounts/:account_id/webhooks/:webhook_id", s.handler.Get)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/webhooks/1/subscriptions/abc", s.account.ID), nil)
|
||||
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/webhooks/abc", s.account.ID), nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
// Get returns 500 for invalid id (parseUintParam error → internal server error path)
|
||||
assert.NotEqual(s.T(), http.StatusOK, w.Code)
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
func (s *WebhookSubscriptionHandlerTestSuite) TestUpdate_BadRequest_InvalidID() {
|
||||
r := gin.New()
|
||||
r.PUT("/api/v1/accounts/:account_id/webhooks/:webhook_id/subscriptions/:webhook_id", s.handler.Update)
|
||||
r.PATCH("/api/v1/accounts/:account_id/webhooks/:webhook_id", s.handler.Update)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("PUT", fmt.Sprintf("/api/v1/accounts/%d/webhooks/1/subscriptions/abc", s.account.ID), bytes.NewBufferString(`{"url":"https://example.com"}`))
|
||||
req, _ := http.NewRequest("PATCH", fmt.Sprintf("/api/v1/accounts/%d/webhooks/abc", s.account.ID), bytes.NewBufferString(`{"webhook":{"url":"https://example.com","subscriptions":["message_created"]}}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
@@ -102,10 +180,10 @@ func (s *WebhookSubscriptionHandlerTestSuite) TestUpdate_BadRequest_InvalidID()
|
||||
|
||||
func (s *WebhookSubscriptionHandlerTestSuite) TestDelete_BadRequest_InvalidID() {
|
||||
r := gin.New()
|
||||
r.DELETE("/api/v1/accounts/:account_id/webhooks/:webhook_id/subscriptions/:webhook_id", s.handler.Delete)
|
||||
r.DELETE("/api/v1/accounts/:account_id/webhooks/:webhook_id", s.handler.Delete)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/webhooks/1/subscriptions/abc", s.account.ID), nil)
|
||||
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/webhooks/abc", s.account.ID), nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
@@ -120,4 +198,4 @@ func (s *WebhookSubscriptionHandlerTestSuite) TestListDeliveries_BadRequest_Inva
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user