Debian Codegraph @

tests/api-testing/REST/content.v1/Update.js

raw symbols SCIP
1 'use strict';
2
3 const { action, assert, REST, utils } = require( 'api-testing' );
4
5 let pathPrefix = 'rest.php/content/v1';
6
7 describe( 'PUT /page/{title}', () => {
8 let client, mindy, mindyToken;
9
10 before( async () => {
11 mindy = await action.mindy();
12 client = new REST( pathPrefix, mindy );
13 mindyToken = await mindy.token();
14 } );
15
16 const checkEditResponse = function ( title, reqBody, body ) {
17 assert.containsAllKeys( body, [ 'title', 'key', 'source', 'latest', 'id', 'license', 'content_model' ] );
18 assert.containsAllKeys( body.latest, [ 'id', 'timestamp' ] );
19 assert.nestedPropertyVal( body, 'source', reqBody.source );
20 assert.nestedPropertyVal( body, 'title', title );
21 assert.nestedPropertyVal( body, 'key', utils.dbkey( title ) );
22 assert.isAbove( body.latest.id, 0 );
23
24 if ( reqBody.content_model ) {
25 assert.nestedPropertyVal( body, 'content_model', reqBody.content_model );
26 }
27 };
28
29 const checkSourceResponse = function ( title, reqBody, body ) {
30 if ( reqBody.content_model ) {
31 assert.nestedPropertyVal( body, 'content_model', reqBody.content_model );
32 }
33
34 assert.nestedPropertyVal( body, 'title', title );
35 assert.nestedPropertyVal( body, 'key', utils.dbkey( title ) );
36 assert.nestedPropertyVal( body, 'source', reqBody.source );
37 };
38
39 describe( 'successful operation', () => {
40 it( 'should create a page if it does not exist', async () => {
41 const title = utils.title( 'Edit Test ' );
42 const normalizedTitle = utils.dbkey( title );
43
44 const reqBody = {
45 token: mindyToken,
46 source: 'Lörem Ipsüm',
47 comment: 'tästing'
48 };
49
50 const { status: editStatus, body: editBody, header: editHeader } =
51 await client.put( `/page/${ title }`, reqBody );
52 assert.equal( editStatus, 201 );
53 assert.match( editHeader[ 'content-type' ], /^application\/json/ );
54 checkEditResponse( title, reqBody, editBody );
55
56 const { status: sourceStatus, body: sourceBody, header: sourceHeader } =
57 await client.get( `/page/${ normalizedTitle }` );
58 assert.equal( sourceStatus, 200 );
59 assert.match( sourceHeader[ 'content-type' ], /^application\/json/ );
60 checkSourceResponse( title, reqBody, sourceBody );
61 } );
62
63 it( 'should create a page with specific content model', async () => {
64 const title = utils.title( 'Edit Test ' );
65 const normalizedTitle = utils.dbkey( title );
66
67 // TODO: test a content model different from the default.
68 const reqBody = {
69 token: mindyToken,
70 source: 'Lörem Ipsüm',
71 comment: 'tästing',
72 content_model: 'wikitext'
73 };
74
75 const { status: editStatus, body: editBody, header: editHeader } =
76 await client.put( `/page/${ title }`, reqBody );
77 assert.equal( editStatus, 201 );
78 assert.match( editHeader[ 'content-type' ], /^application\/json/ );
79 checkEditResponse( title, reqBody, editBody );
80
81 const { status: sourceStatus, body: sourceBody, header: sourceHeader } =
82 await client.get( `/page/${ normalizedTitle }` );
83 assert.equal( sourceStatus, 200 );
84 assert.match( sourceHeader[ 'content-type' ], /^application\/json/ );
85 checkSourceResponse( title, reqBody, sourceBody );
86 } );
87
88 it( 'should update an existing page', async () => {
89 const title = utils.title( 'Edit Test ' );
90 const normalizedTitle = utils.dbkey( title );
91
92 // create
93 await mindy.edit( normalizedTitle, {} );
94 const { body: newPage } = await client.get( `/page/${ normalizedTitle }/bare` );
95
96 const firstRev = newPage.latest;
97
98 const reqBody = {
99 token: mindyToken,
100 source: 'Lörem Ipsüm',
101 comment: 'tästing',
102 latest: firstRev // provide the base revision
103 };
104 const { status: editStatus, body: editBody, header: editHeader } =
105 await client.put( `/page/${ title }`, reqBody );
106
107 assert.equal( editStatus, 200 );
108 assert.match( editHeader[ 'content-type' ], /^application\/json/ );
109 checkEditResponse( title, reqBody, editBody );
110 assert.isAbove( editBody.latest.id, firstRev.id );
111
112 const { status: sourceStatus, body: sourceBody, header: sourceHeader } =
113 await client.get( `/page/${ normalizedTitle }` );
114 assert.equal( sourceStatus, 200 );
115 assert.match( sourceHeader[ 'content-type' ], /^application\/json/ );
116 checkSourceResponse( title, reqBody, sourceBody );
117 } );
118
119 it( 'should handle null-edits (unchanged content) gracefully', async () => {
120 const title = utils.title( 'Edit Test ' );
121
122 // create
123 const firstRev = await mindy.edit( title, {} );
124
125 const reqBody = {
126 token: mindyToken,
127 source: firstRev.param_text,
128 comment: 'nothing at all changed',
129 latest: { id: firstRev.newrevid }
130 };
131 const resp = await client.put( `/page/${ title }`, reqBody );
132 const { status: editStatus, body: editBody, header: editHeader } = resp;
133
134 assert.equal( editStatus, 200 );
135 assert.match( editHeader[ 'content-type' ], /^application\/json/ );
136
137 checkEditResponse( title, reqBody, editBody );
138
139 // No revision was created, new ID is the same as the old ID
140 assert.equal( editBody.latest.id, firstRev.newrevid );
141 } );
142
143 it( 'should automatically solve merge conflicts', async () => {
144 // XXX: this test may fail if the diff3 utility is not found on the web host
145
146 const title = utils.title( 'Edit Test ' );
147
148 // create
149 const firstRev = await mindy.edit( title, { text: 'first line\nlorem ipsum\nsecond line' } );
150 await mindy.edit( title, { text: 'FIRST LINE\nlorem ipsum\nsecond line' } );
151
152 const reqBody = {
153 token: mindyToken,
154 source: 'first line\nlorem ipsum\nSECOND LINE',
155 comment: 'tästing',
156 content_model: 'wikitext',
157 latest: { id: firstRev.newrevid }
158 };
159 const { status: editStatus, body: editBody, header: editHeader } =
160 await client.put( `/page/${ title }`, reqBody );
161
162 assert.equal( editStatus, 200 );
163 assert.match( editHeader[ 'content-type' ], /^application\/json/ );
164 const expectedText = 'FIRST LINE\nlorem ipsum\nSECOND LINE';
165
166 assert.nestedPropertyVal( editBody, 'source', expectedText );
167 } );
168 } );
169
170 describe( 'request validation', () => {
171 const requiredProps = [ 'source', 'comment' ];
172
173 requiredProps.forEach( ( missingPropName ) => {
174 const title = utils.title( 'Edit Test ' );
175 const reqBody = {
176 token: mindyToken,
177 source: 'Lörem Ipsüm',
178 comment: 'tästing',
179 content_model: 'wikitext'
180 };
181
182 it( `should fail when ${ missingPropName } is missing from the request body`, async () => {
183 const incompleteBody = { ...reqBody };
184 delete incompleteBody[ missingPropName ];
185
186 const { status: editStatus, body: editBody, header: editHeader } =
187 await client.put( `/page/${ title }`, incompleteBody );
188
189 assert.equal( editStatus, 400 );
190 assert.match( editHeader[ 'content-type' ], /^application\/json/ );
191 assert.nestedProperty( editBody, 'messageTranslations' );
192 } );
193 } );
194
195 it( 'should fail if no token is given', async () => {
196 const title = utils.title( 'Edit Test ' );
197 const reqBody = {
198 // no token
199 source: 'Lörem Ipsüm',
200 comment: 'tästing'
201 };
202
203 const { status: editStatus, body: editBody, header: editHeader } =
204 await client.put( `/page/${ title }`, reqBody );
205
206 assert.equal( editStatus, 403 );
207 assert.match( editHeader[ 'content-type' ], /^application\/json/ );
208 assert.nestedProperty( editBody, 'messageTranslations' );
209 } );
210
211 it( 'should fail if a bad token is given', async () => {
212 const title = utils.title( 'Edit Test ' );
213 const reqBody = {
214 token: 'BAD',
215 source: 'Lörem Ipsüm',
216 comment: 'tästing'
217 };
218
219 const { status: editStatus, body: editBody, header: editHeader } =
220 await client.put( `/page/${ title }`, reqBody );
221
222 assert.equal( editStatus, 403 );
223 assert.match( editHeader[ 'content-type' ], /^application\/json/ );
224 assert.nestedProperty( editBody, 'messageTranslations' );
225 } );
226
227 it( 'should fail if a bad content model is given', async () => {
228 const title = utils.title( 'Edit Test ' );
229
230 const reqBody = {
231 token: mindyToken,
232 source: 'Lörem Ipsüm',
233 comment: 'tästing',
234 content_model: 'THIS DOES NOT EXIST!'
235 };
236 const { status: editStatus, body: editBody, header: editHeader } =
237 await client.put( `/page/${ title }`, reqBody );
238
239 assert.equal( editStatus, 400 );
240 assert.match( editHeader[ 'content-type' ], /^application\/json/ );
241 assert.nestedProperty( editBody, 'messageTranslations' );
242 } );
243
244 it( 'should fail if a bad title is given', async () => {
245 const title = '_|_'; // not a valid page title
246
247 const reqBody = {
248 source: 'Lörem Ipsüm',
249 comment: 'tästing',
250 content_model: 'wikitext',
251 token: mindyToken
252 };
253 const { status: editStatus, body: editBody, header: editHeader } =
254 await client.put( `/page/${ title }`, reqBody );
255
256 assert.equal( editStatus, 400 );
257 assert.match( editHeader[ 'content-type' ], /^application\/json/ );
258 assert.nestedProperty( editBody, 'messageTranslations' );
259 } );
260
261 it( 'should fail if no title is given', async () => {
262 const reqBody = {
263 source: 'Lörem Ipsüm',
264 comment: 'tästing',
265 content_model: 'wikitext',
266 token: mindyToken
267 };
268 const { status: editStatus, body: editBody, header: editHeader } =
269 await client.put( '/page/', reqBody );
270
271 assert.equal( editStatus, 400 );
272 assert.match( editHeader[ 'content-type' ], /^application\/json/ );
273 assert.nestedProperty( editBody, 'messageTranslations' );
274 } );
275 } );
276
277 describe( 'failures due to system state', () => {
278 it( 'should detect when the target page does not exist but revision ID was given', async () => {
279 const title = utils.title( 'Edit Test ' );
280
281 const reqBody = {
282 token: mindyToken,
283 source: 'Lörem Ipsüm',
284 comment: 'tästing',
285 latest: { id: 1234 }
286 };
287 const { status: editStatus, body: editBody, header: editHeader } =
288 await client.put( `/page/${ title }`, reqBody );
289
290 assert.equal( editStatus, 404 );
291 assert.match( editHeader[ 'content-type' ], /^application\/json/ );
292 assert.nestedProperty( editBody, 'messageTranslations' );
293 } );
294
295 it( 'should detect a conflict if page exist but no revision ID was given', async () => {
296 const title = utils.title( 'Edit Test ' );
297
298 // create
299 await mindy.edit( title, {} );
300
301 const reqBody = {
302 token: mindyToken,
303 source: 'Lörem Ipsüm',
304 comment: 'tästing'
305 // not 'latest' key, so page should be created
306 };
307 const { status: editStatus, body: editBody, header: editHeader } =
308 await client.put( `/page/${ title }`, reqBody );
309
310 assert.equal( editStatus, 409 );
311 assert.match( editHeader[ 'content-type' ], /^application\/json/ );
312 assert.nestedProperty( editBody, 'messageTranslations' );
313 } );
314
315 it( 'should detect a conflict when an old base revision ID is given and conflict resolution fails', async () => {
316 const title = utils.title( 'Edit Test ' );
317
318 // create
319 const firstRev = await mindy.edit( title, { text: 'initial text' } );
320 await mindy.edit( title, { text: 'updated text' } );
321
322 const reqBody = {
323 token: mindyToken,
324 source: 'Lörem Ipsüm',
325 comment: 'tästing',
326 latest: { id: firstRev.newrevid }
327 };
328 const { status: editStatus, body: editBody, header: editHeader } =
329 await client.put( `/page/${ title }`, reqBody );
330
331 assert.equal( editStatus, 409 );
332 assert.match( editHeader[ 'content-type' ], /^application\/json/ );
333 assert.nestedProperty( editBody, 'messageTranslations' );
334 } );
335 } );
336
337 describe( 'permission checks', () => {
338 it( 'should fail when trying to edit a protected page without appropriate permissions', async () => {
339 const title = utils.title( 'Edit Test ' );
340
341 // create a protected page
342 const firstRev = await mindy.edit( title, {} );
343 await mindy.action( 'protect', {
344 title,
345 token: await mindy.token(),
346 protections: 'edit=sysop'
347 }, 'POST' );
348
349 const anon = action.getAnon();
350 const reqBody = {
351 token: await anon.token(),
352 source: 'Lörem Ipsüm',
353 comment: 'tästing',
354 latest: { id: firstRev.newrevid }
355 };
356 const { status: editStatus, body: editBody, header: editHeader } =
357 await client.put( `/page/${ title }`, reqBody );
358
359 assert.equal( editStatus, 403 );
360 assert.match( editHeader[ 'content-type' ], /^application\/json/ );
361 assert.nestedProperty( editBody, 'messageTranslations' );
362 } );
363 } );
364 } );
365
366 // eslint-disable-next-line mocha/no-exports
367 exports.init = function ( pp ) {
368 // Allow testing both legacy and module paths using the same tests
369 pathPrefix = pp;
370 };