| 1 |
'use strict'; |
| 2 |
|
| 3 |
const { action, assert, utils, wiki } = require( 'api-testing' ); |
| 4 |
|
| 5 |
describe( 'The edit action', () => { |
| 6 |
let alice; |
| 7 |
let Clark = action.getAnon(); |
| 8 |
|
| 9 |
before( async () => { |
| 10 |
alice = await action.alice(); |
| 11 |
Clark = await Clark.account( 'Clark_' ); |
| 12 |
} ); |
| 13 |
|
| 14 |
const pageA = utils.title( 'Edit_A_' ); |
| 15 |
const edits = {}; |
| 16 |
|
| 17 |
const testEditAndLog = async ( page, user, isAnonEdit = false ) => { |
| 18 |
const edit = await user.edit( page, { |
| 19 |
text: utils.uniq(), |
| 20 |
summary: utils.uniq() |
| 21 |
} ); |
| 22 |
|
| 23 |
const rev1 = await user.getRevision( page ); |
| 24 |
assert.equal( rev1.revid, edit.newrevid ); |
| 25 |
|
| 26 |
if ( !isAnonEdit ) { |
| 27 |
assert.equal( rev1.user, edit.param_user ); |
| 28 |
} |
| 29 |
assert.equal( rev1.comment, edit.param_summary ); |
| 30 |
assert.equal( rev1.timestamp, edit.newtimestamp ); |
| 31 |
assert.equal( rev1.slots.main[ '*' ], edit.param_text ); |
| 32 |
|
| 33 |
const rc1 = await user.getChangeEntry( { rctitle: page } ); |
| 34 |
assert.equal( rc1.type, edit.oldrevid ? 'edit' : 'new' ); |
| 35 |
assert.equal( rc1.revid, edit.newrevid ); |
| 36 |
if ( !isAnonEdit ) { |
| 37 |
assert.equal( rc1.user, edit.param_user ); |
| 38 |
} |
| 39 |
assert.equal( rc1.comment, edit.param_summary ); |
| 40 |
assert.equal( rc1.timestamp, edit.newtimestamp ); |
| 41 |
|
| 42 |
const html1 = await user.getHtml( page ); |
| 43 |
assert.include( html1, edit.param_text ); |
| 44 |
|
| 45 |
return edit; |
| 46 |
}; |
| 47 |
|
| 48 |
it( 'allows a page to be created and modified by a logged in user', async () => { |
| 49 |
edits.pageA1 = await testEditAndLog( pageA, alice ); |
| 50 |
edits.pageA2 = await testEditAndLog( pageA, alice ); |
| 51 |
} ); |
| 52 |
|
| 53 |
it( 'allows a page to be created and modified by an anonymous user', async () => { |
| 54 |
const title = utils.title(); |
| 55 |
const anon = action.getAnon(); |
| 56 |
const { name } = await anon.meta( 'userinfo', {} ); |
| 57 |
anon.username = name; |
| 58 |
|
| 59 |
await testEditAndLog( title, anon, true ); |
| 60 |
} ); |
| 61 |
|
| 62 |
it( 'skips redundant edit', async () => { |
| 63 |
const result = await alice.edit( pageA, { |
| 64 |
text: edits.pageA2.param_text, |
| 65 |
summary: 'some new fancy comment' |
| 66 |
} ); |
| 67 |
|
| 68 |
assert.notExists( result.newrevid ); |
| 69 |
assert.exists( result.nochange ); |
| 70 |
} ); |
| 71 |
|
| 72 |
it( 'logs page creation', async () => { |
| 73 |
const log = await alice.getLogEntry( { letitle: pageA, letype: 'create' } ); |
| 74 |
|
| 75 |
assert.equal( log.action, 'create' ); |
| 76 |
assert.equal( log.user, edits.pageA1.param_user ); |
| 77 |
assert.equal( log.comment, edits.pageA1.param_summary ); |
| 78 |
assert.equal( log.timestamp, edits.pageA1.newtimestamp ); |
| 79 |
} ); |
| 80 |
|
| 81 |
it( 'applies pre-save transform', async () => { |
| 82 |
await alice.edit( pageA, { |
| 83 |
text: 'Hi -- ~~~~', |
| 84 |
summary: 'test signature' |
| 85 |
} ); |
| 86 |
|
| 87 |
const rev = await alice.getRevision( pageA ); |
| 88 |
assert.notMatch( rev.slots.main[ '*' ], /~~~~/ ); |
| 89 |
assert.match( rev.slots.main[ '*' ], /Alice/ ); |
| 90 |
} ); |
| 91 |
|
| 92 |
it( 'enforces the createonly and nocreate flags', async () => { |
| 93 |
const pageB = utils.title( 'Edit_B_' ); |
| 94 |
|
| 95 |
const error1 = await alice.actionError( 'edit', { |
| 96 |
title: pageB, |
| 97 |
token: await alice.token( 'csrf' ), |
| 98 |
text: 'Updated content', |
| 99 |
summary: 'update', |
| 100 |
nocreate: true |
| 101 |
}, 'POST' ); |
| 102 |
|
| 103 |
assert.equal( error1.code, 'missingtitle' ); |
| 104 |
|
| 105 |
await alice.edit( pageB, { |
| 106 |
text: 'Initial content', |
| 107 |
summary: 'create', |
| 108 |
createonly: true |
| 109 |
} ); |
| 110 |
|
| 111 |
const error2 = await alice.actionError( 'edit', { |
| 112 |
title: pageB, |
| 113 |
token: await alice.token( 'csrf' ), |
| 114 |
text: 'Initial content again', |
| 115 |
summary: 'create again', |
| 116 |
createonly: true |
| 117 |
}, 'POST' ); |
| 118 |
|
| 119 |
assert.equal( error2.code, 'articleexists' ); |
| 120 |
|
| 121 |
await alice.edit( pageB, { |
| 122 |
text: 'Updated content', |
| 123 |
summary: 'update', |
| 124 |
nocreate: true |
| 125 |
} ); |
| 126 |
} ); |
| 127 |
|
| 128 |
it( 'allows bots to set the bot flag (or not)', async () => { |
| 129 |
const robby = await action.robby(); |
| 130 |
|
| 131 |
await robby.edit( pageA, { |
| 132 |
text: 'Bot content', |
| 133 |
summary: 'botting', |
| 134 |
bot: true |
| 135 |
} ); |
| 136 |
|
| 137 |
await wiki.runAllJobs(); |
| 138 |
|
| 139 |
const rc1 = await alice.getChangeEntry( { rctitle: pageA } ); |
| 140 |
assert.exists( rc1.bot ); |
| 141 |
|
| 142 |
await robby.edit( pageA, { |
| 143 |
text: 'Not bot content', |
| 144 |
summary: 'not botting' |
| 145 |
} ); |
| 146 |
|
| 147 |
await wiki.runAllJobs(); |
| 148 |
|
| 149 |
const rc2 = await alice.getChangeEntry( { rctitle: pageA } ); |
| 150 |
assert.notExists( rc2.bot ); |
| 151 |
} ); |
| 152 |
|
| 153 |
it( 'does not allow regular users to set the bot flag', async () => { |
| 154 |
await alice.edit( pageA, { |
| 155 |
text: 'Fake bot content', |
| 156 |
summary: 'fake botting', |
| 157 |
bot: true |
| 158 |
} ); |
| 159 |
|
| 160 |
const rc1 = await alice.getChangeEntry( { rctitle: pageA } ); |
| 161 |
assert.notExists( rc1.bot ); |
| 162 |
} ); |
| 163 |
|
| 164 |
it( 'allows users to set the minor flag', async () => { |
| 165 |
await alice.edit( pageA, { |
| 166 |
text: 'Minor content', |
| 167 |
summary: 'minor change', |
| 168 |
minor: true |
| 169 |
} ); |
| 170 |
|
| 171 |
const rev1 = await alice.getRevision( pageA ); |
| 172 |
assert.exists( rev1.minor ); |
| 173 |
|
| 174 |
const rc1 = await alice.getChangeEntry( { rctitle: pageA } ); |
| 175 |
assert.exists( rc1.minor ); |
| 176 |
} ); |
| 177 |
|
| 178 |
it( 'does not allow anon users to set the minor flag', async () => { |
| 179 |
const anon = action.getAnon(); |
| 180 |
await anon.edit( pageA, { |
| 181 |
text: 'Anon minor content', |
| 182 |
summary: 'anon minor change', |
| 183 |
minor: true |
| 184 |
} ); |
| 185 |
|
| 186 |
const rev1 = await alice.getRevision( pageA ); |
| 187 |
assert.notExists( rev1.minor ); |
| 188 |
|
| 189 |
const rc1 = await alice.getChangeEntry( { rctitle: pageA } ); |
| 190 |
assert.notExists( rc1.minor ); |
| 191 |
} ); |
| 192 |
|
| 193 |
it( 'allows a user to create a page and add sections', async () => { |
| 194 |
const pageB = utils.title( 'Page_B' ); |
| 195 |
const editText = 'Some random text'; |
| 196 |
const sectionText1 = 'Some text for the first section'; |
| 197 |
const sectionText2 = 'Some text for the second section'; |
| 198 |
|
| 199 |
await Clark.edit( pageB, { |
| 200 |
text: editText |
| 201 |
} ); |
| 202 |
|
| 203 |
await Clark.edit( pageB, { |
| 204 |
section: 'new', |
| 205 |
sectiontitle: 'First', |
| 206 |
text: sectionText1 |
| 207 |
} ); |
| 208 |
|
| 209 |
await Clark.edit( pageB, { |
| 210 |
section: 'new', |
| 211 |
sectiontitle: 'Second', |
| 212 |
text: sectionText2 |
| 213 |
} ); |
| 214 |
|
| 215 |
const firstModification = await Clark.getHtml( pageB ); |
| 216 |
assert.include( firstModification, editText ); |
| 217 |
assert.include( firstModification, sectionText1 ); |
| 218 |
assert.include( firstModification, sectionText2 ); |
| 219 |
|
| 220 |
await Clark.edit( pageB, { |
| 221 |
section: 0, |
| 222 |
text: `${ editText } some text` |
| 223 |
} ); |
| 224 |
|
| 225 |
const secondModification = await Clark.getHtml( pageB ); |
| 226 |
assert.include( secondModification, `${ editText } some text` ); |
| 227 |
assert.include( secondModification, sectionText1 ); |
| 228 |
assert.include( secondModification, sectionText2 ); |
| 229 |
|
| 230 |
await Clark.edit( pageB, { |
| 231 |
section: 1, |
| 232 |
text: `== First ==\n${ sectionText1 } some text` |
| 233 |
} ); |
| 234 |
|
| 235 |
const thirdModification = await Clark.getHtml( pageB ); |
| 236 |
assert.include( thirdModification, `${ editText } some text` ); |
| 237 |
assert.include( thirdModification, `${ sectionText1 } some text` ); |
| 238 |
assert.include( thirdModification, sectionText2 ); |
| 239 |
|
| 240 |
await Clark.edit( pageB, { |
| 241 |
section: 1, |
| 242 |
text: '' |
| 243 |
} ); |
| 244 |
|
| 245 |
const fourthModification = await Clark.getHtml( pageB ); |
| 246 |
assert.include( fourthModification, `${ editText } some text` ); |
| 247 |
assert.notInclude( fourthModification, '== First ==' ); |
| 248 |
assert.include( fourthModification, sectionText2 ); |
| 249 |
} ); |
| 250 |
|
| 251 |
it( 'allows a user to append text to the end of the page and prepend text to the beginning of the page', async () => { |
| 252 |
const page = utils.title( 'Page_' ); |
| 253 |
const text = 'text'; |
| 254 |
const bottom = 'bottom'; |
| 255 |
const top = 'top'; |
| 256 |
|
| 257 |
await Clark.edit( page, { |
| 258 |
text: text |
| 259 |
} ); |
| 260 |
await Clark.edit( page, { |
| 261 |
appendtext: bottom |
| 262 |
} ); |
| 263 |
await Clark.edit( page, { |
| 264 |
prependtext: top |
| 265 |
} ); |
| 266 |
|
| 267 |
const pageContent = await Clark.getHtml( page ); |
| 268 |
assert.include( pageContent, `${ top }${ text }${ bottom }` ); |
| 269 |
} ); |
| 270 |
|
| 271 |
it( 'should allow a user make edits on a page passing the timestamp from the previous edit on the same page the basetimetamp', async () => { |
| 272 |
const page = utils.title( 'Page_' ); |
| 273 |
const editContent = 'An edit from Clark'; |
| 274 |
const rev1 = await alice.edit( page, { |
| 275 |
text: 'An edit from Alice' |
| 276 |
} ); |
| 277 |
await Clark.edit( page, { |
| 278 |
text: editContent, |
| 279 |
basetimestamp: rev1.timestamp |
| 280 |
} ); |
| 281 |
const pageContent = await Clark.getHtml( page ); |
| 282 |
assert.include( pageContent, editContent ); |
| 283 |
} ); |
| 284 |
|
| 285 |
it( 'should throw an error when a user tries to set the basetimestamp to a date/time different from the timestamp of the previous edit', async () => { |
| 286 |
const page = utils.title( 'Page_' ); |
| 287 |
const rev1 = await alice.edit( page, { |
| 288 |
text: 'An edit from Alice' |
| 289 |
} ); |
| 290 |
await Clark.edit( page, { |
| 291 |
text: 'An edit from Clark', |
| 292 |
basetimestamp: rev1.timestamp |
| 293 |
} ); |
| 294 |
const error = await alice.actionError( 'edit', { |
| 295 |
title: page, |
| 296 |
text: 'Another edit by Alice', |
| 297 |
token: await alice.token( 'csrf' ), |
| 298 |
basetimestamp: '2001-01-01T00:00:00Z' |
| 299 |
}, 'POST' ); |
| 300 |
assert.equal( error.code, 'editconflict' ); |
| 301 |
} ); |
| 302 |
|
| 303 |
it( 'should throw an error when a wrong value is passed to md5', async () => { |
| 304 |
const page = utils.title( 'Page' ); |
| 305 |
const error = await Clark.actionError( 'edit', { |
| 306 |
title: page, |
| 307 |
token: await Clark.token( 'csrf' ), |
| 308 |
text: 'Some Text', |
| 309 |
summary: 'Bad md5', |
| 310 |
md5: 'sometext' |
| 311 |
}, 'POST' ); |
| 312 |
assert.equal( error.code, 'badmd5' ); |
| 313 |
} ); |
| 314 |
|
| 315 |
it( 'should edit the page when the correct md5 hash of the text parameter is passed to it', async () => { |
| 316 |
const page = utils.title( 'Page_' ); |
| 317 |
const text = 'An edit with the correct md5'; |
| 318 |
await Clark.edit( page, { |
| 319 |
text: text, |
| 320 |
md5: 'd19dc30a12f31ec3684743bc010ef148' |
| 321 |
} ); |
| 322 |
|
| 323 |
const pageContent = await Clark.getHtml( page ); |
| 324 |
assert.include( pageContent, text ); |
| 325 |
} ); |
| 326 |
|
| 327 |
it( 'should edit the page when the correct md5 hash of the appendtext is passed to the md5 parameter as the appendtext parameter should override the text parameter', async () => { |
| 328 |
const page = utils.title( 'Page_' ); |
| 329 |
const text = 'An edit containing text and appendtext parameters with the correct md5'; |
| 330 |
const bottom = 'An edit just for the bottom'; |
| 331 |
await Clark.edit( page, { |
| 332 |
text: text, |
| 333 |
appendtext: bottom, |
| 334 |
md5: '83edcea6d422ba921d0442ca29abce1d' |
| 335 |
} ); |
| 336 |
|
| 337 |
const pageContent = await Clark.getHtml( page ); |
| 338 |
assert.notInclude( pageContent, text ); |
| 339 |
assert.include( pageContent, bottom ); |
| 340 |
} ); |
| 341 |
|
| 342 |
it( 'should edit the page when the correct md5 hash of the prependtext is passed to the md5 parameter as the prependtext parameter should override the text parameter', async () => { |
| 343 |
const page = utils.title( 'Page_' ); |
| 344 |
const text = 'An edit containing text and appendtext parameters with the correct md5'; |
| 345 |
const top = 'An edit just for the top'; |
| 346 |
await Clark.edit( page, { |
| 347 |
text: text, |
| 348 |
prependtext: top, |
| 349 |
md5: '0f5f08a431409bc0c504c090fe4f2f11' |
| 350 |
} ); |
| 351 |
|
| 352 |
const pageContent = await Clark.getHtml( page ); |
| 353 |
assert.notInclude( pageContent, text ); |
| 354 |
assert.include( pageContent, top ); |
| 355 |
} ); |
| 356 |
|
| 357 |
it( 'should edit the page when the correct md5 hash of the prependtext and appendtext concatenated is passed to the md5 parameter', async () => { |
| 358 |
const page = utils.title( 'Page_' ); |
| 359 |
const text = 'An edit containing text and appendtext parameters with the correct md5'; |
| 360 |
const top = 'An edit just for the top'; |
| 361 |
const bottom = 'An edit just for the bottom'; |
| 362 |
await Clark.edit( page, { |
| 363 |
text: text, |
| 364 |
prependtext: top, |
| 365 |
appendtext: bottom, |
| 366 |
md5: 'b1df72139fc1202b6d817d9b1459e2d5' |
| 367 |
} ); |
| 368 |
|
| 369 |
const pageContent = await Clark.getHtml( page ); |
| 370 |
assert.notInclude( pageContent, text ); |
| 371 |
assert.include( pageContent, top + bottom ); |
| 372 |
} ); |
| 373 |
|
| 374 |
it( 'should throw an error when the wrong md5 hash of the prependtext and appendtext concatenated is passed to the md5 parameter', async () => { |
| 375 |
const page = utils.title( 'Page_' ); |
| 376 |
const text = 'An edit containing text and appendtext parameters with the correct md5'; |
| 377 |
const top = 'An edit just for the top'; |
| 378 |
const bottom = 'An edit just for the bottom'; |
| 379 |
const error = await Clark.actionError( 'edit', { |
| 380 |
title: page, |
| 381 |
token: await Clark.token( 'csrf' ), |
| 382 |
text: text, |
| 383 |
prependtext: top, |
| 384 |
appendtext: bottom, |
| 385 |
summary: 'Bad md5', |
| 386 |
md5: 'badmd5139fc1202b6d817d9b1459e2d5' |
| 387 |
}, 'POST' ); |
| 388 |
assert.equal( error.code, 'badmd5' ); |
| 389 |
} ); |
| 390 |
it( 'should override the text parameter with the appendtext parameter', async () => { |
| 391 |
const page = utils.title( 'Page_' ); |
| 392 |
const text = 'A text that is to be overriden'; |
| 393 |
const bottom = 'This text should override the text parameter'; |
| 394 |
|
| 395 |
await Clark.edit( page, { |
| 396 |
text: text, |
| 397 |
appendtext: bottom |
| 398 |
} ); |
| 399 |
|
| 400 |
const pageContent = await Clark.getHtml( page ); |
| 401 |
assert.notInclude( pageContent, text ); |
| 402 |
assert.include( pageContent, bottom ); |
| 403 |
} ); |
| 404 |
|
| 405 |
it( 'should override the text parameter with the prependtext parameter', async () => { |
| 406 |
const page = utils.title( 'Page_' ); |
| 407 |
const text = 'A text that is to be overriden'; |
| 408 |
const top = 'This text should override the text parameter'; |
| 409 |
|
| 410 |
await Clark.edit( page, { |
| 411 |
text: text, |
| 412 |
prependtext: top |
| 413 |
} ); |
| 414 |
|
| 415 |
const pageContent = await Clark.getHtml( page ); |
| 416 |
assert.notInclude( pageContent, text ); |
| 417 |
assert.include( pageContent, top ); |
| 418 |
} ); |
| 419 |
|
| 420 |
it( 'should override the text parameter with the appendtext and prependtext parameters', async () => { |
| 421 |
const page = utils.title( 'Page_' ); |
| 422 |
const text = 'A text that is to be overriden'; |
| 423 |
const top = 'This text should override the text parameter on top'; |
| 424 |
const bottom = 'This text should override the text parameter at the end'; |
| 425 |
|
| 426 |
await Clark.edit( page, { |
| 427 |
text: text, |
| 428 |
prependtext: top, |
| 429 |
appendtext: bottom |
| 430 |
} ); |
| 431 |
|
| 432 |
const pageContent = await Clark.getHtml( page ); |
| 433 |
assert.notInclude( pageContent, text ); |
| 434 |
assert.include( pageContent, top + bottom ); |
| 435 |
} ); |
| 436 |
} ); |