Merge "user: Allow "CAS update failed" exceptions to be normalised"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.user.test.js
1 ( function () {
2 QUnit.module( 'mediawiki.user', QUnit.newMwEnvironment( {
3 setup: function () {
4 this.server = this.sandbox.useFakeServer();
5 // Cannot stub by simple assignment because read-only.
6 // Instead, stub in tests by using 'delete', and re-create
7 // in teardown using the original descriptor (including its
8 // accessors and readonly settings etc.)
9 this.crypto = Object.getOwnPropertyDescriptor( window, 'crypto' );
10 this.msCrypto = Object.getOwnPropertyDescriptor( window, 'msCrypto' );
11 },
12 teardown: function () {
13 if ( this.crypto ) {
14 Object.defineProperty( window, 'crypto', this.crypto );
15 }
16 if ( this.msCrypto ) {
17 Object.defineProperty( window, 'msCrypto', this.msCrypto );
18 }
19 }
20 } ) );
21
22 QUnit.test( 'options', function ( assert ) {
23 assert.ok( mw.user.options instanceof mw.Map, 'options instance of mw.Map' );
24 } );
25
26 QUnit.test( 'getters (anonymous)', function ( assert ) {
27 // Forge an anonymous user
28 mw.config.set( 'wgUserName', null );
29 mw.config.set( 'wgUserId', null );
30
31 assert.strictEqual( mw.user.getName(), null, 'getName()' );
32 assert.strictEqual( mw.user.isAnon(), true, 'isAnon()' );
33 assert.strictEqual( mw.user.getId(), 0, 'getId()' );
34 } );
35
36 QUnit.test( 'getters (logged-in)', function ( assert ) {
37 mw.config.set( 'wgUserName', 'John' );
38 mw.config.set( 'wgUserId', 123 );
39
40 assert.strictEqual( mw.user.getName(), 'John', 'getName()' );
41 assert.strictEqual( mw.user.isAnon(), false, 'isAnon()' );
42 assert.strictEqual( mw.user.getId(), 123, 'getId()' );
43
44 assert.strictEqual( mw.user.id(), 'John', 'user.id()' );
45 } );
46
47 QUnit.test( 'getUserInfo', function ( assert ) {
48 mw.config.set( 'wgUserGroups', [ '*', 'user' ] );
49
50 mw.user.getGroups( function ( groups ) {
51 assert.deepEqual( groups, [ '*', 'user' ], 'Result' );
52 } );
53
54 mw.user.getRights( function ( rights ) {
55 assert.deepEqual( rights, [ 'read', 'edit', 'createtalk' ], 'Result (callback)' );
56 } );
57
58 mw.user.getRights().done( function ( rights ) {
59 assert.deepEqual( rights, [ 'read', 'edit', 'createtalk' ], 'Result (promise)' );
60 } );
61
62 this.server.respondWith( /meta=userinfo/, function ( request ) {
63 request.respond( 200, { 'Content-Type': 'application/json' },
64 '{ "query": { "userinfo": { "groups": [ "unused" ], "rights": [ "read", "edit", "createtalk" ] } } }'
65 );
66 } );
67
68 this.server.respond();
69 } );
70
71 QUnit.test( 'generateRandomSessionId', function ( assert ) {
72 var result, result2;
73
74 result = mw.user.generateRandomSessionId();
75 assert.strictEqual( typeof result, 'string', 'type' );
76 assert.strictEqual( result.trim(), result, 'no whitespace at beginning or end' );
77 assert.strictEqual( result.length, 20, 'size' );
78
79 result2 = mw.user.generateRandomSessionId();
80 assert.notEqual( result, result2, 'different when called multiple times' );
81
82 } );
83
84 QUnit.test( 'generateRandomSessionId (fallback)', function ( assert ) {
85 var result, result2;
86
87 // Pretend crypto API is not there to test the Math.random fallback
88 delete window.crypto;
89 delete window.msCrypto;
90 // Assert that the above actually worked. If we use the wrong method
91 // of stubbing, JavaScript silently continues and we need to know that
92 // it was the wrong method. As of writing, assigning undefined is
93 // ineffective as the window property for Crypto is read-only.
94 // However, deleting does work. (T203275)
95 assert.strictEqual( window.crypto || window.msCrypto, undefined, 'fallback is active' );
96
97 result = mw.user.generateRandomSessionId();
98 assert.strictEqual( typeof result, 'string', 'type' );
99 assert.strictEqual( result.trim(), result, 'no whitespace at beginning or end' );
100 assert.strictEqual( result.length, 20, 'size' );
101
102 result2 = mw.user.generateRandomSessionId();
103 assert.notEqual( result, result2, 'different when called multiple times' );
104 } );
105
106 QUnit.test( 'getPageviewToken', function ( assert ) {
107 var result = mw.user.getPageviewToken(),
108 result2 = mw.user.getPageviewToken();
109 assert.strictEqual( typeof result, 'string', 'type' );
110 assert.strictEqual( /^[a-f0-9]{20}$/.test( result ), true, '20 HEX symbols string' );
111 assert.strictEqual( result2, result, 'sticky' );
112 } );
113
114 QUnit.test( 'sessionId', function ( assert ) {
115 var result = mw.user.sessionId(),
116 result2 = mw.user.sessionId();
117 assert.strictEqual( typeof result, 'string', 'type' );
118 assert.strictEqual( result.trim(), result, 'no leading or trailing whitespace' );
119 assert.strictEqual( result2, result, 'retained' );
120 } );
121 }() );