mediawiki.api: Check that query exists in api.getToken() response
authorStephen Niedzielski <stephen@niedzielski.com>
Fri, 9 Mar 2018 15:30:52 +0000 (09:30 -0600)
committerKrinkle <krinklemail@gmail.com>
Tue, 1 May 2018 20:37:49 +0000 (20:37 +0000)
It's possible that response.query does not exist when requesting a
token. This patch removes the assumption and handles the scenario in the
same way as a missing token.

Additionally, responses with a queryless response are now deleted from the cache.

Corresponding console warning:

> jQuery.Deferred exception: Cannot read property 'tokens' of undefined
>   at apiPromise.then.promise.abort (/w/resources/src/mediawiki/api.js)

Change-Id: Ia29e0c0d657bf4b3d94f1d463b942451eebd68b4

resources/src/mediawiki/api.js
tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js

index 2e5a92e..0038ed8 100644 (file)
                 * @return {jQuery.Promise} Received token.
                 */
                getToken: function ( type, assert ) {
-                       var apiPromise, promiseGroup, d;
+                       var apiPromise, promiseGroup, d, reject;
                        type = mapLegacyToken( type );
                        promiseGroup = promises[ this.defaults.ajax.url ];
                        d = promiseGroup && promiseGroup[ type + 'Token' ];
                                        type: type,
                                        assert: assert
                                } );
+                               reject = function () {
+                                       // Clear promise. Do not cache errors.
+                                       delete promiseGroup[ type + 'Token' ];
+
+                                       // Let caller handle the error code
+                                       return $.Deferred().rejectWith( this, arguments );
+                               };
                                d = apiPromise
                                        .then( function ( res ) {
+                                               if ( !res.query ) {
+                                                       return reject( 'query-missing', res );
+                                               }
                                                // If token type is unknown, it is omitted from the response
                                                if ( !res.query.tokens[ type + 'token' ] ) {
                                                        return $.Deferred().reject( 'token-missing', res );
                                                }
-
                                                return res.query.tokens[ type + 'token' ];
-                                       }, function () {
-                                               // Clear promise. Do not cache errors.
-                                               delete promiseGroup[ type + 'Token' ];
-
-                                               // Let caller handle the error code
-                                               return $.Deferred().rejectWith( this, arguments );
-                                       } )
+                                       }, reject )
                                        // Attach abort handler
                                        .promise( { abort: apiPromise.abort } );
 
index 417ad3d..7431b29 100644 (file)
                        } );
        } );
 
+       QUnit.test( 'getToken() - no query', function ( assert ) {
+               var api = new mw.Api(),
+                       // Same-origin warning and missing query in response.
+                       serverRsp = {
+                               warnings: {
+                                       tokens: {
+                                               '*': 'Tokens may not be obtained when the same-origin policy is not applied.'
+                                       }
+                               }
+                       };
+
+               this.server.respondWith( /type=testnoquery/, [ 200, { 'Content-Type': 'application/json' },
+                       JSON.stringify( serverRsp )
+               ] );
+
+               return api.getToken( 'testnoquery' )
+                       .then( function () { assert.fail( 'Expected response missing a query to be rejected' ); } )
+                       .catch( function ( err, rsp ) {
+                               assert.equal( err, 'query-missing', 'Expected no query error code' );
+                               assert.deepEqual( rsp, serverRsp );
+                       } );
+       } );
+
        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' } } ),