Skip to content

Commit

Permalink
fix(import): ensure import/exports works with 5.x
Browse files Browse the repository at this point in the history
closes TryGhost/Toolbox#256

- we should only need to add the new unversioned base URL here
- also adds 5.x tests
  • Loading branch information
daniellockyer committed Apr 21, 2022
1 parent 6e31c54 commit ae8177b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/tasks/import/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const bases = {
1: '/ghost/api/v0.1',
2: '/ghost/api/v2/admin',
3: '/ghost/api/v3/admin',
4: '/ghost/api/v4/admin'
4: '/ghost/api/v4/admin',
5: '/ghost/api/admin'
};

function getBaseUrl(version, url) {
Expand Down
79 changes: 79 additions & 0 deletions test/unit/tasks/import/api-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ describe('Unit > Tasks > Import > setup', function () {
expect(baseUrl).to.equal('https://example.com/ghost/api/v4/admin');
});

it('5.x', function () {
const baseUrl = getBaseUrl('5.0.0', 'https://example.com/');
expect(baseUrl).to.equal('https://example.com/ghost/api/admin');
});

it('unsupported', function () {
try {
getBaseUrl('0.11.14', 'https://example.com');
Expand Down Expand Up @@ -323,6 +328,36 @@ describe('Unit > Tasks > Import > setup', function () {
expect(sessionScope.isDone()).to.be.true;
expect(importScope.isDone()).to.be.true;
});

it('5.x', async function () {
const sessionScope = nock(testUrl, {
reqheaders: {
Origin: testUrl
}
}).post('/ghost/api/admin/session/', {
username: 'test@example.com',
password: 'password'
}).reply(201, 'Success', {
'Set-Cookie': 'ghost-admin-api-session=test-session-data; Path=/ghost; HttpOnly; Secure; Expires=Tue, 31 Dec 2099 23:59:59 GMT;'
});

const importScope = nock(testUrl, {
reqheaders: {
cookie: [
'ghost-admin-api-session=test-session-data'
],
origin: testUrl
}
}).post('/ghost/api/admin/db/').reply(201, {});

await runImport('5.0.0', 'http://localhost:2368', {
username: 'test@example.com',
password: 'password'
}, path.join(__dirname, 'fixtures/5.x.json'));

expect(sessionScope.isDone()).to.be.true;
expect(importScope.isDone()).to.be.true;
});
});

describe('downloadExport', function () {
Expand Down Expand Up @@ -517,4 +552,48 @@ describe('Unit > Tasks > Import > setup', function () {
expect(fs.readJsonSync(outputFile)).to.deep.equal(exportData);
});
});

it('5.x', async function () {
const sessionScope = nock(testUrl, {
reqheaders: {
Origin: testUrl
}
}).post('/ghost/api/admin/session/', {
username: 'test@example.com',
password: 'password'
}).reply(201, 'Success', {
'Set-Cookie': 'ghost-admin-api-session=test-session-data; Path=/ghost; HttpOnly; Secure; Expires=Tue, 31 Dec 2099 23:59:59 GMT;'
});

const exportData = {
db: [{
meta: {
version: '5.0.0'
},
data: {
users: []
}
}]
};
const exportScope = nock(testUrl, {
reqheaders: {
cookie: [
'ghost-admin-api-session=test-session-data'
],
origin: testUrl
}
}).get('/ghost/api/admin/db/').reply(200, exportData);

const tmpDir = tmp.dirSync();
const outputFile = path.join(tmpDir.name, '5.x.json');

await downloadExport('5.0.0', 'http://localhost:2368', {
username: 'test@example.com',
password: 'password'
}, outputFile);

expect(sessionScope.isDone()).to.be.true;
expect(exportScope.isDone()).to.be.true;
expect(fs.readJsonSync(outputFile)).to.deep.equal(exportData);
});
});
1 change: 1 addition & 0 deletions test/unit/tasks/import/fixtures/5.x.json

Large diffs are not rendered by default.

0 comments on commit ae8177b

Please sign in to comment.