Merge "StringUtils: Add a utility for checking if a string is a valid regex"
[lhc/web/wiklou.git] / tests / selenium / wdio-mediawiki / Api.js
1 const MWBot = require( 'mwbot' );
2
3 // TODO: Once we require Node 7 or later, we can use async-await.
4
5 module.exports = {
6 /**
7 * Get a logged-in instance of `MWBot` with edit token already set up.
8 * Default username, password and base URL is used unless specified.
9 *
10 * @since 0.5.0
11 * @param {string} username - Optional
12 * @param {string} password - Optional
13 * @param {string} baseUrl - Optional
14 * @return {Promise<MWBot>}
15 */
16 bot(
17 username = browser.options.username,
18 password = browser.options.password,
19 baseUrl = browser.options.baseUrl
20 ) {
21 const bot = new MWBot();
22
23 return bot.loginGetEditToken( {
24 apiUrl: `${baseUrl}/api.php`,
25 username: username,
26 password: password
27 } ).then( function () {
28 return bot;
29 } );
30 },
31
32 /**
33 * Shortcut for `MWBot#edit( .. )`.
34 * Default username, password and base URL is used unless specified
35 *
36 * @since 0.1.0
37 * @see <https://www.mediawiki.org/wiki/API:Edit>
38 * @param {string} title
39 * @param {string} content
40 * @param {string} username - Optional
41 * @param {string} password - Optional
42 * @param {baseUrl} baseUrl - Optional
43 * @return {Object} Promise for API action=edit response data.
44 */
45 edit( title,
46 content,
47 username = browser.options.username,
48 password = browser.options.password,
49 baseUrl = browser.options.baseUrl
50 ) {
51 return this.bot( username, password, baseUrl )
52 .then( function ( bot ) {
53 return bot.edit( title, content, `Created or updated page with "${content}"` );
54 } );
55 },
56
57 /**
58 * Shortcut for `MWBot#delete( .. )`.
59 *
60 * @since 0.1.0
61 * @see <https://www.mediawiki.org/wiki/API:Delete>
62 * @param {string} title
63 * @param {string} reason
64 * @return {Object} Promise for API action=delete response data.
65 */
66 delete( title, reason ) {
67 return this.bot()
68 .then( function ( bot ) {
69 return bot.delete( title, reason );
70 } );
71 },
72
73 /**
74 * Shortcut for `MWBot#request( { acount: 'createaccount', .. } )`.
75 *
76 * @since 0.1.0
77 * @see <https://www.mediawiki.org/wiki/API:Account_creation>
78 * @param {string} username
79 * @param {string} password
80 * @return {Object} Promise for API action=createaccount response data.
81 */
82 createAccount( username, password ) {
83 const bot = new MWBot();
84
85 // Log in as admin
86 return bot.loginGetCreateaccountToken( {
87 apiUrl: `${browser.options.baseUrl}/api.php`,
88 username: browser.options.username,
89 password: browser.options.password
90 } ).then( function () {
91 // Create the new account
92 return bot.request( {
93 action: 'createaccount',
94 createreturnurl: browser.options.baseUrl,
95 createtoken: bot.createaccountToken,
96 username: username,
97 password: password,
98 retype: password
99 } );
100 } );
101 },
102
103 /**
104 * Shortcut for `MWBot#request( { action: 'block', .. } )`.
105 *
106 * @since 0.3.0
107 * @see <https://www.mediawiki.org/wiki/API:Block>
108 * @param {string} [username] defaults to user making the request
109 * @param {string} [expiry] default is not set. For format see API docs
110 * @return {Object} Promise for API action=block response data.
111 */
112 blockUser( username, expiry ) {
113 return this.bot()
114 .then( function ( bot ) {
115 // block user. default = admin
116 return bot.request( {
117 action: 'block',
118 user: username || browser.options.username,
119 reason: 'browser test',
120 token: bot.editToken,
121 expiry
122 } );
123 } );
124 },
125
126 /**
127 * Shortcut for `MWBot#request( { action: 'unblock', .. } )`.
128 *
129 * @since 0.3.0
130 * @see <https://www.mediawiki.org/wiki/API:Block>
131 * @param {string} [username] defaults to user making the request
132 * @return {Object} Promise for API action=unblock response data.
133 */
134 unblockUser( username ) {
135 return this.bot()
136 .then( function ( bot ) {
137 // unblock user. default = admin
138 return bot.request( {
139 action: 'unblock',
140 user: username || browser.options.username,
141 reason: 'browser test done',
142 token: bot.editToken
143 } );
144 } );
145 }
146 };