|
| 1 | +package repository |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "trakrlog/internal/model" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "go.mongodb.org/mongo-driver/bson/primitive" |
| 13 | + "go.mongodb.org/mongo-driver/mongo" |
| 14 | + "go.mongodb.org/mongo-driver/mongo/options" |
| 15 | +) |
| 16 | + |
| 17 | +// Helper function to setup test database |
| 18 | +func setupTestDB(t *testing.T) *mongo.Database { |
| 19 | + ctx := context.Background() |
| 20 | + |
| 21 | + // Connect to test MongoDB instance |
| 22 | + client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017")) |
| 23 | + require.NoError(t, err) |
| 24 | + |
| 25 | + // Use a test database |
| 26 | + db := client.Database("trakrlog_test") |
| 27 | + |
| 28 | + // Clean up function |
| 29 | + t.Cleanup(func() { |
| 30 | + err := db.Drop(ctx) |
| 31 | + assert.NoError(t, err) |
| 32 | + err = client.Disconnect(ctx) |
| 33 | + assert.NoError(t, err) |
| 34 | + }) |
| 35 | + |
| 36 | + return db |
| 37 | +} |
| 38 | + |
| 39 | +func TestNotificationSubscriptionRepository_Create(t *testing.T) { |
| 40 | + db := setupTestDB(t) |
| 41 | + repo := NewNotificationSubscriptionRepository(db) |
| 42 | + ctx := context.Background() |
| 43 | + |
| 44 | + subscription := &model.NotificationSubscription{ |
| 45 | + UserID: primitive.NewObjectID(), |
| 46 | + Endpoint: "https://fcm.googleapis.com/fcm/send/test123", |
| 47 | + P256dh: "test-p256dh-key", |
| 48 | + Auth: "test-auth-key", |
| 49 | + UserAgent: "Mozilla/5.0", |
| 50 | + Enabled: true, |
| 51 | + } |
| 52 | + |
| 53 | + err := repo.Create(ctx, subscription) |
| 54 | + require.NoError(t, err) |
| 55 | + assert.NotEqual(t, primitive.NilObjectID, subscription.ID) |
| 56 | + assert.False(t, subscription.CreatedAt.IsZero()) |
| 57 | + assert.False(t, subscription.UpdatedAt.IsZero()) |
| 58 | +} |
| 59 | + |
| 60 | +func TestNotificationSubscriptionRepository_FindByID(t *testing.T) { |
| 61 | + db := setupTestDB(t) |
| 62 | + repo := NewNotificationSubscriptionRepository(db) |
| 63 | + ctx := context.Background() |
| 64 | + |
| 65 | + // Create a subscription |
| 66 | + subscription := &model.NotificationSubscription{ |
| 67 | + UserID: primitive.NewObjectID(), |
| 68 | + Endpoint: "https://fcm.googleapis.com/fcm/send/test123", |
| 69 | + P256dh: "test-p256dh-key", |
| 70 | + Auth: "test-auth-key", |
| 71 | + UserAgent: "Mozilla/5.0", |
| 72 | + Enabled: true, |
| 73 | + } |
| 74 | + err := repo.Create(ctx, subscription) |
| 75 | + require.NoError(t, err) |
| 76 | + |
| 77 | + // Find by ID |
| 78 | + found, err := repo.FindByID(ctx, subscription.ID.Hex()) |
| 79 | + require.NoError(t, err) |
| 80 | + assert.Equal(t, subscription.ID, found.ID) |
| 81 | + assert.Equal(t, subscription.Endpoint, found.Endpoint) |
| 82 | +} |
| 83 | + |
| 84 | +func TestNotificationSubscriptionRepository_FindByID_NotFound(t *testing.T) { |
| 85 | + db := setupTestDB(t) |
| 86 | + repo := NewNotificationSubscriptionRepository(db) |
| 87 | + ctx := context.Background() |
| 88 | + |
| 89 | + _, err := repo.FindByID(ctx, primitive.NewObjectID().Hex()) |
| 90 | + assert.Error(t, err) |
| 91 | + assert.Equal(t, mongo.ErrNoDocuments, err) |
| 92 | +} |
| 93 | + |
| 94 | +func TestNotificationSubscriptionRepository_FindByUserID(t *testing.T) { |
| 95 | + db := setupTestDB(t) |
| 96 | + repo := NewNotificationSubscriptionRepository(db) |
| 97 | + ctx := context.Background() |
| 98 | + |
| 99 | + userID := primitive.NewObjectID() |
| 100 | + |
| 101 | + // Create multiple subscriptions for the same user |
| 102 | + subscription1 := &model.NotificationSubscription{ |
| 103 | + UserID: userID, |
| 104 | + Endpoint: "https://fcm.googleapis.com/fcm/send/test1", |
| 105 | + P256dh: "test-p256dh-key-1", |
| 106 | + Auth: "test-auth-key-1", |
| 107 | + UserAgent: "Mozilla/5.0", |
| 108 | + Enabled: true, |
| 109 | + } |
| 110 | + subscription2 := &model.NotificationSubscription{ |
| 111 | + UserID: userID, |
| 112 | + Endpoint: "https://fcm.googleapis.com/fcm/send/test2", |
| 113 | + P256dh: "test-p256dh-key-2", |
| 114 | + Auth: "test-auth-key-2", |
| 115 | + UserAgent: "Chrome/91.0", |
| 116 | + Enabled: false, |
| 117 | + } |
| 118 | + |
| 119 | + err := repo.Create(ctx, subscription1) |
| 120 | + require.NoError(t, err) |
| 121 | + err = repo.Create(ctx, subscription2) |
| 122 | + require.NoError(t, err) |
| 123 | + |
| 124 | + // Find all subscriptions for user |
| 125 | + subscriptions, err := repo.FindByUserID(ctx, userID.Hex()) |
| 126 | + require.NoError(t, err) |
| 127 | + assert.Len(t, subscriptions, 2) |
| 128 | +} |
| 129 | + |
| 130 | +func TestNotificationSubscriptionRepository_Update(t *testing.T) { |
| 131 | + db := setupTestDB(t) |
| 132 | + repo := NewNotificationSubscriptionRepository(db) |
| 133 | + ctx := context.Background() |
| 134 | + |
| 135 | + subscription := &model.NotificationSubscription{ |
| 136 | + UserID: primitive.NewObjectID(), |
| 137 | + Endpoint: "https://fcm.googleapis.com/fcm/send/test123", |
| 138 | + P256dh: "test-p256dh-key", |
| 139 | + Auth: "test-auth-key", |
| 140 | + UserAgent: "Mozilla/5.0", |
| 141 | + Enabled: true, |
| 142 | + } |
| 143 | + err := repo.Create(ctx, subscription) |
| 144 | + require.NoError(t, err) |
| 145 | + |
| 146 | + // Update subscription |
| 147 | + oldUpdatedAt := subscription.UpdatedAt |
| 148 | + time.Sleep(10 * time.Millisecond) // Ensure timestamp difference |
| 149 | + subscription.Enabled = false |
| 150 | + err = repo.Update(ctx, subscription) |
| 151 | + require.NoError(t, err) |
| 152 | + |
| 153 | + // Verify update |
| 154 | + found, err := repo.FindByID(ctx, subscription.ID.Hex()) |
| 155 | + require.NoError(t, err) |
| 156 | + assert.False(t, found.Enabled) |
| 157 | + assert.True(t, found.UpdatedAt.After(oldUpdatedAt)) |
| 158 | +} |
| 159 | + |
| 160 | +func TestNotificationSubscriptionRepository_Delete(t *testing.T) { |
| 161 | + db := setupTestDB(t) |
| 162 | + repo := NewNotificationSubscriptionRepository(db) |
| 163 | + ctx := context.Background() |
| 164 | + |
| 165 | + subscription := &model.NotificationSubscription{ |
| 166 | + UserID: primitive.NewObjectID(), |
| 167 | + Endpoint: "https://fcm.googleapis.com/fcm/send/test123", |
| 168 | + P256dh: "test-p256dh-key", |
| 169 | + Auth: "test-auth-key", |
| 170 | + UserAgent: "Mozilla/5.0", |
| 171 | + Enabled: true, |
| 172 | + } |
| 173 | + err := repo.Create(ctx, subscription) |
| 174 | + require.NoError(t, err) |
| 175 | + |
| 176 | + // Delete subscription |
| 177 | + err = repo.Delete(ctx, subscription.ID.Hex()) |
| 178 | + require.NoError(t, err) |
| 179 | + |
| 180 | + // Verify deletion |
| 181 | + _, err = repo.FindByID(ctx, subscription.ID.Hex()) |
| 182 | + assert.Error(t, err) |
| 183 | + assert.Equal(t, mongo.ErrNoDocuments, err) |
| 184 | +} |
| 185 | + |
| 186 | +func TestNotificationSubscriptionRepository_FindActiveByUserID(t *testing.T) { |
| 187 | + db := setupTestDB(t) |
| 188 | + repo := NewNotificationSubscriptionRepository(db) |
| 189 | + ctx := context.Background() |
| 190 | + |
| 191 | + userID := primitive.NewObjectID() |
| 192 | + |
| 193 | + // Create active and inactive subscriptions |
| 194 | + activeSubscription := &model.NotificationSubscription{ |
| 195 | + UserID: userID, |
| 196 | + Endpoint: "https://fcm.googleapis.com/fcm/send/active", |
| 197 | + P256dh: "test-p256dh-key-1", |
| 198 | + Auth: "test-auth-key-1", |
| 199 | + UserAgent: "Mozilla/5.0", |
| 200 | + Enabled: true, |
| 201 | + } |
| 202 | + inactiveSubscription := &model.NotificationSubscription{ |
| 203 | + UserID: userID, |
| 204 | + Endpoint: "https://fcm.googleapis.com/fcm/send/inactive", |
| 205 | + P256dh: "test-p256dh-key-2", |
| 206 | + Auth: "test-auth-key-2", |
| 207 | + UserAgent: "Chrome/91.0", |
| 208 | + Enabled: false, |
| 209 | + } |
| 210 | + |
| 211 | + err := repo.Create(ctx, activeSubscription) |
| 212 | + require.NoError(t, err) |
| 213 | + err = repo.Create(ctx, inactiveSubscription) |
| 214 | + require.NoError(t, err) |
| 215 | + |
| 216 | + // Find only active subscriptions |
| 217 | + subscriptions, err := repo.FindActiveByUserID(ctx, userID.Hex()) |
| 218 | + require.NoError(t, err) |
| 219 | + assert.Len(t, subscriptions, 1) |
| 220 | + assert.True(t, subscriptions[0].Enabled) |
| 221 | + assert.Equal(t, "https://fcm.googleapis.com/fcm/send/active", subscriptions[0].Endpoint) |
| 222 | +} |
| 223 | + |
| 224 | +func TestNotificationLogRepository_Create(t *testing.T) { |
| 225 | + db := setupTestDB(t) |
| 226 | + repo := NewNotificationLogRepository(db) |
| 227 | + ctx := context.Background() |
| 228 | + |
| 229 | + log := &model.NotificationLog{ |
| 230 | + UserID: primitive.NewObjectID(), |
| 231 | + EventID: primitive.NewObjectID(), |
| 232 | + SubscriptionID: primitive.NewObjectID(), |
| 233 | + Status: "sent", |
| 234 | + } |
| 235 | + |
| 236 | + err := repo.Create(ctx, log) |
| 237 | + require.NoError(t, err) |
| 238 | + assert.NotEqual(t, primitive.NilObjectID, log.ID) |
| 239 | + assert.False(t, log.SentAt.IsZero()) |
| 240 | +} |
| 241 | + |
| 242 | +func TestNotificationLogRepository_FindByUserID(t *testing.T) { |
| 243 | + db := setupTestDB(t) |
| 244 | + repo := NewNotificationLogRepository(db) |
| 245 | + ctx := context.Background() |
| 246 | + |
| 247 | + userID := primitive.NewObjectID() |
| 248 | + |
| 249 | + // Create multiple logs |
| 250 | + for i := 0; i < 5; i++ { |
| 251 | + log := &model.NotificationLog{ |
| 252 | + UserID: userID, |
| 253 | + EventID: primitive.NewObjectID(), |
| 254 | + SubscriptionID: primitive.NewObjectID(), |
| 255 | + Status: "sent", |
| 256 | + } |
| 257 | + err := repo.Create(ctx, log) |
| 258 | + require.NoError(t, err) |
| 259 | + time.Sleep(10 * time.Millisecond) // Ensure different timestamps |
| 260 | + } |
| 261 | + |
| 262 | + // Find logs with pagination |
| 263 | + logs, err := repo.FindByUserID(ctx, userID.Hex(), 3, 0) |
| 264 | + require.NoError(t, err) |
| 265 | + assert.Len(t, logs, 3) |
| 266 | + |
| 267 | + // Verify sorting (newest first) |
| 268 | + for i := 0; i < len(logs)-1; i++ { |
| 269 | + assert.True(t, logs[i].SentAt.After(logs[i+1].SentAt) || logs[i].SentAt.Equal(logs[i+1].SentAt)) |
| 270 | + } |
| 271 | +} |
| 272 | + |
| 273 | +func TestNotificationLogRepository_FindByEventID(t *testing.T) { |
| 274 | + db := setupTestDB(t) |
| 275 | + repo := NewNotificationLogRepository(db) |
| 276 | + ctx := context.Background() |
| 277 | + |
| 278 | + eventID := primitive.NewObjectID() |
| 279 | + |
| 280 | + // Create logs for the same event |
| 281 | + log1 := &model.NotificationLog{ |
| 282 | + UserID: primitive.NewObjectID(), |
| 283 | + EventID: eventID, |
| 284 | + SubscriptionID: primitive.NewObjectID(), |
| 285 | + Status: "sent", |
| 286 | + } |
| 287 | + log2 := &model.NotificationLog{ |
| 288 | + UserID: primitive.NewObjectID(), |
| 289 | + EventID: eventID, |
| 290 | + SubscriptionID: primitive.NewObjectID(), |
| 291 | + Status: "failed", |
| 292 | + Error: "subscription expired", |
| 293 | + } |
| 294 | + |
| 295 | + err := repo.Create(ctx, log1) |
| 296 | + require.NoError(t, err) |
| 297 | + err = repo.Create(ctx, log2) |
| 298 | + require.NoError(t, err) |
| 299 | + |
| 300 | + // Find logs by event ID |
| 301 | + logs, err := repo.FindByEventID(ctx, eventID.Hex()) |
| 302 | + require.NoError(t, err) |
| 303 | + assert.Len(t, logs, 2) |
| 304 | +} |
0 commit comments