Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for retrieving site settings #10

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
"devDependencies": {
"@types/mocha": "10.0.7",
"@types/node": "20.12.12",
"@types/sinon": "^17.0.3",
"@types/uuid": "10.0.0",
"c8": "10.1.2",
"mocha": "10.5.2",
"sinon": "^18.0.0",
"tsx": "4.11.0",
"typescript": "5.4.5"
},
Expand Down
28 changes: 28 additions & 0 deletions src/ghost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import ky from 'ky';
import {
ACTOR_DEFAULT_ICON,
ACTOR_DEFAULT_NAME,
ACTOR_DEFAULT_SUMMARY
} from './constants';

type SiteSettings = {
site: {
description: string;
icon: string;
title: string;
}
}

export async function getSiteSettings(host: string): Promise<SiteSettings> {
const settings = await ky
.get(`https://${host}/ghost/api/admin/site/`)
.json<Partial<SiteSettings>>();

return {
site: {
description: settings?.site?.description || ACTOR_DEFAULT_SUMMARY,
title: settings?.site?.title || ACTOR_DEFAULT_NAME,
icon: settings?.site?.icon || ACTOR_DEFAULT_ICON
}
};
}
122 changes: 122 additions & 0 deletions src/ghost.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import assert from 'assert';
import sinon from 'sinon';
import ky from 'ky';
import { getSiteSettings } from './ghost';
import {
ACTOR_DEFAULT_ICON,
ACTOR_DEFAULT_NAME,
ACTOR_DEFAULT_SUMMARY
} from './constants';

describe('getSiteSettings', function () {
const host = 'example.com';

let kyGetStub: sinon.SinonStub;

beforeEach(function () {
kyGetStub = sinon.stub(ky, 'get');
});

afterEach(function () {
sinon.restore();
});

it('should retrieve settings from Ghost', async function () {
const settings = {
site: {
description: 'foo',
title: 'bar',
icon: 'https://example.com/baz.png'
}
};

kyGetStub.returns({
json: async () => settings
});

const result = await getSiteSettings(host);

assert.deepStrictEqual(result, settings);
assert.strictEqual(kyGetStub.callCount, 1);
assert.strictEqual(kyGetStub.firstCall.args[0], `https://${host}/ghost/api/admin/site/`);
});

it('should use defaults for missing settings', async function () {
let result;

// Missing description
kyGetStub.returns({
json: async () => ({
site: {
title: 'bar',
icon: 'https://example.com/baz.png'
}
})
});

result = await getSiteSettings(host);

assert.deepStrictEqual(result, {
site: {
description: ACTOR_DEFAULT_SUMMARY,
title: 'bar',
icon: 'https://example.com/baz.png'
}
});

// Missing title
kyGetStub.returns({
json: async () => ({
site: {
description: 'foo',
icon: 'https://example.com/baz.png'
}
})
});

result = await getSiteSettings(host);

assert.deepStrictEqual(result, {
site: {
description: 'foo',
title: ACTOR_DEFAULT_NAME,
icon: 'https://example.com/baz.png'
}
});

// Missing icon
kyGetStub.returns({
json: async () => ({
site: {
description: 'foo',
title: 'bar'
}
})
});

result = await getSiteSettings(host);

assert.deepStrictEqual(result, {
site: {
description: 'foo',
title: 'bar',
icon: ACTOR_DEFAULT_ICON
}
});

// Missing everything
kyGetStub.returns({
json: async () => ({})
});

result = await getSiteSettings(host);

assert.deepStrictEqual(result, {
site: {
description: ACTOR_DEFAULT_SUMMARY,
title: ACTOR_DEFAULT_NAME,
icon: ACTOR_DEFAULT_ICON
}
});
});
});
33 changes: 3 additions & 30 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,13 @@ import {
PUBLIC_COLLECTION
} from '@fedify/fedify';
import { Context, Next } from 'hono';
import ky from 'ky';
import { v4 as uuidv4 } from 'uuid';
import { addToList } from './kv-helpers';
import { toURL } from './toURL';
import { ContextData, HonoContextVariables, fedify } from './app';
import { getSiteSettings } from './ghost';
import type { PersonData } from './user';
import {
ACTOR_DEFAULT_HANDLE,
ACTOR_DEFAULT_ICON,
ACTOR_DEFAULT_NAME,
ACTOR_DEFAULT_SUMMARY
} from './constants';

type GhostSiteSettings = {
site: {
description: string;
icon: string;
title: string;
}
}

async function getGhostSiteSettings(host: string): Promise<GhostSiteSettings> {
const settings = await ky
.get(`https://${host}/ghost/api/admin/site/`)
.json<Partial<GhostSiteSettings>>();

return {
site: {
description: settings?.site?.description || ACTOR_DEFAULT_SUMMARY,
title: settings?.site?.title || ACTOR_DEFAULT_NAME,
icon: settings?.site?.icon || ACTOR_DEFAULT_ICON
}
};
}
import { ACTOR_DEFAULT_HANDLE } from './constants';

async function postToArticle(ctx: RequestContext<ContextData>, post: any) {
if (!post) {
Expand Down Expand Up @@ -161,7 +134,7 @@ export async function siteChangedWebhook(
// Retrieve site settings from Ghost
const host = ctx.req.header('host') || '';

const settings = await getGhostSiteSettings(host);
const settings = await getSiteSettings(host);

// Update the database
const handle = ACTOR_DEFAULT_HANDLE;
Expand Down
Loading
Loading