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
|
'use strict';
const { action, assert, REST, utils, wiki } = require( 'api-testing' );
const superagent = require( 'superagent' );
const xml2js = require( 'xml2js' );
const chai = require( 'chai' );
const expect = chai.expect;
const chaiResponseValidator = require( 'chai-openapi-response-validator' ).default;
describe( 'Search', () => {
const client = new REST( 'rest.php' );
const sharedTitleTerm = 'X' + utils.uniq(); // NOTE: must start with upper-case letter
const pageWithBothTerms = utils.title( `${ sharedTitleTerm }XXX` );
const pageWithOneTerm = utils.title( `${ sharedTitleTerm }YYY` );
const pageWithOwnTitle = utils.title( 'ZZZ' );
const searchTerm = utils.uniq();
const searchTerm2 = utils.uniq();
let alice;
let mindy;
let openApiSpec;
before( async () => {
alice = await action.alice();
mindy = await action.mindy();
await alice.edit( pageWithBothTerms, { text: `${ searchTerm } ${ searchTerm2 }` } );
await alice.edit( pageWithOneTerm, { text: searchTerm2 } );
await alice.edit( pageWithOwnTitle, { text: pageWithOwnTitle } );
await wiki.runAllJobs();
const { status, text } = await client.get( '/specs/v0/module/-' );
assert.deepEqual( status, 200 );
openApiSpec = JSON.parse( text );
chai.use( chaiResponseValidator( openApiSpec ) );
} );
describe( 'GET /search/page?q={term}', () => {
it( 'should return empty array when search term has no title or text matches', async () => {
const nonExistentTerm = utils.uniq();
const res = await client.get( `/v1/search/page?q=${ nonExistentTerm }` );
const { body, headers } = res;
const noResultsResponse = { pages: [] };
assert.match( headers[ 'content-type' ], /^application\/json/ );
assert.deepEqual( noResultsResponse, body );
// eslint-disable-next-line no-unused-expressions
expect( res ).to.satisfyApiSpec;
} );
it( 'should return array of pages when there is only a text match', async () => {
const res = await client.get( `/v1/search/page?q=${ searchTerm }` );
const { body, headers } = res;
assert.match( headers[ 'content-type' ], /^application\/json/ );
assert.lengthOf( body.pages, 1 );
const returnPage = body.pages[ 0 ];
assert.nestedProperty( returnPage, 'title' );
assert.nestedProperty( returnPage, 'id' );
assert.nestedProperty( returnPage, 'key' );
assert.nestedProperty( returnPage, 'excerpt' );
assert.nestedPropertyVal( returnPage, 'thumbnail', null );
assert.nestedPropertyVal( returnPage, 'description', null );
assert.nestedPropertyVal( returnPage, 'matched_title', null );
assert.include( returnPage.excerpt, `<span class="searchmatch">${ searchTerm }</span>` );
// full-text search should not have cache-control
if ( headers[ 'cache-control' ] ) {
assert.notMatch( headers[ 'cache-control' ], /\bpublic\b/ );
assert.notMatch( headers[ 'cache-control' ], /\bmax-age=0*[1-9]\b/ );
assert.notMatch( headers[ 'cache-control' ], /\bs-maxage=0*[1-9]\b/ );
}
// eslint-disable-next-line no-unused-expressions
expect( res ).to.satisfyApiSpec;
} );
it( 'should return array of pages when there is only title match', async () => {
const res = await client.get( `/v1/search/page?q=${ pageWithBothTerms }` );
const { body, headers } = res;
assert.match( headers[ 'content-type' ], /^application\/json/ );
assert.lengthOf( body.pages, 1 );
const returnPage = body.pages[ 0 ];
assert.nestedProperty( returnPage, 'title' );
assert.nestedProperty( returnPage, 'id' );
assert.nestedProperty( returnPage, 'key' );
assert.nestedPropertyVal( returnPage, 'excerpt', null );
assert.nestedPropertyVal( returnPage, 'thumbnail', null );
assert.nestedPropertyVal( returnPage, 'description', null );
assert.nestedPropertyVal( returnPage, 'matched_title', null );
// eslint-disable-next-line no-unused-expressions
expect( res ).to.satisfyApiSpec;
} );
it( 'should return a single page when there is a title and text match on the same page', async () => {
const res = await client.get( `/v1/search/page?q=${ pageWithOwnTitle }` );
const { body, headers } = res;
assert.match( headers[ 'content-type' ], /^application\/json/ );
assert.lengthOf( body.pages, 1 );
const returnPage = body.pages[ 0 ];
assert.nestedProperty( returnPage, 'title' );
assert.nestedProperty( returnPage, 'id' );
assert.nestedProperty( returnPage, 'key' );
assert.nestedPropertyVal( returnPage, 'title', pageWithOwnTitle );
assert.nestedPropertyVal( returnPage, 'thumbnail', null );
assert.nestedPropertyVal( returnPage, 'description', null );
assert.nestedPropertyVal( returnPage, 'matched_title', null );
// eslint-disable-next-line no-unused-expressions
expect( res ).to.satisfyApiSpec;
} );
it( 'should return two pages when both pages match', async () => {
const res = await client.get( `/v1/search/page?q=${ searchTerm2 }` );
const { body, headers } = res;
assert.match( headers[ 'content-type' ], /^application\/json/ );
assert.lengthOf( body.pages, 2 );
const returnedPages = [ body.pages[ 0 ].title, body.pages[ 1 ].title ];
const expectedPages = [ pageWithBothTerms, pageWithOneTerm ];
assert.equal( expectedPages.sort().join( '|' ), returnedPages.sort().join( '|' ) );
// eslint-disable-next-line no-unused-expressions
expect( res ).to.satisfyApiSpec;
} );
it( 'should return only one page when two pages match but limit is 1', async () => {
const res = await client.get( `/v1/search/page?q=${ searchTerm2 }&limit=1` );
const { body, headers } = res;
assert.match( headers[ 'content-type' ], /^application\/json/ );
assert.lengthOf( body.pages, 1 );
// eslint-disable-next-line no-unused-expressions
expect( res ).to.satisfyApiSpec;
} );
it( 'should not return results when page with term has been deleted', async () => {
const pageToDelete = 'Delete Page';
const deleteTerm = `Delete_${ utils.uniq() }`;
const { title } = await alice.edit( pageToDelete, { text: deleteTerm } );
await mindy.action( 'delete', {
title,
summary: 'testing',
token: await mindy.token( 'csrf' )
}, 'POST' );
await wiki.runAllJobs();
const res = await client.get( `/v1/search/page?q=${ deleteTerm }` );
const { body, headers } = res;
assert.match( headers[ 'content-type' ], /^application\/json/ );
assert.lengthOf( body.pages, 0 );
// eslint-disable-next-line no-unused-expressions
expect( res ).to.satisfyApiSpec;
} );
it( 'should ignore duplicate redirect source and target if both pages are a match', async () => {
const redirectSource = utils.title( 'redirect_source_' );
const redirectTarget = utils.title( 'redirect_target_' );
const uniquePageText = utils.uniq();
const { title: redirectTargetTitle } = await alice.edit( redirectTarget, { text: `${ uniquePageText }` } );
await alice.edit( redirectSource,
{ text: `#REDIRECT [[ ${ redirectTarget } ]]. ${ uniquePageText }.` }
);
await wiki.runAllJobs();
const res = await client.get( `/v1/search/page?q=${ uniquePageText }` );
const { body, headers } = res;
assert.match( headers[ 'content-type' ], /^application\/json/ );
assert.lengthOf( body.pages, 1 );
assert.nestedPropertyVal( body.pages[ 0 ], 'title', redirectTargetTitle );
// eslint-disable-next-line no-unused-expressions
expect( res ).to.satisfyApiSpec;
} );
} );
describe( 'GET /search/title?q={term}', () => {
it( 'should return empty array when search term has no title matches', async () => {
const nonExistentTerm = utils.uniq();
const res = await client.get( `/v1/search/title?q=${ nonExistentTerm }` );
const { body, headers } = res;
assert.match( headers[ 'content-type' ], /^application\/json/ );
assert.lengthOf( body.pages, 0 );
// eslint-disable-next-line no-unused-expressions
expect( res ).to.satisfyApiSpec;
} );
it( 'should not return pages when there is only a text match', async () => {
const res = await client.get( `/v1/search/title?q=${ searchTerm }` );
const { body, headers } = res;
assert.match( headers[ 'content-type' ], /^application\/json/ );
assert.lengthOf( body.pages, 0 );
// eslint-disable-next-line no-unused-expressions
expect( res ).to.satisfyApiSpec;
} );
it( 'should return array of pages when there is a title match', async () => {
const res = await client.get( `/v1/search/title?q=${ pageWithBothTerms }` );
const { body, headers } = res;
assert.match( headers[ 'content-type' ], /^application\/json/ );
assert.lengthOf( body.pages, 1 );
const returnPage = body.pages[ 0 ];
assert.nestedProperty( returnPage, 'title' );
assert.nestedProperty( returnPage, 'id' );
assert.nestedProperty( returnPage, 'key' );
assert.nestedProperty( returnPage, 'excerpt' );
assert.nestedPropertyVal( returnPage, 'thumbnail', null );
assert.nestedPropertyVal( returnPage, 'description', null );
assert.nestedPropertyVal( returnPage, 'matched_title', null );
// completion search should encourage caching
assert.nestedProperty( headers, 'cache-control' );
assert.match( headers[ 'cache-control' ], /\bpublic\b/ );
assert.match( headers[ 'cache-control' ], /\bmax-age=[1-9]\d*/ );
// eslint-disable-next-line no-unused-expressions
expect( res ).to.satisfyApiSpec;
} );
it( 'should return two pages when both pages match', async () => {
const res = await client.get( `/v1/search/title?q=${ sharedTitleTerm }` );
const { body, headers } = res;
assert.match( headers[ 'content-type' ], /^application\/json/ );
assert.lengthOf( body.pages, 2 );
const returnedPages = [ body.pages[ 0 ].title, body.pages[ 1 ].title ];
const expectedPages = [ pageWithBothTerms, pageWithOneTerm ];
assert.equal( expectedPages.sort().join( '|' ), returnedPages.sort().join( '|' ) );
// eslint-disable-next-line no-unused-expressions
expect( res ).to.satisfyApiSpec;
} );
it( 'should return only one page when two pages match but limit is 1', async () => {
const res = await client.get( `/v1/search/title?q=${ sharedTitleTerm }&limit=1` );
const { body, headers } = res;
assert.match( headers[ 'content-type' ], /^application\/json/ );
assert.lengthOf( body.pages, 1 );
// eslint-disable-next-line no-unused-expressions
expect( res ).to.satisfyApiSpec;
} );
it( 'should not return deleted page', async () => {
const deleteTerm = utils.uniq();
const pageToDelete = utils.title( `${ deleteTerm }_test_` );
const { title } = await alice.edit( pageToDelete, { text: deleteTerm } );
await mindy.action( 'delete', {
title,
summary: 'testing',
token: await mindy.token( 'csrf' )
}, 'POST' );
await wiki.runAllJobs();
const res = await client.get( `/v1/search/title?q=${ deleteTerm }` );
const { body, headers } = res;
assert.match( headers[ 'content-type' ], /^application\/json/ );
assert.lengthOf( body.pages, 0 );
// eslint-disable-next-line no-unused-expressions
expect( res ).to.satisfyApiSpec;
} );
it( 'should include redirect for page if one exists', async () => {
const redirectSource = utils.title( 'redirect_source_' );
const redirectTarget = utils.title( 'redirect_target_' );
const { title: redirectTargetTitle } = await alice.edit( redirectTarget, { text: 'foo' } );
const { title: redirectSourceTitle } = await alice.edit( redirectSource,
{ text: `#REDIRECT [[ ${ redirectTarget } ]]` }
);
await wiki.runAllJobs();
const res = await client.get( `/v1/search/title?q=${ redirectSourceTitle }` );
const { body, headers } = res;
assert.match( headers[ 'content-type' ], /^application\/json/ );
assert.lengthOf( body.pages, 1 );
assert.nestedPropertyVal( body.pages[ 0 ], 'title', redirectTargetTitle );
assert.nestedPropertyVal( body.pages[ 0 ], 'matched_title', redirectSourceTitle );
// eslint-disable-next-line no-unused-expressions
expect( res ).to.satisfyApiSpec;
} );
} );
describe( 'GET /search', () => {
// enable XML parsing
function parseXML( res, cb ) {
res.text = '';
res.on( 'data', ( chunk ) => {
res.text += chunk;
} );
res.on( 'end', () => xml2js.parseString( res.text, cb ) );
}
superagent.parse[ 'application/xml' ] = parseXML;
superagent.parse[ 'application/opensearchdescription+xml' ] = parseXML;
it( 'should return an OpenSearch Description document', async () => {
const res = await client.get( '/v1/search' );
const { body, headers } = res;
assert.match( headers[ 'content-type' ], /^application\/opensearchdescription\+xml/ );
assert.nestedProperty( body, 'OpenSearchDescription' );
// TODO: assert more about the structure
} );
it( 'should support plain XML output', async () => {
const res = await client.get( '/v1/search' )
.query( { ctype: 'application/xml' } );
const { body, headers } = res;
assert.match( headers[ 'content-type' ], /^application\/xml/ );
assert.nestedProperty( body, 'OpenSearchDescription' );
} );
} );
} );
|