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