122 lines
3.9 KiB
Go
122 lines
3.9 KiB
Go
package v1
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/repository"
|
|
"github.com/gochat/gochat/internal/service"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/suite"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
type ContactMergeHandlerTestSuite struct {
|
|
suite.Suite
|
|
db *gorm.DB
|
|
handler *ContactMergeHandler
|
|
|
|
account *model.Account
|
|
contact1 *model.Contact
|
|
contact2 *model.Contact
|
|
}
|
|
|
|
func (s *ContactMergeHandlerTestSuite) SetupSuite() {
|
|
gin.SetMode(gin.TestMode)
|
|
db, err := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{
|
|
Logger: logger.Default.LogMode(logger.Silent),
|
|
})
|
|
s.Require().NoError(err)
|
|
s.Require().NoError(db.AutoMigrate(&model.Account{}, &model.Contact{}, &model.ContactInbox{}, &model.Conversation{}, &model.Message{}, &model.ContactNote{}, &model.Note{}))
|
|
s.db = db
|
|
|
|
mergeRepo := repository.NewContactMergeRepo(db)
|
|
mergeSvc := service.NewContactMergeService(mergeRepo, db)
|
|
s.handler = NewContactMergeHandler(mergeSvc)
|
|
|
|
s.account = &model.Account{Name: "test-merge-account"}
|
|
s.db.Create(s.account)
|
|
s.contact1 = &model.Contact{Name: "base-contact", AccountID: s.account.ID}
|
|
s.db.Create(s.contact1)
|
|
s.contact2 = &model.Contact{Name: "target-contact", AccountID: s.account.ID}
|
|
s.db.Create(s.contact2)
|
|
}
|
|
|
|
func (s *ContactMergeHandlerTestSuite) TearDownSuite() {
|
|
if s.db != nil {
|
|
sqlDB, _ := s.db.DB()
|
|
sqlDB.Close()
|
|
}
|
|
}
|
|
|
|
func TestContactMergeHandlerSuite(t *testing.T) {
|
|
suite.Run(t, new(ContactMergeHandlerTestSuite))
|
|
}
|
|
|
|
func (s *ContactMergeHandlerTestSuite) TestCreate_Success() {
|
|
r := gin.New()
|
|
r.POST("/api/v1/accounts/:account_id/actions/contact_merge", func(c *gin.Context) {
|
|
c.Set("account_id", float64(s.account.ID))
|
|
c.Next()
|
|
}, s.handler.Create)
|
|
|
|
base := &model.Contact{Name: "merge-base", Email: "base@example.com", AccountID: s.account.ID}
|
|
mergee := &model.Contact{Name: "mergee-contact", PhoneNumber: "+12212345", AccountID: s.account.ID}
|
|
s.Require().NoError(s.db.Create(base).Error)
|
|
s.Require().NoError(s.db.Create(mergee).Error)
|
|
|
|
body := map[string]uint{"base_contact_id": base.ID, "mergee_contact_id": mergee.ID}
|
|
b, _ := json.Marshal(body)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/actions/contact_merge", s.account.ID), bytes.NewBuffer(b))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
var payload map[string]any
|
|
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &payload))
|
|
assert.Equal(s.T(), float64(base.ID), payload["id"])
|
|
assert.Equal(s.T(), "base@example.com", payload["email"])
|
|
assert.NotContains(s.T(), payload, "success")
|
|
assert.NotContains(s.T(), payload, "data")
|
|
}
|
|
|
|
func (s *ContactMergeHandlerTestSuite) TestCreate_BadRequest_NoBody() {
|
|
r := gin.New()
|
|
r.POST("/api/v1/accounts/:account_id/actions/contact_merge", func(c *gin.Context) {
|
|
c.Set("account_id", float64(s.account.ID))
|
|
c.Next()
|
|
}, s.handler.Create)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/actions/contact_merge", s.account.ID), nil)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *ContactMergeHandlerTestSuite) TestCreate_BadRequest_InvalidAccountID() {
|
|
r := gin.New()
|
|
r.POST("/api/v1/accounts/:account_id/actions/contact_merge", s.handler.Create)
|
|
|
|
body := map[string]uint{"base_contact_id": s.contact1.ID, "mergee_contact_id": s.contact2.ID}
|
|
b, _ := json.Marshal(body)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/actions/contact_merge", bytes.NewBuffer(b))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|