db migration 1.0.2, now stores and indexes date of content delivered to subscriber...
[websub-hub] / test / src / db / integration.js
index 3cb07f94c02c1b8bae89e8fd4620e0abf7a453f0..83feac761222a620793cb6963b9a2604e3fb60f5 100644 (file)
@@ -69,7 +69,7 @@ describe('Database Integration', function () {
         // eslint-disable-next-line security/detect-non-literal-require
         DB = require(i.module);
         db = new DB(stubLogger, i.config);
-        await db.schemaCheck();
+        await db.initialize();
         await db._purgeTables(true);
       });
       after(async function () {
@@ -114,6 +114,7 @@ describe('Database Integration', function () {
       }); // Authentication
 
       describe('Topic', function () {
+        let anotherTopicId;
         step('requires data', async function () {
           try {
             await db.context(async (dbCtx) => {
@@ -222,7 +223,7 @@ describe('Database Integration', function () {
         step('deletes a topic', async function () {
           await db.context(async (dbCtx) => {
             const result = await db.topicSet(dbCtx, testData.anotherTopicSet);
-            const anotherTopicId = result.lastInsertRowid;
+            anotherTopicId = result.lastInsertRowid;
             await db.topicDeleted(dbCtx, anotherTopicId);
             const topic = await db.topicGetById(dbCtx, anotherTopicId);
             assert.strictEqual(topic.isDeleted, true);
@@ -231,7 +232,7 @@ describe('Database Integration', function () {
         step('update un-deletes a topic', async function () {
           await db.context(async (dbCtx) => {
             const result = await db.topicSet(dbCtx, testData.anotherTopicSet);
-            const anotherTopicId = result.lastInsertRowid;
+            assert.strictEqual(result.lastInsertRowid, anotherTopicId);
             const topic = await db.topicGetById(dbCtx, anotherTopicId);
             assert.strictEqual(topic.isDeleted, false);
           });
@@ -242,6 +243,15 @@ describe('Database Integration', function () {
             assert(topics.length);
           });
         });
+        // pending delete of deleted topic with no subscriptions
+        step('really deletes unsubscribed deleted topic', async function() {
+          await db.context(async (dbCtx) => {
+            await db.topicDeleted(dbCtx, anotherTopicId);
+            await db.topicPendingDelete(dbCtx, anotherTopicId);
+            const topic = await db.topicGetById(dbCtx, anotherTopicId);
+            assert(!topic);
+          });
+        });
       }); // Topic
 
       describe('Subscription', function () {
@@ -318,7 +328,8 @@ describe('Database Integration', function () {
         step('complete subscription', async function () {
           const { callback } = testData.subscriptionUpsert;
           await db.context(async (dbCtx) => {
-            await db.subscriptionDeliveryComplete(dbCtx, callback, topicId);
+            const topic = await db.topicGetById(dbCtx, topicId);
+            await db.subscriptionDeliveryComplete(dbCtx, callback, topicId, topic.contentUpdated);
             const subscription = await db.subscriptionGetById(dbCtx, subscriptionId);
             assert.strictEqual(Number(subscription.deliveryAttemptsSinceSuccess), 0);
           });
@@ -372,6 +383,28 @@ describe('Database Integration', function () {
             assert(!subscription);
           });
         });
+        step('create expired subscription', async function () {
+          const data = {
+            ...testData.subscriptionUpsert,
+            secret: 'newSecret',
+            topicId,
+            leaseSeconds: -1,
+          };
+          await db.context(async (dbCtx) => {
+            const result = await db.subscriptionUpsert(dbCtx, data);
+            assert(result.lastInsertRowid);
+            assert.notStrictEqual(result.lastInsertRowid, subscriptionId);
+            subscriptionId = result.lastInsertRowid;
+            assert.strictEqual(result.changes, 1);
+          });
+        });
+        step('delete expired subscriptions', async function() {
+          await db.context(async (dbCtx) => {
+            await db.subscriptionDeleteExpired(dbCtx, topicId)
+            const subscription = await db.subscriptionGet(dbCtx, testData.subscriptionUpsert.callback, topicId);
+            assert(!subscription);
+          });
+        });
       }); // Subscription
 
       describe('Verification', function () {