. */ declare(strict_types=1); namespace Tests\Unit\Helpers; use Route; use Symfony\Component\HttpFoundation\Response; use Tests\TestCase; /** * Class AuthenticateTest */ class AuthenticateTest extends TestCase { /** * @covers \FireflyIII\Http\Middleware\Authenticate::handle */ public function testMiddleware() { $this->withoutExceptionHandling(); $response = $this->get('/_test/authenticate'); $this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode()); $response->assertRedirect(route('login')); } /** * @covers \FireflyIII\Http\Middleware\Authenticate::handle */ public function testMiddlewareAjax() { $server = ['HTTP_X-Requested-With' => 'XMLHttpRequest']; $this->withoutExceptionHandling(); $response = $this->get('/_test/authenticate', $server); $this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); } /** * @covers \FireflyIII\Http\Middleware\Authenticate::handle */ public function testMiddlewareAuth() { $this->be($this->user()); $this->withoutExceptionHandling(); $response = $this->get('/_test/authenticate'); $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); } /** * @covers \FireflyIII\Http\Middleware\Authenticate::handle */ public function testMiddlewareBlockedUser() { $this->withoutExceptionHandling(); $user = $this->user(); $user->blocked = 1; $this->be($user); $response = $this->get('/_test/authenticate'); $this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode()); $response->assertSessionHas('logoutMessage', strval(trans('firefly.block_account_logout'))); $response->assertRedirect(route('login')); } /** * @covers \FireflyIII\Http\Middleware\Authenticate::handle */ public function testMiddlewareEmail() { $this->withoutExceptionHandling(); $user = $this->user(); $user->blocked = 1; $user->blocked_code = 'email_changed'; $this->be($user); $response = $this->get('/_test/authenticate'); $this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode()); $response->assertSessionHas('logoutMessage', strval(trans('firefly.email_changed_logout'))); $response->assertRedirect(route('login')); } /** * Set up test */ protected function setUp() { parent::setUp(); Route::middleware('auth')->any( '/_test/authenticate', function () { return 'OK'; } ); } }