X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fqunit%2Fsuites%2Fresources%2Fmediawiki%2Fmediawiki.user.test.js;h=04e002dd90316a4e7ee7cb1d1cc64587457e2ef6;hb=bacc11eb30ddf01624d5448f432b96afdae67c28;hp=96be3d1fb8ca1cc122169d01a4651ad8de566cd8;hpb=8fe0cf970401f2796ca39165a2d0a311b551c47a;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js index 96be3d1fb8..04e002dd90 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js @@ -1,26 +1,32 @@ ( function ( mw, $ ) { - QUnit.module( 'mediawiki.user', QUnit.newMwEnvironment() ); + QUnit.module( 'mediawiki.user', QUnit.newMwEnvironment( { + setup: function () { + this.server = this.sandbox.useFakeServer(); + this.crypto = window.crypto; + this.msCrypto = window.msCrypto; + }, + teardown: function () { + if ( this.crypto ) { + window.crypto = this.crypto; + } + if ( this.msCrypto ) { + window.msCrypto = this.msCrypto; + } + } + } ) ); QUnit.test( 'options', 1, function ( assert ) { assert.ok( mw.user.options instanceof mw.Map, 'options instance of mw.Map' ); } ); - QUnit.test( 'user status', 11, function ( assert ) { - /** - * Tests can be run under three different conditions: - * 1) From tests/qunit/index.html, user will be anonymous. - * 2) Logged in on [[Special:JavaScriptTest/qunit]] - * 3) Anonymously at the same special page. - */ + QUnit.test( 'user status', 7, function ( assert ) { - // Forge an anonymous user: + // Forge an anonymous user mw.config.set( 'wgUserName', null ); delete mw.config.values.wgUserId; assert.strictEqual( mw.user.getName(), null, 'user.getName() returns null when anonymous' ); - assert.strictEqual( mw.user.name(), null, 'user.name() compatibility' ); assert.assertTrue( mw.user.isAnon(), 'user.isAnon() returns true when anonymous' ); - assert.assertTrue( mw.user.anonymous(), 'user.anonymous() compatibility' ); assert.strictEqual( mw.user.getId(), 0, 'user.getId() returns 0 when anonymous' ); // Not part of startUp module @@ -28,30 +34,65 @@ mw.config.set( 'wgUserId', 123 ); assert.equal( mw.user.getName(), 'John', 'user.getName() returns username when logged-in' ); - assert.equal( mw.user.name(), 'John', 'user.name() compatibility' ); assert.assertFalse( mw.user.isAnon(), 'user.isAnon() returns false when logged-in' ); - assert.assertFalse( mw.user.anonymous(), 'user.anonymous() compatibility' ); assert.strictEqual( mw.user.getId(), 123, 'user.getId() returns correct ID when logged-in' ); assert.equal( mw.user.id(), 'John', 'user.id Returns username when logged-in' ); } ); - QUnit.asyncTest( 'getGroups', 3, function ( assert ) { + QUnit.test( 'getUserInfos', 3, function ( assert ) { mw.user.getGroups( function ( groups ) { - // First group should always be '*' - assert.equal( $.type( groups ), 'array', 'Callback gets an array' ); - assert.notStrictEqual( $.inArray( '*', groups ), -1, '"*"" is in the list' ); - // Sort needed because of different methods if creating the arrays, - // only the content matters. - assert.deepEqual( groups.sort(), mw.config.get( 'wgUserGroups' ).sort(), 'Array contains all groups, just like wgUserGroups' ); - QUnit.start(); + assert.deepEqual( groups, [ '*', 'user' ], 'Result' ); } ); - } ); - QUnit.asyncTest( 'getRights', 1, function ( assert ) { mw.user.getRights( function ( rights ) { - assert.equal( $.type( rights ), 'array', 'Callback gets an array' ); - QUnit.start(); + assert.deepEqual( rights, [ 'read', 'edit', 'createtalk' ], 'Result (callback)' ); + } ); + + mw.user.getRights().done( function ( rights ) { + assert.deepEqual( rights, [ 'read', 'edit', 'createtalk' ], 'Result (promise)' ); + } ); + + this.server.respondWith( /meta=userinfo/, function ( request ) { + request.respond( 200, { 'Content-Type': 'application/json' }, + '{ "query": { "userinfo": { "groups": [ "*", "user" ], "rights": [ "read", "edit", "createtalk" ] } } }' + ); } ); + + this.server.respond(); + } ); + + QUnit.test( 'generateRandomSessionId', 4, function ( assert ) { + var result, result2; + + result = mw.user.generateRandomSessionId(); + assert.equal( typeof result, 'string', 'type' ); + assert.equal( $.trim( result ), result, 'no whitespace at beginning or end' ); + assert.equal( result.length, 16, 'size' ); + + result2 = mw.user.generateRandomSessionId(); + assert.notEqual( result, result2, 'different when called multiple times' ); + + } ); + + QUnit.test( 'generateRandomSessionId (fallback)', 4, function ( assert ) { + var result, result2; + + // Pretend crypto API is not there to test the Math.random fallback + if ( window.crypto ) { + window.crypto = undefined; + } + if ( window.msCrypto ) { + window.msCrypto = undefined; + } + + result = mw.user.generateRandomSessionId(); + assert.equal( typeof result, 'string', 'type' ); + assert.equal( $.trim( result ), result, 'no whitespace at beginning or end' ); + assert.equal( result.length, 16, 'size' ); + + result2 = mw.user.generateRandomSessionId(); + assert.notEqual( result, result2, 'different when called multiple times' ); + } ); }( mediaWiki, jQuery ) );