mediawiki.api: Fix API postWithToken method
authorjrobson <jrobson@wikimedia.org>
Tue, 29 Apr 2014 22:42:43 +0000 (15:42 -0700)
committerTimo Tijhof <krinklemail@gmail.com>
Mon, 5 May 2014 21:04:30 +0000 (23:04 +0200)
Referred to "this" instead of "api" ("this" here refers to the Deferred object).
Fixes "Uncaught TypeError: Cannot read property 'ajax' of undefined".

Change-Id: I78ba47a614512f6218e23d03e7c688e2c9efbe45

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

index b37e2a6..9962534 100644 (file)
                                        function ( code ) {
                                                if ( code === 'badtoken' ) {
                                                        // Clear from cache
-                                                       deferreds[ this.defaults.ajax.url ][ tokenType + 'Token' ] =
+                                                       deferreds[ api.defaults.ajax.url ][ tokenType + 'Token' ] =
                                                                params.token = undefined;
 
                                                        // Try again, once
index a93f572..05eb6b9 100644 (file)
                } );
        } );
 
+       QUnit.test( 'postWithToken()', function ( assert ) {
+               QUnit.expect( 1 );
+
+               var api = new mw.Api( { ajax: { url: '/postWithToken/api.php' } } );
+
+               // - Requests token
+               // - Performs action=example
+               api.postWithToken( 'testsimpletoken', { action: 'example', key: 'foo' } )
+                       .done( function ( data ) {
+                               assert.deepEqual( data, { example: { foo: 'quux' } } );
+                       } );
+
+               this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' },
+                       '{ "tokens": { "testsimpletokentoken": "a-bad-token" } }'
+               );
+
+               this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' },
+                       '{ "example": { "foo": "quux" } }'
+               );
+       } );
+
+       QUnit.test( 'postWithToken() - badtoken', function ( assert ) {
+               QUnit.expect( 1 );
+
+               var api = new mw.Api();
+
+               // - Request: token
+               // - Request: action=example -> badtoken error
+               // - Request: new token
+               // - Request: action=example
+               api.postWithToken( 'testbadtoken', { action: 'example', key: 'foo' } )
+                       .done( function ( data ) {
+                               assert.deepEqual( data, { example: { foo: 'quux' } } );
+                       } );
+
+               this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' },
+                       '{ "tokens": { "testbadtokentoken": "a-bad-token" } }'
+               );
+
+               this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' },
+                       '{ "error": { "code": "badtoken" } }'
+               );
+
+               this.server.requests[2].respond( 200, { 'Content-Type': 'application/json' },
+                       '{ "tokens": { "testbadtokentoken": "a-good-token" } }'
+               );
+
+               this.server.requests[3].respond( 200, { 'Content-Type': 'application/json' },
+                       '{ "example": { "foo": "quux" } }'
+               );
+
+       } );
+
+       QUnit.test( 'postWithToken() - badtoken-cached', function ( assert ) {
+               QUnit.expect( 2 );
+
+               var api = new mw.Api();
+
+               // - Request: token
+               // - Request: action=example
+               api.postWithToken( 'testbadtokencache', { action: 'example', key: 'foo' } )
+                       .done( function ( data ) {
+                               assert.deepEqual( data, { example: { foo: 'quux' } } );
+                       } );
+
+               // - Cache: Try previously cached token
+               // - Request: action=example -> badtoken error
+               // - Request: new token
+               // - Request: action=example
+               api.postWithToken( 'testbadtokencache', { action: 'example', key: 'bar' } )
+                       .done( function ( data ) {
+                               assert.deepEqual( data, { example: { bar: 'quux' } } );
+                       } );
+
+               this.server.requests[0].respond( 200, { 'Content-Type': 'application/json' },
+                       '{ "tokens": { "testbadtokencachetoken": "a-good-token-once" } }'
+               );
+
+               this.server.requests[1].respond( 200, { 'Content-Type': 'application/json' },
+                       '{ "example": { "foo": "quux" } }'
+               );
+
+               this.server.requests[2].respond( 200, { 'Content-Type': 'application/json' },
+                       '{ "error": { "code": "badtoken" } }'
+               );
+
+               this.server.requests[3].respond( 200, { 'Content-Type': 'application/json' },
+                       '{ "tokens": { "testbadtokencachetoken": "a-good-new-token" } }'
+               );
+
+               this.server.requests[4].respond( 200, { 'Content-Type': 'application/json' },
+                       '{ "example": { "bar": "quux" } }'
+               );
+
+       } );
+
 }( mediaWiki ) );