Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
164 lines
6.0 KiB
Go
164 lines
6.0 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 NotificationSettingHandlerTestSuite struct {
|
|
suite.Suite
|
|
db *gorm.DB
|
|
handler *NotificationSettingHandler
|
|
account *model.Account
|
|
}
|
|
|
|
func (s *NotificationSettingHandlerTestSuite) 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.NotificationSetting{}))
|
|
s.db = db
|
|
|
|
repo := repository.NewNotificationSettingRepo(db)
|
|
svc := service.NewNotificationSettingService(repo)
|
|
s.handler = NewNotificationSettingHandler(svc)
|
|
|
|
s.account = &model.Account{Name: "test-notif-setting-account"}
|
|
s.Require().NoError(db.Create(s.account).Error)
|
|
}
|
|
|
|
func (s *NotificationSettingHandlerTestSuite) TearDownSuite() {
|
|
if s.db != nil {
|
|
sqlDB, _ := s.db.DB()
|
|
sqlDB.Close()
|
|
}
|
|
}
|
|
|
|
func TestNotificationSettingHandlerSuite(t *testing.T) {
|
|
suite.Run(t, new(NotificationSettingHandlerTestSuite))
|
|
}
|
|
|
|
func (s *NotificationSettingHandlerTestSuite) TestShow_BadRequest_InvalidAccountID() {
|
|
r := gin.New()
|
|
r.GET("/api/v1/accounts/:account_id/notification_settings", func(c *gin.Context) {
|
|
c.Set("user_id", float64(1))
|
|
s.handler.Show(c)
|
|
})
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/api/v1/accounts/abc/notification_settings", nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *NotificationSettingHandlerTestSuite) TestShow_Success() {
|
|
r := gin.New()
|
|
r.GET("/api/v1/accounts/:account_id/notification_settings", func(c *gin.Context) {
|
|
c.Set("user_id", float64(11))
|
|
s.handler.Show(c)
|
|
})
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/notification_settings", s.account.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
|
|
var body map[string]interface{}
|
|
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &body))
|
|
assert.NotContains(s.T(), body, "success")
|
|
assert.NotContains(s.T(), body, "data")
|
|
assert.NotContains(s.T(), body, "notification_setting")
|
|
assert.Equal(s.T(), float64(s.account.ID), body["account_id"])
|
|
assert.Equal(s.T(), float64(11), body["user_id"])
|
|
assert.Equal(s.T(), stringSliceToInterfaceSlice(model.AllEmailFlagNames()), body["all_email_flags"])
|
|
assert.Equal(s.T(), stringSliceToInterfaceSlice(model.AllPushFlagNames()), body["all_push_flags"])
|
|
assert.Equal(s.T(), stringSliceToInterfaceSlice(model.AllEmailFlagNames()), body["selected_email_flags"])
|
|
assert.Equal(s.T(), stringSliceToInterfaceSlice(model.AllPushFlagNames()), body["selected_push_flags"])
|
|
}
|
|
|
|
func (s *NotificationSettingHandlerTestSuite) TestUpdate_BadRequest_InvalidAccountID() {
|
|
r := gin.New()
|
|
r.PUT("/api/v1/accounts/:account_id/notification_settings", func(c *gin.Context) {
|
|
c.Set("user_id", float64(1))
|
|
s.handler.Update(c)
|
|
})
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("PUT", "/api/v1/accounts/abc/notification_settings", bytes.NewBufferString(`{"enable_email":true}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *NotificationSettingHandlerTestSuite) TestUpdate_ReturnsRawChatwootPayload() {
|
|
r := gin.New()
|
|
r.GET("/api/v1/accounts/:account_id/notification_settings", func(c *gin.Context) {
|
|
c.Set("user_id", float64(12))
|
|
s.handler.Show(c)
|
|
})
|
|
r.PATCH("/api/v1/accounts/:account_id/notification_settings", func(c *gin.Context) {
|
|
c.Set("user_id", float64(12))
|
|
s.handler.Update(c)
|
|
})
|
|
|
|
body := `{"notification_settings":{"selected_email_flags":["email_conversation_assignment"],"selected_push_flags":["push_conversation_mention"]}}`
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("PATCH", fmt.Sprintf("/api/v1/accounts/%d/notification_settings", s.account.ID), bytes.NewBufferString(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
var data map[string]interface{}
|
|
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &data))
|
|
assert.NotContains(s.T(), data, "success")
|
|
assert.NotContains(s.T(), data, "data")
|
|
assert.NotContains(s.T(), data, "notification_setting")
|
|
assert.Equal(s.T(), float64(s.account.ID), data["account_id"])
|
|
assert.Equal(s.T(), float64(12), data["user_id"])
|
|
assert.Equal(s.T(), stringSliceToInterfaceSlice(model.AllEmailFlagNames()), data["all_email_flags"])
|
|
assert.Equal(s.T(), stringSliceToInterfaceSlice(model.AllPushFlagNames()), data["all_push_flags"])
|
|
assert.Equal(s.T(), []interface{}{"email_conversation_assignment"}, data["selected_email_flags"])
|
|
assert.Equal(s.T(), []interface{}{"push_conversation_mention"}, data["selected_push_flags"])
|
|
|
|
var persisted model.NotificationSetting
|
|
s.Require().NoError(s.db.Where("account_id = ? AND user_id = ?", s.account.ID, 12).First(&persisted).Error)
|
|
assert.Equal(s.T(), []string{"email_conversation_assignment"}, persisted.SelectedEmailFlagNames())
|
|
assert.Equal(s.T(), []string{"push_conversation_mention"}, persisted.SelectedPushFlagNames())
|
|
|
|
w = httptest.NewRecorder()
|
|
req, _ = http.NewRequest("GET", fmt.Sprintf("/api/v1/accounts/%d/notification_settings", s.account.ID), nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(s.T(), http.StatusOK, w.Code)
|
|
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &data))
|
|
assert.Equal(s.T(), []interface{}{"email_conversation_assignment"}, data["selected_email_flags"])
|
|
assert.Equal(s.T(), []interface{}{"push_conversation_mention"}, data["selected_push_flags"])
|
|
}
|
|
|
|
func stringSliceToInterfaceSlice(values []string) []interface{} {
|
|
items := make([]interface{}, 0, len(values))
|
|
for _, value := range values {
|
|
items = append(items, value)
|
|
}
|
|
return items
|
|
}
|