diff options
Diffstat (limited to 'www/wiki/tests/qunit/suites/resources/mediawiki.api')
9 files changed, 1130 insertions, 0 deletions
diff --git a/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.ForeignApi.test.js b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.ForeignApi.test.js new file mode 100644 index 00000000..69ab7975 --- /dev/null +++ b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.ForeignApi.test.js @@ -0,0 +1,32 @@ +( function ( mw ) { + QUnit.module( 'mediawiki.ForeignApi', QUnit.newMwEnvironment( { + setup: function () { + this.server = this.sandbox.useFakeServer(); + this.server.respondImmediately = true; + } + } ) ); + + QUnit.test( 'origin is included in GET requests', function ( assert ) { + var api = new mw.ForeignApi( '//localhost:4242/w/api.php' ); + + this.server.respond( function ( request ) { + assert.ok( request.url.match( /origin=/ ), 'origin is included in GET requests' ); + request.respond( 200, { 'Content-Type': 'application/json' }, '[]' ); + } ); + + return api.get( {} ); + } ); + + QUnit.test( 'origin is included in POST requests', function ( assert ) { + var api = new mw.ForeignApi( '//localhost:4242/w/api.php' ); + + this.server.respond( function ( request ) { + assert.ok( request.requestBody.match( /origin=/ ), 'origin is included in POST request body' ); + assert.ok( request.url.match( /origin=/ ), 'origin is included in POST request URL, too' ); + request.respond( 200, { 'Content-Type': 'application/json' }, '[]' ); + } ); + + return api.post( {} ); + } ); + +}( mediaWiki ) ); diff --git a/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.category.test.js b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.category.test.js new file mode 100644 index 00000000..50fa6d15 --- /dev/null +++ b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.category.test.js @@ -0,0 +1,114 @@ +( function ( mw ) { + QUnit.module( 'mediawiki.api.category', QUnit.newMwEnvironment( { + setup: function () { + this.server = this.sandbox.useFakeServer(); + this.server.respondImmediately = true; + } + } ) ); + + QUnit.test( '.getCategoriesByPrefix()', function ( assert ) { + this.server.respondWith( [ 200, { 'Content-Type': 'application/json' }, + '{ "query": { "allpages": [ ' + + '{ "title": "Category:Food" },' + + '{ "title": "Category:Fool Supermarine S.6" },' + + '{ "title": "Category:Fools" }' + + '] } }' + ] ); + + return new mw.Api().getCategoriesByPrefix( 'Foo' ).then( function ( matches ) { + assert.deepEqual( + matches, + [ 'Food', 'Fool Supermarine S.6', 'Fools' ] + ); + } ); + } ); + + QUnit.test( '.isCategory("")', function ( assert ) { + this.server.respondWith( /titles=$/, [ + 200, + { 'Content-Type': 'application/json' }, + '{"batchcomplete":true}' + ] ); + return new mw.Api().isCategory( '' ).then( function ( response ) { + assert.equal( response, false ); + } ); + } ); + + QUnit.test( '.isCategory("#")', function ( assert ) { + this.server.respondWith( /titles=%23$/, [ + 200, + { 'Content-Type': 'application/json' }, + '{"batchcomplete":true,"query":{"normalized":[{"fromencoded":false,"from":"#","to":""}]}}' + ] ); + return new mw.Api().isCategory( '#' ).then( function ( response ) { + assert.equal( response, false ); + } ); + } ); + + QUnit.test( '.isCategory("mw:")', function ( assert ) { + this.server.respondWith( /titles=mw%3A$/, [ + 200, + { 'Content-Type': 'application/json' }, + '{"batchcomplete":true,"query":{"interwiki":[{"title":"mw:","iw":"mw"}]}}' + ] ); + return new mw.Api().isCategory( 'mw:' ).then( function ( response ) { + assert.equal( response, false ); + } ); + } ); + + QUnit.test( '.isCategory("|")', function ( assert ) { + this.server.respondWith( /titles=%1F%7C$/, [ + 200, + { 'Content-Type': 'application/json' }, + '{"batchcomplete":true,"query":{"pages":[{"title":"|","invalidreason":"The requested page title contains invalid characters: \\"|\\".","invalid":true}]}}' + ] ); + return new mw.Api().isCategory( '|' ).then( function ( response ) { + assert.equal( response, false ); + } ); + } ); + + QUnit.test( '.getCategories("")', function ( assert ) { + this.server.respondWith( /titles=$/, [ + 200, + { 'Content-Type': 'application/json' }, + '{"batchcomplete":true}' + ] ); + return new mw.Api().getCategories( '' ).then( function ( response ) { + assert.equal( response, false ); + } ); + } ); + + QUnit.test( '.getCategories("#")', function ( assert ) { + this.server.respondWith( /titles=%23$/, [ + 200, + { 'Content-Type': 'application/json' }, + '{"batchcomplete":true,"query":{"normalized":[{"fromencoded":false,"from":"#","to":""}]}}' + ] ); + return new mw.Api().getCategories( '#' ).then( function ( response ) { + assert.equal( response, false ); + } ); + } ); + + QUnit.test( '.getCategories("mw:")', function ( assert ) { + this.server.respondWith( /titles=mw%3A$/, [ + 200, + { 'Content-Type': 'application/json' }, + '{"batchcomplete":true,"query":{"interwiki":[{"title":"mw:","iw":"mw"}]}}' + ] ); + return new mw.Api().getCategories( 'mw:' ).then( function ( response ) { + assert.equal( response, false ); + } ); + } ); + + QUnit.test( '.getCategories("|")', function ( assert ) { + this.server.respondWith( /titles=%1F%7C$/, [ + 200, + { 'Content-Type': 'application/json' }, + '{"batchcomplete":true,"query":{"pages":[{"title":"|","invalidreason":"The requested page title contains invalid characters: \\"|\\".","invalid":true}]}}' + ] ); + return new mw.Api().getCategories( '|' ).then( function ( response ) { + assert.equal( response, false ); + } ); + } ); + +}( mediaWiki ) ); diff --git a/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.edit.test.js b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.edit.test.js new file mode 100644 index 00000000..4ce7c5db --- /dev/null +++ b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.edit.test.js @@ -0,0 +1,220 @@ +( function ( mw, $ ) { + QUnit.module( 'mediawiki.api.edit', QUnit.newMwEnvironment( { + setup: function () { + this.server = this.sandbox.useFakeServer(); + this.server.respondImmediately = true; + } + } ) ); + + QUnit.test( 'edit( title, transform String )', function ( assert ) { + this.server.respond( function ( req ) { + if ( /query.+titles=Sandbox/.test( req.url ) ) { + req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( { + curtimestamp: '2016-01-02T12:00:00Z', + query: { + pages: [ { + pageid: 1, + ns: 0, + title: 'Sandbox', + revisions: [ { + timestamp: '2016-01-01T12:00:00Z', + contentformat: 'text/x-wiki', + contentmodel: 'wikitext', + content: 'Sand.' + } ] + } ] + } + } ) ); + } + if ( /edit.+basetimestamp=2016-01-01.+starttimestamp=2016-01-02.+text=Box%2E/.test( req.requestBody ) ) { + req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( { + edit: { + result: 'Success', + oldrevid: 11, + newrevid: 13, + newtimestamp: '2016-01-03T12:00:00Z' + } + } ) ); + } + } ); + + return new mw.Api() + .edit( 'Sandbox', function ( revision ) { + return revision.content.replace( 'Sand', 'Box' ); + } ) + .then( function ( edit ) { + assert.equal( edit.newrevid, 13 ); + } ); + } ); + + QUnit.test( 'edit( mw.Title, transform String )', function ( assert ) { + this.server.respond( function ( req ) { + if ( /query.+titles=Sandbox/.test( req.url ) ) { + req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( { + curtimestamp: '2016-01-02T12:00:00Z', + query: { + pages: [ { + pageid: 1, + ns: 0, + title: 'Sandbox', + revisions: [ { + timestamp: '2016-01-01T12:00:00Z', + contentformat: 'text/x-wiki', + contentmodel: 'wikitext', + content: 'Sand.' + } ] + } ] + } + } ) ); + } + if ( /edit.+basetimestamp=2016-01-01.+starttimestamp=2016-01-02.+text=Box%2E/.test( req.requestBody ) ) { + req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( { + edit: { + result: 'Success', + oldrevid: 11, + newrevid: 13, + newtimestamp: '2016-01-03T12:00:00Z' + } + } ) ); + } + } ); + + return new mw.Api() + .edit( new mw.Title( 'Sandbox' ), function ( revision ) { + return revision.content.replace( 'Sand', 'Box' ); + } ) + .then( function ( edit ) { + assert.equal( edit.newrevid, 13 ); + } ); + } ); + + QUnit.test( 'edit( title, transform Promise )', function ( assert ) { + this.server.respond( function ( req ) { + if ( /query.+titles=Async/.test( req.url ) ) { + req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( { + curtimestamp: '2016-02-02T12:00:00Z', + query: { + pages: [ { + pageid: 4, + ns: 0, + title: 'Async', + revisions: [ { + timestamp: '2016-02-01T12:00:00Z', + contentformat: 'text/x-wiki', + contentmodel: 'wikitext', + content: 'Async.' + } ] + } ] + } + } ) ); + } + if ( /edit.+basetimestamp=2016-02-01.+starttimestamp=2016-02-02.+text=Promise%2E/.test( req.requestBody ) ) { + req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( { + edit: { + result: 'Success', + oldrevid: 21, + newrevid: 23, + newtimestamp: '2016-02-03T12:00:00Z' + } + } ) ); + } + } ); + + return new mw.Api() + .edit( 'Async', function ( revision ) { + return $.Deferred().resolve( revision.content.replace( 'Async', 'Promise' ) ); + } ) + .then( function ( edit ) { + assert.equal( edit.newrevid, 23 ); + } ); + } ); + + QUnit.test( 'edit( title, transform Object )', function ( assert ) { + this.server.respond( function ( req ) { + if ( /query.+titles=Param/.test( req.url ) ) { + req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( { + curtimestamp: '2016-03-02T12:00:00Z', + query: { + pages: [ { + pageid: 3, + ns: 0, + title: 'Param', + revisions: [ { + timestamp: '2016-03-01T12:00:00Z', + contentformat: 'text/x-wiki', + contentmodel: 'wikitext', + content: '...' + } ] + } ] + } + } ) ); + } + if ( /edit.+basetimestamp=2016-03-01.+starttimestamp=2016-03-02.+text=Content&summary=Sum/.test( req.requestBody ) ) { + req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( { + edit: { + result: 'Success', + oldrevid: 31, + newrevid: 33, + newtimestamp: '2016-03-03T12:00:00Z' + } + } ) ); + } + } ); + + return new mw.Api() + .edit( 'Param', function () { + return { text: 'Content', summary: 'Sum' }; + } ) + .then( function ( edit ) { + assert.equal( edit.newrevid, 33 ); + } ); + } ); + + QUnit.test( 'edit( invalid-title, transform String )', function ( assert ) { + this.server.respond( function ( req ) { + if ( /query.+titles=%1F%7C/.test( req.url ) ) { + req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( { + query: { + pages: [ { + title: '|', + invalidreason: 'The requested page title contains invalid characters: "|".', + invalid: true + } ] + } + } ) ); + } + } ); + + return new mw.Api() + .edit( '|', function ( revision ) { + return revision.content.replace( 'Sand', 'Box' ); + } ) + .then( function () { + return $.Deferred().reject( 'Unexpected success' ); + }, function ( reason ) { + assert.equal( reason, 'invalidtitle' ); + } ); + } ); + + QUnit.test( 'create( title, content )', function ( assert ) { + this.server.respond( function ( req ) { + if ( /edit.+text=Sand/.test( req.requestBody ) ) { + req.respond( 200, { 'Content-Type': 'application/json' }, JSON.stringify( { + edit: { + new: true, + result: 'Success', + newrevid: 41, + newtimestamp: '2016-04-01T12:00:00Z' + } + } ) ); + } + } ); + + return new mw.Api() + .create( 'Sandbox', { summary: 'Load sand particles.' }, 'Sand.' ) + .then( function ( page ) { + assert.equal( page.newrevid, 41 ); + } ); + } ); + +}( mediaWiki, jQuery ) ); diff --git a/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.messages.test.js b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.messages.test.js new file mode 100644 index 00000000..7282b3fb --- /dev/null +++ b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.messages.test.js @@ -0,0 +1,29 @@ +( function ( mw ) { + QUnit.module( 'mediawiki.api.messages', QUnit.newMwEnvironment( { + setup: function () { + this.server = this.sandbox.useFakeServer(); + this.server.respondImmediately = true; + } + } ) ); + + QUnit.test( '.getMessages()', function ( assert ) { + this.server.respondWith( /ammessages=foo%7Cbaz/, [ + 200, + { 'Content-Type': 'application/json' }, + '{ "query": { "allmessages": [' + + '{ "name": "foo", "content": "Foo bar" },' + + '{ "name": "baz", "content": "Baz Quux" }' + + '] } }' + ] ); + + return new mw.Api().getMessages( [ 'foo', 'baz' ] ).then( function ( messages ) { + assert.deepEqual( + messages, + { + foo: 'Foo bar', + baz: 'Baz Quux' + } + ); + } ); + } ); +}( mediaWiki ) ); diff --git a/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.options.test.js b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.options.test.js new file mode 100644 index 00000000..997a42c8 --- /dev/null +++ b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.options.test.js @@ -0,0 +1,141 @@ +( function ( mw ) { + QUnit.module( 'mediawiki.api.options', QUnit.newMwEnvironment( { + setup: function () { + this.server = this.sandbox.useFakeServer(); + this.server.respondImmediately = true; + } + } ) ); + + QUnit.test( 'saveOption', function ( assert ) { + var api = new mw.Api(), + stub = this.sandbox.stub( mw.Api.prototype, 'saveOptions' ); + + api.saveOption( 'foo', 'bar' ); + + assert.ok( stub.calledOnce, '#saveOptions called once' ); + assert.deepEqual( stub.getCall( 0 ).args, [ { foo: 'bar' } ], '#saveOptions called correctly' ); + } ); + + QUnit.test( 'saveOptions without Unit Separator', function ( assert ) { + var api = new mw.Api( { useUS: false } ); + + // We need to respond to the request for token first, otherwise the other requests won't be sent + // until after the server.respond call, which confuses sinon terribly. This sucks a lot. + api.getToken( 'options' ); + this.server.respond( + /meta=tokens&type=csrf/, + [ 200, { 'Content-Type': 'application/json' }, + '{ "query": { "tokens": { "csrftoken": "+\\\\" } } }' ] + ); + + // Requests are POST, match requestBody instead of url + this.server.respond( function ( request ) { + if ( [ + // simple + 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C', + // two options + 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C', + // not bundleable + 'action=options&format=json&formatversion=2&optionname=foo&optionvalue=bar%7Cquux&token=%2B%5C', + 'action=options&format=json&formatversion=2&optionname=bar&optionvalue=a%7Cb%7Cc&token=%2B%5C', + 'action=options&format=json&formatversion=2&change=baz%3Dquux&token=%2B%5C', + // reset an option + 'action=options&format=json&formatversion=2&change=foo&token=%2B%5C', + // reset an option, not bundleable + 'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C' + ].indexOf( request.requestBody ) !== -1 ) { + assert.ok( true, 'Repond to ' + request.requestBody ); + request.respond( 200, { 'Content-Type': 'application/json' }, + '{ "options": "success" }' ); + } else { + assert.ok( false, 'Unexpected request: ' + request.requestBody ); + } + } ); + + return QUnit.whenPromisesComplete( + api.saveOptions( {} ).then( function () { + assert.ok( true, 'Request completed: empty case' ); + } ), + api.saveOptions( { foo: 'bar' } ).then( function () { + assert.ok( true, 'Request completed: simple' ); + } ), + api.saveOptions( { foo: 'bar', baz: 'quux' } ).then( function () { + assert.ok( true, 'Request completed: two options' ); + } ), + api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', baz: 'quux' } ).then( function () { + assert.ok( true, 'Request completed: not bundleable' ); + } ), + api.saveOptions( { foo: null } ).then( function () { + assert.ok( true, 'Request completed: reset an option' ); + } ), + api.saveOptions( { 'foo|bar=quux': null } ).then( function () { + assert.ok( true, 'Request completed: reset an option, not bundleable' ); + } ) + ); + } ); + + QUnit.test( 'saveOptions with Unit Separator', function ( assert ) { + var api = new mw.Api( { useUS: true } ); + + // We need to respond to the request for token first, otherwise the other requests won't be sent + // until after the server.respond call, which confuses sinon terribly. This sucks a lot. + api.getToken( 'options' ); + this.server.respond( + /meta=tokens&type=csrf/, + [ 200, { 'Content-Type': 'application/json' }, + '{ "query": { "tokens": { "csrftoken": "+\\\\" } } }' ] + ); + + // Requests are POST, match requestBody instead of url + this.server.respond( function ( request ) { + if ( [ + // simple + 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C', + // two options + 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C', + // bundleable with unit separator + 'action=options&format=json&formatversion=2&change=%1Ffoo%3Dbar%7Cquux%1Fbar%3Da%7Cb%7Cc%1Fbaz%3Dquux&token=%2B%5C', + // not bundleable with unit separator + 'action=options&format=json&formatversion=2&optionname=baz%3Dbaz&optionvalue=quux&token=%2B%5C', + 'action=options&format=json&formatversion=2&change=%1Ffoo%3Dbar%7Cquux%1Fbar%3Da%7Cb%7Cc&token=%2B%5C', + // reset an option + 'action=options&format=json&formatversion=2&change=foo&token=%2B%5C', + // reset an option, not bundleable + 'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C' + ].indexOf( request.requestBody ) !== -1 ) { + assert.ok( true, 'Repond to ' + request.requestBody ); + request.respond( + 200, + { 'Content-Type': 'application/json' }, + '{ "options": "success" }' + ); + } else { + assert.ok( false, 'Unexpected request: ' + request.requestBody ); + } + } ); + + return QUnit.whenPromisesComplete( + api.saveOptions( {} ).done( function () { + assert.ok( true, 'Request completed: empty case' ); + } ), + api.saveOptions( { foo: 'bar' } ).done( function () { + assert.ok( true, 'Request completed: simple' ); + } ), + api.saveOptions( { foo: 'bar', baz: 'quux' } ).done( function () { + assert.ok( true, 'Request completed: two options' ); + } ), + api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', baz: 'quux' } ).done( function () { + assert.ok( true, 'Request completed: bundleable with unit separator' ); + } ), + api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', 'baz=baz': 'quux' } ).done( function () { + assert.ok( true, 'Request completed: not bundleable with unit separator' ); + } ), + api.saveOptions( { foo: null } ).done( function () { + assert.ok( true, 'Request completed: reset an option' ); + } ), + api.saveOptions( { 'foo|bar=quux': null } ).done( function () { + assert.ok( true, 'Request completed: reset an option, not bundleable' ); + } ) + ); + } ); +}( mediaWiki ) ); diff --git a/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.parse.test.js b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.parse.test.js new file mode 100644 index 00000000..74da0098 --- /dev/null +++ b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.parse.test.js @@ -0,0 +1,45 @@ +( function ( mw ) { + QUnit.module( 'mediawiki.api.parse', QUnit.newMwEnvironment( { + setup: function () { + this.server = this.sandbox.useFakeServer(); + this.server.respondImmediately = true; + } + } ) ); + + QUnit.test( '.parse( string )', function ( assert ) { + this.server.respondWith( /action=parse.*&text='''Hello(\+|%20)world'''/, [ 200, + { 'Content-Type': 'application/json' }, + '{ "parse": { "text": "<p><b>Hello world</b></p>" } }' + ] ); + + return new mw.Api().parse( '\'\'\'Hello world\'\'\'' ).done( function ( html ) { + assert.equal( html, '<p><b>Hello world</b></p>', 'Parse wikitext by string' ); + } ); + } ); + + QUnit.test( '.parse( Object.toString )', function ( assert ) { + this.server.respondWith( /action=parse.*&text='''Hello(\+|%20)world'''/, [ 200, + { 'Content-Type': 'application/json' }, + '{ "parse": { "text": "<p><b>Hello world</b></p>" } }' + ] ); + + return new mw.Api().parse( { + toString: function () { + return '\'\'\'Hello world\'\'\''; + } + } ).done( function ( html ) { + assert.equal( html, '<p><b>Hello world</b></p>', 'Parse wikitext by toString object' ); + } ); + } ); + + QUnit.test( '.parse( mw.Title )', function ( assert ) { + this.server.respondWith( /action=parse.*&page=Earth/, [ 200, + { 'Content-Type': 'application/json' }, + '{ "parse": { "text": "<p><b>Earth</b> is a planet.</p>" } }' + ] ); + + return new mw.Api().parse( new mw.Title( 'Earth' ) ).done( function ( html ) { + assert.equal( html, '<p><b>Earth</b> is a planet.</p>', 'Parse page by Title object' ); + } ); + } ); +}( mediaWiki ) ); diff --git a/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js new file mode 100644 index 00000000..417ad3d8 --- /dev/null +++ b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js @@ -0,0 +1,456 @@ +( function ( mw, $ ) { + QUnit.module( 'mediawiki.api', QUnit.newMwEnvironment( { + setup: function () { + this.server = this.sandbox.useFakeServer(); + this.server.respondImmediately = true; + } + } ) ); + + function sequence( responses ) { + var i = 0; + return function ( request ) { + var response = responses[ i ]; + if ( response ) { + i++; + request.respond.apply( request, response ); + } + }; + } + + function sequenceBodies( status, headers, bodies ) { + bodies.forEach( function ( body, i ) { + bodies[ i ] = [ status, headers, body ]; + } ); + return sequence( bodies ); + } + + // Utility to make inline use with an assert easier + function match( text, pattern ) { + var m = text.match( pattern ); + return m && m[ 1 ] || null; + } + + QUnit.test( 'get()', function ( assert ) { + var api = new mw.Api(); + + this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '[]' ] ); + + return api.get( {} ).then( function ( data ) { + assert.deepEqual( data, [], 'If request succeeds without errors, resolve deferred' ); + } ); + } ); + + QUnit.test( 'post()', function ( assert ) { + var api = new mw.Api(); + + this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '[]' ] ); + + return api.post( {} ).then( function ( data ) { + assert.deepEqual( data, [], 'Simple POST request' ); + } ); + } ); + + QUnit.test( 'API error errorformat=bc', function ( assert ) { + var api = new mw.Api(); + + this.server.respond( [ 200, { 'Content-Type': 'application/json' }, + '{ "error": { "code": "unknown_action" } }' + ] ); + + api.get( { action: 'doesntexist' } ) + .fail( function ( errorCode ) { + assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' ); + } ) + .always( assert.async() ); + } ); + + QUnit.test( 'API error errorformat!=bc', function ( assert ) { + var api = new mw.Api(); + + this.server.respond( [ 200, { 'Content-Type': 'application/json' }, + '{ "errors": [ { "code": "unknown_action", "key": "unknown-error", "params": [] } ] }' + ] ); + + api.get( { action: 'doesntexist' } ) + .fail( function ( errorCode ) { + assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' ); + } ) + .always( assert.async() ); + } ); + + QUnit.test( 'FormData support', function ( assert ) { + var api = new mw.Api(); + + this.server.respond( function ( request ) { + if ( window.FormData ) { + assert.ok( !request.url.match( /action=/ ), 'Request has no query string' ); + assert.ok( request.requestBody instanceof FormData, 'Request uses FormData body' ); + } else { + assert.ok( !request.url.match( /action=test/ ), 'Request has no query string' ); + assert.equal( request.requestBody, 'action=test&format=json', 'Request uses query string body' ); + } + request.respond( 200, { 'Content-Type': 'application/json' }, '[]' ); + } ); + + return api.post( { action: 'test' }, { contentType: 'multipart/form-data' } ); + } ); + + QUnit.test( 'Converting arrays to pipe-separated (string)', function ( assert ) { + var api = new mw.Api(); + + this.server.respond( function ( request ) { + assert.equal( match( request.url, /test=([^&]+)/ ), 'foo%7Cbar%7Cbaz', 'Pipe-separated value was submitted' ); + request.respond( 200, { 'Content-Type': 'application/json' }, '[]' ); + } ); + + return api.get( { test: [ 'foo', 'bar', 'baz' ] } ); + } ); + + QUnit.test( 'Converting arrays to pipe-separated (mw.Title)', function ( assert ) { + var api = new mw.Api(); + + this.server.respond( function ( request ) { + assert.equal( match( request.url, /test=([^&]+)/ ), 'Foo%7CBar', 'Pipe-separated value was submitted' ); + request.respond( 200, { 'Content-Type': 'application/json' }, '[]' ); + } ); + + return api.get( { test: [ new mw.Title( 'Foo' ), new mw.Title( 'Bar' ) ] } ); + } ); + + QUnit.test( 'Converting arrays to pipe-separated (misc primitives)', function ( assert ) { + var api = new mw.Api(); + + this.server.respond( function ( request ) { + assert.equal( match( request.url, /test=([^&]+)/ ), 'true%7Cfalse%7C%7C%7C0%7C1%2E2', 'Pipe-separated value was submitted' ); + request.respond( 200, { 'Content-Type': 'application/json' }, '[]' ); + } ); + + // undefined/null will become empty string + return api.get( { test: [ true, false, undefined, null, 0, 1.2 ] } ); + } ); + + QUnit.test( 'Omitting false booleans', function ( assert ) { + var api = new mw.Api(); + + this.server.respond( function ( request ) { + assert.ok( !request.url.match( /foo/ ), 'foo query parameter is not present' ); + assert.ok( request.url.match( /bar=true/ ), 'bar query parameter is present with value true' ); + request.respond( 200, { 'Content-Type': 'application/json' }, '[]' ); + } ); + + return api.get( { foo: false, bar: true } ); + } ); + + QUnit.test( 'getToken() - cached', function ( assert ) { + var api = new mw.Api(), + test = this; + + // Get csrfToken for local wiki, this should not make + // a request as it should be retrieved from mw.user.tokens. + return api.getToken( 'csrf' ) + .then( function ( token ) { + assert.ok( token.length, 'Got a token' ); + }, function ( err ) { + assert.equal( '', err, 'API error' ); + } ) + .then( function () { + assert.equal( test.server.requests.length, 0, 'Requests made' ); + } ); + } ); + + QUnit.test( 'getToken() - uncached', function ( assert ) { + var api = new mw.Api(), + firstDone = assert.async(), + secondDone = assert.async(); + + this.server.respondWith( /type=testuncached/, [ 200, { 'Content-Type': 'application/json' }, + '{ "query": { "tokens": { "testuncachedtoken": "good" } } }' + ] ); + + // Get a token of a type that isn't prepopulated by user.tokens. + // Could use "block" or "delete" here, but those could in theory + // be added to user.tokens, use a fake one instead. + api.getToken( 'testuncached' ) + .done( function ( token ) { + assert.equal( token, 'good', 'The token' ); + } ) + .fail( function ( err ) { + assert.equal( err, '', 'API error' ); + } ) + .always( firstDone ); + + api.getToken( 'testuncached' ) + .done( function ( token ) { + assert.equal( token, 'good', 'The cached token' ); + } ) + .fail( function ( err ) { + assert.equal( err, '', 'API error' ); + } ) + .always( secondDone ); + + assert.equal( this.server.requests.length, 1, 'Requests made' ); + } ); + + QUnit.test( 'getToken() - error', function ( assert ) { + var api = new mw.Api(); + + this.server.respondWith( /type=testerror/, sequenceBodies( 200, { 'Content-Type': 'application/json' }, + [ + '{ "error": { "code": "bite-me", "info": "Smite me, O Mighty Smiter" } }', + '{ "query": { "tokens": { "testerrortoken": "good" } } }' + ] + ) ); + + // Don't cache error (T67268) + return api.getToken( 'testerror' ) + .catch( function ( err ) { + assert.equal( err, 'bite-me', 'Expected error' ); + + return api.getToken( 'testerror' ); + } ) + .then( function ( token ) { + assert.equal( token, 'good', 'The token' ); + } ); + } ); + + QUnit.test( 'getToken() - deprecated', function ( assert ) { + // Cache API endpoint from default to avoid cachehit in mw.user.tokens + var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } ), + test = this; + + this.server.respondWith( /type=csrf/, [ 200, { 'Content-Type': 'application/json' }, + '{ "query": { "tokens": { "csrftoken": "csrfgood" } } }' + ] ); + + // Get a token of a type that is in the legacy map. + return api.getToken( 'email' ) + .done( function ( token ) { + assert.equal( token, 'csrfgood', 'Token' ); + } ) + .fail( function ( err ) { + assert.equal( err, '', 'API error' ); + } ) + .always( function () { + assert.equal( test.server.requests.length, 1, 'Requests made' ); + } ); + } ); + + QUnit.test( 'badToken()', function ( assert ) { + var api = new mw.Api(), + test = this; + + this.server.respondWith( /type=testbad/, sequenceBodies( 200, { 'Content-Type': 'application/json' }, + [ + '{ "query": { "tokens": { "testbadtoken": "bad" } } }', + '{ "query": { "tokens": { "testbadtoken": "good" } } }' + ] + ) ); + + return api.getToken( 'testbad' ) + .then( function () { + api.badToken( 'testbad' ); + return api.getToken( 'testbad' ); + } ) + .then( function ( token ) { + assert.equal( token, 'good', 'The token' ); + assert.equal( test.server.requests.length, 2, 'Requests made' ); + } ); + + } ); + + QUnit.test( 'badToken( legacy )', function ( assert ) { + var api = new mw.Api( { ajax: { url: '/badTokenLegacy/api.php' } } ), + test = this; + + this.server.respondWith( /type=csrf/, sequenceBodies( 200, { 'Content-Type': 'application/json' }, + [ + '{ "query": { "tokens": { "csrftoken": "badlegacy" } } }', + '{ "query": { "tokens": { "csrftoken": "goodlegacy" } } }' + ] + ) ); + + return api.getToken( 'options' ) + .then( function () { + api.badToken( 'options' ); + return api.getToken( 'options' ); + } ) + .then( function ( token ) { + assert.equal( token, 'goodlegacy', 'The token' ); + assert.equal( test.server.requests.length, 2, 'Request made' ); + } ); + + } ); + + QUnit.test( 'postWithToken( tokenType, params )', function ( assert ) { + var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } ); + + this.server.respondWith( 'GET', /type=testpost/, [ 200, { 'Content-Type': 'application/json' }, + '{ "query": { "tokens": { "testposttoken": "good" } } }' + ] ); + this.server.respondWith( 'POST', /api/, function ( request ) { + if ( request.requestBody.match( /token=good/ ) ) { + request.respond( 200, { 'Content-Type': 'application/json' }, + '{ "example": { "foo": "quux" } }' + ); + } + } ); + + return api.postWithToken( 'testpost', { action: 'example', key: 'foo' } ) + .then( function ( data ) { + assert.deepEqual( data, { example: { foo: 'quux' } } ); + } ); + } ); + + QUnit.test( 'postWithToken( tokenType, params with assert )', function ( assert ) { + var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } ), + test = this; + + this.server.respondWith( /assert=user/, [ 200, { 'Content-Type': 'application/json' }, + '{ "error": { "code": "assertuserfailed", "info": "Assertion failed" } }' + ] ); + + return api.postWithToken( 'testassertpost', { action: 'example', key: 'foo', assert: 'user' } ) + // Cast error to success and vice versa + .then( function () { + return $.Deferred().reject( 'Unexpected success' ); + }, function ( errorCode ) { + assert.equal( errorCode, 'assertuserfailed', 'getToken fails assert' ); + return $.Deferred().resolve(); + } ) + .then( function () { + assert.equal( test.server.requests.length, 1, 'Requests made' ); + } ); + } ); + + QUnit.test( 'postWithToken( tokenType, params, ajaxOptions )', function ( assert ) { + var api = new mw.Api(), + test = this; + + this.server.respond( [ 200, { 'Content-Type': 'application/json' }, '{ "example": "quux" }' ] ); + + return api.postWithToken( 'csrf', + { action: 'example' }, + { + headers: { + 'X-Foo': 'Bar' + } + } + ).then( function () { + assert.equal( test.server.requests[ 0 ].requestHeaders[ 'X-Foo' ], 'Bar', 'Header sent' ); + + return api.postWithToken( 'csrf', + { action: 'example' }, + function () { + assert.ok( false, 'This parameter cannot be a callback' ); + } + ); + } ).then( function ( data ) { + assert.equal( data.example, 'quux' ); + + assert.equal( test.server.requests.length, 2, 'Request made' ); + } ); + } ); + + QUnit.test( 'postWithToken() - badtoken', function ( assert ) { + var api = new mw.Api(); + + this.server.respondWith( /type=testbadtoken/, sequenceBodies( 200, { 'Content-Type': 'application/json' }, + [ + '{ "query": { "tokens": { "testbadtokentoken": "bad" } } }', + '{ "query": { "tokens": { "testbadtokentoken": "good" } } }' + ] + ) ); + this.server.respondWith( 'POST', /api/, function ( request ) { + if ( request.requestBody.match( /token=bad/ ) ) { + request.respond( 200, { 'Content-Type': 'application/json' }, + '{ "error": { "code": "badtoken" } }' + ); + } + if ( request.requestBody.match( /token=good/ ) ) { + request.respond( 200, { 'Content-Type': 'application/json' }, + '{ "example": { "foo": "quux" } }' + ); + } + } ); + + // - Request: new token -> bad + // - Request: action=example -> badtoken error + // - Request: new token -> good + // - Request: action=example -> success + return api.postWithToken( 'testbadtoken', { action: 'example', key: 'foo' } ) + .then( function ( data ) { + assert.deepEqual( data, { example: { foo: 'quux' } } ); + } ); + } ); + + QUnit.test( 'postWithToken() - badtoken-cached', function ( assert ) { + var sequenceA, + api = new mw.Api(); + + this.server.respondWith( /type=testonce/, sequenceBodies( 200, { 'Content-Type': 'application/json' }, + [ + '{ "query": { "tokens": { "testoncetoken": "good-A" } } }', + '{ "query": { "tokens": { "testoncetoken": "good-B" } } }' + ] + ) ); + sequenceA = sequenceBodies( 200, { 'Content-Type': 'application/json' }, + [ + '{ "example": { "value": "A" } }', + '{ "error": { "code": "badtoken" } }' + ] + ); + this.server.respondWith( 'POST', /api/, function ( request ) { + if ( request.requestBody.match( /token=good-A/ ) ) { + sequenceA( request ); + } else if ( request.requestBody.match( /token=good-B/ ) ) { + request.respond( 200, { 'Content-Type': 'application/json' }, + '{ "example": { "value": "B" } }' + ); + } + } ); + + // - Request: new token -> A + // - Request: action=example + return api.postWithToken( 'testonce', { action: 'example', key: 'foo' } ) + .then( function ( data ) { + assert.deepEqual( data, { example: { value: 'A' } } ); + + // - Request: action=example w/ token A -> badtoken error + // - Request: new token -> B + // - Request: action=example w/ token B -> success + return api.postWithToken( 'testonce', { action: 'example', key: 'bar' } ); + } ) + .then( function ( data ) { + assert.deepEqual( data, { example: { value: 'B' } } ); + } ); + } ); + + QUnit.module( 'mediawiki.api (2)', { + setup: function () { + var self = this, + requests = this.requests = []; + this.api = new mw.Api(); + this.sandbox.stub( jQuery, 'ajax', function () { + var request = $.extend( { + abort: self.sandbox.spy() + }, $.Deferred() ); + requests.push( request ); + return request; + } ); + } + } ); + + QUnit.test( '#abort', function ( assert ) { + this.api.get( { + a: 1 + } ); + this.api.post( { + b: 2 + } ); + this.api.abort(); + assert.ok( this.requests.length === 2, 'Check both requests triggered' ); + this.requests.forEach( function ( request, i ) { + assert.ok( request.abort.calledOnce, 'abort request number ' + i ); + } ); + } ); +}( mediaWiki, jQuery ) ); diff --git a/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.upload.test.js b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.upload.test.js new file mode 100644 index 00000000..788a427e --- /dev/null +++ b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.upload.test.js @@ -0,0 +1,33 @@ +( function ( mw, $ ) { + QUnit.module( 'mediawiki.api.upload', QUnit.newMwEnvironment( {} ) ); + + QUnit.test( 'Basic functionality', function ( assert ) { + var api = new mw.Api(); + assert.ok( api.upload ); + assert.throws( function () { + api.upload(); + } ); + } ); + + QUnit.test( 'Set up iframe upload', function ( assert ) { + var $iframe, $form, $input, + api = new mw.Api(); + + this.sandbox.stub( api, 'getEditToken', function () { + return $.Deferred().promise(); + } ); + + api.uploadWithIframe( $( '<input>' )[ 0 ], { filename: 'Testing API upload.jpg' } ); + + $iframe = $( 'iframe:last-child' ); + $form = $( 'form.mw-api-upload-form' ); + $input = $form.find( 'input[name=filename]' ); + + assert.ok( $form.length > 0, 'form' ); + assert.ok( $input.length > 0, 'input' ); + assert.ok( $iframe.length > 0, 'frame' ); + assert.strictEqual( $form.prop( 'target' ), $iframe.prop( 'id' ), 'form.target and frame.id ' ); + assert.strictEqual( $input.val(), 'Testing API upload.jpg', 'input value' ); + } ); + +}( mediaWiki, jQuery ) ); diff --git a/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.watch.test.js b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.watch.test.js new file mode 100644 index 00000000..86414691 --- /dev/null +++ b/www/wiki/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.watch.test.js @@ -0,0 +1,60 @@ +( function ( mw ) { + QUnit.module( 'mediawiki.api.watch', QUnit.newMwEnvironment( { + setup: function () { + this.server = this.sandbox.useFakeServer(); + this.server.respondImmediately = true; + } + } ) ); + + QUnit.test( '.watch( string )', function ( assert ) { + this.server.respond( function ( req ) { + // Match POST requestBody + if ( /action=watch.*&titles=Foo(&|$)/.test( req.requestBody ) ) { + req.respond( 200, { 'Content-Type': 'application/json' }, + '{ "watch": [ { "title": "Foo", "watched": true, "message": "<b>Added</b>" } ] }' + ); + } + } ); + + return new mw.Api().watch( 'Foo' ).done( function ( item ) { + assert.equal( item.title, 'Foo' ); + } ); + } ); + + // Ensure we don't mistake a single item array for a single item and vice versa. + // The query parameter in request is the same either way (separated by pipe). + QUnit.test( '.watch( Array ) - single', function ( assert ) { + this.server.respond( function ( req ) { + // Match POST requestBody + if ( /action=watch.*&titles=Foo(&|$)/.test( req.requestBody ) ) { + req.respond( 200, { 'Content-Type': 'application/json' }, + '{ "watch": [ { "title": "Foo", "watched": true, "message": "<b>Added</b>" } ] }' + ); + } + } ); + + return new mw.Api().watch( [ 'Foo' ] ).done( function ( items ) { + assert.equal( items[ 0 ].title, 'Foo' ); + } ); + } ); + + QUnit.test( '.watch( Array ) - multi', function ( assert ) { + this.server.respond( function ( req ) { + // Match POST requestBody + if ( /action=watch.*&titles=Foo%7CBar/.test( req.requestBody ) ) { + req.respond( 200, { 'Content-Type': 'application/json' }, + '{ "watch": [ ' + + '{ "title": "Foo", "watched": true, "message": "<b>Added</b>" },' + + '{ "title": "Bar", "watched": true, "message": "<b>Added</b>" }' + + '] }' + ); + } + } ); + + return new mw.Api().watch( [ 'Foo', 'Bar' ] ).done( function ( items ) { + assert.equal( items[ 0 ].title, 'Foo' ); + assert.equal( items[ 1 ].title, 'Bar' ); + } ); + } ); + +}( mediaWiki ) ); |