Don't check namespace in SpecialWantedtemplates
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiLoginTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 *
8 * @covers ApiLogin
9 */
10 class ApiLoginTest extends ApiTestCase {
11
12 /**
13 * Test result of attempted login with an empty username
14 */
15 public function testApiLoginNoName() {
16 $data = $this->doApiRequest( array( 'action' => 'login',
17 'lgname' => '', 'lgpassword' => self::$users['sysop']->password,
18 ) );
19 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
20 }
21
22 public function testApiLoginBadPass() {
23 global $wgServer;
24
25 $user = self::$users['sysop'];
26 $user->user->logOut();
27
28 if ( !isset( $wgServer ) ) {
29 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
30 }
31 $ret = $this->doApiRequest( array(
32 "action" => "login",
33 "lgname" => $user->username,
34 "lgpassword" => "bad",
35 ) );
36
37 $result = $ret[0];
38
39 $this->assertNotInternalType( "bool", $result );
40 $a = $result["login"]["result"];
41 $this->assertEquals( "NeedToken", $a );
42
43 $token = $result["login"]["token"];
44
45 $ret = $this->doApiRequest(
46 array(
47 "action" => "login",
48 "lgtoken" => $token,
49 "lgname" => $user->username,
50 "lgpassword" => "badnowayinhell",
51 ),
52 $ret[2]
53 );
54
55 $result = $ret[0];
56
57 $this->assertNotInternalType( "bool", $result );
58 $a = $result["login"]["result"];
59
60 $this->assertEquals( "WrongPass", $a );
61 }
62
63 public function testApiLoginGoodPass() {
64 global $wgServer;
65
66 if ( !isset( $wgServer ) ) {
67 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
68 }
69
70 $user = self::$users['sysop'];
71 $user->user->logOut();
72
73 $ret = $this->doApiRequest( array(
74 "action" => "login",
75 "lgname" => $user->username,
76 "lgpassword" => $user->password,
77 )
78 );
79
80 $result = $ret[0];
81 $this->assertNotInternalType( "bool", $result );
82 $this->assertNotInternalType( "null", $result["login"] );
83
84 $a = $result["login"]["result"];
85 $this->assertEquals( "NeedToken", $a );
86 $token = $result["login"]["token"];
87
88 $ret = $this->doApiRequest(
89 array(
90 "action" => "login",
91 "lgtoken" => $token,
92 "lgname" => $user->username,
93 "lgpassword" => $user->password,
94 ),
95 $ret[2]
96 );
97
98 $result = $ret[0];
99
100 $this->assertNotInternalType( "bool", $result );
101 $a = $result["login"]["result"];
102
103 $this->assertEquals( "Success", $a );
104 }
105
106 /**
107 * @group Broken
108 */
109 public function testApiLoginGotCookie() {
110 $this->markTestIncomplete( "The server can't do external HTTP requests, "
111 . "and the internal one won't give cookies" );
112
113 global $wgServer, $wgScriptPath;
114
115 if ( !isset( $wgServer ) ) {
116 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
117 }
118 $user = self::$users['sysop'];
119
120 $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
121 array( "method" => "POST",
122 "postData" => array(
123 "lgname" => $user->username,
124 "lgpassword" => $user->password
125 )
126 ),
127 __METHOD__
128 );
129 $req->execute();
130
131 libxml_use_internal_errors( true );
132 $sxe = simplexml_load_string( $req->getContent() );
133 $this->assertNotInternalType( "bool", $sxe );
134 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
135 $this->assertNotInternalType( "null", $sxe->login[0] );
136
137 $a = $sxe->login[0]->attributes()->result[0];
138 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
139 $token = (string)$sxe->login[0]->attributes()->token;
140
141 $req->setData( array(
142 "lgtoken" => $token,
143 "lgname" => $user->username,
144 "lgpassword" => $user->password ) );
145 $req->execute();
146
147 $cj = $req->getCookieJar();
148 $serverName = parse_url( $wgServer, PHP_URL_HOST );
149 $this->assertNotEquals( false, $serverName );
150 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
151 $this->assertNotEquals( '', $serializedCookie );
152 $this->assertRegexp(
153 '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/',
154 $serializedCookie
155 );
156 }
157
158 public function testRunLogin() {
159 $sysopUser = self::$users['sysop'];
160 $data = $this->doApiRequest( array(
161 'action' => 'login',
162 'lgname' => $sysopUser->username,
163 'lgpassword' => $sysopUser->password ) );
164
165 $this->assertArrayHasKey( "login", $data[0] );
166 $this->assertArrayHasKey( "result", $data[0]['login'] );
167 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
168 $token = $data[0]['login']['token'];
169
170 $data = $this->doApiRequest( array(
171 'action' => 'login',
172 "lgtoken" => $token,
173 "lgname" => $sysopUser->username,
174 "lgpassword" => $sysopUser->password ), $data[2] );
175
176 $this->assertArrayHasKey( "login", $data[0] );
177 $this->assertArrayHasKey( "result", $data[0]['login'] );
178 $this->assertEquals( "Success", $data[0]['login']['result'] );
179 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
180 }
181
182 }