feat(macros): align attachment uploads
This commit is contained in:
@@ -41,6 +41,7 @@ func (s *MacroHandlerTestSuite) SetupSuite() {
|
||||
s.Require().NoError(err)
|
||||
s.Require().NoError(db.AutoMigrate(
|
||||
&model.Account{}, &model.User{}, &model.AccountUser{}, &model.Inbox{}, &model.Contact{}, &model.Conversation{}, &model.Message{},
|
||||
&model.Attachment{}, &model.DirectUpload{},
|
||||
&automation.Macro{}, &automation.MacroExecution{}, &automation.ConversationLabel{}, &automation.ConversationMute{},
|
||||
))
|
||||
s.db = db
|
||||
@@ -54,6 +55,62 @@ func (s *MacroHandlerTestSuite) SetupSuite() {
|
||||
s.db.Create(s.user)
|
||||
}
|
||||
|
||||
func (s *MacroHandlerTestSuite) TestCreate_AttachmentActionAcceptsSignedUploadAndSerializesFiles() {
|
||||
upload := &model.DirectUpload{
|
||||
UploadUUID: "macro-upload-signed-id",
|
||||
AccountID: s.account.ID,
|
||||
Status: model.DirectUploadStatusPending,
|
||||
Source: model.DirectUploadSourceAccount,
|
||||
OriginalName: "avatar.png",
|
||||
FileType: "image",
|
||||
MimeType: "image/png",
|
||||
FileSize: 123,
|
||||
FileURL: "/uploads/account/1/avatar.png",
|
||||
ThumbURL: "/uploads/account/1/avatar.png",
|
||||
}
|
||||
s.Require().NoError(s.db.Create(upload).Error)
|
||||
r := gin.New()
|
||||
r.POST("/api/v1/accounts/:account_id/macros", func(c *gin.Context) {
|
||||
c.Set("user_id", float64(s.user.ID))
|
||||
c.Next()
|
||||
}, s.handler.Create)
|
||||
body := []byte(`{"name":"send file","visibility":"global","actions":[{"action_name":"send_attachment","action_params":["macro-upload-signed-id"]}]}`)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/macros", s.account.ID), bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
var resp map[string]interface{}
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &resp))
|
||||
payload := resp["payload"].(map[string]interface{})
|
||||
actions := payload["actions"].([]interface{})
|
||||
action := actions[0].(map[string]interface{})
|
||||
assert.Equal(s.T(), []interface{}{float64(upload.ID)}, action["action_params"])
|
||||
files := payload["files"].([]interface{})
|
||||
s.Require().Len(files, 1)
|
||||
file := files[0].(map[string]interface{})
|
||||
assert.Equal(s.T(), float64(upload.ID), file["blob_id"])
|
||||
assert.Equal(s.T(), "avatar.png", file["filename"])
|
||||
}
|
||||
|
||||
func (s *MacroHandlerTestSuite) TestCreate_AttachmentActionRejectsInvalidBlob() {
|
||||
r := gin.New()
|
||||
r.POST("/api/v1/accounts/:account_id/macros", func(c *gin.Context) {
|
||||
c.Set("user_id", float64(s.user.ID))
|
||||
c.Next()
|
||||
}, s.handler.Create)
|
||||
body := []byte(`{"name":"bad file","visibility":"global","actions":[{"action_name":"send_attachment","action_params":["missing-upload"]}]}`)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/macros", s.account.ID), bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusUnprocessableEntity, w.Code)
|
||||
}
|
||||
|
||||
func (s *MacroHandlerTestSuite) TearDownSuite() {
|
||||
if s.db != nil {
|
||||
sqlDB, _ := s.db.DB()
|
||||
|
||||
Reference in New Issue
Block a user