summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/api/ApiCheckTokenTest.php
blob: f1d95d03d6575c079c4039a3138be64932ea27b6 (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
<?php

use MediaWiki\Session\Token;

/**
 * @group API
 * @group medium
 * @covers ApiCheckToken
 */
class ApiCheckTokenTest extends ApiTestCase {

	/**
	 * Test result of checking previously queried token (should be valid)
	 */
	public function testCheckTokenValid() {
		// Query token which will be checked later
		$tokens = $this->doApiRequest( [
			'action' => 'query',
			'meta' => 'tokens',
		] );

		$data = $this->doApiRequest( [
			'action' => 'checktoken',
			'type' => 'csrf',
			'token' => $tokens[0]['query']['tokens']['csrftoken'],
		], $tokens[1]->getSessionArray() );

		$this->assertEquals( 'valid', $data[0]['checktoken']['result'] );
		$this->assertArrayHasKey( 'generated', $data[0]['checktoken'] );
	}

	/**
	 * Test result of checking invalid token
	 */
	public function testCheckTokenInvalid() {
		$session = [];
		$data = $this->doApiRequest( [
			'action' => 'checktoken',
			'type' => 'csrf',
			'token' => 'invalid_token',
		], $session );

		$this->assertEquals( 'invalid', $data[0]['checktoken']['result'] );
	}

	/**
	 * Test result of checking token with negative max age (should be expired)
	 */
	public function testCheckTokenExpired() {
		// Query token which will be checked later
		$tokens = $this->doApiRequest( [
			'action' => 'query',
			'meta' => 'tokens',
		] );

		$data = $this->doApiRequest( [
			'action' => 'checktoken',
			'type' => 'csrf',
			'token' => $tokens[0]['query']['tokens']['csrftoken'],
			'maxtokenage' => -1,
		], $tokens[1]->getSessionArray() );

		$this->assertEquals( 'expired', $data[0]['checktoken']['result'] );
		$this->assertArrayHasKey( 'generated', $data[0]['checktoken'] );
	}

	/**
	 * Test if using token with incorrect suffix will produce a warning
	 */
	public function testCheckTokenSuffixWarning() {
		// Query token which will be checked later
		$tokens = $this->doApiRequest( [
			'action' => 'query',
			'meta' => 'tokens',
		] );

		// Get token and change the suffix
		$token = $tokens[0]['query']['tokens']['csrftoken'];
		$token = substr( $token, 0, -strlen( Token::SUFFIX ) ) . urldecode( Token::SUFFIX );

		$data = $this->doApiRequest( [
			'action' => 'checktoken',
			'type' => 'csrf',
			'token' => $token,
			'errorformat' => 'raw',
		], $tokens[1]->getSessionArray() );

		$this->assertEquals( 'invalid', $data[0]['checktoken']['result'] );
		$this->assertArrayHasKey( 'warnings', $data[0] );
		$this->assertCount( 1, $data[0]['warnings'] );
		$this->assertEquals( 'checktoken', $data[0]['warnings'][0]['module'] );
		$this->assertEquals( 'checktoken-percentencoding', $data[0]['warnings'][0]['code'] );
	}

}