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