File: JsonBodyValidatorTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (206 lines) | stat: -rw-r--r-- 4,882 bytes parent folder | download
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php

namespace MediaWiki\Tests\Rest;

use MediaWiki\Rest\LocalizedHttpException;
use MediaWiki\Rest\RequestData;
use MediaWiki\Rest\Validator\JsonBodyValidator;
use MediaWiki\Rest\Validator\Validator;
use MediaWikiUnitTestCase;
use Wikimedia\Message\ListParam;
use Wikimedia\Message\ListType;
use Wikimedia\Message\MessageValue;
use Wikimedia\ParamValidator\ParamValidator;

/**
 * @covers \MediaWiki\Rest\Validator\JsonBodyValidator
 */
class JsonBodyValidatorTest extends MediaWikiUnitTestCase {

	protected function setUp(): void {
		parent::setUp();
		$this->filterDeprecated( '/JsonBodyValidator/' );
	}

	public static function provideValidateBody() {
		yield 'empty object' => [
			[],
			new RequestData( [
				'bodyContents' => json_encode( (object)[] ),
			] ),
			[]
		];

		yield 'missing optional' => [
			[
				'number' => [
					ParamValidator::PARAM_TYPE => 'integer',
					ParamValidator::PARAM_REQUIRED => false,
				]
			],
			new RequestData( [
				'bodyContents' => json_encode( (object)[] ),
			] ),
			[
				'number' => null,
			]
		];

		yield 'apply default' => [
			[
				'number' => [
					ParamValidator::PARAM_TYPE => 'integer',
					ParamValidator::PARAM_REQUIRED => false,
					ParamValidator::PARAM_DEFAULT => 10,
				]
			],
			new RequestData( [
				'bodyContents' => json_encode( (object)[] ),
			] ),
			[
				'number' => 10,
			]
		];
	}

	/**
	 * @dataProvider provideValidateBody
	 */
	public function testValidateBody( array $settings, RequestData $requestData, array $expected ) {
		$validator = new JsonBodyValidator( $settings );
		$actual = $validator->validateBody( $requestData );
		$this->assertArrayEquals( $expected, $actual, false, true );
	}

	public static function provideValidateBody_failure() {
		yield 'empty body' => [
			[],
			new RequestData( [
				'bodyContents' => '',
			] ),
			new LocalizedHttpException( new MessageValue( 'rest-json-body-parse-error' ), 400 ),
		];

		yield 'bad syntax' => [
			[],
			new RequestData( [
				'bodyContents' => '.....',
			] ),
			new LocalizedHttpException( new MessageValue( 'rest-json-body-parse-error' ), 400 ),
		];

		yield 'not an object' => [
			[],
			new RequestData( [
				'bodyContents' => json_encode( 'evil' ),
			] ),
			new LocalizedHttpException( new MessageValue( 'rest-bad-json-body' ), 400 ),
		];

		yield 'missing optional' => [
			[
				'number' => [
					ParamValidator::PARAM_TYPE => 'integer',
					ParamValidator::PARAM_REQUIRED => true,
				]
			],
			new RequestData( [
				'bodyContents' => json_encode( (object)[
					'kittens' => 'cute',
				] ),
			] ),
			new LocalizedHttpException( new MessageValue( 'rest-missing-body-field' ), 400 ),
		];

		yield 'extraneous parameters' => [
			[
				'foo' => [
					ParamValidator::PARAM_TYPE => 'integer',
					ParamValidator::PARAM_REQUIRED => false,
				]
			],
			new RequestData( [
				'bodyContents' => json_encode( (object)[
					'f00' => 123,
					'bar' => 456,
				] ),
			] ),
			new LocalizedHttpException(
				new MessageValue(
					'rest-extraneous-body-fields',
					[ new ListParam( ListType::COMMA, [ 'f00', 'bar' ] ) ]
				),
				400
			),
		];
	}

	/**
	 * @dataProvider provideValidateBody_failure
	 */
	public function testValidateBody_failure( $settings, RequestData $requestData, $expected ) {
		$validator = new JsonBodyValidator( $settings );

		$this->expectExceptionObject( $expected );
		$validator->validateBody( $requestData );
	}

	public function testOpenAPISpec() {
		$settings = [
			'first' => [
				ParamValidator::PARAM_TYPE => 'string',
				Validator::PARAM_SOURCE => 'path',
			],
			'second' => [
				ParamValidator::PARAM_TYPE => 'float',
				Validator::PARAM_SOURCE => 'query',
				ParamValidator::PARAM_REQUIRED => false,
			],
			'third' => [
				ParamValidator::PARAM_TYPE => [ 'a', 'b', 'c' ],
				Validator::PARAM_SOURCE => 'body',
				Validator::PARAM_DESCRIPTION => 'just a test',
				ParamValidator::PARAM_REQUIRED => true,
			],
			'fourth' => [
				ParamValidator::PARAM_TYPE => 'timestamp',
			],
		];
		$expected = [
			'properties' => [
				'first' => [
					'type' => 'string',
					'description' => 'first parameter',
				],
				'second' => [
					'type' => 'number',
					'format' => 'float',
					'description' => 'second parameter',
				],
				'third' => [
					'type' => 'string',
					'enum' => [ 'a', 'b', 'c' ],
					'description' => 'just a test',
				],
				'fourth' => [
					'type' => 'string',
					'format' => 'mw-timestamp',
					'description' => 'fourth parameter',
				],
			],
			'required' => [
				'first', 'third'
			]
		];

		$validator = new JsonBodyValidator( $settings );
		$spec = $validator->getOpenAPISpec();
		$this->assertArrayEquals( $expected, $spec, false, true );
	}

	public function testOpenAPISpec_empty() {
		$validator = new JsonBodyValidator( [] );
		$this->assertSame( [], $validator->getOpenAPISpec() );
	}

}