File size: 6,380 Bytes
7e79d67 | 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 | ( function () {
QUnit.module( 'mediawiki.api.options', QUnit.newMwEnvironment( {
config: {
wgUserName: 'Foo'
},
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' );
} )
);
} );
QUnit.test( 'saveOptions (anonymous)', function ( assert ) {
var promise, test = this;
mw.config.set( 'wgUserName', null );
promise = new mw.Api().saveOptions( { foo: 'bar' } );
assert.rejects( promise, /notloggedin/, 'Can not save options while not logged in' );
return promise
.catch( function () {
return $.Deferred().resolve();
} )
.then( function () {
assert.strictEqual( test.server.requests.length, 0, 'No requests made' );
} );
} );
}() );
|