summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UploadWizard/tests/qunit/transports/mw.FormDataTransport.test.js
blob: d451c0e447fadae4da8e87f29ab21bdf4921c02f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * This file is part of the MediaWiki extension UploadWizard.
 *
 * UploadWizard is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * UploadWizard is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with UploadWizard.  If not, see <http://www.gnu.org/licenses/>.
 */

( function ( mw, $ ) {
	QUnit.module( 'mw.FormDataTransport', QUnit.newMwEnvironment() );

	function createTransport( chunkSize, api ) {
		var config;

		chunkSize = chunkSize || 0;
		api = api || {};

		config = {
			useRetryTimeout: false,
			chunkSize: chunkSize,
			maxPhpUploadSize: chunkSize
		};

		return new mw.FormDataTransport( api, {}, config );
	}

	QUnit.test( 'Constructor sanity test', function ( assert ) {
		var transport = createTransport();

		assert.ok( transport );
	} );

	QUnit.test( 'abort', function ( assert ) {
		var transport = createTransport( 0 ),
			request = $.Deferred().promise( { abort: this.sandbox.stub() } );

		transport.request = request;

		assert.ok( request.abort.notCalled );

		transport.abort();

		assert.ok( request.abort.called );
		assert.ok( transport.aborted );
	} );

	QUnit.test( 'createParams', function ( assert ) {
		var transport = createTransport( 10 ),
			params = transport.createParams( 'foobar.jpg', 0 );

		assert.ok( params );

		assert.strictEqual( params.filename, 'foobar.jpg' );
		assert.strictEqual( params.offset, 0 );
	} );

	QUnit.test( 'post', function ( assert ) {
		var stub = this.sandbox.stub(),
			// post() works on a promise and binds .then, so we have to make
			// sure it actually is a promise, but also that it calls our stub
			transport = createTransport( 10, { post: function () {
				stub();
				return $.Deferred().resolve();
			} } );

		this.sandbox.useFakeXMLHttpRequest();
		this.sandbox.useFakeServer();

		assert.ok( stub.notCalled );

		transport.post( {} );

		assert.ok( stub.called );
	} );

	QUnit.test( 'upload', function ( assert ) {
		var request,
			transport = createTransport( 10, new mw.Api() ),
			fakeFile = {
				name: 'test file for fdt.jpg',
				size: 5
			};

		this.sandbox.useFakeXMLHttpRequest();
		this.sandbox.useFakeServer();

		transport.upload( fakeFile, 'test file for fdt.jpg' );

		assert.strictEqual( this.sandbox.server.requests.length, 1 );
		request = this.sandbox.server.requests[ 0 ];
		assert.strictEqual( request.method, 'POST' );
		assert.strictEqual( request.url, mw.util.wikiScript( 'api' ) );
		assert.ok( request.async );

		transport.abort();
	} );

	QUnit.test( 'uploadChunk', function ( assert ) {
		var request,
			transport = createTransport( 10, new mw.Api() ),
			fakeFile = {
				name: 'test file for fdt.jpg',
				size: 20,
				slice: function ( offset ) {
					return {
						name: 'test file for fdt.jpg',
						offset: offset,
						size: 10
					};
				}
			};

		this.sandbox.useFakeXMLHttpRequest();
		this.sandbox.useFakeServer();

		transport.uploadChunk( fakeFile, 0 );

		assert.strictEqual( this.sandbox.server.requests.length, 1 );
		request = this.sandbox.server.requests[ 0 ];
		assert.strictEqual( request.method, 'POST' );
		assert.strictEqual( request.url, mw.util.wikiScript( 'api' ) );
		assert.ok( request.async );

		transport.abort();
	} );

	// test invalid server response (in missing 'stage' param)
	QUnit.test( 'checkStatus invalid API response', function ( assert ) {
		var done = assert.async(),
			transport = createTransport( 10, new mw.Api() ),
			tstub = this.sandbox.stub(),
			poststub = this.sandbox.stub( transport.api, 'post' ),
			postd = $.Deferred();

		// prepare a bogus invalid API result
		poststub.returns( postd.promise() );
		postd.resolve( { upload: { result: 'Poll' } } );

		// call tstub upon checkStatus failure, and verify it got called correctly
		transport.checkStatus().fail( tstub, function () {
			assert.ok( tstub.calledWith( 'server-error', { errors: [ {
				code: 'server-error',
				html: mw.message( 'apierror-unknownerror' ).parse()
			} ] } ) );
			done();
		} );
	} );

	// test retry after server responds upload is still incomplete
	QUnit.test( 'checkStatus retry', function ( assert ) {
		var transport = createTransport( 10, new mw.Api() ),
			usstub = this.sandbox.stub(),
			poststub = this.sandbox.stub( transport.api, 'post' ),
			postd = $.Deferred(),
			postd2 = $.Deferred();

		transport.on( 'update-stage', usstub );

		// prepare a first API call that responds with 'Poll' (upload
		// concatenation is not yet complete) followed by a second call that
		// marks the upload successful
		poststub
			.onFirstCall().returns( postd.promise() )
			.onSecondCall().returns( postd2.promise() );

		// resolve 3 API calls, where server first responds upload is not yet
		// assembled, and second says it's published
		postd.resolve( { upload: { result: 'Poll', stage: 'queued' } } );
		postd2.resolve( { upload: { result: 'Success' } } );

		// confirm that, once second API call was successful, status resolves,
		// 2 API calls have gone out & the failed call updates stage accordingly
		return transport.checkStatus().done( function () {
			assert.ok( poststub.calledTwice );
			assert.ok( usstub.firstCall.calledWith( 'queued' ) );
		} );
	} );

	QUnit.test( 'checkStatus success', function ( assert ) {
		var transport = createTransport( 10, new mw.Api() ),
			tstub = this.sandbox.stub(),
			usstub = this.sandbox.stub(),
			poststub = this.sandbox.stub( transport.api, 'post' ),
			postd = $.Deferred();

		transport.on( 'update-stage', usstub );

		// prepare a bogus valid API result
		poststub.returns( postd.promise() );
		postd.resolve( 'testing' );

		return transport.checkStatus().done( tstub, function () {
			assert.ok( tstub.calledWith( 'testing' ) );
			assert.ok( !usstub.called );
		} );
	} );

	QUnit.test( 'checkStatus error API response', function ( assert ) {
		var done = assert.async(),
			transport = createTransport( 10, new mw.Api() ),
			tstub = this.sandbox.stub(),
			usstub = this.sandbox.stub(),
			poststub = this.sandbox.stub( transport.api, 'post' ),
			postd = $.Deferred();

		transport.on( 'update-stage', usstub );

		// prepare an error API response
		poststub.returns( postd.promise() );
		postd.reject( 'testing', { error: 'testing' } );

		transport.checkStatus().fail( tstub, function () {
			assert.ok( tstub.calledWith( 'testing', { error: 'testing' } ) );
			assert.ok( !usstub.called );
			done();
		} );
	} );

}( mediaWiki, jQuery ) );