107 lines
3.3 KiB
TypeScript
107 lines
3.3 KiB
TypeScript
|
import { expect, tap } from '@push.rocks/tapbundle';
|
||
|
import * as qenv from '@push.rocks/qenv';
|
||
|
const testQenv = new qenv.Qenv('./', './.nogit/');
|
||
|
|
||
|
import * as ghost from '../ts/index.js';
|
||
|
|
||
|
let testGhostInstance: ghost.Ghost;
|
||
|
let createdWebhook: any;
|
||
|
|
||
|
tap.test('initialize Ghost instance', async () => {
|
||
|
testGhostInstance = new ghost.Ghost({
|
||
|
baseUrl: 'http://localhost:2368',
|
||
|
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
|
||
|
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
|
||
|
});
|
||
|
expect(testGhostInstance).toBeInstanceOf(ghost.Ghost);
|
||
|
});
|
||
|
|
||
|
tap.test('should get all webhooks', async () => {
|
||
|
try {
|
||
|
const webhooks = await testGhostInstance.getWebhooks();
|
||
|
expect(webhooks).toBeArray();
|
||
|
console.log(`Found ${webhooks.length} webhooks`);
|
||
|
if (webhooks.length > 0) {
|
||
|
console.log(`First webhook: ${webhooks[0].name || 'unnamed'}`);
|
||
|
}
|
||
|
} catch (error: any) {
|
||
|
if (error.message?.includes('not a function') || error.statusCode === 403) {
|
||
|
console.log('Webhooks API not available in this Ghost version - skipping test');
|
||
|
} else {
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
tap.test('should create webhook', async () => {
|
||
|
try {
|
||
|
const timestamp = Date.now();
|
||
|
createdWebhook = await testGhostInstance.createWebhook({
|
||
|
event: 'post.published',
|
||
|
target_url: `https://example.com/webhook/${timestamp}`,
|
||
|
name: `Test Webhook ${timestamp}`
|
||
|
});
|
||
|
expect(createdWebhook).toBeTruthy();
|
||
|
expect(createdWebhook.id).toBeTruthy();
|
||
|
console.log(`Created webhook: ${createdWebhook.id}`);
|
||
|
} catch (error: any) {
|
||
|
if (error.message?.includes('not a function') || error.statusCode === 403) {
|
||
|
console.log('Webhooks API not available - skipping test');
|
||
|
} else {
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
tap.test('should get webhook by ID', async () => {
|
||
|
if (createdWebhook) {
|
||
|
try {
|
||
|
const webhook = await testGhostInstance.getWebhookById(createdWebhook.id);
|
||
|
expect(webhook).toBeTruthy();
|
||
|
expect(webhook.id).toEqual(createdWebhook.id);
|
||
|
console.log(`Got webhook by ID: ${webhook.id}`);
|
||
|
} catch (error: any) {
|
||
|
if (error.message?.includes('not a function') || error.statusCode === 403) {
|
||
|
console.log('Webhooks API not available - skipping test');
|
||
|
} else {
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
tap.test('should update webhook', async () => {
|
||
|
if (createdWebhook) {
|
||
|
try {
|
||
|
const updatedWebhook = await testGhostInstance.updateWebhook(createdWebhook.id, {
|
||
|
target_url: 'https://example.com/webhook/updated'
|
||
|
});
|
||
|
expect(updatedWebhook).toBeTruthy();
|
||
|
console.log(`Updated webhook: ${updatedWebhook.id}`);
|
||
|
} catch (error: any) {
|
||
|
if (error.message?.includes('not a function') || error.statusCode === 403) {
|
||
|
console.log('Webhooks API not available - skipping test');
|
||
|
} else {
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
tap.test('should delete webhook', async () => {
|
||
|
if (createdWebhook) {
|
||
|
try {
|
||
|
await testGhostInstance.deleteWebhook(createdWebhook.id);
|
||
|
console.log(`Deleted webhook: ${createdWebhook.id}`);
|
||
|
} catch (error: any) {
|
||
|
if (error.message?.includes('not a function') || error.statusCode === 403) {
|
||
|
console.log('Webhooks API not available - skipping test');
|
||
|
} else {
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
export default tap.start();
|