mirror of
https://github.com/flarum/core.git
synced 2025-07-30 21:20:24 +02:00
API Client: Use new error handling mechanism
This commit is contained in:
@@ -17,7 +17,6 @@ use Flarum\Api\Client;
|
||||
use Flarum\Api\Controller\CreateGroupController;
|
||||
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
||||
use Flarum\Tests\integration\TestCase;
|
||||
use Flarum\User\Exception\PermissionDeniedException;
|
||||
use Flarum\User\Guest;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -63,11 +62,10 @@ class AuthenticateWithApiKeyTest extends TestCase
|
||||
{
|
||||
/** @var Client $api */
|
||||
$api = $this->app()->getContainer()->make(Client::class);
|
||||
$api->setErrorHandler(null);
|
||||
|
||||
$this->expectException(PermissionDeniedException::class);
|
||||
$response = $api->send(CreateGroupController::class, new Guest);
|
||||
|
||||
$api->send(CreateGroupController::class, new Guest);
|
||||
$this->assertEquals(403, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -53,8 +53,6 @@ abstract class ApiControllerTestCase extends TestCase
|
||||
/** @var Client $api */
|
||||
$api = $this->app()->getContainer()->make(Client::class);
|
||||
|
||||
$api->setErrorHandler(null);
|
||||
|
||||
return $api->send($controller, $actor ?? new Guest, $queryParams, $body);
|
||||
}
|
||||
}
|
||||
|
@@ -15,7 +15,6 @@ use Flarum\Api\Controller\CreateDiscussionController;
|
||||
use Flarum\Discussion\Discussion;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class CreateDiscussionControllerTest extends ApiControllerTestCase
|
||||
{
|
||||
@@ -72,11 +71,9 @@ class CreateDiscussionControllerTest extends ApiControllerTestCase
|
||||
$this->actor = User::find(1);
|
||||
|
||||
$data = Arr::except($this->data, 'content');
|
||||
$response = $this->callWith($data);
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
$this->expectExceptionMessage('The given data was invalid.');
|
||||
|
||||
$this->callWith($data);
|
||||
$this->assertEquals(422, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,10 +84,8 @@ class CreateDiscussionControllerTest extends ApiControllerTestCase
|
||||
$this->actor = User::find(1);
|
||||
|
||||
$data = Arr::except($this->data, 'title');
|
||||
$response = $this->callWith($data);
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
$this->expectExceptionMessage('The given data was invalid.');
|
||||
|
||||
$this->callWith($data);
|
||||
$this->assertEquals(422, $response->getStatusCode());
|
||||
}
|
||||
}
|
||||
|
@@ -13,11 +13,9 @@ namespace Flarum\Tests\integration\api\Controller;
|
||||
|
||||
use Flarum\Api\Controller\CreateGroupController;
|
||||
use Flarum\Group\Group;
|
||||
use Flarum\User\Exception\PermissionDeniedException;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class CreateGroupControllerTest extends ApiControllerTestCase
|
||||
{
|
||||
@@ -55,10 +53,7 @@ class CreateGroupControllerTest extends ApiControllerTestCase
|
||||
{
|
||||
$this->actor = User::find(1);
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
$this->expectExceptionMessage('The given data was invalid.');
|
||||
|
||||
$this->callWith();
|
||||
$this->assertEquals(422, $this->callWith()->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,8 +84,6 @@ class CreateGroupControllerTest extends ApiControllerTestCase
|
||||
{
|
||||
$this->actor = User::find(2);
|
||||
|
||||
$this->expectException(PermissionDeniedException::class);
|
||||
|
||||
$this->callWith($this->data);
|
||||
$this->assertEquals(403, $this->callWith($this->data)->getStatusCode());
|
||||
}
|
||||
}
|
||||
|
@@ -13,10 +13,8 @@ namespace Flarum\Tests\integration\api\Controller;
|
||||
|
||||
use Flarum\Api\Controller\CreateUserController;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\User\Exception\PermissionDeniedException;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class CreateUserControllerTest extends ApiControllerTestCase
|
||||
{
|
||||
@@ -53,10 +51,9 @@ class CreateUserControllerTest extends ApiControllerTestCase
|
||||
*/
|
||||
public function cannot_create_user_without_data()
|
||||
{
|
||||
$this->expectException(ValidationException::class);
|
||||
$this->expectExceptionMessage('The given data was invalid.');
|
||||
$response = $this->callWith();
|
||||
|
||||
$this->callWith();
|
||||
$this->assertEquals(422, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,12 +103,9 @@ class CreateUserControllerTest extends ApiControllerTestCase
|
||||
$settings = app(SettingsRepositoryInterface::class);
|
||||
$settings->set('allow_sign_up', false);
|
||||
|
||||
$this->expectException(PermissionDeniedException::class);
|
||||
$response = $this->callWith($this->data);
|
||||
$this->assertEquals(403, $response->getStatusCode());
|
||||
|
||||
try {
|
||||
$this->callWith($this->data);
|
||||
} finally {
|
||||
$settings->set('allow_sign_up', true);
|
||||
}
|
||||
$settings->set('allow_sign_up', true);
|
||||
}
|
||||
}
|
||||
|
@@ -12,7 +12,6 @@
|
||||
namespace Flarum\Tests\integration\api\Controller;
|
||||
|
||||
use Flarum\Api\Controller\ListNotificationsController;
|
||||
use Flarum\User\Exception\PermissionDeniedException;
|
||||
use Flarum\User\User;
|
||||
|
||||
class ListNotificationsControllerTest extends ApiControllerTestCase
|
||||
@@ -35,9 +34,9 @@ class ListNotificationsControllerTest extends ApiControllerTestCase
|
||||
*/
|
||||
public function disallows_index_for_guest()
|
||||
{
|
||||
$this->expectException(PermissionDeniedException::class);
|
||||
$response = $this->callWith();
|
||||
|
||||
$this->callWith();
|
||||
$this->assertEquals(403, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -12,7 +12,6 @@
|
||||
namespace Flarum\Tests\integration\api\Controller;
|
||||
|
||||
use Flarum\Api\Controller\ListUsersController;
|
||||
use Flarum\User\Exception\PermissionDeniedException;
|
||||
use Flarum\User\User;
|
||||
|
||||
class ListUsersControllerTest extends ApiControllerTestCase
|
||||
@@ -41,9 +40,9 @@ class ListUsersControllerTest extends ApiControllerTestCase
|
||||
*/
|
||||
public function disallows_index_for_guest()
|
||||
{
|
||||
$this->expectException(PermissionDeniedException::class);
|
||||
$response = $this->callWith();
|
||||
|
||||
$this->callWith();
|
||||
$this->assertEquals(403, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -15,7 +15,6 @@ use Carbon\Carbon;
|
||||
use Flarum\Api\Controller\ShowDiscussionController;
|
||||
use Flarum\Discussion\Discussion;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
class ShowDiscussionControllerTest extends ApiControllerTestCase
|
||||
{
|
||||
@@ -73,11 +72,9 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase
|
||||
*/
|
||||
public function guest_cannot_see_empty_discussion()
|
||||
{
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
|
||||
$response = $this->callWith([], ['id' => 1]);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals(404, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,8 +92,8 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase
|
||||
*/
|
||||
public function guests_cannot_see_private_discussion()
|
||||
{
|
||||
$this->expectException(ModelNotFoundException::class);
|
||||
$response = $this->callWith([], ['id' => 3]);
|
||||
|
||||
$this->callWith([], ['id' => 3]);
|
||||
$this->assertEquals(404, $response->getStatusCode());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user