summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/api/ApiCreateAccountTest.php
blob: 8d134f7635c7617414f30c2339980381ccb3610b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php

/**
 * @group Database
 * @group API
 * @group medium
 *
 * @covers ApiCreateAccount
 */
class ApiCreateAccountTest extends ApiTestCase {
	protected function setUp() {
		parent::setUp();
		LoginForm::setCreateaccountToken();
		$this->setMwGlobals( array( 'wgEnableEmail' => true ) );
	}

	/**
	 * Test the account creation API with a valid request. Also
	 * make sure the new account can log in and is valid.
	 *
	 * This test does multiple API requests so it might end up being
	 * a bit slow. Raise the default timeout.
	 * @group medium
	 */
	public function testValid() {
		global $wgServer;

		if ( !isset( $wgServer ) ) {
			$this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
		}

		$password = User::randomPassword();

		$ret = $this->doApiRequest( array(
			'action' => 'createaccount',
			'name' => 'Apitestnew',
			'password' => $password,
			'email' => 'test@domain.test',
			'realname' => 'Test Name'
		) );

		$result = $ret[0];
		$this->assertNotInternalType( 'bool', $result );
		$this->assertNotInternalType( 'null', $result['createaccount'] );

		// Should first ask for token.
		$a = $result['createaccount'];
		$this->assertEquals( 'NeedToken', $a['result'] );
		$token = $a['token'];

		// Finally create the account
		$ret = $this->doApiRequest(
			array(
				'action' => 'createaccount',
				'name' => 'Apitestnew',
				'password' => $password,
				'token' => $token,
				'email' => 'test@domain.test',
				'realname' => 'Test Name'
			),
			$ret[2]
		);

		$result = $ret[0];
		$this->assertNotInternalType( 'bool', $result );
		$this->assertEquals( 'Success', $result['createaccount']['result'] );

		// Try logging in with the new user.
		$ret = $this->doApiRequest( array(
			'action' => 'login',
			'lgname' => 'Apitestnew',
			'lgpassword' => $password,
		) );

		$result = $ret[0];
		$this->assertNotInternalType( 'bool', $result );
		$this->assertNotInternalType( 'null', $result['login'] );

		$a = $result['login']['result'];
		$this->assertEquals( 'NeedToken', $a );
		$token = $result['login']['token'];

		$ret = $this->doApiRequest(
			array(
				'action' => 'login',
				'lgtoken' => $token,
				'lgname' => 'Apitestnew',
				'lgpassword' => $password,
			),
			$ret[2]
		);

		$result = $ret[0];

		$this->assertNotInternalType( 'bool', $result );
		$a = $result['login']['result'];

		$this->assertEquals( 'Success', $a );

		// log out to destroy the session
		$ret = $this->doApiRequest(
			array(
				'action' => 'logout',
			),
			$ret[2]
		);
		$this->assertEquals( array(), $ret[0] );
	}

	/**
	 * Make sure requests with no names are invalid.
	 * @expectedException UsageException
	 */
	public function testNoName() {
		$this->doApiRequest( array(
			'action' => 'createaccount',
			'token' => LoginForm::getCreateaccountToken(),
			'password' => 'password',
		) );
	}

	/**
	 * Make sure requests with no password are invalid.
	 * @expectedException UsageException
	 */
	public function testNoPassword() {
		$this->doApiRequest( array(
			'action' => 'createaccount',
			'name' => 'testName',
			'token' => LoginForm::getCreateaccountToken(),
		) );
	}

	/**
	 * Make sure requests with existing users are invalid.
	 * @expectedException UsageException
	 */
	public function testExistingUser() {
		$this->doApiRequest( array(
			'action' => 'createaccount',
			'name' => 'Apitestsysop',
			'token' => LoginForm::getCreateaccountToken(),
			'password' => 'password',
			'email' => 'test@domain.test',
		) );
	}

	/**
	 * Make sure requests with invalid emails are invalid.
	 * @expectedException UsageException
	 */
	public function testInvalidEmail() {
		$this->doApiRequest( array(
			'action' => 'createaccount',
			'name' => 'Test User',
			'token' => LoginForm::getCreateaccountToken(),
			'password' => 'password',
			'email' => 'invalid',
		) );
	}
}