Commit e052250d authored by Karamel's avatar Karamel
Browse files

Add response 400 for invalid role in user route, add test.

parent 869e0a90
<?php
use \Pasteque\Server\Exception\InvalidFieldException;
use \Pasteque\Server\Model\User;
use \Pasteque\Server\System\API\APICaller;
use \Pasteque\Server\System\API\APIResult;
......@@ -75,7 +76,11 @@ $app->POST('/api/user',
if ($user === null) {
$user = new User();
}
$user->merge($tab, $ptApp->getDao());
try {
$user->merge($tab, $ptApp->getDao());
} catch (InvalidFieldException $e) {
return $response->reject($e);
}
return $response->withApiResult(APICaller::run($ptApp, 'user', 'write',
$user));
});
......@@ -173,4 +173,25 @@ class HttpUserTest extends TestCase
$this->assertEquals(1, count($dbUser));
$this->assertEquals('Edited User', $dbUser[0]->getName());
}
public function testPostInvalidRole() {
$newUser = new User();
$newUser->setName('New User');
$newUser->setRole($this->role);
$postData = $newUser->toStruct();
unset($postData['id']);
$postData['role'] = $this->role->getId() + 1;
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/user'));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postData);
$resp = curl_exec($this->curl);
$this->assertEquals(400, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('InvalidField', $jsResp['error']);
$this->assertEquals('AssociationNotFound', $jsResp['constraint']);
$this->assertEquals(User::class, $jsResp['class']);
$this->assertEquals('role', $jsResp['field']);
$this->assertEquals($this->role->getId() + 1, $jsResp['value']);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment