Merge "maintenance: Document secondary purpose of --server"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.user.test.js
1 ( function ( mw, $ ) {
2 QUnit.module( 'mediawiki.user', QUnit.newMwEnvironment( {
3 setup: function () {
4 this.server = this.sandbox.useFakeServer();
5 this.crypto = window.crypto;
6 this.msCrypto = window.msCrypto;
7 },
8 teardown: function () {
9 if ( this.crypto ) {
10 window.crypto = this.crypto;
11 }
12 if ( this.msCrypto ) {
13 window.msCrypto = this.msCrypto;
14 }
15 }
16 } ) );
17
18 QUnit.test( 'options', function ( assert ) {
19 assert.ok( mw.user.options instanceof mw.Map, 'options instance of mw.Map' );
20 } );
21
22 QUnit.test( 'getters (anonymous)', function ( assert ) {
23 // Forge an anonymous user
24 mw.config.set( 'wgUserName', null );
25 mw.config.set( 'wgUserId', null );
26
27 assert.strictEqual( mw.user.getName(), null, 'getName()' );
28 assert.strictEqual( mw.user.isAnon(), true, 'isAnon()' );
29 assert.strictEqual( mw.user.getId(), 0, 'getId()' );
30 } );
31
32 QUnit.test( 'getters (logged-in)', function ( assert ) {
33 mw.config.set( 'wgUserName', 'John' );
34 mw.config.set( 'wgUserId', 123 );
35
36 assert.equal( mw.user.getName(), 'John', 'getName()' );
37 assert.strictEqual( mw.user.isAnon(), false, 'isAnon()' );
38 assert.strictEqual( mw.user.getId(), 123, 'getId()' );
39
40 assert.equal( mw.user.id(), 'John', 'user.id()' );
41 } );
42
43 QUnit.test( 'getUserInfo', function ( assert ) {
44 mw.config.set( 'wgUserGroups', [ '*', 'user' ] );
45
46 mw.user.getGroups( function ( groups ) {
47 assert.deepEqual( groups, [ '*', 'user' ], 'Result' );
48 } );
49
50 mw.user.getRights( function ( rights ) {
51 assert.deepEqual( rights, [ 'read', 'edit', 'createtalk' ], 'Result (callback)' );
52 } );
53
54 mw.user.getRights().done( function ( rights ) {
55 assert.deepEqual( rights, [ 'read', 'edit', 'createtalk' ], 'Result (promise)' );
56 } );
57
58 this.server.respondWith( /meta=userinfo/, function ( request ) {
59 request.respond( 200, { 'Content-Type': 'application/json' },
60 '{ "query": { "userinfo": { "groups": [ "unused" ], "rights": [ "read", "edit", "createtalk" ] } } }'
61 );
62 } );
63
64 this.server.respond();
65 } );
66
67 QUnit.test( 'generateRandomSessionId', function ( assert ) {
68 var result, result2;
69
70 result = mw.user.generateRandomSessionId();
71 assert.equal( typeof result, 'string', 'type' );
72 assert.equal( $.trim( result ), result, 'no whitespace at beginning or end' );
73 assert.equal( result.length, 16, 'size' );
74
75 result2 = mw.user.generateRandomSessionId();
76 assert.notEqual( result, result2, 'different when called multiple times' );
77
78 } );
79
80 QUnit.test( 'generateRandomSessionId (fallback)', function ( assert ) {
81 var result, result2;
82
83 // Pretend crypto API is not there to test the Math.random fallback
84 if ( window.crypto ) {
85 window.crypto = undefined;
86 }
87 if ( window.msCrypto ) {
88 window.msCrypto = undefined;
89 }
90
91 result = mw.user.generateRandomSessionId();
92 assert.equal( typeof result, 'string', 'type' );
93 assert.equal( $.trim( result ), result, 'no whitespace at beginning or end' );
94 assert.equal( result.length, 16, 'size' );
95
96 result2 = mw.user.generateRandomSessionId();
97 assert.notEqual( result, result2, 'different when called multiple times' );
98 } );
99
100 QUnit.test( 'stickyRandomId', function ( assert ) {
101 var result = mw.user.stickyRandomId(),
102 result2 = mw.user.stickyRandomId();
103 assert.equal( typeof result, 'string', 'type' );
104 assert.strictEqual( /^[a-f0-9]{16}$/.test( result ), true, '16 HEX symbols string' );
105 assert.equal( result2, result, 'sticky' );
106 } );
107
108 QUnit.test( 'sessionId', function ( assert ) {
109 var result = mw.user.sessionId(),
110 result2 = mw.user.sessionId();
111 assert.equal( typeof result, 'string', 'type' );
112 assert.equal( $.trim( result ), result, 'no leading or trailing whitespace' );
113 assert.equal( result2, result, 'retained' );
114 } );
115 }( mediaWiki, jQuery ) );