| 1 |
'use strict'; |
| 2 |
|
| 3 |
const { action, assert, utils } = require( 'api-testing' ); |
| 4 |
|
| 5 |
describe( 'Page protection', () => { |
| 6 |
|
| 7 |
let anonymousUser, admin, wikiUser; |
| 8 |
|
| 9 |
beforeEach( async () => { |
| 10 |
admin = await action.mindy(); |
| 11 |
wikiUser = await action.alice(); |
| 12 |
anonymousUser = action.getAnon(); |
| 13 |
} ); |
| 14 |
|
| 15 |
describe( 'permission checks', () => { |
| 16 |
it( 'should allow only admins to protect and unprotect an existing page', async () => { |
| 17 |
const page = utils.title( 'ProtectionTest_' ); |
| 18 |
await admin.edit( page, { text: 'A page to protect' } ); |
| 19 |
|
| 20 |
|
| 21 |
const wikiUserToken = await wikiUser.token(); |
| 22 |
const userTriesToProtect = await wikiUser.actionError( 'protect', { |
| 23 |
title: page, |
| 24 |
token: wikiUserToken, |
| 25 |
protections: 'edit=autoconfirmed' |
| 26 |
}, 'POST' ); |
| 27 |
|
| 28 |
assert.equal( userTriesToProtect.code, 'permissiondenied' ); |
| 29 |
|
| 30 |
|
| 31 |
const adminToken = await admin.token(); |
| 32 |
const adminProtects = await admin.action( 'protect', { |
| 33 |
title: page, |
| 34 |
token: adminToken, |
| 35 |
protections: 'edit=sysop' |
| 36 |
}, 'POST' ); |
| 37 |
assert.equal( adminProtects.protect.protections[ 0 ].edit, 'sysop' ); |
| 38 |
|
| 39 |
|
| 40 |
const anonToken = await anonymousUser.token(); |
| 41 |
const anonTriesToEdit = await anonymousUser.actionError( 'edit', { |
| 42 |
title: page, |
| 43 |
token: anonToken, |
| 44 |
text: 'Eve can not edit' |
| 45 |
}, 'POST' ); |
| 46 |
|
| 47 |
assert.equal( anonTriesToEdit.code, 'protectedpage' ); |
| 48 |
|
| 49 |
|
| 50 |
const userTriesToUnprotect = await wikiUser.actionError( 'protect', { |
| 51 |
title: page, |
| 52 |
token: wikiUserToken, |
| 53 |
protections: '' |
| 54 |
}, 'POST' ); |
| 55 |
|
| 56 |
assert.equal( userTriesToUnprotect.code, 'permissiondenied' ); |
| 57 |
|
| 58 |
|
| 59 |
await admin.action( 'protect', { |
| 60 |
title: page, |
| 61 |
token: adminToken, |
| 62 |
protections: '' |
| 63 |
}, 'POST' ); |
| 64 |
|
| 65 |
|
| 66 |
await anonymousUser.edit( page, { text: 'even can edit now!' } ); |
| 67 |
} ); |
| 68 |
|
| 69 |
} ); |
| 70 |
|
| 71 |
describe( 'cascading protection', () => { |
| 72 |
it( 'should prevent regular users from editing templates used on a protected page', async () => { |
| 73 |
const template = utils.title( 'Template:Protected_' ); |
| 74 |
const page = utils.title( 'Unprotected_' ); |
| 75 |
|
| 76 |
await admin.edit( template, { text: 'Whatever' } ); |
| 77 |
|
| 78 |
await admin.edit( page, { text: `Using {{${ template }}}` } ); |
| 79 |
await admin.action( 'protect', { |
| 80 |
title: page, |
| 81 |
token: await admin.token(), |
| 82 |
protections: 'edit=sysop', |
| 83 |
cascade: 'yes' |
| 84 |
}, 'POST' ); |
| 85 |
|
| 86 |
const userTriesToEditTemplate = await wikiUser.actionError( 'edit', { |
| 87 |
title: page, |
| 88 |
token: await wikiUser.token(), |
| 89 |
text: 'evil content' |
| 90 |
}, 'POST' ); |
| 91 |
|
| 92 |
assert.equal( userTriesToEditTemplate.code, 'protectedpage' ); |
| 93 |
} ); |
| 94 |
} ); |
| 95 |
|
| 96 |
describe( 'levels', () => { |
| 97 |
const protectedPage = utils.title( 'Protected_' ); |
| 98 |
const semiProtectedPage = utils.title( 'SemiProtected_' ); |
| 99 |
const protectedNonexistingPage = utils.title( 'Nonexisting_' ); |
| 100 |
|
| 101 |
before( async () => { |
| 102 |
|
| 103 |
const adminEditToken = await admin.token(); |
| 104 |
|
| 105 |
|
| 106 |
await admin.edit( protectedPage, { text: 'Protected Page' } ); |
| 107 |
|
| 108 |
|
| 109 |
await admin.edit( semiProtectedPage, { text: 'Semi Protected Page' } ); |
| 110 |
|
| 111 |
|
| 112 |
const addSysopProtection = await admin.action( 'protect', { title: protectedPage, token: adminEditToken, protections: 'edit=sysop' }, 'POST' ); |
| 113 |
assert.equal( addSysopProtection.protect.protections[ 0 ].edit, 'sysop' ); |
| 114 |
|
| 115 |
const addSysopProtectionNonexisting = await admin.action( 'protect', { title: protectedNonexistingPage, token: adminEditToken, protections: 'create=sysop' }, 'POST' ); |
| 116 |
assert.equal( addSysopProtectionNonexisting.protect.protections[ 0 ].create, 'sysop' ); |
| 117 |
|
| 118 |
|
| 119 |
const addAutoConfirmedProtection = await admin.action( 'protect', { title: semiProtectedPage, token: adminEditToken, protections: 'edit=autoconfirmed' }, 'POST' ); |
| 120 |
assert.equal( addAutoConfirmedProtection.protect.protections[ 0 ].edit, 'autoconfirmed' ); |
| 121 |
} ); |
| 122 |
|
| 123 |
it( 'should allow admin to edit Protected page', async () => { |
| 124 |
await admin.edit( protectedPage, { text: 'Admin editing protected page' } ); |
| 125 |
} ); |
| 126 |
|
| 127 |
it( 'should allow admin to edit Semi Protected page', async () => { |
| 128 |
await admin.edit( semiProtectedPage, { text: 'Admin editing semi protected page' } ); |
| 129 |
} ); |
| 130 |
|
| 131 |
it( 'should NOT allow autoconfirmed user to edit Protected page', async () => { |
| 132 |
const token = await wikiUser.token(); |
| 133 |
const editPage = await wikiUser.actionError( 'edit', { |
| 134 |
title: protectedPage, |
| 135 |
token, |
| 136 |
text: 'wikiUser editing protected page' |
| 137 |
}, 'POST' ); |
| 138 |
|
| 139 |
assert.equal( editPage.code, 'protectedpage' ); |
| 140 |
} ); |
| 141 |
|
| 142 |
it( 'should NOT allow autoconfirmed user to create a Protected title', async () => { |
| 143 |
const token = await wikiUser.token(); |
| 144 |
const editPage = await wikiUser.actionError( 'edit', { |
| 145 |
title: protectedNonexistingPage, |
| 146 |
token, |
| 147 |
text: 'wikiUser creating protected title' |
| 148 |
}, 'POST' ); |
| 149 |
|
| 150 |
assert.equal( editPage.code, 'protectedpage' ); |
| 151 |
} ); |
| 152 |
|
| 153 |
it( 'should allow autoconfirmed user to edit Semi Protected page', async () => { |
| 154 |
await wikiUser.edit( semiProtectedPage, { text: 'wikiUser editing semi protected page' } ); |
| 155 |
} ); |
| 156 |
|
| 157 |
it( 'should NOT allow anonymous user to edit Protected page', async () => { |
| 158 |
const token = await anonymousUser.token(); |
| 159 |
const editPage = await anonymousUser.actionError( 'edit', { |
| 160 |
title: protectedPage, |
| 161 |
token, |
| 162 |
text: 'anonymous user editing protected page' |
| 163 |
}, 'POST' ); |
| 164 |
|
| 165 |
assert.equal( editPage.code, 'protectedpage' ); |
| 166 |
} ); |
| 167 |
|
| 168 |
it( 'should NOT allow anonymous user to edit Semi Protected page', async () => { |
| 169 |
const token = await anonymousUser.token(); |
| 170 |
const editPage = await anonymousUser.actionError( 'edit', { |
| 171 |
title: semiProtectedPage, |
| 172 |
token, |
| 173 |
text: 'anonymous user editing semi protected page' |
| 174 |
}, 'POST' ); |
| 175 |
|
| 176 |
assert.equal( editPage.code, 'protectedpage' ); |
| 177 |
} ); |
| 178 |
|
| 179 |
it( 'should allow a restriction to be specified that allows users to edit it and only sysops to move a page', async () => { |
| 180 |
const adminEditToken = await admin.token(); |
| 181 |
const token = await anonymousUser.token(); |
| 182 |
const title = utils.title( 'TestExpiry_' ); |
| 183 |
await admin.edit( title, { |
| 184 |
text: 'Test for Expiry' |
| 185 |
} ); |
| 186 |
await admin.action( 'protect', { |
| 187 |
title, |
| 188 |
token: adminEditToken, |
| 189 |
expiry: 'infinite', |
| 190 |
protections: 'edit=autoconfirmed|move=sysop' |
| 191 |
}, 'POST' ); |
| 192 |
|
| 193 |
const editPage = await anonymousUser.actionError( 'edit', { |
| 194 |
title, |
| 195 |
token, |
| 196 |
text: 'wikiUser editing protected page' |
| 197 |
}, 'POST' ); |
| 198 |
|
| 199 |
const movePage = await anonymousUser.actionError( 'move', { |
| 200 |
from: title, |
| 201 |
to: 'Page with new Title', |
| 202 |
token, |
| 203 |
reason: 'to test that the page cannot be moved by a wikiUser' |
| 204 |
}, 'POST' ); |
| 205 |
|
| 206 |
assert.equal( editPage.code, 'protectedpage' ); |
| 207 |
assert.equal( movePage.code, 'cantmove-anon' ); |
| 208 |
} ); |
| 209 |
} ); |
| 210 |
|
| 211 |
describe( '"expiry" parameter', () => { |
| 212 |
|
| 213 |
const testForExpiry = async ( expiry ) => { |
| 214 |
const adminEditToken = await admin.token(); |
| 215 |
const token = await wikiUser.token(); |
| 216 |
const title = utils.title( 'TestExpiry_' ); |
| 217 |
await admin.edit( title, { |
| 218 |
text: 'Test for Expiry' |
| 219 |
} ); |
| 220 |
await admin.action( 'protect', { |
| 221 |
title, |
| 222 |
token: adminEditToken, |
| 223 |
expiry: expiry, |
| 224 |
protections: 'edit=sysop' |
| 225 |
}, 'POST' ); |
| 226 |
|
| 227 |
const editPage = await wikiUser.actionError( 'edit', { |
| 228 |
title, |
| 229 |
token, |
| 230 |
text: 'wikiUser editing protected page' |
| 231 |
}, 'POST' ); |
| 232 |
return editPage.code; |
| 233 |
}; |
| 234 |
|
| 235 |
it( 'should apply protections to a page for an indefinite duration', async () => { |
| 236 |
const errorCode = await testForExpiry( 'indefinite' ); |
| 237 |
assert.equal( errorCode, 'protectedpage' ); |
| 238 |
} ); |
| 239 |
|
| 240 |
it( 'should apply protections to a page for an infinite duration', async () => { |
| 241 |
const errorCode = await testForExpiry( 'infinite' ); |
| 242 |
assert.equal( errorCode, 'protectedpage' ); |
| 243 |
} ); |
| 244 |
|
| 245 |
it( 'should apply protections to a page for an infinity duration', async () => { |
| 246 |
const errorCode = await testForExpiry( 'infinity' ); |
| 247 |
assert.equal( errorCode, 'protectedpage' ); |
| 248 |
} ); |
| 249 |
|
| 250 |
it( 'should apply protections to a page for some centuries to come', async () => { |
| 251 |
const errorCode = await testForExpiry( '9999-01-01T00:00:00Z' ); |
| 252 |
assert.equal( errorCode, 'protectedpage' ); |
| 253 |
} ); |
| 254 |
} ); |
| 255 |
|
| 256 |
describe( '"watchlist" parameter', () => { |
| 257 |
|
| 258 |
let eve; |
| 259 |
|
| 260 |
const testWatchlist = utils.title( 'TestWatchlist_' ); |
| 261 |
|
| 262 |
before( async () => { |
| 263 |
eve = action.getAnon(); |
| 264 |
await eve.account( 'Eve' ); |
| 265 |
const root = await action.root(); |
| 266 |
await root.addGroups( eve.username, [ 'sysop' ] ); |
| 267 |
|
| 268 |
|
| 269 |
await eve.edit( testWatchlist, { |
| 270 |
text: 'Protecting this page' |
| 271 |
} ); |
| 272 |
} ); |
| 273 |
|
| 274 |
const protectWithWatchParam = async ( watchedBefore, watchlistParam, title ) => { |
| 275 |
const watchArgs = { |
| 276 |
title: title, |
| 277 |
token: await eve.token( 'watch' ) |
| 278 |
}; |
| 279 |
if ( watchedBefore !== 'watched' ) { |
| 280 |
watchArgs.unwatch = true; |
| 281 |
} |
| 282 |
await eve.action( 'watch', watchArgs, 'POST' ); |
| 283 |
const token = await eve.token(); |
| 284 |
await eve.action( 'protect', { |
| 285 |
title, |
| 286 |
token, |
| 287 |
protections: 'edit=autoconfirmed', |
| 288 |
watchlist: watchlistParam |
| 289 |
}, 'POST' ); |
| 290 |
const res = await eve.action( 'query', { |
| 291 |
list: 'watchlistraw', |
| 292 |
wrfromtitle: title, |
| 293 |
wrtotitle: title |
| 294 |
} ); |
| 295 |
return res.watchlistraw; |
| 296 |
}; |
| 297 |
|
| 298 |
it( 'should have added the page to the user\'s watchlist by default', async () => { |
| 299 |
const list = await eve.action( 'query', { |
| 300 |
list: 'watchlistraw', |
| 301 |
wrfromtitle: testWatchlist, |
| 302 |
wrtotitle: testWatchlist |
| 303 |
} ); |
| 304 |
assert.sameTitle( list.watchlistraw[ 0 ].title, testWatchlist ); |
| 305 |
} ); |
| 306 |
|
| 307 |
it( 'should add the page to the watchlist if set to "preferences"', async () => { |
| 308 |
const list = await protectWithWatchParam( 'watched', 'preferences', testWatchlist ); |
| 309 |
assert.sameTitle( list[ 0 ].title, testWatchlist ); |
| 310 |
} ); |
| 311 |
|
| 312 |
it( 'should not change the pages on the watchlist if set to "nochange"', async () => { |
| 313 |
const list = await protectWithWatchParam( 'watched', 'nochange', testWatchlist ); |
| 314 |
assert.sameTitle( list[ 0 ].title, testWatchlist ); |
| 315 |
} ); |
| 316 |
|
| 317 |
it( 'should remove a page from the users watchlist if set to "unwatch"', async () => { |
| 318 |
const list = await protectWithWatchParam( 'watched', 'unwatch', testWatchlist ); |
| 319 |
assert.isEmpty( list ); |
| 320 |
} ); |
| 321 |
|
| 322 |
it( 'should add a page to the users watchlist if set to "watch"', async () => { |
| 323 |
const list = await protectWithWatchParam( 'unwatched', 'watch', testWatchlist ); |
| 324 |
assert.sameTitle( list[ 0 ].title, testWatchlist ); |
| 325 |
} ); |
| 326 |
} ); |
| 327 |
} ); |