aboutsummaryrefslogtreecommitdiffstats
path: root/tests/api-testing/REST/Update.js
blob: 97489517ca1d9a3ab5b92dcff71b6d7b51dba9ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
'use strict';

const { action, assert, REST, utils } = require( 'api-testing' );

describe( 'PUT /page/{title}', () => {
	const client = new REST();
	let mindy, anon, anonToken;

	before( async () => {
		mindy = await action.mindy();

		// NOTE: this only works because the same token is shared by all anons.
		// TODO: add support for login and tokens to RESTClient.
		anon = action.getAnon();
		anonToken = await anon.token();
	} );

	const checkEditResponse = function ( title, reqBody, body ) {
		assert.containsAllKeys( body, [ 'title', 'key', 'source', 'latest', 'id', 'license', 'content_model' ] );
		assert.containsAllKeys( body.latest, [ 'id', 'timestamp' ] );
		assert.nestedPropertyVal( body, 'source', reqBody.source );
		assert.nestedPropertyVal( body, 'title', title );
		assert.nestedPropertyVal( body, 'key', utils.dbkey( title ) );
		assert.isAbove( body.latest.id, 0 );

		if ( reqBody.content_model ) {
			assert.nestedPropertyVal( body, 'content_model', reqBody.content_model );
		}
	};

	const checkSourceResponse = function ( title, reqBody, body ) {
		if ( reqBody.content_model ) {
			assert.nestedPropertyVal( body, 'content_model', reqBody.content_model );
		}

		assert.nestedPropertyVal( body, 'title', title );
		assert.nestedPropertyVal( body, 'key', utils.dbkey( title ) );
		assert.nestedPropertyVal( body, 'source', reqBody.source );
	};

	describe( 'successful operation', () => {
		it( 'should create a page if it does not exist', async () => {
			const title = utils.title( 'Edit Test ' );

			const reqBody = {
				token: anonToken,
				source: 'Lörem Ipsüm',
				comment: 'tästing'
			};

			const { status: editStatus, body: editBody } = await client.put( `/page/${title}`, reqBody );
			assert.equal( editStatus, 201 );
			checkEditResponse( title, reqBody, editBody );

			const { status: sourceStatus, body: sourceBody } = await client.get( `/page/${title}` );
			assert.equal( sourceStatus, 200 );
			checkSourceResponse( title, reqBody, sourceBody );
		} );

		it( 'should create a page with specific content model', async () => {
			const title = utils.title( 'Edit Test ' );

			// TODO: test a content model different from the default.
			//       But that requires the chnagecontentmodel permission, which anons don't have.
			const reqBody = {
				token: anonToken,
				source: 'Lörem Ipsüm',
				comment: 'tästing',
				content_model: 'wikitext'
			};

			const { status: editStatus, body: editBody } = await client.put( `/page/${title}`, reqBody );
			assert.equal( editStatus, 201 );
			checkEditResponse( title, reqBody, editBody );

			const { status: sourceStatus, body: sourceBody } = await client.get( `/page/${title}` );
			assert.equal( sourceStatus, 200 );
			checkSourceResponse( title, reqBody, sourceBody );
		} );

		it( 'should update an existing page', async () => {
			const title = utils.title( 'Edit Test ' );

			// create
			const firstRev = await mindy.edit( title, {} );

			const reqBody = {
				token: anonToken,
				source: 'Lörem Ipsüm',
				comment: 'tästing',
				latest: { id: firstRev.newrevid }
			};
			const { status: editStatus, body: editBody } = await client.put( `/page/${title}`, reqBody );

			assert.equal( editStatus, 200 );
			checkEditResponse( title, reqBody, editBody );
			assert.isAbove( editBody.latest.id, firstRev.newrevid );

			const { status: sourceStatus, body: sourceBody } = await client.get( `/page/${title}` );
			assert.equal( sourceStatus, 200 );
			checkSourceResponse( title, reqBody, sourceBody );
		} );

		it( 'should handle null-edits (unchanged content) gracefully', async () => {
			const title = utils.title( 'Edit Test ' );

			// create
			const firstRev = await mindy.edit( title, {} );

			const reqBody = {
				token: anonToken,
				source: firstRev.param_text,
				comment: 'nothing at all changed',
				latest: { id: firstRev.newrevid }
			};
			const resp = await client.put( `/page/${title}`, reqBody );
			const { status: editStatus, body: editBody } = resp;

			assert.equal( editStatus, 200 );
			checkEditResponse( title, reqBody, editBody );

			// No revision was created, new ID is the same as the old ID
			assert.equal( editBody.latest.id, firstRev.newrevid );
		} );

		it( 'should automatically solve merge conflicts', async () => {
			// XXX: this test may fail if the diff3 utility is not found on the web host

			const title = utils.title( 'Edit Test ' );

			// create
			const firstRev = await mindy.edit( title, { text: 'first line\nlorem ipsum\nsecond line' } );
			await mindy.edit( title, { text: 'FIRST LINE\nlorem ipsum\nsecond line' } );

			const reqBody = {
				token: anonToken,
				source: 'first line\nlorem ipsum\nSECOND LINE',
				comment: 'tästing',
				content_model: 'wikitext',
				latest: { id: firstRev.newrevid }
			};
			const { status: editStatus, body: editBody } = await client.put( `/page/${title}`, reqBody );

			assert.equal( editStatus, 200 );
			const expectedText = 'FIRST LINE\nlorem ipsum\nSECOND LINE';

			assert.nestedPropertyVal( editBody, 'source', expectedText );
		} );
	} );

	describe( 'request validation', () => {
		const requiredProps = [ 'source', 'comment' ];

		requiredProps.forEach( ( missingPropName ) => {
			const title = utils.title( 'Edit Test ' );
			const reqBody = {
				token: anonToken,
				source: 'Lörem Ipsüm',
				comment: 'tästing',
				content_model: 'wikitext'
			};

			it( `should fail when ${missingPropName} is missing from the request body`, async () => {
				const incompleteBody = { ...reqBody };
				delete incompleteBody[ missingPropName ];

				const { status: editStatus, body: editBody } =
					await client.put( `/page/${title}`, incompleteBody );

				assert.equal( editStatus, 400 );
				assert.nestedProperty( editBody, 'messageTranslations' );
			} );
		} );

		it( 'should fail if no token is given', async () => {
			const title = utils.title( 'Edit Test ' );
			const reqBody = {
				// no token
				source: 'Lörem Ipsüm',
				comment: 'tästing'
			};

			const { status: editStatus, body: editBody } = await client.put( `/page/${title}`, reqBody );

			assert.equal( editStatus, 400 );
			assert.nestedProperty( editBody, 'messageTranslations' );
		} );

		it( 'should fail if a bad token is given', async () => {
			const title = utils.title( 'Edit Test ' );
			const reqBody = {
				token: 'BAD',
				source: 'Lörem Ipsüm',
				comment: 'tästing'
			};

			const { status: editStatus, body: editBody } = await client.put( `/page/${title}`, reqBody );

			assert.equal( editStatus, 403 );
			assert.nestedProperty( editBody, 'messageTranslations' );
		} );

		it( 'should fail if a bad content model is given', async () => {
			const title = utils.title( 'Edit Test ' );

			const reqBody = {
				token: anonToken,
				source: 'Lörem Ipsüm',
				comment: 'tästing',
				content_model: 'THIS DOES NOT EXIST!'
			};
			const { status: editStatus, body: editBody } = await client.put( `/page/${title}`, reqBody );

			assert.equal( editStatus, 400 );
			assert.nestedProperty( editBody, 'messageTranslations' );
		} );

		it( 'should fail if a bad title is given', async () => {
			const title = '_|_'; // not a valid page title

			const reqBody = {
				source: 'Lörem Ipsüm',
				comment: 'tästing',
				content_model: 'wikitext'
			};
			const { status: editStatus, body: editBody } = await client.put( `/page/${title}`, reqBody );

			assert.equal( editStatus, 400 );
			assert.nestedProperty( editBody, 'messageTranslations' );
		} );

		it( 'should fail if no title is given', async () => {
			const reqBody = {
				source: 'Lörem Ipsüm',
				comment: 'tästing',
				content_model: 'wikitext'
			};
			const { status: editStatus, body: editBody } = await client.put( '/page/', reqBody );

			assert.equal( editStatus, 400 );
			assert.nestedProperty( editBody, 'messageTranslations' );
		} );
	} );

	describe( 'failures due to system state', () => {
		it( 'should detect when the target page does not exist but revision ID was given', async () => {
			const title = utils.title( 'Edit Test ' );

			const reqBody = {
				token: anonToken,
				source: 'Lörem Ipsüm',
				comment: 'tästing',
				latest: { id: 1234 }
			};
			const { status: editStatus, body: editBody } = await client.put( `/page/${title}`, reqBody );

			assert.equal( editStatus, 404 );
			assert.nestedProperty( editBody, 'messageTranslations' );
		} );

		it( 'should detect a conflict if page exist but no revision ID was given', async () => {
			const title = utils.title( 'Edit Test ' );

			// create
			await mindy.edit( title, {} );

			const reqBody = {
				token: anonToken,
				source: 'Lörem Ipsüm',
				comment: 'tästing'
				// not 'latest' key, so page should be created
			};
			const { status: editStatus, body: editBody } = await client.put( `/page/${title}`, reqBody );

			assert.equal( editStatus, 409 );
			assert.nestedProperty( editBody, 'messageTranslations' );
		} );

		it( 'should detect a conflict when an old base revision ID is given and conflict resolution fails', async () => {
			const title = utils.title( 'Edit Test ' );

			// create
			const firstRev = await mindy.edit( title, { text: 'initial text' } );
			await mindy.edit( title, { text: 'updated text' } );

			const reqBody = {
				token: anonToken,
				source: 'Lörem Ipsüm',
				comment: 'tästing',
				latest: { id: firstRev.newrevid }
			};
			const { status: editStatus, body: editBody } = await client.put( `/page/${title}`, reqBody );

			assert.equal( editStatus, 409 );
			assert.nestedProperty( editBody, 'messageTranslations' );
		} );
	} );

	describe( 'permission checks', () => {
		it( 'should fail when trying to edit a protected page without appropriate permissions', async () => {
			const title = utils.title( 'Edit Test ' );

			// create a protected page
			const firstRev = await mindy.edit( title, {} );
			await mindy.action( 'protect', {
				title,
				token: await mindy.token(),
				protections: 'edit=sysop'
			}, 'POST' );

			const reqBody = {
				token: anonToken,
				source: 'Lörem Ipsüm',
				comment: 'tästing',
				latest: { id: firstRev.newrevid }
			};
			const { status: editStatus, body: editBody } = await client.put( `/page/${title}`, reqBody );

			assert.equal( editStatus, 403 );
			assert.nestedProperty( editBody, 'messageTranslations' );
		} );

	} );
} );