Files
gochat/backend/internal/repository/notification_subscription_repo_test.go
T

276 lines
10 KiB
Go

package repository
import (
"context"
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
"github.com/gochat/gochat/internal/model"
)
func TestNotificationSubscriptionRepo_Create(t *testing.T) {
db := setupTestDB(t, &model.NotificationSubscription{})
repo := NewNotificationSubscriptionRepo(db)
account := &model.Account{Name: "NSubOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
user := &model.User{Name: "NSubUser", Email: "nsubuser@test.com", AccountID: account.ID}
require.NoError(t, db.Create(user).Error)
sub := &model.NotificationSubscription{
Identifier: "sub-1",
SubscriptionAttributes: json.RawMessage(`{"push_token":"abc"}`),
SubscriptionType: model.NotificationSubBrowserPush,
UserID: user.ID,
}
err := repo.Create(context.Background(), sub)
assert.NoError(t, err)
assert.NotZero(t, sub.ID)
}
func TestNotificationSubscriptionRepo_Save(t *testing.T) {
db := setupTestDB(t, &model.NotificationSubscription{})
repo := NewNotificationSubscriptionRepo(db)
account := &model.Account{Name: "NSubSaveOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
user := &model.User{Name: "NSubSaveUser", Email: "nsubsaveuser@test.com", AccountID: account.ID}
require.NoError(t, db.Create(user).Error)
sub := &model.NotificationSubscription{
Identifier: "sub-save",
SubscriptionAttributes: json.RawMessage(`{}`),
SubscriptionType: model.NotificationSubBrowserPush,
UserID: user.ID,
}
require.NoError(t, repo.Create(context.Background(), sub))
sub.SubscriptionAttributes = json.RawMessage(`{"endpoint":"updated"}`)
err := repo.Save(context.Background(), sub)
assert.NoError(t, err)
}
func TestNotificationSubscriptionRepo_FindByID(t *testing.T) {
db := setupTestDB(t, &model.NotificationSubscription{})
repo := NewNotificationSubscriptionRepo(db)
account := &model.Account{Name: "NSubByIDOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
user := &model.User{Name: "NSubByIDUser", Email: "nsubbyiduser@test.com", AccountID: account.ID}
require.NoError(t, db.Create(user).Error)
sub := &model.NotificationSubscription{
Identifier: "sub-find",
SubscriptionAttributes: json.RawMessage(`{}`),
SubscriptionType: model.NotificationSubFCM,
UserID: user.ID,
}
require.NoError(t, repo.Create(context.Background(), sub))
found, err := repo.FindByID(context.Background(), sub.ID)
assert.NoError(t, err)
assert.Equal(t, sub.ID, found.ID)
}
func TestNotificationSubscriptionRepo_FindByID_NotFound(t *testing.T) {
db := setupTestDB(t, &model.NotificationSubscription{})
repo := NewNotificationSubscriptionRepo(db)
_, err := repo.FindByID(context.Background(), 9999)
assert.Error(t, err)
assert.ErrorIs(t, err, gorm.ErrRecordNotFound)
}
func TestNotificationSubscriptionRepo_FindByUserID(t *testing.T) {
db := setupTestDB(t, &model.NotificationSubscription{})
repo := NewNotificationSubscriptionRepo(db)
account := &model.Account{Name: "NSubByUserOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
user := &model.User{Name: "NSubByUserUser", Email: "nsubbyuseruser@test.com", AccountID: account.ID}
require.NoError(t, db.Create(user).Error)
for i := 0; i < 3; i++ {
require.NoError(t, repo.Create(context.Background(), &model.NotificationSubscription{
Identifier: "sub" + string(rune('a'+i)),
SubscriptionAttributes: json.RawMessage(`{}`),
SubscriptionType: model.NotificationSubBrowserPush,
UserID: user.ID,
}))
}
subs, err := repo.FindByUserID(context.Background(), user.ID)
assert.NoError(t, err)
assert.Len(t, subs, 3)
}
func TestNotificationSubscriptionRepo_FindByUserIDAndPushToken_IdentifierMatch(t *testing.T) {
db := setupTestDB(t, &model.NotificationSubscription{})
repo := NewNotificationSubscriptionRepo(db)
account := &model.Account{Name: "NSubPTOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
user := &model.User{Name: "NSubPTUser", Email: "nsubptuser@test.com", AccountID: account.ID}
require.NoError(t, db.Create(user).Error)
sub := &model.NotificationSubscription{
Identifier: "token123",
SubscriptionAttributes: json.RawMessage(`{}`),
SubscriptionType: model.NotificationSubBrowserPush,
UserID: user.ID,
}
require.NoError(t, repo.Create(context.Background(), sub))
found, err := repo.FindByUserIDAndPushToken(context.Background(), user.ID, "token123")
assert.NoError(t, err)
assert.Equal(t, sub.ID, found.ID)
}
func TestNotificationSubscriptionRepo_FindByUserIDAndPushToken_AttrsMatch(t *testing.T) {
db := setupTestDB(t, &model.NotificationSubscription{})
repo := NewNotificationSubscriptionRepo(db)
account := &model.Account{Name: "NSubAttrOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
user := &model.User{Name: "NSubAttrUser", Email: "nsubattruser@test.com", AccountID: account.ID}
require.NoError(t, db.Create(user).Error)
sub := &model.NotificationSubscription{
Identifier: "other-id",
SubscriptionAttributes: json.RawMessage(`{"push_token":"pt_val"}`),
SubscriptionType: model.NotificationSubBrowserPush,
UserID: user.ID,
}
require.NoError(t, repo.Create(context.Background(), sub))
found, err := repo.FindByUserIDAndPushToken(context.Background(), user.ID, "pt_val")
assert.NoError(t, err)
assert.Equal(t, sub.ID, found.ID)
}
func TestNotificationSubscriptionRepo_FindByUserIDAndPushToken_EndpointMatch(t *testing.T) {
db := setupTestDB(t, &model.NotificationSubscription{})
repo := NewNotificationSubscriptionRepo(db)
account := &model.Account{Name: "NSubEPOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
user := &model.User{Name: "NSubEPUser", Email: "nsubepuser@test.com", AccountID: account.ID}
require.NoError(t, db.Create(user).Error)
sub := &model.NotificationSubscription{
Identifier: "ep-id",
SubscriptionAttributes: json.RawMessage(`{"endpoint":"https://push.example.com/abc"}`),
SubscriptionType: model.NotificationSubBrowserPush,
UserID: user.ID,
}
require.NoError(t, repo.Create(context.Background(), sub))
found, err := repo.FindByUserIDAndPushToken(context.Background(), user.ID, "https://push.example.com/abc")
assert.NoError(t, err)
assert.Equal(t, sub.ID, found.ID)
}
func TestNotificationSubscriptionRepo_FindByUserIDAndPushToken_NotFound(t *testing.T) {
db := setupTestDB(t, &model.NotificationSubscription{})
repo := NewNotificationSubscriptionRepo(db)
account := &model.Account{Name: "NSubNFOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
user := &model.User{Name: "NSubNFUser", Email: "nsubnfuser@test.com", AccountID: account.ID}
require.NoError(t, db.Create(user).Error)
sub := &model.NotificationSubscription{
Identifier: "some-id",
SubscriptionAttributes: json.RawMessage(`{"push_token":"abc"}`),
SubscriptionType: model.NotificationSubBrowserPush,
UserID: user.ID,
}
require.NoError(t, repo.Create(context.Background(), sub))
_, err := repo.FindByUserIDAndPushToken(context.Background(), user.ID, "nonexistent_token")
assert.Error(t, err)
assert.ErrorIs(t, err, gorm.ErrRecordNotFound)
}
func TestNotificationSubscriptionRepo_FindByIdentifier(t *testing.T) {
db := setupTestDB(t, &model.NotificationSubscription{})
repo := NewNotificationSubscriptionRepo(db)
account := &model.Account{Name: "NSubIdentOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
user := &model.User{Name: "NSubIdentUser", Email: "nsubidentuser@test.com", AccountID: account.ID}
require.NoError(t, db.Create(user).Error)
sub := &model.NotificationSubscription{
Identifier: "unique-ident",
SubscriptionAttributes: json.RawMessage(`{}`),
SubscriptionType: model.NotificationSubBrowserPush,
UserID: user.ID,
}
require.NoError(t, repo.Create(context.Background(), sub))
found, err := repo.FindByIdentifier(context.Background(), "unique-ident")
assert.NoError(t, err)
assert.Equal(t, sub.ID, found.ID)
}
func TestNotificationSubscriptionRepo_FindByIdentifier_NotFound(t *testing.T) {
db := setupTestDB(t, &model.NotificationSubscription{})
repo := NewNotificationSubscriptionRepo(db)
_, err := repo.FindByIdentifier(context.Background(), "nonexistent")
assert.Error(t, err)
}
func TestNotificationSubscriptionRepo_Delete(t *testing.T) {
db := setupTestDB(t, &model.NotificationSubscription{})
repo := NewNotificationSubscriptionRepo(db)
account := &model.Account{Name: "NSubDelOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
user := &model.User{Name: "NSubDelUser", Email: "nsubdeluser@test.com", AccountID: account.ID}
require.NoError(t, db.Create(user).Error)
sub := &model.NotificationSubscription{
Identifier: "del-id",
SubscriptionAttributes: json.RawMessage(`{}`),
SubscriptionType: model.NotificationSubBrowserPush,
UserID: user.ID,
}
require.NoError(t, repo.Create(context.Background(), sub))
err := repo.Delete(context.Background(), sub.ID)
assert.NoError(t, err)
_, err = repo.FindByID(context.Background(), sub.ID)
assert.Error(t, err)
}
func TestNotificationSubscriptionRepo_DeleteByUserIDAndIdentifier(t *testing.T) {
db := setupTestDB(t, &model.NotificationSubscription{})
repo := NewNotificationSubscriptionRepo(db)
account := &model.Account{Name: "NSubDelUIOrg", Locale: "en", Active: true}
require.NoError(t, db.Create(account).Error)
user := &model.User{Name: "NSubDelUIUser", Email: "nsubdeluiuser@test.com", AccountID: account.ID}
require.NoError(t, db.Create(user).Error)
sub := &model.NotificationSubscription{
Identifier: "del-ui-id",
SubscriptionAttributes: json.RawMessage(`{}`),
SubscriptionType: model.NotificationSubBrowserPush,
UserID: user.ID,
}
require.NoError(t, repo.Create(context.Background(), sub))
err := repo.DeleteByUserIDAndIdentifier(context.Background(), user.ID, "del-ui-id")
assert.NoError(t, err)
_, err = repo.FindByID(context.Background(), sub.ID)
assert.Error(t, err)
}