138 lines
4.1 KiB
Go
138 lines
4.1 KiB
Go
package v1
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
canned "github.com/gochat/gochat/internal/canned"
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/suite"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
type cannedDBProvider struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func (p *cannedDBProvider) DB() *gorm.DB { return p.db }
|
|
|
|
type CannedResponseHandlerTestSuite struct {
|
|
suite.Suite
|
|
db *gorm.DB
|
|
handler *CannedResponseHandler
|
|
account *model.Account
|
|
}
|
|
|
|
func (s *CannedResponseHandlerTestSuite) 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{}, &canned.CannedResponse{}))
|
|
s.db = db
|
|
|
|
provider := &cannedDBProvider{db: db}
|
|
svc := canned.NewCannedResponseService(provider)
|
|
s.handler = NewCannedResponseHandler(svc)
|
|
|
|
s.account = &model.Account{Name: "test-canned-account"}
|
|
s.Require().NoError(db.Create(s.account).Error)
|
|
}
|
|
|
|
func (s *CannedResponseHandlerTestSuite) TearDownSuite() {
|
|
if s.db != nil {
|
|
sqlDB, _ := s.db.DB()
|
|
sqlDB.Close()
|
|
}
|
|
}
|
|
|
|
func TestCannedResponseHandlerSuite(t *testing.T) {
|
|
suite.Run(t, new(CannedResponseHandlerTestSuite))
|
|
}
|
|
|
|
func (s *CannedResponseHandlerTestSuite) TestList_BadRequest_InvalidAccountID() {
|
|
r := gin.New()
|
|
r.GET("/api/v1/accounts/:account_id/canned_responses", s.handler.List)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/canned_responses", nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *CannedResponseHandlerTestSuite) TestList_Success() {
|
|
r := gin.New()
|
|
r.GET("/api/v1/accounts/:account_id/canned_responses", s.handler.List)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/canned_responses", s.account.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
}
|
|
|
|
func (s *CannedResponseHandlerTestSuite) TestCreate_BadRequest_EmptyBody() {
|
|
r := gin.New()
|
|
r.POST("/api/v1/accounts/:account_id/canned_responses", s.handler.Create)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/canned_responses", 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 *CannedResponseHandlerTestSuite) TestGet_BadRequest_InvalidID() {
|
|
r := gin.New()
|
|
r.GET("/api/v1/accounts/:account_id/canned_responses/:id", s.handler.Get)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/canned_responses/abc", s.account.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *CannedResponseHandlerTestSuite) TestUpdate_BadRequest_InvalidID() {
|
|
r := gin.New()
|
|
r.PUT("/api/v1/accounts/:account_id/canned_responses/:id", s.handler.Update)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/api/v1/accounts/%d/canned_responses/abc", s.account.ID), bytes.NewBufferString(`{"content":"updated"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *CannedResponseHandlerTestSuite) TestDelete_BadRequest_InvalidID() {
|
|
r := gin.New()
|
|
r.DELETE("/api/v1/accounts/:account_id/canned_responses/:id", s.handler.Delete)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/canned_responses/abc", s.account.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *CannedResponseHandlerTestSuite) TestSearch_BadRequest_InvalidAccountID() {
|
|
r := gin.New()
|
|
r.GET("/api/v1/accounts/:account_id/canned_responses/search", s.handler.Search)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/canned_responses/search?q=test", nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
} |