Debian Codegraph @

tests/api-testing/REST/content.v1/Revision.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( 'Revision', () => {
8 const page = utils.title( 'Revision' );
9 let client;
10 let mindy;
11 let newrevid, pageid, param_summary;
12
13 before( async () => {
14 client = new REST( pathPrefix );
15 mindy = await action.mindy();
16
17 const resp = await mindy.edit( page, {
18 text: 'Hello World',
19 summary: 'creating page'
20 } );
21 ( { newrevid, pageid, param_summary } = resp );
22 } );
23
24 describe( 'GET /revision/{id}', () => {
25 it( 'should successfully get source and metadata for revision', async () => {
26 const { status, body, text, headers } = await client.get( `/revision/${ newrevid }` );
27
28 assert.strictEqual( status, 200, text );
29 assert.match( headers[ 'content-type' ], /^application\/json/ );
30 assert.match( headers.vary, /\bx-restbase-compat\b/ );
31 assert.strictEqual( body.id, newrevid );
32 assert.strictEqual( body.minor, false );
33 assert.deepEqual( body.page, { id: pageid, title: page, key: utils.dbkey( page ) } );
34 assert.nestedProperty( body, 'timestamp' );
35 assert.nestedPropertyVal( body, 'user.name', mindy.username );
36 assert.strictEqual( body.comment, param_summary );
37 assert.isOk( headers.etag, 'etag' );
38 assert.equal( Date.parse( body.timestamp ), Date.parse( headers[ 'last-modified' ] ) );
39 assert.nestedPropertyVal( body, 'source', 'Hello World' );
40 } );
41
42 it( 'should return 404 for revision that does not exist', async () => {
43 const { status } = await client.get( '/revision/99999999' );
44
45 assert.strictEqual( status, 404 );
46 } );
47 } );
48
49 describe( 'GET /revision/{id}/bare', () => {
50 it( 'should successfully get information about revision', async () => {
51 const { status, body, text, headers } = await client.get( `/revision/${ newrevid }/bare` );
52
53 assert.strictEqual( status, 200, text );
54 assert.match( headers[ 'content-type' ], /^application\/json/ );
55 assert.strictEqual( body.id, newrevid );
56 assert.strictEqual( body.minor, false );
57 assert.deepEqual( body.page, { id: pageid, title: page, key: utils.dbkey( page ) } );
58 assert.nestedProperty( body, 'timestamp' );
59 assert.nestedPropertyVal( body, 'user.name', mindy.username );
60 assert.strictEqual( body.comment, param_summary );
61 assert.isOk( headers.etag, 'etag' );
62 assert.equal( Date.parse( body.timestamp ), Date.parse( headers[ 'last-modified' ] ) );
63 assert.nestedProperty( body, 'html_url' );
64 } );
65
66 it( 'should return 404 for revision that does not exist', async () => {
67 const { status } = await client.get( '/revision/99999999/bare' );
68
69 assert.strictEqual( status, 404 );
70 } );
71 } );
72
73 describe( 'GET /revision/{id}/bare with x-restbase-compat', () => {
74 it( 'Should successfully return restbase-compatible revision meta-data', async () => {
75 const { status, body, text, headers } = await client
76 .get( `/revision/${ newrevid }/bare` )
77 .set( 'x-restbase-compat', 'true' );
78
79 assert.deepEqual( status, 200, text );
80 assert.match( headers[ 'content-type' ], /^application\/json/ );
81 assert.match( headers.vary, /\bx-restbase-compat\b/ );
82 assert.containsAllKeys( body.items[ 0 ], [ 'title', 'page_id', 'rev', 'tid', 'namespace', 'user_id',
83 'user_text', 'timestamp', 'comment', 'tags', 'restrictions', 'page_language', 'redirect' ] );
84
85 assert.deepEqual( body.items[ 0 ].title, utils.dbkey( page ) );
86 assert.deepEqual( body.items[ 0 ].page_id, pageid );
87 assert.deepEqual( body.items[ 0 ].rev, newrevid );
88 } );
89
90 it( 'Should successfully return restbase-compatible errors', async () => {
91 const { status, body, text, headers } = await client
92 .get( '/revision/987654321/bare' )
93 .set( 'x-restbase-compat', 'true' );
94
95 assert.deepEqual( status, 404, text );
96 assert.match( headers[ 'content-type' ], /^application\/json/ );
97 assert.containsAllKeys( body, [ 'type', 'title', 'method', 'detail', 'uri' ] );
98 } );
99 } );
100
101 describe( 'GET /revision/{id}/with_html', () => {
102 it( 'should successfully get metadata and HTML of revision', async () => {
103 const { status, body, text, headers } = await client.get( `/revision/${ newrevid }/with_html` );
104
105 assert.strictEqual( status, 200, text );
106 assert.match( headers[ 'content-type' ], /^application\/json/ );
107 assert.strictEqual( body.id, newrevid );
108 assert.strictEqual( body.minor, false );
109 assert.deepEqual( body.page, { id: pageid, title: page, key: utils.dbkey( page ) } );
110 assert.nestedProperty( body, 'timestamp' );
111 assert.nestedPropertyVal( body, 'user.name', mindy.username );
112 assert.strictEqual( body.comment, param_summary );
113 assert.isOk( headers.etag, 'etag' );
114
115 assert.nestedProperty( body, 'html' );
116 assert.match( body.html, /<html / );
117 assert.match( body.html, /Hello World/ );
118
119 // The last-modified date is the render timestamp, which may be newer than the revision
120 const headerDate = Date.parse( headers[ 'last-modified' ] );
121 const revDate = Date.parse( body.timestamp );
122 assert.strictEqual( revDate.valueOf() <= headerDate.valueOf(), true );
123 } );
124
125 it( 'should return 404 for revision that does not exist', async () => {
126 const { status } = await client.get( '/revision/99999999/with_html' );
127
128 assert.strictEqual( status, 404 );
129 } );
130
131 it( 'should perform variant conversion', async () => {
132 const { headers, text } = await client.get( `/revision/${ newrevid }/with_html`, null, {
133 'accept-language': 'en-x-piglatin'
134 } );
135
136 assert.match( text, /Ellohay/ );
137 assert.match( text, /Orldway/ );
138 assert.match( headers[ 'content-type' ], /^application\/json/ );
139 assert.match( headers.vary, /\bAccept-Language\b/i );
140 assert.match( headers.etag, /en-x-piglatin/i );
141 // Since with_html returns JSON, content language is not set
142 // but if its set, we expect it to be set correctly.
143 const contentLanguageHeader = headers[ 'content-language' ];
144 if ( contentLanguageHeader ) {
145 assert.match( headers[ 'content-language' ], /en-x-piglatin/i );
146 }
147 } );
148 } );
149
150 describe( 'GET /revision/{id}/html', () => {
151 it( 'should successfully get HTML of revision', async () => {
152 const { status, text, headers } = await client.get( `/revision/${ newrevid }/html` );
153
154 assert.strictEqual( status, 200, text );
155 assert.containsAllKeys( headers, [ 'etag', 'cache-control', 'last-modified', 'content-type' ] );
156 assert.match( headers[ 'content-type' ], /^text\/html/ );
157
158 assert.match( text, /<html / );
159 assert.match( text, /Hello World/ );
160 assert.match( headers.etag, /^".*"$/, 'ETag must be present and not marked weak' );
161 } );
162
163 it( 'should return 404 for revision that does not exist', async () => {
164 const { status } = await client.get( '/revision/99999999/html' );
165
166 assert.strictEqual( status, 404 );
167 } );
168
169 it( 'should perform variant conversion', async () => {
170 const { headers, text } = await client.get( `/revision/${ newrevid }/html`, null, {
171 'accept-language': 'en-x-piglatin'
172 } );
173
174 assert.match( text, /Ellohay/ );
175 assert.match( text, /Orldway/ );
176 assert.match( headers[ 'content-type' ], /^text\/html/ );
177 assert.match( headers.vary, /\bAccept-Language\b/i );
178 assert.match( headers[ 'content-language' ], /en-x-piglatin/i );
179 assert.match( headers.etag, /en-x-piglatin/i );
180 } );
181 } );
182
183 describe( 'GET /revision/{id}/html with x-restbase-compat', () => {
184 it( 'Should successfully return restbase-compatible errors', async () => {
185 const { body, headers } = await client
186 .get( '/revision/99999999/html' )
187 .set( 'x-restbase-compat', 'true' );
188
189 assert.match( headers[ 'content-type' ], /^application\/json/ );
190 assert.containsAllKeys( body, [ 'type', 'title', 'method', 'detail', 'uri' ] );
191 } );
192 } );
193 } );
194
195 // eslint-disable-next-line mocha/no-exports
196 exports.init = function ( pp ) {
197 // Allow testing both legacy and module paths using the same tests
198 pathPrefix = pp;
199 };