Merge "Type hint against LinkTarget in WatchedItemStore"
[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 * Shortcut for `MWBot#edit( .. )`.
8 * Default username, password and base URL is used unless specified
9 *
10 * @since 1.0.0
11 * @see <https://www.mediawiki.org/wiki/API:Edit>
12 * @param {string} title
13 * @param {string} content
14 * @param {string} username - Optional
15 * @param {string} password - Optional
16 * @param {baseUrl} baseUrl - Optional
17 * @return {Object} Promise for API action=edit response data.
18 */
19 edit( title,
20 content,
21 username = browser.options.username,
22 password = browser.options.password,
23 baseUrl = browser.options.baseUrl
24 ) {
25 const bot = new MWBot();
26
27 return bot.loginGetEditToken( {
28 apiUrl: `${baseUrl}/api.php`,
29 username: username,
30 password: password
31 } ).then( function () {
32 return bot.edit( title, content, `Created or updated page with "${content}"` );
33 } );
34 },
35
36 /**
37 * Shortcut for `MWBot#delete( .. )`.
38 *
39 * @since 1.0.0
40 * @see <https://www.mediawiki.org/wiki/API:Delete>
41 * @param {string} title
42 * @param {string} reason
43 * @return {Object} Promise for API action=delete response data.
44 */
45 delete( title, reason ) {
46 const bot = new MWBot();
47
48 return bot.loginGetEditToken( {
49 apiUrl: `${browser.options.baseUrl}/api.php`,
50 username: browser.options.username,
51 password: browser.options.password
52 } ).then( function () {
53 return bot.delete( title, reason );
54 } );
55 },
56
57 /**
58 * Shortcut for `MWBot#request( { acount: 'createaccount', .. } )`.
59 *
60 * @since 1.0.0
61 * @see <https://www.mediawiki.org/wiki/API:Account_creation>
62 * @param {string} username
63 * @param {string} password
64 * @return {Object} Promise for API action=createaccount response data.
65 */
66 createAccount( username, password ) {
67 const bot = new MWBot();
68
69 // Log in as admin
70 return bot.loginGetCreateaccountToken( {
71 apiUrl: `${browser.options.baseUrl}/api.php`,
72 username: browser.options.username,
73 password: browser.options.password
74 } ).then( function () {
75 // Create the new account
76 return bot.request( {
77 action: 'createaccount',
78 createreturnurl: browser.options.baseUrl,
79 createtoken: bot.createaccountToken,
80 username: username,
81 password: password,
82 retype: password
83 } );
84 } );
85 },
86
87 /**
88 * Shortcut for `MWBot#request( { action: 'block', .. } )`.
89 *
90 * @since 0.3.0
91 * @see <https://www.mediawiki.org/wiki/API:Block>
92 * @param {string} [username] defaults to user making the request
93 * @param {string} [expiry] default is not set. For format see API docs
94 * @return {Object} Promise for API action=block response data.
95 */
96 blockUser( username, expiry ) {
97 const bot = new MWBot();
98
99 // Log in as admin
100 return bot.loginGetEditToken( {
101 apiUrl: `${browser.options.baseUrl}/api.php`,
102 username: browser.options.username,
103 password: browser.options.password
104 } ).then( () => {
105 // block user. default = admin
106 return bot.request( {
107 action: 'block',
108 user: username || browser.options.username,
109 reason: 'browser test',
110 token: bot.editToken,
111 expiry
112 } );
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 unblockUser( username ) {
125 const bot = new MWBot();
126
127 // Log in as admin
128 return bot.loginGetEditToken( {
129 apiUrl: `${browser.options.baseUrl}/api.php`,
130 username: browser.options.username,
131 password: browser.options.password
132 } ).then( () => {
133 // unblock user. default = admin
134 return bot.request( {
135 action: 'unblock',
136 user: username || browser.options.username,
137 reason: 'browser test done',
138 token: bot.editToken
139 } );
140 } );
141 }
142 };