Merge "Fix various fatals and IDEA warnings in CopyFileBackend"
[lhc/web/wiklou.git] / tests / selenium / wdio-mediawiki / Api.js
index 40bce32..6b674b9 100644 (file)
@@ -5,22 +5,31 @@ const MWBot = require( 'mwbot' );
 module.exports = {
        /**
         * Shortcut for `MWBot#edit( .. )`.
+        * Default username, password and base URL is used unless specified
         *
         * @since 1.0.0
         * @see <https://www.mediawiki.org/wiki/API:Edit>
         * @param {string} title
         * @param {string} content
+        * @param {string} username - Optional
+        * @param {string} password - Optional
+        * @param {baseUrl} baseUrl - Optional
         * @return {Object} Promise for API action=edit response data.
         */
-       edit( title, content ) {
-               let bot = new MWBot();
+       edit( title,
+               content,
+               username = browser.options.username,
+               password = browser.options.password,
+               baseUrl = browser.options.baseUrl
+       ) {
+               const bot = new MWBot();
 
                return bot.loginGetEditToken( {
-                       apiUrl: `${browser.options.baseUrl}/api.php`,
-                       username: browser.options.username,
-                       password: browser.options.password
+                       apiUrl: `${baseUrl}/api.php`,
+                       username: username,
+                       password: password
                } ).then( function () {
-                       return bot.edit( title, content, `Created page with "${content}"` );
+                       return bot.edit( title, content, `Created or updated page with "${content}"` );
                } );
        },
 
@@ -34,7 +43,7 @@ module.exports = {
         * @return {Object} Promise for API action=delete response data.
         */
        delete( title, reason ) {
-               let bot = new MWBot();
+               const bot = new MWBot();
 
                return bot.loginGetEditToken( {
                        apiUrl: `${browser.options.baseUrl}/api.php`,
@@ -55,7 +64,7 @@ module.exports = {
         * @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( {
@@ -73,5 +82,61 @@ module.exports = {
                                retype: password
                        } );
                } );
+       },
+
+       /**
+        * Shortcut for `MWBot#request( { action: 'block', .. } )`.
+        *
+        * @since 0.3.0
+        * @see <https://www.mediawiki.org/wiki/API:Block>
+        * @param {string} [username] defaults to user making the request
+        * @param {string} [expiry] default is not set. For format see API docs
+        * @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
+                       } );
+               } );
+       },
+
+       /**
+        * Shortcut for `MWBot#request( { action: 'unblock', .. } )`.
+        *
+        * @since 0.3.0
+        * @see <https://www.mediawiki.org/wiki/API:Block>
+        * @param {string} [username] defaults to user making the request
+        * @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
+                       } );
+               } );
        }
 };