X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=tests%2Fselenium%2Fwdio-mediawiki%2FApi.js;h=a1cfa5fe76b0a79f635c1dd675768889faa2769c;hb=1955a8aa5;hp=7947ff504cfc5ba5338a51644eecccfc6258f644;hpb=3a026473873ac3cc9d5c181f05961f474495d32c;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/selenium/wdio-mediawiki/Api.js b/tests/selenium/wdio-mediawiki/Api.js index 7947ff504c..a1cfa5fe76 100644 --- a/tests/selenium/wdio-mediawiki/Api.js +++ b/tests/selenium/wdio-mediawiki/Api.js @@ -3,11 +3,37 @@ const MWBot = require( 'mwbot' ); // TODO: Once we require Node 7 or later, we can use async-await. module.exports = { + /** + * Get a logged-in instance of `MWBot` with edit token already set up. + * Default username, password and base URL is used unless specified. + * + * @since 0.5.0 + * @param {string} username - Optional + * @param {string} password - Optional + * @param {string} baseUrl - Optional + * @return {Promise} + */ + bot( + username = browser.config.mwUser, + password = browser.config.mwPwd, + baseUrl = browser.config.baseUrl + ) { + const bot = new MWBot(); + + return bot.loginGetEditToken( { + apiUrl: `${baseUrl}/api.php`, + username: username, + password: password + } ).then( function () { + return bot; + } ); + }, + /** * Shortcut for `MWBot#edit( .. )`. * Default username, password and base URL is used unless specified * - * @since 1.0.0 + * @since 0.1.0 * @see * @param {string} title * @param {string} content @@ -18,64 +44,54 @@ module.exports = { */ edit( title, content, - username = browser.options.username, - password = browser.options.password, - baseUrl = browser.options.baseUrl + username = browser.config.mwUser, + password = browser.config.mwPwd, + baseUrl = browser.config.baseUrl ) { - let bot = new MWBot(); - - return bot.loginGetEditToken( { - apiUrl: `${baseUrl}/api.php`, - username: username, - password: password - } ).then( function () { - return bot.edit( title, content, `Created or updated page with "${content}"` ); - } ); + return this.bot( username, password, baseUrl ) + .then( function ( bot ) { + return bot.edit( title, content, `Created or updated page with "${content}"` ); + } ); }, /** * Shortcut for `MWBot#delete( .. )`. * - * @since 1.0.0 + * @since 0.1.0 * @see * @param {string} title * @param {string} reason * @return {Object} Promise for API action=delete response data. */ delete( title, reason ) { - let bot = new MWBot(); - - return bot.loginGetEditToken( { - apiUrl: `${browser.options.baseUrl}/api.php`, - username: browser.options.username, - password: browser.options.password - } ).then( function () { - return bot.delete( title, reason ); - } ); + return this.bot() + .then( function ( bot ) { + return bot.delete( title, reason ); + } ); }, /** * Shortcut for `MWBot#request( { acount: 'createaccount', .. } )`. * - * @since 1.0.0 + * @since 0.1.0 * @see * @param {string} username * @param {string} password * @return {Object} Promise for API action=createaccount response data. */ createAccount( username, password ) { - let bot = new MWBot(); + const bot = new MWBot(); // Log in as admin return bot.loginGetCreateaccountToken( { - apiUrl: `${browser.options.baseUrl}/api.php`, - username: browser.options.username, - password: browser.options.password + apiUrl: `${browser.config.baseUrl}/api.php`, + username: browser.config.mwUser, + password: browser.config.mwPwd } ).then( function () { // Create the new account return bot.request( { action: 'createaccount', - createreturnurl: browser.options.baseUrl, + createreturnurl: browser.config.baseUrl, createtoken: bot.createaccountToken, username: username, password: password, @@ -94,23 +110,17 @@ module.exports = { * @return {Object} Promise for API action=block response data. */ blockUser( username, expiry ) { - let bot = new MWBot(); - - // Log in as admin - return bot.loginGetEditToken( { - apiUrl: `${browser.options.baseUrl}/api.php`, - username: browser.options.username, - password: browser.options.password - } ).then( () => { - // block user. default = admin - return bot.request( { - action: 'block', - user: username || browser.options.username, - reason: 'browser test', - token: bot.editToken, - expiry + return this.bot() + .then( function ( bot ) { + // block user. default = admin + return bot.request( { + action: 'block', + user: username || browser.config.mwUser, + reason: 'browser test', + token: bot.editToken, + expiry + } ); } ); - } ); }, /** @@ -122,21 +132,15 @@ module.exports = { * @return {Object} Promise for API action=unblock response data. */ unblockUser( username ) { - let bot = new MWBot(); - - // Log in as admin - return bot.loginGetEditToken( { - apiUrl: `${browser.options.baseUrl}/api.php`, - username: browser.options.username, - password: browser.options.password - } ).then( () => { - // unblock user. default = admin - return bot.request( { - action: 'unblock', - user: username || browser.options.username, - reason: 'browser test done', - token: bot.editToken + return this.bot() + .then( function ( bot ) { + // unblock user. default = admin + return bot.request( { + action: 'unblock', + user: username || browser.config.mwUser, + reason: 'browser test done', + token: bot.editToken + } ); } ); - } ); } };