| 1 |
'use strict'; |
| 2 |
|
| 3 |
const { action, assert, REST, utils } = require( 'api-testing' ); |
| 4 |
const supertest = require( 'supertest' ); |
| 5 |
|
| 6 |
let pathPrefix = 'rest.php/content/v1'; |
| 7 |
|
| 8 |
describe( 'POST /page', () => { |
| 9 |
let client, mindy, anon, anonToken; |
| 10 |
|
| 11 |
beforeEach( async () => { |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
mindy = await action.mindy(); |
| 16 |
client = new REST( pathPrefix ); |
| 17 |
anon = await action.getAnon(); |
| 18 |
anonToken = await anon.token(); |
| 19 |
} ); |
| 20 |
|
| 21 |
const checkEditResponse = function ( title, reqBody, body ) { |
| 22 |
assert.containsAllKeys( body, [ 'title', 'key', 'source', 'latest', 'id', |
| 23 |
'license', 'content_model' ] ); |
| 24 |
assert.containsAllKeys( body.latest, [ 'id', 'timestamp' ] ); |
| 25 |
assert.nestedPropertyVal( body, 'source', reqBody.source ); |
| 26 |
assert.nestedPropertyVal( body, 'title', title ); |
| 27 |
assert.nestedPropertyVal( body, 'key', utils.dbkey( title ) ); |
| 28 |
assert.isAbove( body.latest.id, 0 ); |
| 29 |
|
| 30 |
if ( reqBody.content_model ) { |
| 31 |
assert.nestedPropertyVal( body, 'content_model', reqBody.content_model ); |
| 32 |
} |
| 33 |
}; |
| 34 |
|
| 35 |
const checkSourceResponse = function ( title, reqBody, body ) { |
| 36 |
if ( reqBody.content_model ) { |
| 37 |
assert.nestedPropertyVal( body, 'content_model', reqBody.content_model ); |
| 38 |
} |
| 39 |
|
| 40 |
assert.nestedPropertyVal( body, 'title', title ); |
| 41 |
assert.nestedPropertyVal( body, 'key', utils.dbkey( title ) ); |
| 42 |
assert.nestedPropertyVal( body, 'source', reqBody.source ); |
| 43 |
}; |
| 44 |
|
| 45 |
describe( 'successful operation', () => { |
| 46 |
it( 'should create a page if it does not exist', async () => { |
| 47 |
const titleSuffix = utils.title(); |
| 48 |
const title = 'A B+C:D@E-' + titleSuffix; |
| 49 |
const normalizedTitle = utils.dbkey( title ); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
const encodedTitle = 'A_B%2BC:D@E-' + titleSuffix; |
| 56 |
|
| 57 |
const reqBody = { |
| 58 |
token: anonToken, |
| 59 |
source: 'Lörem Ipsüm', |
| 60 |
comment: 'tästing', |
| 61 |
title |
| 62 |
}; |
| 63 |
|
| 64 |
const { status: editStatus, body: editBody, header } = |
| 65 |
await client.post( '/page', reqBody ); |
| 66 |
assert.equal( editStatus, 201 ); |
| 67 |
assert.match( header[ 'content-type' ], /^application\/json/ ); |
| 68 |
|
| 69 |
assert.nestedProperty( header, 'location' ); |
| 70 |
const location = header.location; |
| 71 |
assert.match( location, new RegExp( `^https?://.*/v1/page/${ encodedTitle }$` ) ); |
| 72 |
checkEditResponse( title, reqBody, editBody ); |
| 73 |
|
| 74 |
|
| 75 |
const { status: redirStatus, body: redirBody, header: redirHeader } = |
| 76 |
await supertest.agent( location ).get( '' ); |
| 77 |
assert.equal( redirStatus, 200 ); |
| 78 |
assert.match( redirHeader[ 'content-type' ], /^application\/json/ ); |
| 79 |
checkSourceResponse( title, reqBody, redirBody ); |
| 80 |
|
| 81 |
|
| 82 |
const { status: sourceStatus, body: sourceBody, header: sourceHeader } = |
| 83 |
await client.get( `/page/${ normalizedTitle }` ); |
| 84 |
assert.equal( sourceStatus, 200 ); |
| 85 |
assert.match( sourceHeader[ 'content-type' ], /^application\/json/ ); |
| 86 |
checkSourceResponse( title, reqBody, sourceBody ); |
| 87 |
} ); |
| 88 |
|
| 89 |
it( 'should create a page with specified model', async () => { |
| 90 |
const title = utils.title( 'Edit Test ' ); |
| 91 |
const normalizedTitle = utils.dbkey( title ); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
const reqBody = { |
| 96 |
token: anonToken, |
| 97 |
source: 'Lörem Ipsüm', |
| 98 |
comment: 'tästing', |
| 99 |
content_model: 'wikitext', |
| 100 |
title |
| 101 |
}; |
| 102 |
|
| 103 |
const { status: editStatus, body: editBody, header: editHeader } = |
| 104 |
await client.post( '/page', reqBody ); |
| 105 |
assert.equal( editStatus, 201 ); |
| 106 |
assert.match( editHeader[ 'content-type' ], /^application\/json/ ); |
| 107 |
checkEditResponse( title, reqBody, editBody ); |
| 108 |
|
| 109 |
const { status: sourceStatus, body: sourceBody, header: sourceHeader } = |
| 110 |
await client.get( `/page/${ normalizedTitle }` ); |
| 111 |
assert.equal( sourceStatus, 200 ); |
| 112 |
assert.match( sourceHeader[ 'content-type' ], /^application\/json/ ); |
| 113 |
checkSourceResponse( title, reqBody, sourceBody ); |
| 114 |
} ); |
| 115 |
} ); |
| 116 |
|
| 117 |
describe( 'request validation', () => { |
| 118 |
const requiredProps = [ 'source', 'comment', 'title' ]; |
| 119 |
|
| 120 |
requiredProps.forEach( ( missingPropName ) => { |
| 121 |
const title = utils.title( 'Edit Test ' ); |
| 122 |
const reqBody = { |
| 123 |
token: anonToken, |
| 124 |
source: 'Lörem Ipsüm', |
| 125 |
comment: 'tästing', |
| 126 |
title |
| 127 |
}; |
| 128 |
|
| 129 |
it( `should fail when ${ missingPropName } is missing from the request body`, async () => { |
| 130 |
const incompleteBody = { ...reqBody }; |
| 131 |
delete incompleteBody[ missingPropName ]; |
| 132 |
|
| 133 |
const { status: editStatus, body: editBody, header: editHeader } = |
| 134 |
await client.post( '/page', incompleteBody ); |
| 135 |
|
| 136 |
assert.equal( editStatus, 400 ); |
| 137 |
assert.match( editHeader[ 'content-type' ], /^application\/json/ ); |
| 138 |
assert.nestedProperty( editBody, 'messageTranslations' ); |
| 139 |
} ); |
| 140 |
} ); |
| 141 |
|
| 142 |
it( 'should fail if no token is given', async () => { |
| 143 |
const title = utils.title( 'Edit Test ' ); |
| 144 |
const reqBody = { |
| 145 |
|
| 146 |
source: 'Lörem Ipsüm', |
| 147 |
comment: 'tästing', |
| 148 |
title |
| 149 |
}; |
| 150 |
|
| 151 |
const { status: editStatus, body: editBody, header: editHeader } = |
| 152 |
await client.post( '/page', reqBody ); |
| 153 |
|
| 154 |
assert.equal( editStatus, 403 ); |
| 155 |
assert.match( editHeader[ 'content-type' ], /^application\/json/ ); |
| 156 |
assert.nestedProperty( editBody, 'messageTranslations' ); |
| 157 |
} ); |
| 158 |
|
| 159 |
it( 'should fail if a bad token is given', async () => { |
| 160 |
const title = utils.title( 'Edit Test ' ); |
| 161 |
const reqBody = { |
| 162 |
token: 'BAD', |
| 163 |
source: 'Lörem Ipsüm', |
| 164 |
comment: 'tästing', |
| 165 |
title |
| 166 |
}; |
| 167 |
|
| 168 |
const { status: editStatus, body: editBody, header: editHeader } = |
| 169 |
await client.post( '/page', reqBody ); |
| 170 |
|
| 171 |
assert.equal( editStatus, 403 ); |
| 172 |
assert.match( editHeader[ 'content-type' ], /^application\/json/ ); |
| 173 |
assert.nestedProperty( editBody, 'messageTranslations' ); |
| 174 |
} ); |
| 175 |
|
| 176 |
it( 'should fail if a bad content model is given', async () => { |
| 177 |
const title = utils.title( 'Edit Test ' ); |
| 178 |
|
| 179 |
const reqBody = { |
| 180 |
token: anonToken, |
| 181 |
source: 'Lörem Ipsüm', |
| 182 |
comment: 'tästing', |
| 183 |
content_model: 'THIS DOES NOT EXIST!', |
| 184 |
title |
| 185 |
}; |
| 186 |
const { status: editStatus, body: editBody, header: editHeader } = |
| 187 |
await client.post( '/page', reqBody ); |
| 188 |
|
| 189 |
assert.equal( editStatus, 400 ); |
| 190 |
assert.match( editHeader[ 'content-type' ], /^application\/json/ ); |
| 191 |
assert.nestedProperty( editBody, 'messageTranslations' ); |
| 192 |
} ); |
| 193 |
|
| 194 |
it( 'should fail if a bad title is given', async () => { |
| 195 |
const title = '_|_'; |
| 196 |
|
| 197 |
const reqBody = { |
| 198 |
token: anonToken, |
| 199 |
source: 'Lörem Ipsüm', |
| 200 |
comment: 'tästing', |
| 201 |
title |
| 202 |
}; |
| 203 |
const { status: editStatus, body: editBody, header: editHeader } = |
| 204 |
await client.post( '/page', reqBody ); |
| 205 |
|
| 206 |
assert.equal( editStatus, 400 ); |
| 207 |
assert.match( editHeader[ 'content-type' ], /^application\/json/ ); |
| 208 |
assert.nestedProperty( editBody, 'messageTranslations' ); |
| 209 |
} ); |
| 210 |
} ); |
| 211 |
|
| 212 |
describe( 'failures due to system state', () => { |
| 213 |
it( 'should detect a conflict if page exist', async () => { |
| 214 |
const title = utils.title( 'Edit Test ' ); |
| 215 |
|
| 216 |
|
| 217 |
await mindy.edit( title, {} ); |
| 218 |
|
| 219 |
const reqBody = { |
| 220 |
token: anonToken, |
| 221 |
source: 'Lörem Ipsüm', |
| 222 |
comment: 'tästing', |
| 223 |
title |
| 224 |
}; |
| 225 |
const { status: editStatus, body: editBody, header: editHeader } = |
| 226 |
await client.post( '/page', reqBody ); |
| 227 |
|
| 228 |
assert.equal( editStatus, 409 ); |
| 229 |
assert.match( editHeader[ 'content-type' ], /^application\/json/ ); |
| 230 |
assert.nestedProperty( editBody, 'messageTranslations' ); |
| 231 |
} ); |
| 232 |
} ); |
| 233 |
|
| 234 |
describe( 'permission checks', () => { |
| 235 |
it( 'should fail when trying to create a protected title without appropriate permissions', async () => { |
| 236 |
const title = utils.title( 'Edit Test ' ); |
| 237 |
|
| 238 |
|
| 239 |
await mindy.action( 'protect', { |
| 240 |
title, |
| 241 |
token: await mindy.token(), |
| 242 |
protections: 'create=sysop' |
| 243 |
}, 'POST' ); |
| 244 |
|
| 245 |
const reqBody = { |
| 246 |
token: anonToken, |
| 247 |
source: 'Lörem Ipsüm', |
| 248 |
comment: 'tästing', |
| 249 |
title |
| 250 |
}; |
| 251 |
const { status: editStatus, body: editBody, header: editHeader } = |
| 252 |
await client.post( '/page', reqBody ); |
| 253 |
|
| 254 |
assert.equal( editStatus, 403 ); |
| 255 |
assert.match( editHeader[ 'content-type' ], /^application\/json/ ); |
| 256 |
assert.nestedProperty( editBody, 'messageTranslations' ); |
| 257 |
} ); |
| 258 |
|
| 259 |
} ); |
| 260 |
} ); |
| 261 |
|
| 262 |
|
| 263 |
exports.init = function ( pp ) { |
| 264 |
|
| 265 |
pathPrefix = pp; |
| 266 |
}; |