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.
180 lines
7.7 KiB
Go
180 lines
7.7 KiB
Go
package v1
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"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/require"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func setupNotificationSubscriptionHandlerTest(t *testing.T, userID uint) (*gin.Engine, *gorm.DB) {
|
|
t.Helper()
|
|
gin.SetMode(gin.TestMode)
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
require.NoError(t, err)
|
|
require.NoError(t, db.AutoMigrate(&model.User{}, &model.NotificationSubscription{}))
|
|
|
|
handler := NewNotificationSubscriptionHandler(service.NewNotificationSubscriptionService(repository.NewNotificationSubscriptionRepo(db)))
|
|
router := gin.New()
|
|
router.Use(func(c *gin.Context) {
|
|
c.Set("user_id", userID)
|
|
c.Next()
|
|
})
|
|
router.POST("/api/v1/notification_subscriptions", handler.Create)
|
|
router.DELETE("/api/v1/notification_subscriptions", handler.Destroy)
|
|
router.DELETE("/api/v1/notification_subscriptions/:identifier", handler.Destroy)
|
|
router.POST("/api/v1/accounts/:account_id/notification_subscriptions/", handler.Create)
|
|
router.DELETE("/api/v1/accounts/:account_id/notification_subscriptions/", handler.Destroy)
|
|
router.DELETE("/api/v1/accounts/:account_id/notification_subscriptions/:identifier", handler.Destroy)
|
|
return router, db
|
|
}
|
|
|
|
func TestNotificationSubscriptionCreateAcceptsPushHelperPayload(t *testing.T) {
|
|
router, db := setupNotificationSubscriptionHandlerTest(t, 7)
|
|
body := `{"subscription_type":"browser_push","subscription_attributes":{"endpoint":"https://push.example/sub","p256dh":"key","auth":"secret"}}`
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/notification_subscriptions", bytes.NewBufferString(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
var payload map[string]any
|
|
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &payload), w.Body.String())
|
|
require.NotContains(t, payload, "success")
|
|
require.NotContains(t, payload, "data")
|
|
require.NotZero(t, payload["id"])
|
|
require.Equal(t, "https://push.example/sub", payload["identifier"])
|
|
require.Equal(t, "browser_push", payload["subscription_type"])
|
|
require.Equal(t, float64(7), payload["user_id"])
|
|
require.NotEmpty(t, payload["created_at"])
|
|
require.NotEmpty(t, payload["updated_at"])
|
|
require.Equal(t, map[string]any{
|
|
"endpoint": "https://push.example/sub",
|
|
"p256dh": "key",
|
|
"auth": "secret",
|
|
}, payload["subscription_attributes"])
|
|
|
|
var sub model.NotificationSubscription
|
|
require.NoError(t, db.First(&sub).Error)
|
|
require.Equal(t, uint(7), sub.UserID)
|
|
require.Equal(t, "https://push.example/sub", sub.Identifier)
|
|
require.Equal(t, model.NotificationSubBrowserPush, sub.SubscriptionType)
|
|
require.JSONEq(t, `{"endpoint":"https://push.example/sub","p256dh":"key","auth":"secret"}`, string(sub.SubscriptionAttributes))
|
|
}
|
|
|
|
func TestNotificationSubscriptionCreateAcceptsAccountScopedPushHelperPayload(t *testing.T) {
|
|
router, db := setupNotificationSubscriptionHandlerTest(t, 9)
|
|
body := `{"subscription_type":"browser_push","subscription_attributes":{"endpoint":"https://push.example/account-scoped","p256dh":"account-key","auth":"account-secret"}}`
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/accounts/42/notification_subscriptions/", bytes.NewBufferString(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
var payload map[string]any
|
|
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &payload), w.Body.String())
|
|
require.NotContains(t, payload, "success")
|
|
require.NotContains(t, payload, "data")
|
|
require.Equal(t, "https://push.example/account-scoped", payload["identifier"])
|
|
require.Equal(t, "browser_push", payload["subscription_type"])
|
|
require.Equal(t, float64(9), payload["user_id"])
|
|
require.Equal(t, map[string]any{
|
|
"endpoint": "https://push.example/account-scoped",
|
|
"p256dh": "account-key",
|
|
"auth": "account-secret",
|
|
}, payload["subscription_attributes"])
|
|
|
|
var sub model.NotificationSubscription
|
|
require.NoError(t, db.First(&sub).Error)
|
|
require.Equal(t, uint(9), sub.UserID)
|
|
require.Equal(t, "https://push.example/account-scoped", sub.Identifier)
|
|
require.JSONEq(t, `{"endpoint":"https://push.example/account-scoped","p256dh":"account-key","auth":"account-secret"}`, string(sub.SubscriptionAttributes))
|
|
}
|
|
|
|
func TestNotificationSubscriptionCreateAcceptsRailsWrapperAndUpdatesExisting(t *testing.T) {
|
|
router, db := setupNotificationSubscriptionHandlerTest(t, 11)
|
|
existing := model.NotificationSubscription{
|
|
Identifier: "device-1",
|
|
UserID: 2,
|
|
SubscriptionType: model.NotificationSubFCM,
|
|
SubscriptionAttributes: json.RawMessage(`{"device_id":"device-1","push_token":"old"}`),
|
|
}
|
|
require.NoError(t, db.Create(&existing).Error)
|
|
body := `{"notification_subscription":{"subscription_type":"fcm","subscription_attributes":{"device_id":"device-1","push_token":"new"}}}`
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/notification_subscriptions", bytes.NewBufferString(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
var sub model.NotificationSubscription
|
|
require.NoError(t, db.First(&sub, existing.ID).Error)
|
|
require.Equal(t, uint(11), sub.UserID)
|
|
require.JSONEq(t, `{"device_id":"device-1","push_token":"new"}`, string(sub.SubscriptionAttributes))
|
|
}
|
|
|
|
func TestNotificationSubscriptionDestroyUsesPushTokenAndReturnsEmptyOK(t *testing.T) {
|
|
router, db := setupNotificationSubscriptionHandlerTest(t, 7)
|
|
sub := model.NotificationSubscription{
|
|
Identifier: "https://push.example/sub",
|
|
UserID: 7,
|
|
SubscriptionType: model.NotificationSubBrowserPush,
|
|
SubscriptionAttributes: json.RawMessage(`{"endpoint":"https://push.example/sub","p256dh":"key","auth":"secret"}`),
|
|
}
|
|
require.NoError(t, db.Create(&sub).Error)
|
|
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/notification_subscriptions?push_token=https%3A%2F%2Fpush.example%2Fsub", nil)
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
require.Empty(t, w.Body.String())
|
|
var count int64
|
|
require.NoError(t, db.Model(&model.NotificationSubscription{}).Where("id = ?", sub.ID).Count(&count).Error)
|
|
require.Equal(t, int64(0), count)
|
|
}
|
|
|
|
func TestNotificationSubscriptionDestroyAccountScopedUsesPushToken(t *testing.T) {
|
|
router, db := setupNotificationSubscriptionHandlerTest(t, 7)
|
|
sub := model.NotificationSubscription{
|
|
Identifier: "https://push.example/account-scoped-delete",
|
|
UserID: 7,
|
|
SubscriptionType: model.NotificationSubBrowserPush,
|
|
SubscriptionAttributes: json.RawMessage(`{"endpoint":"https://push.example/account-scoped-delete","p256dh":"key","auth":"secret"}`),
|
|
}
|
|
require.NoError(t, db.Create(&sub).Error)
|
|
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/accounts/42/notification_subscriptions/?push_token=https%3A%2F%2Fpush.example%2Faccount-scoped-delete", nil)
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
require.Empty(t, w.Body.String())
|
|
var count int64
|
|
require.NoError(t, db.Model(&model.NotificationSubscription{}).Where("id = ?", sub.ID).Count(&count).Error)
|
|
require.Equal(t, int64(0), count)
|
|
}
|
|
|
|
func TestNotificationSubscriptionDestroyMissingTokenStillOK(t *testing.T) {
|
|
router, _ := setupNotificationSubscriptionHandlerTest(t, 7)
|
|
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/notification_subscriptions", nil)
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
require.Empty(t, w.Body.String())
|
|
}
|