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 CustomRoleHandlerTestSuite struct { suite.Suite db *gorm.DB handler *CustomRoleHandler account *model.Account } func (s *CustomRoleHandlerTestSuite) 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.CustomRole{})) s.db = db repo := repository.NewCustomRoleRepo(db) svc := service.NewCustomRoleService(repo) s.handler = NewCustomRoleHandler(svc) s.account = &model.Account{Name: "test-custom-role-account"} s.Require().NoError(db.Create(s.account).Error) } func (s *CustomRoleHandlerTestSuite) TearDownSuite() { if s.db != nil { sqlDB, _ := s.db.DB() sqlDB.Close() } } func TestCustomRoleHandlerSuite(t *testing.T) { suite.Run(t, new(CustomRoleHandlerTestSuite)) } func (s *CustomRoleHandlerTestSuite) TestList_Success() { r := gin.New() r.GET("/api/v1/accounts/:account_id/custom_roles", s.handler.List) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/custom_roles", s.account.ID), nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusOK, w.Code) } func (s *CustomRoleHandlerTestSuite) TestCreate_BadRequest_EmptyBody() { r := gin.New() r.POST("/api/v1/accounts/:account_id/custom_roles", s.handler.Create) w := httptest.NewRecorder() req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/custom_roles", 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 *CustomRoleHandlerTestSuite) TestGet_BadRequest_InvalidID() { r := gin.New() r.GET("/api/v1/accounts/:account_id/custom_roles/:id", s.handler.Get) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/custom_roles/abc", s.account.ID), nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusBadRequest, w.Code) } func (s *CustomRoleHandlerTestSuite) TestUpdate_BadRequest_InvalidID() { r := gin.New() r.PUT("/api/v1/accounts/:account_id/custom_roles/:id", s.handler.Update) w := httptest.NewRecorder() req, _ := http.NewRequest("PUT", fmt.Sprintf("/api/v1/accounts/%d/custom_roles/abc", 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 *CustomRoleHandlerTestSuite) TestDelete_BadRequest_InvalidID() { r := gin.New() r.DELETE("/api/v1/accounts/:account_id/custom_roles/:id", s.handler.Delete) w := httptest.NewRecorder() req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/custom_roles/abc", s.account.ID), nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusBadRequest, w.Code) } func (s *CustomRoleHandlerTestSuite) TestCreate_Success() { r := gin.New() r.POST("/api/v1/accounts/:account_id/custom_roles", func(c *gin.Context) { c.Set("account_id", uint(s.account.ID)) s.handler.Create(c) }) w := httptest.NewRecorder() body := fmt.Sprintf(`{"custom_role":{"name":"test-role","permissions":{"conversation_manage":"full"}}}`) req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/custom_roles", 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 *CustomRoleHandlerTestSuite) TestGet_Success() { // Create a custom role first role := &model.CustomRole{AccountID: s.account.ID, Name: "get-test-role", Permissions: "inbox_read"} s.Require().NoError(s.db.Create(role).Error) r := gin.New() r.GET("/api/v1/accounts/:account_id/custom_roles/:id", s.handler.Get) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/custom_roles/%d", s.account.ID, role.ID), nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusOK, w.Code) } func (s *CustomRoleHandlerTestSuite) TestUpdate_Success() { role := &model.CustomRole{AccountID: s.account.ID, Name: "update-test-role", Permissions: "inbox_read"} s.Require().NoError(s.db.Create(role).Error) r := gin.New() r.PUT("/api/v1/accounts/:account_id/custom_roles/:id", func(c *gin.Context) { c.Set("account_id", uint(s.account.ID)) s.handler.Update(c) }) w := httptest.NewRecorder() body := fmt.Sprintf(`{"custom_role":{"name":"updated-role","permissions":{"conversation_manage":"full"}}}`) req, _ := http.NewRequest("PUT", fmt.Sprintf("/api/v1/accounts/%d/custom_roles/%d", s.account.ID, role.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 *CustomRoleHandlerTestSuite) TestDelete_Success() { role := &model.CustomRole{AccountID: s.account.ID, Name: "delete-test-role", Permissions: "inbox_read"} s.Require().NoError(s.db.Create(role).Error) r := gin.New() r.DELETE("/api/v1/accounts/:account_id/custom_roles/:id", func(c *gin.Context) { c.Set("account_id", uint(s.account.ID)) s.handler.Delete(c) }) w := httptest.NewRecorder() req, _ := http.NewRequest("DELETE", fmt.Sprintf("/api/v1/accounts/%d/custom_roles/%d", s.account.ID, role.ID), nil) r.ServeHTTP(w, req) assert.Equal(s.T(), http.StatusNoContent, w.Code) }