05eb6b988e7ace5792ec01853e0ff1f5b6c08e33
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki.api / mediawiki.api.test.js
1 ( function ( mw ) {
2 QUnit.module( 'mediawiki.api', QUnit.newMwEnvironment( {
3 setup: function () {
4 this.server = this.sandbox.useFakeServer();
5 }
6 } ) );
7
8 QUnit.test( 'Basic functionality', function ( assert ) {
9 QUnit.expect( 2 );
10
11 var api = new mw.Api();
12
13 api.get( {} )
14 .done( function ( data ) {
15 assert.deepEqual( data, [], 'If request succeeds without errors, resolve deferred' );
16 } );
17
18 api.post( {} )
19 .done( function ( data ) {
20 assert.deepEqual( data, [], 'Simple POST request' );
21 } );
22
23 this.server.respond( function ( request ) {
24 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
25 } );
26 } );
27
28 QUnit.test( 'API error', function ( assert ) {
29 QUnit.expect( 1 );
30
31 var api = new mw.Api();
32
33 api.get( { action: 'doesntexist' } )
34 .fail( function ( errorCode ) {
35 assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' );
36 } );
37
38 this.server.respond( function ( request ) {
39 request.respond( 200, { 'Content-Type': 'application/json' },
40 '{ "error": { "code": "unknown_action" } }'
41 );
42 } );
43 } );
44
45 QUnit.test( 'FormData support', function ( assert ) {
46 QUnit.expect( 2 );
47
48 var api = new mw.Api();
49
50 api.post( { action: 'test' }, { contentType: 'multipart/form-data' } );
51
52 this.server.respond( function ( request ) {
53 if ( window.FormData ) {
54 assert.ok( !request.url.match( /action=/), 'Request has no query string' );
55 assert.ok( request.requestBody instanceof FormData, 'Request uses FormData body' );
56 } else {
57 assert.ok( !request.url.match( /action=test/), 'Request has no query string' );
58 assert.equal( request.requestBody, 'action=test&format=json', 'Request uses query string body' );
59 }
60 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
61 } );
62 } );
63
64 QUnit.test( 'Deprecated callback methods', function ( assert ) {
65 QUnit.expect( 3 );
66
67 var api = new mw.Api();
68
69 this.suppressWarnings();
70
71 api.get( {}, function () {
72 assert.ok( true, 'Function argument treated as success callback.' );
73 } );
74
75 api.get( {}, {
76 ok: function () {
77 assert.ok( true, '"ok" property treated as success callback.' );
78 }
79 } );
80
81 api.get( { action: 'doesntexist' }, {
82 err: function () {
83 assert.ok( true, '"err" property treated as error callback.' );
84 }
85 } );
86
87 this.restoreWarnings();
88
89 this.server.respondWith( /action=query/, function ( request ) {
90 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
91 } );
92
93 this.server.respondWith( /action=doesntexist/, function ( request ) {
94 request.respond( 200, { 'Content-Type': 'application/json' },
95 '{ "error": { "code": "unknown_action" } }'
96 );
97 } );
98
99 this.server.respond();
100 } );
101
102 QUnit.test( 'getToken( cached )', function ( assert ) {
103 QUnit.expect( 2 );
104
105 var api = new mw.Api();
106
107 // Get editToken for local wiki, this should not make
108 // a request as it should be retrieved from user.tokens.
109 api.getToken( 'edit' )
110 .done( function ( token ) {
111 assert.ok( token.length, 'Got a token' );
112 } )
113 .fail( function ( err ) {
114 assert.equal( '', err, 'API error' );
115 } );
116
117 assert.equal( this.server.requests.length, 0, 'Requests made' );
118 } );
119
120 QUnit.test( 'getToken( uncached )', function ( assert ) {
121 QUnit.expect( 2 );
122
123 var api = new mw.Api();
124
125 // Get a token of a type that isn't prepopulated by user.tokens.
126 // Could use "block" or "delete" here, but those could in theory
127 // be added to user.tokens, use a fake one instead.
128 api.getToken( 'testaction' )
129 .done( function ( token ) {
130 assert.ok( token.length, 'Got a token' );
131 } )
132 .fail( function ( err ) {
133 assert.equal( '', err, 'API error' );
134 } );
135
136 assert.equal( this.server.requests.length, 1, 'Requests made' );
137
138 this.server.respond( function ( request ) {
139 request.respond( 200, { 'Content-Type': 'application/json' },
140 '{ "tokens": { "testactiontoken": "0123abc" } }'
141 );
142 } );
143 } );
144
145 QUnit.test( 'postWithToken()', function ( assert ) {
146 QUnit.expect( 1 );
147
148 var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
149
150 // - Requests token
151 // - Performs action=example
152 api.postWithToken( 'testsimpletoken', { action: 'example', key: 'foo' } )
153 .done( function ( data ) {
154 assert.deepEqual( data, { example: { foo: 'quux' } } );
155 } );
156
157 this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' },
158 '{ "tokens": { "testsimpletokentoken": "a-bad-token" } }'
159 );
160
161 this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' },
162 '{ "example": { "foo": "quux" } }'
163 );
164 } );
165
166 QUnit.test( 'postWithToken() - badtoken', function ( assert ) {
167 QUnit.expect( 1 );
168
169 var api = new mw.Api();
170
171 // - Request: token
172 // - Request: action=example -> badtoken error
173 // - Request: new token
174 // - Request: action=example
175 api.postWithToken( 'testbadtoken', { action: 'example', key: 'foo' } )
176 .done( function ( data ) {
177 assert.deepEqual( data, { example: { foo: 'quux' } } );
178 } );
179
180 this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' },
181 '{ "tokens": { "testbadtokentoken": "a-bad-token" } }'
182 );
183
184 this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' },
185 '{ "error": { "code": "badtoken" } }'
186 );
187
188 this.server.requests[2].respond( 200, { 'Content-Type': 'application/json' },
189 '{ "tokens": { "testbadtokentoken": "a-good-token" } }'
190 );
191
192 this.server.requests[3].respond( 200, { 'Content-Type': 'application/json' },
193 '{ "example": { "foo": "quux" } }'
194 );
195
196 } );
197
198 QUnit.test( 'postWithToken() - badtoken-cached', function ( assert ) {
199 QUnit.expect( 2 );
200
201 var api = new mw.Api();
202
203 // - Request: token
204 // - Request: action=example
205 api.postWithToken( 'testbadtokencache', { action: 'example', key: 'foo' } )
206 .done( function ( data ) {
207 assert.deepEqual( data, { example: { foo: 'quux' } } );
208 } );
209
210 // - Cache: Try previously cached token
211 // - Request: action=example -> badtoken error
212 // - Request: new token
213 // - Request: action=example
214 api.postWithToken( 'testbadtokencache', { action: 'example', key: 'bar' } )
215 .done( function ( data ) {
216 assert.deepEqual( data, { example: { bar: 'quux' } } );
217 } );
218
219 this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' },
220 '{ "tokens": { "testbadtokencachetoken": "a-good-token-once" } }'
221 );
222
223 this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' },
224 '{ "example": { "foo": "quux" } }'
225 );
226
227 this.server.requests[2].respond( 200, { 'Content-Type': 'application/json' },
228 '{ "error": { "code": "badtoken" } }'
229 );
230
231 this.server.requests[3].respond( 200, { 'Content-Type': 'application/json' },
232 '{ "tokens": { "testbadtokencachetoken": "a-good-new-token" } }'
233 );
234
235 this.server.requests[4].respond( 200, { 'Content-Type': 'application/json' },
236 '{ "example": { "bar": "quux" } }'
237 );
238
239 } );
240
241 }( mediaWiki ) );