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 PortalHandlerTestSuite struct { suite.Suite db *gorm.DB handler *PortalHandler account *model.Account } func (s *PortalHandlerTestSuite) 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.Portal{}, &model.Article{}, &model.Category{}, &model.Folder{}, &model.PortalMember{})) s.db = db repo := repository.NewPortalRepo(db) svc := service.NewPortalService(repo) s.handler = NewPortalHandler(svc) s.account = &model.Account{Name: "test-portal-account"} s.Require().NoError(db.Create(s.account).Error) } func (s *PortalHandlerTestSuite) SetupTest() { s.db.Exec("DELETE FROM portals") } func (s *PortalHandlerTestSuite) TearDownSuite() { if s.db != nil { sqlDB, _ := s.db.DB() sqlDB.Close() } } func TestPortalHandlerSuite(t *testing.T) { suite.Run(t, new(PortalHandlerTestSuite)) } func (s *PortalHandlerTestSuite) TestCreate_Success() { r := gin.New() r.POST("/api/v1/accounts/:account_id/portals", s.handler.Create) w := httptest.NewRecorder() body := fmt.Sprintf(`{"name":"test-portal","slug":"test-slug"}`) req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/portals", s.account.ID), bytes.NewBufferString(body)) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusCreated, w.Code) } func (s *PortalHandlerTestSuite) TestGet_Success() { portal := &model.Portal{AccountID: s.account.ID, Name: "get-portal", Slug: "get-slug"} s.Require().NoError(s.db.Create(portal).Error) r := gin.New() r.GET("/api/v1/accounts/:account_id/portals/:portal_id", s.handler.Get) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/portals/%d", s.account.ID, portal.ID), nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusOK, w.Code) } func (s *PortalHandlerTestSuite) TestUpdate_Success() { portal := &model.Portal{AccountID: s.account.ID, Name: "update-portal", Slug: "update-slug"} s.Require().NoError(s.db.Create(portal).Error) r := gin.New() r.PUT("/api/v1/accounts/:account_id/portals/:portal_id", s.handler.Update) w := httptest.NewRecorder() body := `{"name":"updated-portal","slug":"updated-slug"}` req, _ := http.NewRequest("PUT", fmt.Sprintf("/api/v1/accounts/%d/portals/%d", s.account.ID, portal.ID), bytes.NewBufferString(body)) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusOK, w.Code) } func (s *PortalHandlerTestSuite) TestDelete_Success() { portal := &model.Portal{AccountID: s.account.ID, Name: "delete-portal", Slug: "delete-slug"} s.Require().NoError(s.db.Create(portal).Error) r := gin.New() r.DELETE("/api/v1/accounts/:account_id/portals/:portal_id", s.handler.Delete) w := httptest.NewRecorder() req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/portals/%d", s.account.ID, portal.ID), nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusNoContent, w.Code) } func (s *PortalHandlerTestSuite) TestCreate_BadRequest_InvalidAccountID() { r := gin.New() r.POST("/api/v1/accounts/:account_id/portals", s.handler.Create) w := httptest.NewRecorder() body := `{"name":"test-portal","slug":"test-slug"}` req, _ := http.NewRequest("POST", "/api/v1/accounts/abc/portals", bytes.NewBufferString(body)) req.Header.Set("Content-Type", "application/json") r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusBadRequest, w.Code) } func (s *PortalHandlerTestSuite) TestGet_BadRequest_InvalidID() { r := gin.New() r.GET("/api/v1/accounts/:account_id/portals/:portal_id", s.handler.Get) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/portals/abc", s.account.ID), nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusBadRequest, w.Code) }