throw error on goodie page if no goodie

This commit is contained in:
Xu 2024-04-15 22:33:17 +02:00 committed by Igor Scheller
parent 0fb09280b3
commit 4a0f5c2e78
2 changed files with 55 additions and 3 deletions

View File

@ -9,6 +9,7 @@ use Engelsystem\Config\GoodieType;
use Engelsystem\Controllers\BaseController; use Engelsystem\Controllers\BaseController;
use Engelsystem\Controllers\HasUserNotifications; use Engelsystem\Controllers\HasUserNotifications;
use Engelsystem\Helpers\Authenticator; use Engelsystem\Helpers\Authenticator;
use Engelsystem\Http\Exceptions\HttpNotFound;
use Engelsystem\Http\Redirector; use Engelsystem\Http\Redirector;
use Engelsystem\Http\Request; use Engelsystem\Http\Request;
use Engelsystem\Http\Response; use Engelsystem\Http\Response;
@ -35,8 +36,16 @@ class UserGoodieController extends BaseController
) { ) {
} }
private function checkActive(): void
{
if (GoodieType::from(config('goodie_type')) == GoodieType::None) {
throw new HttpNotFound();
}
}
public function editGoodie(Request $request): Response public function editGoodie(Request $request): Response
{ {
$this->checkActive();
$userId = (int) $request->getAttribute('user_id'); $userId = (int) $request->getAttribute('user_id');
$user = $this->user->findOrFail($userId); $user = $this->user->findOrFail($userId);
@ -52,6 +61,7 @@ class UserGoodieController extends BaseController
public function saveGoodie(Request $request): Response public function saveGoodie(Request $request): Response
{ {
$this->checkActive();
$userId = (int) $request->getAttribute('user_id'); $userId = (int) $request->getAttribute('user_id');
$shirtEnabled = $this->config->get('goodie_type') === GoodieType::Tshirt->value; $shirtEnabled = $this->config->get('goodie_type') === GoodieType::Tshirt->value;
/** @var User $user */ /** @var User $user */

View File

@ -7,6 +7,7 @@ namespace Engelsystem\Test\Unit\Controllers\Admin;
use Engelsystem\Config\GoodieType; use Engelsystem\Config\GoodieType;
use Engelsystem\Controllers\Admin\UserGoodieController; use Engelsystem\Controllers\Admin\UserGoodieController;
use Engelsystem\Helpers\Authenticator; use Engelsystem\Helpers\Authenticator;
use Engelsystem\Http\Exceptions\HttpNotFound;
use Engelsystem\Http\Exceptions\ValidationException; use Engelsystem\Http\Exceptions\ValidationException;
use Engelsystem\Http\Redirector; use Engelsystem\Http\Redirector;
use Engelsystem\Http\Validation\Validator; use Engelsystem\Http\Validation\Validator;
@ -28,6 +29,7 @@ class UserGoodieControllerTest extends ControllerTest
*/ */
public function testIndex(): void public function testIndex(): void
{ {
$this->config->set('goodie_type', GoodieType::Tshirt->value);
$request = $this->request->withAttribute('user_id', 1); $request = $this->request->withAttribute('user_id', 1);
/** @var Authenticator|MockObject $auth */ /** @var Authenticator|MockObject $auth */
$auth = $this->createMock(Authenticator::class); $auth = $this->createMock(Authenticator::class);
@ -48,6 +50,7 @@ class UserGoodieControllerTest extends ControllerTest
*/ */
public function testIndexUserNotFound(): void public function testIndexUserNotFound(): void
{ {
$this->config->set('goodie_type', GoodieType::Goodie->value);
/** @var Authenticator|MockObject $auth */ /** @var Authenticator|MockObject $auth */
$auth = $this->createMock(Authenticator::class); $auth = $this->createMock(Authenticator::class);
/** @var Redirector|MockObject $redirector */ /** @var Redirector|MockObject $redirector */
@ -60,6 +63,44 @@ class UserGoodieControllerTest extends ControllerTest
$controller->editGoodie($this->request); $controller->editGoodie($this->request);
} }
/**
* @covers \Engelsystem\Controllers\Admin\UserGoodieController::editGoodie
* @covers \Engelsystem\Controllers\Admin\UserGoodieController::checkActive
*/
public function testEditShirtGoodieNone(): void
{
$this->config->set('goodie_type', GoodieType::None->value);
/** @var Authenticator|MockObject $auth */
$auth = $this->createMock(Authenticator::class);
/** @var Redirector|MockObject $redirector */
$redirector = $this->createMock(Redirector::class);
$user = new User();
$controller = new UserGoodieController($auth, $this->config, $this->log, $redirector, $this->response, $user);
$this->expectException(HttpNotFound::class);
$controller->editGoodie($this->request);
}
/**
* @covers \Engelsystem\Controllers\Admin\UserGoodieController::saveGoodie
* @covers \Engelsystem\Controllers\Admin\UserGoodieController::checkActive
*/
public function testSaveShirtGoodieNone(): void
{
$this->config->set('goodie_type', GoodieType::None->value);
/** @var Authenticator|MockObject $auth */
$auth = $this->createMock(Authenticator::class);
/** @var Redirector|MockObject $redirector */
$redirector = $this->createMock(Redirector::class);
$user = new User();
$controller = new UserGoodieController($auth, $this->config, $this->log, $redirector, $this->response, $user);
$this->expectException(HttpNotFound::class);
$controller->saveGoodie($this->request);
}
/** /**
* @todo Factor out separate tests. Isolated User, Config and permissions per test. * @todo Factor out separate tests. Isolated User, Config and permissions per test.
* @covers \Engelsystem\Controllers\Admin\UserGoodieController::saveGoodie * @covers \Engelsystem\Controllers\Admin\UserGoodieController::saveGoodie
@ -156,8 +197,8 @@ class UserGoodieControllerTest extends ControllerTest
$user = User::find(1); $user = User::find(1);
$this->assertFalse($user->state->arrived); $this->assertFalse($user->state->arrived);
// Goodie disabled // Goodie enabled but not a shirt
$this->config->set('goodie_type', GoodieType::None->value); $this->config->set('goodie_type', GoodieType::Goodie->value);
$request = $request $request = $request
->withParsedBody([ ->withParsedBody([
'shirt_size' => 'XS', 'shirt_size' => 'XS',
@ -184,6 +225,7 @@ class UserGoodieControllerTest extends ControllerTest
*/ */
public function testSaveGoodieUserNotFound(): void public function testSaveGoodieUserNotFound(): void
{ {
$this->config->set('goodie_type', GoodieType::Goodie->value);
/** @var Authenticator|MockObject $auth */ /** @var Authenticator|MockObject $auth */
$auth = $this->createMock(Authenticator::class); $auth = $this->createMock(Authenticator::class);
/** @var Redirector|MockObject $redirector */ /** @var Redirector|MockObject $redirector */