| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Rest; |
| 4 |
|
| 5 |
use MediaWiki\Rest\LocalizedHttpException; |
| 6 |
use MediaWiki\Rest\RequestData; |
| 7 |
use MediaWiki\Rest\Validator\JsonBodyValidator; |
| 8 |
use MediaWiki\Rest\Validator\Validator; |
| 9 |
use MediaWikiUnitTestCase; |
| 10 |
use Wikimedia\Message\ListParam; |
| 11 |
use Wikimedia\Message\ListType; |
| 12 |
use Wikimedia\Message\MessageValue; |
| 13 |
use Wikimedia\ParamValidator\ParamValidator; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class JsonBodyValidatorTest extends MediaWikiUnitTestCase { |
| 19 |
|
| 20 |
protected function setUp(): void { |
| 21 |
parent::setUp(); |
| 22 |
$this->filterDeprecated( '/JsonBodyValidator/' ); |
| 23 |
} |
| 24 |
|
| 25 |
public static function provideValidateBody() { |
| 26 |
yield 'empty object' => [ |
| 27 |
[], |
| 28 |
new RequestData( [ |
| 29 |
'bodyContents' => json_encode( (object)[] ), |
| 30 |
] ), |
| 31 |
[] |
| 32 |
]; |
| 33 |
|
| 34 |
yield 'missing optional' => [ |
| 35 |
[ |
| 36 |
'number' => [ |
| 37 |
ParamValidator::PARAM_TYPE => 'integer', |
| 38 |
ParamValidator::PARAM_REQUIRED => false, |
| 39 |
] |
| 40 |
], |
| 41 |
new RequestData( [ |
| 42 |
'bodyContents' => json_encode( (object)[] ), |
| 43 |
] ), |
| 44 |
[ |
| 45 |
'number' => null, |
| 46 |
] |
| 47 |
]; |
| 48 |
|
| 49 |
yield 'apply default' => [ |
| 50 |
[ |
| 51 |
'number' => [ |
| 52 |
ParamValidator::PARAM_TYPE => 'integer', |
| 53 |
ParamValidator::PARAM_REQUIRED => false, |
| 54 |
ParamValidator::PARAM_DEFAULT => 10, |
| 55 |
] |
| 56 |
], |
| 57 |
new RequestData( [ |
| 58 |
'bodyContents' => json_encode( (object)[] ), |
| 59 |
] ), |
| 60 |
[ |
| 61 |
'number' => 10, |
| 62 |
] |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function testValidateBody( array $settings, RequestData $requestData, array $expected ) { |
| 70 |
$validator = new JsonBodyValidator( $settings ); |
| 71 |
$actual = $validator->validateBody( $requestData ); |
| 72 |
$this->assertArrayEquals( $expected, $actual, false, true ); |
| 73 |
} |
| 74 |
|
| 75 |
public static function provideValidateBody_failure() { |
| 76 |
yield 'empty body' => [ |
| 77 |
[], |
| 78 |
new RequestData( [ |
| 79 |
'bodyContents' => '', |
| 80 |
] ), |
| 81 |
new LocalizedHttpException( new MessageValue( 'rest-json-body-parse-error' ), 400 ), |
| 82 |
]; |
| 83 |
|
| 84 |
yield 'bad syntax' => [ |
| 85 |
[], |
| 86 |
new RequestData( [ |
| 87 |
'bodyContents' => '.....', |
| 88 |
] ), |
| 89 |
new LocalizedHttpException( new MessageValue( 'rest-json-body-parse-error' ), 400 ), |
| 90 |
]; |
| 91 |
|
| 92 |
yield 'not an object' => [ |
| 93 |
[], |
| 94 |
new RequestData( [ |
| 95 |
'bodyContents' => json_encode( 'evil' ), |
| 96 |
] ), |
| 97 |
new LocalizedHttpException( new MessageValue( 'rest-bad-json-body' ), 400 ), |
| 98 |
]; |
| 99 |
|
| 100 |
yield 'missing optional' => [ |
| 101 |
[ |
| 102 |
'number' => [ |
| 103 |
ParamValidator::PARAM_TYPE => 'integer', |
| 104 |
ParamValidator::PARAM_REQUIRED => true, |
| 105 |
] |
| 106 |
], |
| 107 |
new RequestData( [ |
| 108 |
'bodyContents' => json_encode( (object)[ |
| 109 |
'kittens' => 'cute', |
| 110 |
] ), |
| 111 |
] ), |
| 112 |
new LocalizedHttpException( new MessageValue( 'rest-missing-body-field' ), 400 ), |
| 113 |
]; |
| 114 |
|
| 115 |
yield 'extraneous parameters' => [ |
| 116 |
[ |
| 117 |
'foo' => [ |
| 118 |
ParamValidator::PARAM_TYPE => 'integer', |
| 119 |
ParamValidator::PARAM_REQUIRED => false, |
| 120 |
] |
| 121 |
], |
| 122 |
new RequestData( [ |
| 123 |
'bodyContents' => json_encode( (object)[ |
| 124 |
'f00' => 123, |
| 125 |
'bar' => 456, |
| 126 |
] ), |
| 127 |
] ), |
| 128 |
new LocalizedHttpException( |
| 129 |
new MessageValue( |
| 130 |
'rest-extraneous-body-fields', |
| 131 |
[ new ListParam( ListType::COMMA, [ 'f00', 'bar' ] ) ] |
| 132 |
), |
| 133 |
400 |
| 134 |
), |
| 135 |
]; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function testValidateBody_failure( $settings, RequestData $requestData, $expected ) { |
| 142 |
$validator = new JsonBodyValidator( $settings ); |
| 143 |
|
| 144 |
$this->expectExceptionObject( $expected ); |
| 145 |
$validator->validateBody( $requestData ); |
| 146 |
} |
| 147 |
|
| 148 |
public function testOpenAPISpec() { |
| 149 |
$settings = [ |
| 150 |
'first' => [ |
| 151 |
ParamValidator::PARAM_TYPE => 'string', |
| 152 |
Validator::PARAM_SOURCE => 'path', |
| 153 |
], |
| 154 |
'second' => [ |
| 155 |
ParamValidator::PARAM_TYPE => 'float', |
| 156 |
Validator::PARAM_SOURCE => 'query', |
| 157 |
ParamValidator::PARAM_REQUIRED => false, |
| 158 |
], |
| 159 |
'third' => [ |
| 160 |
ParamValidator::PARAM_TYPE => [ 'a', 'b', 'c' ], |
| 161 |
Validator::PARAM_SOURCE => 'body', |
| 162 |
Validator::PARAM_DESCRIPTION => 'just a test', |
| 163 |
ParamValidator::PARAM_REQUIRED => true, |
| 164 |
], |
| 165 |
'fourth' => [ |
| 166 |
ParamValidator::PARAM_TYPE => 'timestamp', |
| 167 |
], |
| 168 |
]; |
| 169 |
$expected = [ |
| 170 |
'properties' => [ |
| 171 |
'first' => [ |
| 172 |
'type' => 'string', |
| 173 |
'description' => 'first parameter', |
| 174 |
], |
| 175 |
'second' => [ |
| 176 |
'type' => 'number', |
| 177 |
'format' => 'float', |
| 178 |
'description' => 'second parameter', |
| 179 |
], |
| 180 |
'third' => [ |
| 181 |
'type' => 'string', |
| 182 |
'enum' => [ 'a', 'b', 'c' ], |
| 183 |
'description' => 'just a test', |
| 184 |
], |
| 185 |
'fourth' => [ |
| 186 |
'type' => 'string', |
| 187 |
'format' => 'mw-timestamp', |
| 188 |
'description' => 'fourth parameter', |
| 189 |
], |
| 190 |
], |
| 191 |
'required' => [ |
| 192 |
'first', 'third' |
| 193 |
] |
| 194 |
]; |
| 195 |
|
| 196 |
$validator = new JsonBodyValidator( $settings ); |
| 197 |
$spec = $validator->getOpenAPISpec(); |
| 198 |
$this->assertArrayEquals( $expected, $spec, false, true ); |
| 199 |
} |
| 200 |
|
| 201 |
public function testOpenAPISpec_empty() { |
| 202 |
$validator = new JsonBodyValidator( [] ); |
| 203 |
$this->assertSame( [], $validator->getOpenAPISpec() ); |
| 204 |
} |
| 205 |
|
| 206 |
} |