Merge "tests: Prefer assertSame() when comparing the integer 0"
[lhc/web/wiklou.git] / tests / selenium / wdio-mediawiki / Api.js
index 6b674b9..8a28e4d 100644 (file)
@@ -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<MWBot>}
+        */
+       bot(
+               username = browser.options.username,
+               password = browser.options.password,
+               baseUrl = browser.options.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 <https://www.mediawiki.org/wiki/API:Edit>
         * @param {string} title
         * @param {string} content
@@ -22,42 +48,32 @@ module.exports = {
                password = browser.options.password,
                baseUrl = browser.options.baseUrl
        ) {
-               const 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 <https://www.mediawiki.org/wiki/API:Delete>
         * @param {string} title
         * @param {string} reason
         * @return {Object} Promise for API action=delete response data.
         */
        delete( title, reason ) {
-               const 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 <https://www.mediawiki.org/wiki/API:Account_creation>
         * @param {string} username
         * @param {string} password
@@ -94,23 +110,17 @@ module.exports = {
         * @return {Object} Promise for API action=block response data.
         */
        blockUser( username, expiry ) {
-               const 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.options.username,
+                                       reason: 'browser test',
+                                       token: bot.editToken,
+                                       expiry
+                               } );
                        } );
-               } );
        },
 
        /**
@@ -122,21 +132,15 @@ module.exports = {
         * @return {Object} Promise for API action=unblock response data.
         */
        unblockUser( username ) {
-               const 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.options.username,
+                                       reason: 'browser test done',
+                                       token: bot.editToken
+                               } );
                        } );
-               } );
        }
 };