Add block and unblock commands to WDIO
authorThomas Arrow <thomas.arrow_ext@wikimedia.de>
Fri, 11 Jan 2019 17:36:12 +0000 (17:36 +0000)
committerThomas Arrow <thomas.arrow_ext@wikimedia.de>
Tue, 22 Jan 2019 14:58:57 +0000 (14:58 +0000)
Defaults to expiry time of never to mirror API default

This leaves it up to individual test suite to assert that the user
they use is in the right blocked/unblocked state

Bug: T211038
Change-Id: Icf94458eb9c9a1fa25e5ef9291d9fc2c54a5567c

tests/selenium/wdio-mediawiki/Api.js

index 40bce32..f68fee9 100644 (file)
@@ -73,5 +73,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 ) {
+               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
+                       } );
+               } );
+       },
+
+       /**
+        * 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 ) {
+               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
+                       } );
+               } );
        }
 };