2017-07-28 17:32:46 +00:00
import { createModel } from '../../../src/preview/model' ;
import createRESTBaseGateway from '../../../src/gateway/rest' ;
var DEFAULT _CONSTANTS = {
2017-02-22 10:01:45 +00:00
THUMBNAIL _SIZE : 512
} ,
RESTBASE _RESPONSE = {
title : 'Barack Obama' ,
extract : 'Barack Hussein Obama II born August 4, 1961) ...' ,
thumbnail : {
2017-02-28 14:28:40 +00:00
source : 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.jpg/200px-President_Barack_Obama.jpg' ,
2017-02-22 10:01:45 +00:00
width : 200 ,
height : 250
} ,
originalimage : {
source : 'https://upload.wikimedia.org/wikipedia/commons/8/8d/President_Barack_Obama.jpg' ,
width : 800 ,
height : 1000
} ,
lang : 'en' ,
dir : 'ltr' ,
timestamp : '2017-01-30T10:17:41Z' ,
description : '44th President of the United States of America'
} ,
2017-02-28 14:28:40 +00:00
SVG _RESTBASE _RESPONSE = {
title : 'Barack Obama' ,
extract : 'Barack Hussein Obama II born August 4, 1961) ...' ,
thumbnail : {
2017-03-06 14:37:42 +00:00
source : 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.svg/200px-President_Barack_Obama.svg.png' ,
2017-02-28 14:28:40 +00:00
width : 200 ,
height : 250
} ,
originalimage : {
source : 'https://upload.wikimedia.org/wikipedia/commons/8/8d/President_Barack_Obama.svg' ,
width : 800 ,
height : 1000
} ,
lang : 'en' ,
dir : 'ltr' ,
timestamp : '2017-01-30T10:17:41Z' ,
description : '44th President of the United States of America'
} ,
2017-02-22 10:01:45 +00:00
RESTBASE _RESPONSE _WITHOUT _IMAGE = {
title : 'Barack Obama' ,
extract : 'Barack Hussein Obama II born August 4, 1961) ...' ,
lang : 'en' ,
dir : 'ltr' ,
timestamp : '2017-01-30T10:17:41Z' ,
description : '44th President of the United States of America'
} ,
2017-03-06 14:37:42 +00:00
// Note well that the thumbnail and originalimage properties are
// identical. This can be the case where RESTBase requests a thumbnail at
// 320px width but the original image isn't that wide, in which instance
// PageImages - and therefore RESTBase - return the original image as the
// thumbnail.
//
// See https://phabricator.wikimedia.org/T158632#3071104 onward for additional
// context.
RESTBASE _RESPONSE _WITH _SMALL _IMAGE = {
2017-04-25 12:12:36 +00:00
title : 'PreviewsNonFreeImage/sandbox' ,
extract : 'Hello, I am the non-free image and parenthetical page (YOU CAN\'T SEE THIS). My preview should contain an image that is not free. My preview should contain a parenthetical you cannot see..' ,
2017-03-06 14:37:42 +00:00
thumbnail : {
2017-04-25 12:12:36 +00:00
source : 'https://upload.wikimedia.org/wikipedia/commons/2/2c/RH-Fedora_logo-nonfree.png' ,
2017-03-06 14:37:42 +00:00
width : 300 ,
2017-03-10 10:21:53 +00:00
height : 126 ,
2017-04-25 12:12:36 +00:00
original : 'https://upload.wikimedia.org/wikipedia/commons/2/2c/RH-Fedora_logo-nonfree.png'
2017-03-06 14:37:42 +00:00
} ,
originalimage : {
2017-04-25 12:12:36 +00:00
source : 'https://upload.wikimedia.org/wikipedia/commons/2/2c/RH-Fedora_logo-nonfree.png' ,
2017-03-06 14:37:42 +00:00
width : 300 ,
height : 126
} ,
2017-04-25 12:12:36 +00:00
lang : 'en' ,
dir : 'ltr' ,
timestamp : '2017-02-17T22:29:56Z'
2017-03-06 14:37:42 +00:00
} ,
2017-03-10 10:21:53 +00:00
RESTBASE _RESPONSE _WITH _LANDSCAPE _IMAGE = {
2017-04-25 12:12:36 +00:00
title : 'Landscape' ,
extract : 'Landscape' ,
2017-03-10 10:21:53 +00:00
thumbnail : {
2017-04-25 12:12:36 +00:00
source : 'http://foo/bar/baz.png/500px-baz.png' ,
2017-03-10 10:21:53 +00:00
width : 500 ,
height : 300 ,
2017-04-25 12:12:36 +00:00
original : 'http://foo/bar/baz.png'
2017-03-10 10:21:53 +00:00
} ,
originalimage : {
2017-04-25 12:12:36 +00:00
source : 'http://foo/bar/baz.png' ,
2017-03-10 10:21:53 +00:00
width : 1000 ,
height : 600
} ,
2017-04-25 12:12:36 +00:00
lang : 'en' ,
dir : 'ltr' ,
timestamp : '2017-02-17T22:29:56Z'
2017-03-10 10:21:53 +00:00
} ,
2017-02-22 10:01:45 +00:00
RESTBASE _RESPONSE _PREVIEW _MODEL = createModel (
'Barack Obama' ,
'url/Barack Obama' , // Generated in the stub below
'en' ,
'ltr' ,
2017-06-08 13:29:57 +00:00
'!Barack Hussein Obama II born August 4, 1961) ...!' ,
2017-02-22 10:01:45 +00:00
{
2017-03-10 10:21:53 +00:00
source : 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.jpg/409px-President_Barack_Obama.jpg' ,
width : 409 ,
height : 512
2017-02-22 10:01:45 +00:00
}
) ;
2017-06-08 13:29:57 +00:00
function provideParsedExtract ( page ) {
return '!' + page . extract + '!' ;
}
2017-02-22 10:01:45 +00:00
QUnit . module ( 'gateway/rest' , {
beforeEach : function ( ) {
2017-06-08 00:58:30 +00:00
window . mediaWiki . Title = function ( title ) {
2017-02-22 10:01:45 +00:00
this . getUrl = function ( ) { return 'url/' + title ; } ;
} ;
} ,
afterEach : function ( ) {
2017-06-08 00:58:30 +00:00
window . mediaWiki . Title = null ;
2017-02-22 10:01:45 +00:00
}
} ) ;
QUnit . test ( 'RESTBase gateway is called with correct arguments' , function ( assert ) {
var getSpy = this . sandbox . spy ( ) ,
gateway = createRESTBaseGateway ( getSpy ) ,
expectedOptions = {
url : '/api/rest_v1/page/summary/' + encodeURIComponent ( 'Test Title' ) ,
headers : {
2017-06-06 09:48:05 +00:00
Accept : 'application/json; charset=utf-8; ' +
'profile="https://www.mediawiki.org/wiki/Specs/Summary/1.2.0"'
2017-02-22 10:01:45 +00:00
}
} ;
gateway . fetch ( 'Test Title' ) ;
assert . deepEqual ( getSpy . getCall ( 0 ) . args [ 0 ] , expectedOptions , 'options' ) ;
} ) ;
2017-06-08 13:29:57 +00:00
QUnit . test ( 'RESTBase provider uses extract parser' , function ( assert ) {
var getSpy = this . sandbox . spy ( ) ,
gateway = createRESTBaseGateway ( ) ;
gateway . convertPageToModel ( RESTBASE _RESPONSE , 512 , getSpy ) ;
assert . deepEqual ( getSpy . getCall ( 0 ) . args [ 0 ] , RESTBASE _RESPONSE ) ;
} ) ;
2018-01-11 03:23:28 +00:00
QUnit . test ( 'RESTBase gateway is correctly converting the page data to a model' , function ( assert ) {
2017-02-22 10:01:45 +00:00
var gateway = createRESTBaseGateway ( ) ;
assert . deepEqual (
2017-06-08 13:29:57 +00:00
gateway . convertPageToModel ( RESTBASE _RESPONSE , 512 , provideParsedExtract ) ,
2017-02-22 10:01:45 +00:00
RESTBASE _RESPONSE _PREVIEW _MODEL
) ;
} ) ;
QUnit . test ( 'RESTBase gateway doesn\'t stretch thumbnails' , function ( assert ) {
var model ,
2017-04-25 12:12:36 +00:00
gateway = createRESTBaseGateway ( ) ;
2017-02-22 10:01:45 +00:00
2017-06-08 13:29:57 +00:00
model = gateway . convertPageToModel ( RESTBASE _RESPONSE , 2000 , provideParsedExtract ) ;
2017-02-22 10:01:45 +00:00
2017-03-06 14:37:42 +00:00
assert . deepEqual (
model . thumbnail ,
RESTBASE _RESPONSE . originalimage ,
2018-01-11 03:23:28 +00:00
'If the requested thumbnail size is bigger than that of the original, then use the original.'
2017-03-06 14:37:42 +00:00
) ;
// ---
2017-06-08 13:29:57 +00:00
model = gateway . convertPageToModel ( RESTBASE _RESPONSE , RESTBASE _RESPONSE . originalimage . height , provideParsedExtract ) ;
2017-03-06 14:37:42 +00:00
assert . deepEqual (
model . thumbnail ,
RESTBASE _RESPONSE . originalimage ,
'If the requested thumbnail size is the same as that of the original, then use the original.'
) ;
// ---
2017-06-08 13:29:57 +00:00
model = gateway . convertPageToModel ( RESTBASE _RESPONSE _WITH _SMALL _IMAGE , 320 , provideParsedExtract ) ;
2017-03-06 14:37:42 +00:00
assert . deepEqual (
model . thumbnail ,
RESTBASE _RESPONSE _WITH _SMALL _IMAGE . originalimage ,
2018-01-11 03:23:28 +00:00
'If the requested thumbnail can\'t be generated because the original is too small, then use the original.'
2017-03-06 14:37:42 +00:00
) ;
2017-03-10 10:21:53 +00:00
// ---
2017-06-08 13:29:57 +00:00
model = gateway . convertPageToModel ( RESTBASE _RESPONSE _WITH _LANDSCAPE _IMAGE , 640 , provideParsedExtract ) ;
2017-03-10 10:21:53 +00:00
assert . deepEqual (
model . thumbnail ,
{
source : 'http://foo/bar/baz.png/640px-baz.png' ,
width : 640 ,
height : 384 // ( 640 / 500 ) * 300
} ,
'When the requested thumbnail is scaled, then its largest dimension is preserved.'
) ;
2017-03-06 14:37:42 +00:00
} ) ;
2018-01-11 03:23:28 +00:00
QUnit . test ( 'RESTBase gateway handles awkward thumbnails' , function ( assert ) {
2017-03-06 14:37:42 +00:00
var gateway = createRESTBaseGateway ( ) ,
response ,
model ;
response = Object . assign ( { } , RESTBASE _RESPONSE ) ;
response . thumbnail = Object . assign ( { } , RESTBASE _RESPONSE . thumbnail ) ;
response . thumbnail . source = 'http://foo.bar/baz/Qux-320px-Quux.png/800px-Qux-320px-Quux.png' ;
2017-06-08 13:29:57 +00:00
model = gateway . convertPageToModel ( response , 500 , provideParsedExtract ) ;
2017-03-06 14:37:42 +00:00
assert . deepEqual (
2017-02-22 10:01:45 +00:00
model . thumbnail . source ,
2017-03-10 10:21:53 +00:00
'http://foo.bar/baz/Qux-320px-Quux.png/400px-Qux-320px-Quux.png' ,
2017-03-06 14:37:42 +00:00
'If the requested thumbnail size is the same as that of the original, then use the original.'
2017-02-22 10:01:45 +00:00
) ;
} ) ;
QUnit . test ( 'RESTBase gateway stretches SVGs' , function ( assert ) {
var model ,
gateway = createRESTBaseGateway ( ) ;
2017-06-08 13:29:57 +00:00
model = gateway . convertPageToModel ( SVG _RESTBASE _RESPONSE , 2000 , provideParsedExtract ) ;
2017-02-22 10:01:45 +00:00
assert . equal (
model . thumbnail . source ,
2017-03-10 10:21:53 +00:00
'https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.svg/1600px-President_Barack_Obama.svg.png' ,
2017-03-06 14:37:42 +00:00
'If the requested thumbnail is for an SVG, then it\'s always scaled.'
2017-02-22 10:01:45 +00:00
) ;
} ) ;
2018-01-11 03:23:28 +00:00
QUnit . test ( 'RESTBase gateway handles API failure' , function ( assert ) {
var api = this . sandbox . stub ( ) . returns ( $ . Deferred ( ) . reject ( { status : 500 } ) . promise ( ) ) ,
2017-08-16 18:27:35 +00:00
gateway = createRESTBaseGateway ( api ) ;
2017-02-22 10:01:45 +00:00
2017-08-16 18:27:35 +00:00
return gateway . getPageSummary ( 'Test Title' ) . catch ( function ( ) {
2017-02-22 10:01:45 +00:00
assert . ok ( true ) ;
} ) ;
2017-04-03 23:37:52 +00:00
} ) ;
QUnit . test ( 'RESTBase gateway does not treat a 404 as a failure' , function ( assert ) {
2018-01-11 03:23:28 +00:00
var response = {
status : 404 ,
type : 'https://mediawiki.org/wiki/HyperSwitch/errors/not_found' ,
title : 'Not found.' ,
method : 'get' ,
detail : 'Page or revision not found.' ,
uri : '/en.wikipedia.org/v1/page/summary/Missing_page'
} ,
api = this . sandbox . stub ( ) . returns (
$ . Deferred ( ) . reject ( response ) . promise ( )
) ,
gateway = createRESTBaseGateway ( api , DEFAULT _CONSTANTS , provideParsedExtract ) ;
2017-04-03 23:37:52 +00:00
2018-01-11 03:23:28 +00:00
return gateway . getPageSummary ( 'Missing Page' ) . then ( function ( result ) {
assert . equal ( result . title , 'Missing Page' , 'Title' ) ;
// Extract is undefined since the parser is only invoked for successful responses.
assert . equal ( result . extract , undefined , 'Extract' ) ;
2017-04-03 23:37:52 +00:00
} ) ;
2017-02-22 10:01:45 +00:00
} ) ;
QUnit . test ( 'RESTBase gateway returns the correct data ' , function ( assert ) {
var api = this . sandbox . stub ( ) . returns (
$ . Deferred ( ) . resolve ( RESTBASE _RESPONSE ) . promise ( )
) ,
2017-08-16 18:27:35 +00:00
gateway = createRESTBaseGateway ( api , DEFAULT _CONSTANTS , provideParsedExtract ) ;
2017-02-22 10:01:45 +00:00
2017-08-22 11:28:05 +00:00
return gateway . getPageSummary ( 'Test Title' ) . then ( function ( result ) {
2017-02-22 10:01:45 +00:00
assert . deepEqual ( result , RESTBASE _RESPONSE _PREVIEW _MODEL ) ;
} ) ;
} ) ;
QUnit . test ( 'RESTBase gateway handles missing images ' , function ( assert ) {
var model ,
gateway = createRESTBaseGateway ( ) ;
2017-06-08 13:29:57 +00:00
model = gateway . convertPageToModel ( RESTBASE _RESPONSE _WITHOUT _IMAGE , 300 , provideParsedExtract ) ;
2017-02-22 10:01:45 +00:00
assert . equal (
model . originalimage ,
undefined ,
'If restbase handles missing image information'
) ;
} ) ;
2017-12-12 21:25:30 +00:00
QUnit . test ( 'RESTBase gateway handles missing extracts' , function ( assert ) {
var api = this . sandbox . stub ( ) . returns ( $ . Deferred ( ) . resolve ( { } ) . promise ( ) ) ,
gateway = createRESTBaseGateway ( api , DEFAULT _CONSTANTS , provideParsedExtract ) ;
return gateway . getPageSummary ( 'Test Title with missing extract' ) . then ( function ( result ) {
2018-01-11 03:23:28 +00:00
assert . equal ( result . title , 'Test Title with missing extract' , 'Title' ) ;
2017-12-12 21:25:30 +00:00
assert . equal ( result . extract , '!!' , 'Extract' ) ;
} ) ;
} ) ;
QUnit . test ( 'RESTBase gateway handles no content success responses' , function ( assert ) {
var api = this . sandbox . stub ( ) . returns ( $ . Deferred ( ) . resolve ( { status : 204 } ) . promise ( ) ) ,
gateway = createRESTBaseGateway ( api , DEFAULT _CONSTANTS , provideParsedExtract ) ;
return gateway . getPageSummary ( 'Test Title with empty response' ) . then ( function ( result ) {
2018-01-11 03:23:28 +00:00
assert . equal ( result . title , 'Test Title with empty response' , 'Title' ) ;
2017-12-12 21:25:30 +00:00
assert . equal ( result . extract , '!!' , 'Extract' ) ;
} ) ;
} ) ;