mediawiki.api: Refactor getToken and postWithToken methods
[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.clock = this.sandbox.useFakeTimers();
5 this.server = this.sandbox.useFakeServer();
6 },
7 teardown: function () {
8 this.clock.tick( 1 );
9 }
10 } ) );
11
12 QUnit.test( 'Basic functionality', function ( assert ) {
13 QUnit.expect( 2 );
14
15 var api = new mw.Api();
16
17 api.get( {} )
18 .done( function ( data ) {
19 assert.deepEqual( data, [], 'If request succeeds without errors, resolve deferred' );
20 } );
21
22 api.post( {} )
23 .done( function ( data ) {
24 assert.deepEqual( data, [], 'Simple POST request' );
25 } );
26
27 this.server.respond( function ( request ) {
28 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
29 } );
30 } );
31
32 QUnit.test( 'API error', function ( assert ) {
33 QUnit.expect( 1 );
34
35 var api = new mw.Api();
36
37 api.get( { action: 'doesntexist' } )
38 .fail( function ( errorCode ) {
39 assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' );
40 } );
41
42 this.server.respond( function ( request ) {
43 request.respond( 200, { 'Content-Type': 'application/json' },
44 '{ "error": { "code": "unknown_action" } }'
45 );
46 } );
47 } );
48
49 QUnit.test( 'Deprecated callback methods', function ( assert ) {
50 QUnit.expect( 3 );
51
52 var api = new mw.Api();
53
54 api.get( {}, function () {
55 assert.ok( true, 'Function argument treated as success callback.' );
56 } );
57
58 api.get( {}, {
59 ok: function () {
60 assert.ok( true, '"ok" property treated as success callback.' );
61 }
62 } );
63
64 api.get( { action: 'doesntexist' }, {
65 err: function () {
66 assert.ok( true, '"err" property treated as error callback.' );
67 }
68 } );
69
70 this.server.respondWith( /action=query/, function ( request ) {
71 request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
72 } );
73
74 this.server.respondWith( /action=doesntexist/, function ( request ) {
75 request.respond( 200, { 'Content-Type': 'application/json' },
76 '{ "error": { "code": "unknown_action" } }'
77 );
78 } );
79
80 this.server.respond();
81 } );
82
83 QUnit.test( 'getToken( cached )', function ( assert ) {
84 QUnit.expect( 2 );
85
86 var api = new mw.Api();
87
88 // Get editToken for local wiki, this should not make
89 // a request as it should be retrieved from user.tokens.
90 api.getToken( 'edit' )
91 .done( function ( token ) {
92 assert.ok( token.length, 'Got a token' );
93 } )
94 .fail( function ( err ) {
95 assert.equal( '', err, 'API error' );
96 } );
97
98 assert.equal( this.server.requests.length, 0, 'Requests made' );
99 } );
100
101 QUnit.test( 'getToken( uncached )', function ( assert ) {
102 QUnit.expect( 2 );
103
104 var api = new mw.Api();
105
106 // Get a token of a type that isn't prepopulated by user.tokens.
107 // Could use "block" or "delete" here, but those could in theory
108 // be added to user.tokens, use a fake one instead.
109 api.getToken( 'testaction' )
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, 1, 'Requests made' );
118
119 this.server.respond( function ( request ) {
120 request.respond( 200, { 'Content-Type': 'application/json' },
121 '{ "tokens": { "testactiontoken": "0123abc" } }'
122 );
123 } );
124 } );
125
126 }( mediaWiki ) );