2022-02-21 07:19:07 +03:00
|
|
|
const TestAgent = require('./test-agent');
|
2022-07-06 11:41:38 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2022-02-21 07:04:47 +03:00
|
|
|
|
|
|
|
/**
|
2022-02-21 07:19:07 +03:00
|
|
|
* NOTE: this class is not doing much at the moment. It's rather a placeholder to put
|
|
|
|
* any Members API specific functionality into. If there is none in the nearest
|
|
|
|
* future, it would make sense to remove it alltogether.
|
2022-02-21 07:04:47 +03:00
|
|
|
* @constructor
|
|
|
|
* @param {Object} app Ghost express app instance
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {String} options.apiURL
|
|
|
|
* @param {String} options.originURL
|
|
|
|
*/
|
2022-02-21 07:19:07 +03:00
|
|
|
class MembersAPITestAgent extends TestAgent {
|
2022-11-18 16:58:35 +03:00
|
|
|
#bootOptions = null;
|
|
|
|
|
2022-02-21 07:04:47 +03:00
|
|
|
constructor(app, options) {
|
2022-02-21 07:19:07 +03:00
|
|
|
super(app, options);
|
2022-11-18 16:58:35 +03:00
|
|
|
this.#bootOptions = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
duplicate() {
|
|
|
|
return new MembersAPITestAgent(this.app, this.#bootOptions);
|
2022-02-21 07:04:47 +03:00
|
|
|
}
|
2022-07-06 11:41:38 +03:00
|
|
|
|
|
|
|
async loginAs(email) {
|
2022-07-26 19:00:37 +03:00
|
|
|
const membersService = require('../../../core/server/services/members');
|
2022-11-09 18:25:42 +03:00
|
|
|
const memberRepository = membersService.api.members;
|
|
|
|
|
|
|
|
const member = await memberRepository.get({email});
|
|
|
|
|
|
|
|
if (!member) {
|
|
|
|
// Create the member first with context internal if it doesn't exist to prevent sending a signup email
|
|
|
|
await memberRepository.create({name: '', email}, {context: {internal: true}});
|
|
|
|
}
|
|
|
|
|
|
|
|
const magicLink = await membersService.api.getMagicLink(email, 'signin');
|
2022-07-06 11:41:38 +03:00
|
|
|
const magicLinkUrl = new URL(magicLink);
|
|
|
|
const token = magicLinkUrl.searchParams.get('token');
|
|
|
|
|
|
|
|
const res = await this.get(`/?token=${token}`);
|
|
|
|
|
|
|
|
if (res.statusCode !== 302) {
|
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: res.body.errors[0].message
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.headers['set-cookie'];
|
|
|
|
}
|
2022-02-21 07:04:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MembersAPITestAgent;
|