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.
174 lines
5.5 KiB
Go
174 lines
5.5 KiB
Go
package v1
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/suite"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/repository"
|
|
"github.com/gochat/gochat/internal/service"
|
|
)
|
|
|
|
type PushSubscriptionHandlerTestSuite struct {
|
|
suite.Suite
|
|
db *gorm.DB
|
|
handler *PushSubscriptionHandler
|
|
router *gin.Engine
|
|
user *model.User
|
|
}
|
|
|
|
func (s *PushSubscriptionHandlerTestSuite) SetupSuite() {
|
|
s.db, _ = gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
s.db.AutoMigrate(&model.PushToken{}, &model.User{}, &model.Account{})
|
|
|
|
repo := repository.NewPushTokenRepo(s.db)
|
|
svc := service.NewPushSubscriptionService(repo)
|
|
s.handler = NewPushSubscriptionHandler(svc)
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
r := gin.New()
|
|
|
|
// Middleware to inject user_id into context (PushSubscription uses getUserID, not getAccountID)
|
|
r.Use(func(c *gin.Context) {
|
|
c.Set("user_id", uint(1))
|
|
c.Next()
|
|
})
|
|
|
|
pushGroup := r.Group("/api/v1/push_subscriptions")
|
|
pushGroup.GET("", s.handler.List)
|
|
pushGroup.POST("", s.handler.Create)
|
|
pushGroup.DELETE("/:id", s.handler.Delete)
|
|
|
|
s.router = r
|
|
|
|
// Create test user
|
|
s.user = &model.User{Name: "TestUser", Email: "test@test.com"}
|
|
s.db.Create(s.user)
|
|
}
|
|
|
|
func (s *PushSubscriptionHandlerTestSuite) TearDownSuite() {
|
|
s.db.Exec("DELETE FROM push_tokens")
|
|
s.db.Exec("DELETE FROM users")
|
|
}
|
|
|
|
func (s *PushSubscriptionHandlerTestSuite) SetupTest() {
|
|
s.db.Exec("DELETE FROM push_tokens")
|
|
}
|
|
|
|
func TestPushSubscriptionHandlerTestSuite(t *testing.T) {
|
|
suite.Run(t, new(PushSubscriptionHandlerTestSuite))
|
|
}
|
|
|
|
// --- List tests ---
|
|
|
|
func (s *PushSubscriptionHandlerTestSuite) TestList_Success() {
|
|
s.db.Create(&model.PushToken{UserID: s.user.ID, Token: "token1", Platform: "web"})
|
|
s.db.Create(&model.PushToken{UserID: s.user.ID, Token: "token2", Platform: "ios"})
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/push_subscriptions", nil)
|
|
s.router.ServeHTTP(w, req)
|
|
s.Equal(http.StatusOK, w.Code)
|
|
|
|
var resp map[string]interface{}
|
|
s.NoError(json.Unmarshal(w.Body.Bytes(), &resp))
|
|
s.NotNil(resp["data"])
|
|
}
|
|
|
|
func (s *PushSubscriptionHandlerTestSuite) TestList_Empty() {
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/push_subscriptions", nil)
|
|
s.router.ServeHTTP(w, req)
|
|
s.Equal(http.StatusOK, w.Code)
|
|
|
|
var resp map[string]interface{}
|
|
s.NoError(json.Unmarshal(w.Body.Bytes(), &resp))
|
|
data := resp["data"].(map[string]interface{})
|
|
subs := data["push_subscriptions"].([]interface{})
|
|
s.Len(subs, 0)
|
|
}
|
|
|
|
// --- Create tests ---
|
|
|
|
func (s *PushSubscriptionHandlerTestSuite) TestCreate_Success() {
|
|
body := `{"token":"newtoken","platform":"web","device_id":"device1"}`
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/push_subscriptions", bytes.NewBufferString(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
s.router.ServeHTTP(w, req)
|
|
s.Equal(http.StatusCreated, w.Code)
|
|
|
|
var resp map[string]interface{}
|
|
s.NoError(json.Unmarshal(w.Body.Bytes(), &resp))
|
|
data := resp["data"].(map[string]interface{})
|
|
s.Equal("newtoken", data["token"])
|
|
s.Equal("web", data["platform"])
|
|
}
|
|
|
|
func (s *PushSubscriptionHandlerTestSuite) TestCreate_MissingToken() {
|
|
body := `{"platform":"web"}`
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/push_subscriptions", bytes.NewBufferString(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
s.router.ServeHTTP(w, req)
|
|
// ShouldBindJSON: missing token → empty string → binding:"required" triggers error
|
|
s.Equal(http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *PushSubscriptionHandlerTestSuite) TestCreate_InvalidJSON() {
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/push_subscriptions", bytes.NewBufferString(`{invalid`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
s.router.ServeHTTP(w, req)
|
|
s.Equal(http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *PushSubscriptionHandlerTestSuite) TestCreate_WithWebPushKeys() {
|
|
body := `{"token":"webpush-token","platform":"web","p256dh":"key123","auth":"secret456"}`
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/push_subscriptions", bytes.NewBufferString(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
s.router.ServeHTTP(w, req)
|
|
s.Equal(http.StatusCreated, w.Code)
|
|
|
|
var resp map[string]interface{}
|
|
s.NoError(json.Unmarshal(w.Body.Bytes(), &resp))
|
|
data := resp["data"].(map[string]interface{})
|
|
s.Equal("webpush-token", data["token"])
|
|
}
|
|
|
|
// --- Delete tests ---
|
|
|
|
func (s *PushSubscriptionHandlerTestSuite) TestDelete_Success() {
|
|
pt := &model.PushToken{UserID: s.user.ID, Token: "deletetoken", Platform: "web"}
|
|
s.db.Create(pt)
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/push_subscriptions/"+fmt.Sprintf("%d", pt.ID), nil)
|
|
s.router.ServeHTTP(w, req)
|
|
s.Equal(http.StatusNoContent, w.Code)
|
|
}
|
|
|
|
func (s *PushSubscriptionHandlerTestSuite) TestDelete_InvalidID() {
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/push_subscriptions/abc", nil)
|
|
s.router.ServeHTTP(w, req)
|
|
s.Equal(http.StatusBadRequest, w.Code)
|
|
}
|
|
|
|
func (s *PushSubscriptionHandlerTestSuite) TestDelete_NonExistent() {
|
|
// GORM Delete on non-existent → nil → NoContent(204)
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/push_subscriptions/99999", nil)
|
|
s.router.ServeHTTP(w, req)
|
|
s.Equal(http.StatusNoContent, w.Code)
|
|
} |