Debian Codegraph @

tests/qunit/resources/mediawiki.api/mediawiki.api.options.test.js

raw symbols SCIP
1 QUnit.module( 'mediawiki.api.options', QUnit.newMwEnvironment(), ( hooks ) => {
2 mw.config.set( {
3 wgUserName: 'Foo'
4 } );
5
6 hooks.beforeEach( function () {
7 this.server = this.sandbox.useFakeServer();
8 this.server.respondImmediately = true;
9 } );
10
11 QUnit.test( 'saveOption', function ( assert ) {
12 const api = new mw.Api(),
13 stub = this.sandbox.stub( mw.Api.prototype, 'saveOptions' );
14
15 api.saveOption( 'foo', 'bar' );
16
17 assert.true( stub.calledOnce, '#saveOptions called once' );
18 assert.deepEqual( stub.getCall( 0 ).args, [ { foo: 'bar' }, undefined ], '#saveOptions called correctly' );
19 } );
20
21 QUnit.test( 'saveOptions without Unit Separator', async function ( assert ) {
22 const api = new mw.Api( { useUS: false } );
23
24 // We need to respond to the request for token first, otherwise the other requests won't be sent
25 // until after the server.respond call, which confuses sinon terribly. This sucks a lot.
26 api.badToken( 'csrf' );
27 api.getToken( 'csrf' );
28 this.server.respond(
29 /meta=tokens&type=csrf/,
30 [ 200, { 'Content-Type': 'application/json' },
31 '{ "query": { "tokens": { "csrftoken": "+\\\\" } } }' ]
32 );
33
34 // Requests are POST, match requestBody instead of url
35 this.server.respond( ( request ) => {
36 if ( !request.requestBody ) {
37 // GET request for the token, already responded above
38 } else if ( [
39 // simple
40 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C',
41 // two options
42 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C',
43 // not bundleable
44 'action=options&format=json&formatversion=2&optionname=foo&optionvalue=bar%7Cquux&token=%2B%5C',
45 'action=options&format=json&formatversion=2&optionname=bar&optionvalue=a%7Cb%7Cc&token=%2B%5C',
46 'action=options&format=json&formatversion=2&change=baz%3Dquux&token=%2B%5C',
47 // reset an option
48 'action=options&format=json&formatversion=2&change=foo&token=%2B%5C',
49 // reset an option, not bundleable
50 'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C'
51 ].includes( request.requestBody ) ) {
52 assert.true( true, 'Repond to ' + request.requestBody );
53 request.respond( 200, { 'Content-Type': 'application/json' },
54 '{ "options": "success" }' );
55 } else {
56 assert.true( false, 'Unexpected request: ' + request.requestBody );
57 }
58 } );
59
60 // empty case
61 await api.saveOptions( {} );
62 // simple
63 await api.saveOptions( { foo: 'bar' } );
64 // two options
65 await api.saveOptions( { foo: 'bar', baz: 'quux' } );
66 // not bundleable
67 await api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', baz: 'quux' } );
68 // reset an option
69 await api.saveOptions( { foo: null } );
70 // reset an option, not bundleable
71 await api.saveOptions( { 'foo|bar=quux': null } );
72 } );
73
74 QUnit.test( 'saveOptions with Unit Separator', async function ( assert ) {
75 const api = new mw.Api( { useUS: true } );
76
77 // We need to respond to the request for token first, otherwise the other requests won't be sent
78 // until after the server.respond call, which confuses sinon terribly. This sucks a lot.
79 api.badToken( 'csrf' );
80 api.getToken( 'csrf' );
81 this.server.respond(
82 /meta=tokens&type=csrf/,
83 [ 200, { 'Content-Type': 'application/json' },
84 '{ "query": { "tokens": { "csrftoken": "+\\\\" } } }' ]
85 );
86
87 // Requests are POST, match requestBody instead of url
88 this.server.respond( ( request ) => {
89 if ( !request.requestBody ) {
90 // GET request for the token, already responded above
91 } else if ( [
92 // simple
93 'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C',
94 // two options
95 'action=options&format=json&formatversion=2&change=foo%3Dbar%7Cbaz%3Dquux&token=%2B%5C',
96 // bundleable with unit separator
97 'action=options&format=json&formatversion=2&change=%1Ffoo%3Dbar%7Cquux%1Fbar%3Da%7Cb%7Cc%1Fbaz%3Dquux&token=%2B%5C',
98 // not bundleable with unit separator
99 'action=options&format=json&formatversion=2&optionname=baz%3Dbaz&optionvalue=quux&token=%2B%5C',
100 'action=options&format=json&formatversion=2&change=%1Ffoo%3Dbar%7Cquux%1Fbar%3Da%7Cb%7Cc&token=%2B%5C',
101 // reset an option
102 'action=options&format=json&formatversion=2&change=foo&token=%2B%5C',
103 // reset an option, not bundleable
104 'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C'
105 ].includes( request.requestBody ) ) {
106 assert.true( true, 'Repond to ' + request.requestBody );
107 request.respond(
108 200,
109 { 'Content-Type': 'application/json' },
110 '{ "options": "success" }'
111 );
112 } else {
113 assert.true( false, 'Unexpected request: ' + request.requestBody );
114 }
115 } );
116
117 // empty case
118 await api.saveOptions( {} );
119 // simple
120 await api.saveOptions( { foo: 'bar' } );
121 // two options
122 await api.saveOptions( { foo: 'bar', baz: 'quux' } );
123 // bundleable with unit separator
124 await api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', baz: 'quux' } );
125 // not bundleable with unit separator
126 await api.saveOptions( { foo: 'bar|quux', bar: 'a|b|c', 'baz=baz': 'quux' } );
127 // reset an option
128 await api.saveOptions( { foo: null } );
129 // reset an option, not bundleable
130 await api.saveOptions( { 'foo|bar=quux': null } );
131 } );
132
133 QUnit.test( 'saveOptions (anonymous)', async function ( assert ) {
134 mw.config.set( 'wgUserName', null );
135
136 await assert.rejects(
137 new mw.Api().saveOptions( { foo: 'bar' } ),
138 /notloggedin/,
139 'Can not save options while not logged in'
140 );
141
142 assert.strictEqual( this.server.requests.length, 0, 'No requests made' );
143 } );
144 } );