Skip to content

Commit

Permalink
fix(import): ensure import/exports works with 4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
acburdine committed Mar 18, 2021
1 parent 1ce8a49 commit e9b95bc
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 @@ -10,7 +10,8 @@ const {SystemError} = require('../../errors');
const bases = {
1: '/ghost/api/v0.1',
2: '/ghost/api/v2/admin',
3: '/ghost/api/v3/admin'
3: '/ghost/api/v3/admin',
4: '/ghost/api/v4/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 @@ -26,6 +26,11 @@ describe('Unit > Tasks > Import > setup', function () {
expect(baseUrl).to.equal('https://example.com/ghost/api/v3/admin');
});

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

it('unsupported', function () {
try {
getBaseUrl('0.11.14', 'https://example.com');
Expand Down Expand Up @@ -288,6 +293,36 @@ describe('Unit > Tasks > Import > setup', function () {

expect.fail('runImport should have errored');
});

it('4.x', async function () {
const sessionScope = nock(testUrl, {
reqheaders: {
Origin: testUrl
}
}).post('/ghost/api/v4/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/v4/admin/db/').reply(201, {});

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

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

describe('downloadExport', function () {
Expand Down Expand Up @@ -437,5 +472,49 @@ describe('Unit > Tasks > Import > setup', function () {
expect(exportScope.isDone()).to.be.true;
expect(fs.readJsonSync(outputFile)).to.deep.equal(exportData);
});

it('4.x', async function () {
const sessionScope = nock(testUrl, {
reqheaders: {
Origin: testUrl
}
}).post('/ghost/api/v4/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: '4.0.0'
},
data: {
users: []
}
}]
};
const exportScope = nock(testUrl, {
reqheaders: {
cookie: [
'ghost-admin-api-session=test-session-data'
],
origin: testUrl
}
}).get('/ghost/api/v4/admin/db/').reply(200, exportData);

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

await downloadExport('4.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/4.x.json

Large diffs are not rendered by default.

0 comments on commit e9b95bc

Please sign in to comment.