2 QUnit.module( 'mediawiki.user', QUnit.newMwEnvironment( {
4 this.server = this.sandbox.useFakeServer();
5 this.crypto = window.crypto;
6 this.msCrypto = window.msCrypto;
8 teardown: function () {
10 window.crypto = this.crypto;
12 if ( this.msCrypto ) {
13 window.msCrypto = this.msCrypto;
18 QUnit.test( 'options', 1, function ( assert ) {
19 assert.ok( mw.user.options instanceof mw.Map, 'options instance of mw.Map' );
22 QUnit.test( 'user status', 7, function ( assert ) {
24 // Forge an anonymous user
25 mw.config.set( 'wgUserName', null );
26 delete mw.config.values.wgUserId;
28 assert.strictEqual( mw.user.getName(), null, 'user.getName() returns null when anonymous' );
29 assert.assertTrue( mw.user.isAnon(), 'user.isAnon() returns true when anonymous' );
30 assert.strictEqual( mw.user.getId(), 0, 'user.getId() returns 0 when anonymous' );
32 // Not part of startUp module
33 mw.config.set( 'wgUserName', 'John' );
34 mw.config.set( 'wgUserId', 123 );
36 assert.equal( mw.user.getName(), 'John', 'user.getName() returns username when logged-in' );
37 assert.assertFalse( mw.user.isAnon(), 'user.isAnon() returns false when logged-in' );
38 assert.strictEqual( mw.user.getId(), 123, 'user.getId() returns correct ID when logged-in' );
40 assert.equal( mw.user.id(), 'John', 'user.id Returns username when logged-in' );
43 QUnit.test( 'getUserInfos', 3, function ( assert ) {
44 mw.user.getGroups( function ( groups ) {
45 assert.deepEqual( groups, [ '*', 'user' ], 'Result' );
48 mw.user.getRights( function ( rights ) {
49 assert.deepEqual( rights, [ 'read', 'edit', 'createtalk' ], 'Result (callback)' );
52 mw.user.getRights().done( function ( rights ) {
53 assert.deepEqual( rights, [ 'read', 'edit', 'createtalk' ], 'Result (promise)' );
56 this.server.respondWith( /meta=userinfo/, function ( request ) {
57 request.respond( 200, { 'Content-Type': 'application/json' },
58 '{ "query": { "userinfo": { "groups": [ "*", "user" ], "rights": [ "read", "edit", "createtalk" ] } } }'
62 this.server.respond();
65 QUnit.test( 'generateRandomSessionId', 4, function ( assert ) {
68 result = mw.user.generateRandomSessionId();
69 assert.equal( typeof result, 'string', 'type' );
70 assert.equal( $.trim( result ), result, 'no whitespace at beginning or end' );
71 assert.equal( result.length, 16, 'size' );
73 result2 = mw.user.generateRandomSessionId();
74 assert.notEqual( result, result2, 'different when called multiple times' );
78 QUnit.test( 'generateRandomSessionId (fallback)', 4, function ( assert ) {
81 // Pretend crypto API is not there to test the Math.random fallback
82 if ( window.crypto ) {
83 window.crypto = undefined;
85 if ( window.msCrypto ) {
86 window.msCrypto = undefined;
89 result = mw.user.generateRandomSessionId();
90 assert.equal( typeof result, 'string', 'type' );
91 assert.equal( $.trim( result ), result, 'no whitespace at beginning or end' );
92 assert.equal( result.length, 16, 'size' );
94 result2 = mw.user.generateRandomSessionId();
95 assert.notEqual( result, result2, 'different when called multiple times' );
98 }( mediaWiki, jQuery ) );