126 lines
3.8 KiB
Go
126 lines
3.8 KiB
Go
package v1
|
|
|
|
import (
|
|
"bytes"
|
|
"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 NoteHandlerTestSuite struct {
|
|
suite.Suite
|
|
db *gorm.DB
|
|
handler *NoteHandler
|
|
account *model.Account
|
|
contact *model.Contact
|
|
}
|
|
|
|
func (s *NoteHandlerTestSuite) 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.Note{}))
|
|
s.db = db
|
|
|
|
repo := repository.NewNoteRepo(db)
|
|
svc := service.NewNoteService(repo)
|
|
s.handler = NewNoteHandler(svc)
|
|
|
|
s.account = &model.Account{Name: "test-note-account"}
|
|
s.Require().NoError(db.Create(s.account).Error)
|
|
|
|
s.contact = &model.Contact{AccountID: s.account.ID, Name: "test-contact"}
|
|
s.Require().NoError(db.Create(s.contact).Error)
|
|
}
|
|
|
|
func (s *NoteHandlerTestSuite) TearDownSuite() {
|
|
if s.db != nil {
|
|
sqlDB, _ := s.db.DB()
|
|
sqlDB.Close()
|
|
}
|
|
}
|
|
|
|
func TestNoteHandlerSuite(t *testing.T) {
|
|
suite.Run(t, new(NoteHandlerTestSuite))
|
|
}
|
|
|
|
func (s *NoteHandlerTestSuite) TestList_BadRequest_InvalidContactID() {
|
|
r := gin.New()
|
|
r.GET("/api/v1/accounts/:account_id/contacts/:contact_id/notes", s.handler.List)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/contacts/abc/notes", s.account.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *NoteHandlerTestSuite) TestList_Success() {
|
|
r := gin.New()
|
|
r.GET("/api/v1/accounts/:account_id/contacts/:contact_id/notes", s.handler.List)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/contacts/%d/notes", s.account.ID, s.contact.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
}
|
|
|
|
func (s *NoteHandlerTestSuite) TestCreate_BadRequest_EmptyBody() {
|
|
r := gin.New()
|
|
r.POST("/api/v1/accounts/:account_id/contacts/:contact_id/notes", s.handler.Create)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/contacts/%d/notes", s.account.ID, s.contact.ID), nil)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *NoteHandlerTestSuite) TestShow_BadRequest_InvalidID() {
|
|
r := gin.New()
|
|
r.GET("/api/v1/accounts/:account_id/contacts/:contact_id/notes/:id", s.handler.Show)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/contacts/%d/notes/abc", s.account.ID, s.contact.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *NoteHandlerTestSuite) TestUpdate_BadRequest_InvalidID() {
|
|
r := gin.New()
|
|
r.PUT("/api/v1/accounts/:account_id/contacts/:contact_id/notes/:id", s.handler.Update)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/api/v1/accounts/%d/contacts/%d/notes/abc", s.account.ID, s.contact.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 *NoteHandlerTestSuite) TestDestroy_BadRequest_InvalidID() {
|
|
r := gin.New()
|
|
r.DELETE("/api/v1/accounts/:account_id/contacts/:contact_id/notes/:id", s.handler.Destroy)
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/contacts/%d/notes/abc", s.account.ID, s.contact.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
} |