-
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(import): improve handling of auth errors (#1051)
no issue - return more accurate error messages for authentication errors
- Loading branch information
Showing
2 changed files
with
178 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,54 @@ describe('Unit > Tasks > Import > setup', function () { | |
expect(importScope.isDone()).to.be.true; | ||
}); | ||
|
||
it('handles 404 auth error', async function () { | ||
const clientId = 'client-id'; | ||
const clientSecret = 'client-secret'; | ||
const configBody = { | ||
configuration: [{ | ||
clientId, clientSecret | ||
}] | ||
}; | ||
|
||
const tokenRequestBody = { | ||
grant_type: 'password', | ||
client_id: clientId, | ||
client_secret: clientSecret, | ||
username: '[email protected]', | ||
password: 'password' | ||
}; | ||
|
||
const configScope = nock(testUrl) | ||
.get('/ghost/api/v0.1/configuration/') | ||
.reply(200, configBody); | ||
|
||
const tokenScope = nock(testUrl) | ||
.post('/ghost/api/v0.1/authentication/token/', tokenRequestBody) | ||
.reply(404, {}); | ||
|
||
const importScope = nock(testUrl, { | ||
reqheaders: { | ||
Authorization: 'Bearer access-token' | ||
} | ||
}).post('/ghost/api/v0.1/db/').reply(200, {}); | ||
|
||
try { | ||
await runImport('1.0.0', testUrl, { | ||
username: '[email protected]', | ||
password: 'password' | ||
}, path.join(__dirname, 'fixtures/0.11.x.json')); | ||
} catch (error) { | ||
expect(error).to.be.an.instanceof(SystemError); | ||
expect(error.message).to.equal('There is no user with that email address.'); | ||
expect(configScope.isDone()).to.be.true; | ||
expect(tokenScope.isDone()).to.be.true; | ||
expect(importScope.isDone()).to.be.false; | ||
return; | ||
} | ||
|
||
expect.fail('runImport should have errored'); | ||
}); | ||
|
||
it('2.x', async function () { | ||
const sessionScope = nock(testUrl, { | ||
reqheaders: { | ||
|
@@ -141,6 +189,41 @@ describe('Unit > Tasks > Import > setup', function () { | |
expect(importScope.isDone()).to.be.true; | ||
}); | ||
|
||
it('handles 422 auth error', async function () { | ||
const sessionScope = nock(testUrl, { | ||
reqheaders: { | ||
Origin: testUrl | ||
} | ||
}).post('/ghost/api/v2/admin/session/', { | ||
username: '[email protected]', | ||
password: 'password' | ||
}).reply(422, 'Error'); | ||
|
||
const importScope = nock(testUrl, { | ||
reqheaders: { | ||
cookie: [ | ||
'ghost-admin-api-session=test-session-data' | ||
], | ||
origin: testUrl | ||
} | ||
}).post('/ghost/api/v2/admin/db/').reply(201, {}); | ||
|
||
try { | ||
await runImport('2.0.0', 'http://localhost:2368', { | ||
username: '[email protected]', | ||
password: 'password' | ||
}, path.join(__dirname, 'fixtures/2.x.json')); | ||
} catch (error) { | ||
expect(error).to.be.an.instanceof(SystemError); | ||
expect(error.message).to.equal('Your password is incorrect.'); | ||
expect(sessionScope.isDone()).to.be.true; | ||
expect(importScope.isDone()).to.be.false; | ||
return; | ||
} | ||
|
||
expect.fail('runImport should have errored'); | ||
}); | ||
|
||
it('3.x', async function () { | ||
const sessionScope = nock(testUrl, { | ||
reqheaders: { | ||
|
@@ -170,6 +253,41 @@ describe('Unit > Tasks > Import > setup', function () { | |
expect(sessionScope.isDone()).to.be.true; | ||
expect(importScope.isDone()).to.be.true; | ||
}); | ||
|
||
it('rethrows non-auth errors', async function () { | ||
const sessionScope = nock(testUrl, { | ||
reqheaders: { | ||
Origin: testUrl | ||
} | ||
}).post('/ghost/api/v3/admin/session/', { | ||
username: '[email protected]', | ||
password: 'password' | ||
}).reply(500, 'Error'); | ||
|
||
const importScope = nock(testUrl, { | ||
reqheaders: { | ||
cookie: [ | ||
'ghost-admin-api-session=test-session-data' | ||
], | ||
origin: testUrl | ||
} | ||
}).post('/ghost/api/v3/admin/db/').reply(201, {}); | ||
|
||
try { | ||
await runImport('3.0.0', 'http://localhost:2368', { | ||
username: '[email protected]', | ||
password: 'password' | ||
}, path.join(__dirname, 'fixtures/3.x.json')); | ||
} catch (error) { | ||
expect(error.response).to.exist; | ||
expect(error.response.statusCode).to.equal(500); | ||
expect(sessionScope.isDone()).to.be.true; | ||
expect(importScope.isDone()).to.be.false; | ||
return; | ||
} | ||
|
||
expect.fail('runImport should have errored'); | ||
}); | ||
}); | ||
|
||
describe('downloadExport', function () { | ||
|