| 1 |
'use strict'; |
| 2 |
|
| 3 |
const { action, assert, REST, utils, wiki } = require( 'api-testing' ); |
| 4 |
const superagent = require( 'superagent' ); |
| 5 |
const xml2js = require( 'xml2js' ); |
| 6 |
|
| 7 |
describe( 'Search', () => { |
| 8 |
const client = new REST( 'rest.php/v1' ); |
| 9 |
const sharedTitleTerm = 'X' + utils.uniq(); |
| 10 |
const pageWithBothTerms = utils.title( `${ sharedTitleTerm }XXX` ); |
| 11 |
const pageWithOneTerm = utils.title( `${ sharedTitleTerm }YYY` ); |
| 12 |
const pageWithOwnTitle = utils.title( 'ZZZ' ); |
| 13 |
const searchTerm = utils.uniq(); |
| 14 |
const searchTerm2 = utils.uniq(); |
| 15 |
let alice; |
| 16 |
let mindy; |
| 17 |
|
| 18 |
before( async () => { |
| 19 |
alice = await action.alice(); |
| 20 |
mindy = await action.mindy(); |
| 21 |
await alice.edit( pageWithBothTerms, { text: `${ searchTerm } ${ searchTerm2 }` } ); |
| 22 |
await alice.edit( pageWithOneTerm, { text: searchTerm2 } ); |
| 23 |
await alice.edit( pageWithOwnTitle, { text: pageWithOwnTitle } ); |
| 24 |
await wiki.runAllJobs(); |
| 25 |
} ); |
| 26 |
|
| 27 |
describe( 'GET /search/page?q={term}', () => { |
| 28 |
it( 'should return empty array when search term has no title or text matches', async () => { |
| 29 |
const nonExistentTerm = utils.uniq(); |
| 30 |
const { body, headers } = await client.get( `/search/page?q=${ nonExistentTerm }` ); |
| 31 |
const noResultsResponse = { pages: [] }; |
| 32 |
assert.match( headers[ 'content-type' ], /^application\/json/ ); |
| 33 |
assert.deepEqual( noResultsResponse, body ); |
| 34 |
} ); |
| 35 |
it( 'should return array of pages when there is only a text match', async () => { |
| 36 |
const { body, headers } = await client.get( `/search/page?q=${ searchTerm }` ); |
| 37 |
assert.match( headers[ 'content-type' ], /^application\/json/ ); |
| 38 |
assert.lengthOf( body.pages, 1 ); |
| 39 |
const returnPage = body.pages[ 0 ]; |
| 40 |
assert.nestedProperty( returnPage, 'title' ); |
| 41 |
assert.nestedProperty( returnPage, 'id' ); |
| 42 |
assert.nestedProperty( returnPage, 'key' ); |
| 43 |
assert.nestedProperty( returnPage, 'excerpt' ); |
| 44 |
assert.nestedPropertyVal( returnPage, 'thumbnail', null ); |
| 45 |
assert.nestedPropertyVal( returnPage, 'description', null ); |
| 46 |
assert.nestedPropertyVal( returnPage, 'matched_title', null ); |
| 47 |
assert.include( returnPage.excerpt, `<span class="searchmatch">${ searchTerm }</span>` ); |
| 48 |
|
| 49 |
|
| 50 |
if ( headers[ 'cache-control' ] ) { |
| 51 |
assert.notMatch( headers[ 'cache-control' ], /\bpublic\b/ ); |
| 52 |
assert.notMatch( headers[ 'cache-control' ], /\bmax-age=0*[1-9]\b/ ); |
| 53 |
assert.notMatch( headers[ 'cache-control' ], /\bs-maxage=0*[1-9]\b/ ); |
| 54 |
} |
| 55 |
} ); |
| 56 |
it( 'should return array of pages when there is only title match', async () => { |
| 57 |
const { body, headers } = await client.get( `/search/page?q=${ pageWithBothTerms }` ); |
| 58 |
assert.match( headers[ 'content-type' ], /^application\/json/ ); |
| 59 |
assert.lengthOf( body.pages, 1 ); |
| 60 |
const returnPage = body.pages[ 0 ]; |
| 61 |
assert.nestedProperty( returnPage, 'title' ); |
| 62 |
assert.nestedProperty( returnPage, 'id' ); |
| 63 |
assert.nestedProperty( returnPage, 'key' ); |
| 64 |
assert.nestedPropertyVal( returnPage, 'excerpt', null ); |
| 65 |
assert.nestedPropertyVal( returnPage, 'thumbnail', null ); |
| 66 |
assert.nestedPropertyVal( returnPage, 'description', null ); |
| 67 |
assert.nestedPropertyVal( returnPage, 'matched_title', null ); |
| 68 |
} ); |
| 69 |
it( 'should return a single page when there is a title and text match on the same page', async () => { |
| 70 |
const { body, headers } = await client.get( `/search/page?q=${ pageWithOwnTitle }` ); |
| 71 |
assert.match( headers[ 'content-type' ], /^application\/json/ ); |
| 72 |
assert.lengthOf( body.pages, 1 ); |
| 73 |
const returnPage = body.pages[ 0 ]; |
| 74 |
assert.nestedProperty( returnPage, 'title' ); |
| 75 |
assert.nestedProperty( returnPage, 'id' ); |
| 76 |
assert.nestedProperty( returnPage, 'key' ); |
| 77 |
assert.nestedPropertyVal( returnPage, 'title', pageWithOwnTitle ); |
| 78 |
assert.nestedPropertyVal( returnPage, 'thumbnail', null ); |
| 79 |
assert.nestedPropertyVal( returnPage, 'description', null ); |
| 80 |
assert.nestedPropertyVal( returnPage, 'matched_title', null ); |
| 81 |
} ); |
| 82 |
it( 'should return two pages when both pages match', async () => { |
| 83 |
const { body, headers } = await client.get( `/search/page?q=${ searchTerm2 }` ); |
| 84 |
assert.match( headers[ 'content-type' ], /^application\/json/ ); |
| 85 |
assert.lengthOf( body.pages, 2 ); |
| 86 |
const returnedPages = [ body.pages[ 0 ].title, body.pages[ 1 ].title ]; |
| 87 |
const expectedPages = [ pageWithBothTerms, pageWithOneTerm ]; |
| 88 |
assert.equal( expectedPages.sort().join( '|' ), returnedPages.sort().join( '|' ) ); |
| 89 |
} ); |
| 90 |
it( 'should return only one page when two pages match but limit is 1', async () => { |
| 91 |
const { body, headers } = await client.get( `/search/page?q=${ searchTerm2 }&limit=1` ); |
| 92 |
assert.match( headers[ 'content-type' ], /^application\/json/ ); |
| 93 |
assert.lengthOf( body.pages, 1 ); |
| 94 |
} ); |
| 95 |
it( 'should not return results when page with term has been deleted', async () => { |
| 96 |
const pageToDelete = 'Delete Page'; |
| 97 |
const deleteTerm = `Delete_${ utils.uniq() }`; |
| 98 |
const { title } = await alice.edit( pageToDelete, { text: deleteTerm } ); |
| 99 |
await mindy.action( 'delete', { |
| 100 |
title, |
| 101 |
summary: 'testing', |
| 102 |
token: await mindy.token( 'csrf' ) |
| 103 |
}, 'POST' ); |
| 104 |
await wiki.runAllJobs(); |
| 105 |
const { body, headers } = await client.get( `/search/page?q=${ deleteTerm }` ); |
| 106 |
assert.match( headers[ 'content-type' ], /^application\/json/ ); |
| 107 |
assert.lengthOf( body.pages, 0 ); |
| 108 |
} ); |
| 109 |
it( 'should ignore duplicate redirect source and target if both pages are a match', async () => { |
| 110 |
const redirectSource = utils.title( 'redirect_source_' ); |
| 111 |
const redirectTarget = utils.title( 'redirect_target_' ); |
| 112 |
const uniquePageText = utils.uniq(); |
| 113 |
|
| 114 |
await alice.edit( redirectSource, |
| 115 |
{ text: `#REDIRECT [[ ${ redirectTarget } ]]. ${ uniquePageText }.` } |
| 116 |
); |
| 117 |
|
| 118 |
const { title: redirectTargetTitle } = await alice.edit( redirectTarget, { text: `${ uniquePageText }` } ); |
| 119 |
|
| 120 |
await wiki.runAllJobs(); |
| 121 |
const { body, headers } = await client.get( `/search/page?q=${ uniquePageText }` ); |
| 122 |
assert.match( headers[ 'content-type' ], /^application\/json/ ); |
| 123 |
assert.lengthOf( body.pages, 1 ); |
| 124 |
assert.nestedPropertyVal( body.pages[ 0 ], 'title', redirectTargetTitle ); |
| 125 |
} ); |
| 126 |
} ); |
| 127 |
|
| 128 |
describe( 'GET /search/title?q={term}', () => { |
| 129 |
it( 'should return empty array when search term has no title matches', async () => { |
| 130 |
const nonExistentTerm = utils.uniq(); |
| 131 |
const { body, headers } = await client.get( `/search/title?q=${ nonExistentTerm }` ); |
| 132 |
assert.match( headers[ 'content-type' ], /^application\/json/ ); |
| 133 |
assert.lengthOf( body.pages, 0 ); |
| 134 |
} ); |
| 135 |
it( 'should not return pages when there is only a text match', async () => { |
| 136 |
const { body, headers } = await client.get( `/search/title?q=${ searchTerm }` ); |
| 137 |
assert.match( headers[ 'content-type' ], /^application\/json/ ); |
| 138 |
assert.lengthOf( body.pages, 0 ); |
| 139 |
} ); |
| 140 |
it( 'should return array of pages when there is a title match', async () => { |
| 141 |
const { body, headers } = await client.get( `/search/title?q=${ pageWithBothTerms }` ); |
| 142 |
assert.match( headers[ 'content-type' ], /^application\/json/ ); |
| 143 |
assert.lengthOf( body.pages, 1 ); |
| 144 |
const returnPage = body.pages[ 0 ]; |
| 145 |
assert.nestedProperty( returnPage, 'title' ); |
| 146 |
assert.nestedProperty( returnPage, 'id' ); |
| 147 |
assert.nestedProperty( returnPage, 'key' ); |
| 148 |
assert.nestedProperty( returnPage, 'excerpt' ); |
| 149 |
assert.nestedPropertyVal( returnPage, 'thumbnail', null ); |
| 150 |
assert.nestedPropertyVal( returnPage, 'description', null ); |
| 151 |
assert.nestedPropertyVal( returnPage, 'matched_title', null ); |
| 152 |
|
| 153 |
|
| 154 |
assert.nestedProperty( headers, 'cache-control' ); |
| 155 |
assert.match( headers[ 'cache-control' ], /\bpublic\b/ ); |
| 156 |
assert.match( headers[ 'cache-control' ], /\bmax-age=[1-9]\d*/ ); |
| 157 |
} ); |
| 158 |
it( 'should return two pages when both pages match', async () => { |
| 159 |
const { body, headers } = await client.get( `/search/title?q=${ sharedTitleTerm }` ); |
| 160 |
assert.match( headers[ 'content-type' ], /^application\/json/ ); |
| 161 |
assert.lengthOf( body.pages, 2 ); |
| 162 |
const returnedPages = [ body.pages[ 0 ].title, body.pages[ 1 ].title ]; |
| 163 |
const expectedPages = [ pageWithBothTerms, pageWithOneTerm ]; |
| 164 |
assert.equal( expectedPages.sort().join( '|' ), returnedPages.sort().join( '|' ) ); |
| 165 |
} ); |
| 166 |
it( 'should return only one page when two pages match but limit is 1', async () => { |
| 167 |
const { body, headers } = await client.get( `/search/title?q=${ sharedTitleTerm }&limit=1` ); |
| 168 |
assert.match( headers[ 'content-type' ], /^application\/json/ ); |
| 169 |
assert.lengthOf( body.pages, 1 ); |
| 170 |
} ); |
| 171 |
it( 'should not return deleted page', async () => { |
| 172 |
const deleteTerm = utils.uniq(); |
| 173 |
const pageToDelete = utils.title( `${ deleteTerm }_test_` ); |
| 174 |
const { title } = await alice.edit( pageToDelete, { text: deleteTerm } ); |
| 175 |
await mindy.action( 'delete', { |
| 176 |
title, |
| 177 |
summary: 'testing', |
| 178 |
token: await mindy.token( 'csrf' ) |
| 179 |
}, 'POST' ); |
| 180 |
await wiki.runAllJobs(); |
| 181 |
const { body, headers } = await client.get( `/search/title?q=${ deleteTerm }` ); |
| 182 |
assert.match( headers[ 'content-type' ], /^application\/json/ ); |
| 183 |
assert.lengthOf( body.pages, 0 ); |
| 184 |
} ); |
| 185 |
it( 'should include redirect for page if one exists', async () => { |
| 186 |
const redirectSource = utils.title( 'redirect_source_' ); |
| 187 |
const redirectTarget = utils.title( 'redirect_target_' ); |
| 188 |
|
| 189 |
const { title: redirectSourceTitle } = await alice.edit( redirectSource, |
| 190 |
{ text: `#REDIRECT [[ ${ redirectTarget } ]]` } |
| 191 |
); |
| 192 |
|
| 193 |
const { title: redirectTargetTitle } = await alice.edit( redirectTarget, { text: 'foo' } ); |
| 194 |
await wiki.runAllJobs(); |
| 195 |
|
| 196 |
const { body, headers } = await client.get( `/search/title?q=${ redirectSourceTitle }` ); |
| 197 |
assert.match( headers[ 'content-type' ], /^application\/json/ ); |
| 198 |
assert.lengthOf( body.pages, 1 ); |
| 199 |
assert.nestedPropertyVal( body.pages[ 0 ], 'title', redirectTargetTitle ); |
| 200 |
assert.nestedPropertyVal( body.pages[ 0 ], 'matched_title', redirectSourceTitle ); |
| 201 |
} ); |
| 202 |
} ); |
| 203 |
|
| 204 |
describe( 'GET /search', () => { |
| 205 |
|
| 206 |
function parseXML( res, cb ) { |
| 207 |
res.text = ''; |
| 208 |
res.on( 'data', ( chunk ) => { |
| 209 |
res.text += chunk; |
| 210 |
} ); |
| 211 |
res.on( 'end', () => xml2js.parseString( res.text, cb ) ); |
| 212 |
} |
| 213 |
|
| 214 |
superagent.parse[ 'application/xml' ] = parseXML; |
| 215 |
superagent.parse[ 'application/opensearchdescription+xml' ] = parseXML; |
| 216 |
|
| 217 |
it( 'should return an OpenSearch Description document', async () => { |
| 218 |
const { body, headers } = await client.get( '/search' ); |
| 219 |
assert.match( headers[ 'content-type' ], /^application\/opensearchdescription\+xml/ ); |
| 220 |
assert.nestedProperty( body, 'OpenSearchDescription' ); |
| 221 |
|
| 222 |
} ); |
| 223 |
|
| 224 |
it( 'should support plain XML output', async () => { |
| 225 |
const { body, headers } = await client.get( '/search' ) |
| 226 |
.query( { ctype: 'application/xml' } ); |
| 227 |
|
| 228 |
assert.match( headers[ 'content-type' ], /^application\/xml/ ); |
| 229 |
assert.nestedProperty( body, 'OpenSearchDescription' ); |
| 230 |
} ); |
| 231 |
} ); |
| 232 |
} ); |