fix(syncedinstance): Prevent same-instance syncs and sanitize post update payloads; update tests and docs
This commit is contained in:
95
test/test.syncedinstance.validation.node+deno.ts
Normal file
95
test/test.syncedinstance.validation.node+deno.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
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;
|
||||
|
||||
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 throw error when creating SyncedInstance with same instance', async () => {
|
||||
let errorThrown = false;
|
||||
let errorMessage = '';
|
||||
|
||||
try {
|
||||
new ghost.SyncedInstance(testGhostInstance, [testGhostInstance]);
|
||||
} catch (error) {
|
||||
errorThrown = true;
|
||||
errorMessage = error instanceof Error ? error.message : String(error);
|
||||
}
|
||||
|
||||
expect(errorThrown).toEqual(true);
|
||||
expect(errorMessage).toContain('Cannot sync to the same instance');
|
||||
expect(errorMessage).toContain('localhost:2368');
|
||||
console.log(`Correctly prevented same-instance sync: ${errorMessage}`);
|
||||
});
|
||||
|
||||
tap.test('should throw error when target array includes same instance', async () => {
|
||||
let errorThrown = false;
|
||||
let errorMessage = '';
|
||||
|
||||
const anotherInstance = new ghost.Ghost({
|
||||
baseUrl: 'http://localhost:2368',
|
||||
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
|
||||
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
|
||||
});
|
||||
|
||||
try {
|
||||
new ghost.SyncedInstance(testGhostInstance, [anotherInstance]);
|
||||
} catch (error) {
|
||||
errorThrown = true;
|
||||
errorMessage = error instanceof Error ? error.message : String(error);
|
||||
}
|
||||
|
||||
expect(errorThrown).toEqual(true);
|
||||
expect(errorMessage).toContain('Cannot sync to the same instance');
|
||||
console.log(`Correctly prevented sync with duplicate URL: ${errorMessage}`);
|
||||
});
|
||||
|
||||
tap.test('should normalize URLs when comparing (trailing slash)', async () => {
|
||||
let errorThrown = false;
|
||||
|
||||
const instanceWithTrailingSlash = new ghost.Ghost({
|
||||
baseUrl: 'http://localhost:2368/',
|
||||
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
|
||||
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
|
||||
});
|
||||
|
||||
try {
|
||||
new ghost.SyncedInstance(testGhostInstance, [instanceWithTrailingSlash]);
|
||||
} catch (error) {
|
||||
errorThrown = true;
|
||||
}
|
||||
|
||||
expect(errorThrown).toEqual(true);
|
||||
console.log('Correctly detected same instance despite trailing slash difference');
|
||||
});
|
||||
|
||||
tap.test('should normalize URLs when comparing (case insensitive)', async () => {
|
||||
let errorThrown = false;
|
||||
|
||||
const instanceWithUpperCase = new ghost.Ghost({
|
||||
baseUrl: 'http://LOCALHOST:2368',
|
||||
adminApiKey: await testQenv.getEnvVarOnDemand('ADMIN_APIKEY'),
|
||||
contentApiKey: await testQenv.getEnvVarOnDemand('CONTENT_APIKEY'),
|
||||
});
|
||||
|
||||
try {
|
||||
new ghost.SyncedInstance(testGhostInstance, [instanceWithUpperCase]);
|
||||
} catch (error) {
|
||||
errorThrown = true;
|
||||
}
|
||||
|
||||
expect(errorThrown).toEqual(true);
|
||||
console.log('Correctly detected same instance despite case difference');
|
||||
});
|
||||
|
||||
export default tap.start();
|
Reference in New Issue
Block a user