firefly-iii/tests/Unit/Middleware/SandstormTest.php

184 lines
5.8 KiB
PHP
Raw Normal View History

2017-12-26 10:33:53 -06:00
<?php
/**
* SandstormTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2018-01-25 13:38:50 -06:00
namespace Tests\Unit\Middleware;
2017-12-26 10:33:53 -06:00
use FireflyIII\Http\Middleware\Sandstorm;
2018-01-25 11:41:27 -06:00
use FireflyIII\Models\Role;
2017-12-26 10:33:53 -06:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Route;
use Symfony\Component\HttpFoundation\Response;
use Tests\TestCase;
/**
* Class RangeTest
*/
class SandstormTest extends TestCase
{
/**
* @covers \FireflyIII\Http\Middleware\Sandstorm::handle
*/
public function testMiddlewareAnonEmpty(): void
2017-12-26 10:33:53 -06:00
{
putenv('SANDSTORM=1');
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('count')->once()->andReturn(0);
$response = $this->get('/_test/sandstorm');
$this->assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $response->getStatusCode());
$response->assertSee('The first visit to a new Firefly III administration cannot be by a guest user.');
putenv('SANDSTORM=0');
}
/**
* @covers \FireflyIII\Http\Middleware\Sandstorm::handle
*/
public function testMiddlewareAnonLoggedIn(): void
2017-12-26 10:33:53 -06:00
{
putenv('SANDSTORM=1');
2017-12-29 02:05:35 -06:00
$this->be($this->user());
2017-12-26 10:33:53 -06:00
$response = $this->get('/_test/sandstorm');
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
$response->assertSee('sandstorm-anon: true');
putenv('SANDSTORM=0');
}
/**
* @covers \FireflyIII\Http\Middleware\Sandstorm::handle
*/
public function testMiddlewareAnonUser(): void
2017-12-26 10:33:53 -06:00
{
putenv('SANDSTORM=1');
2017-12-29 02:05:35 -06:00
$repository = $this->mock(UserRepositoryInterface::class);
2018-01-25 11:41:27 -06:00
$repository->shouldReceive('count')->twice()->andReturn(1);
2018-07-01 02:27:22 -05:00
$repository->shouldReceive('hasRole')->andReturn(true);
2017-12-29 02:05:35 -06:00
$response = $this->get('/_test/sandstorm');
2017-12-26 10:33:53 -06:00
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
2017-12-29 02:05:35 -06:00
$response->assertSee('sandstorm-anon: true');
2017-12-26 10:33:53 -06:00
putenv('SANDSTORM=0');
}
/**
* @covers \FireflyIII\Http\Middleware\Sandstorm::handle
*/
public function testMiddlewareLoggedIn(): void
2017-12-26 10:33:53 -06:00
{
putenv('SANDSTORM=1');
$this->be($this->user());
2017-12-29 02:05:35 -06:00
$response = $this->get('/_test/sandstorm', ['X-Sandstorm-User-Id' => 'abcd']);
2017-12-26 10:33:53 -06:00
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
2017-12-29 02:05:35 -06:00
$response->assertSee('sandstorm-anon: false');
2017-12-26 10:33:53 -06:00
putenv('SANDSTORM=0');
}
/**
* @covers \FireflyIII\Http\Middleware\Sandstorm::handle
*/
public function testMiddlewareMultiUser(): void
2017-12-26 10:33:53 -06:00
{
putenv('SANDSTORM=1');
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('count')->once()->andReturn(2);
$response = $this->get('/_test/sandstorm');
$this->assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $response->getStatusCode());
$response->assertSee('Your Firefly III installation has more than one user, which is weird.');
putenv('SANDSTORM=0');
}
/**
* @covers \FireflyIII\Http\Middleware\Sandstorm::handle
*/
public function testMiddlewareNoUser(): void
2017-12-26 10:33:53 -06:00
{
putenv('SANDSTORM=1');
$repository = $this->mock(UserRepositoryInterface::class);
2018-01-25 11:41:27 -06:00
$repository->shouldReceive('count')->twice()->andReturn(0);
2017-12-26 10:33:53 -06:00
$repository->shouldReceive('store')->once()->andReturn($this->user());
$repository->shouldReceive('attachRole')->once()->andReturn(true);
//$repository->shouldReceive('getRole')->once()->andReturn(new Role);
2018-07-01 02:27:22 -05:00
$repository->shouldReceive('hasRole')->andReturn(false);
2017-12-26 10:33:53 -06:00
$response = $this->get('/_test/sandstorm', ['X-Sandstorm-User-Id' => 'abcd']);
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
$response->assertSee('sandstorm-anon: false');
putenv('SANDSTORM=0');
}
/**
* @covers \FireflyIII\Http\Middleware\Sandstorm::handle
*/
public function testMiddlewareNotSandstorm(): void
2017-12-26 10:33:53 -06:00
{
$this->withoutExceptionHandling();
$response = $this->get('/_test/sandstorm');
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
}
/**
* @covers \FireflyIII\Http\Middleware\Sandstorm::handle
*/
public function testMiddlewareOneUser(): void
2017-12-26 10:33:53 -06:00
{
putenv('SANDSTORM=1');
$repository = $this->mock(UserRepositoryInterface::class);
2018-01-25 11:41:27 -06:00
$repository->shouldReceive('count')->twice()->andReturn(1);
2017-12-26 10:33:53 -06:00
$repository->shouldReceive('first')->once()->andReturn($this->user());
2018-07-01 02:27:22 -05:00
$repository->shouldReceive('hasRole')->andReturn(true);
2017-12-26 10:33:53 -06:00
$response = $this->get('/_test/sandstorm', ['X-Sandstorm-User-Id' => 'abcd']);
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
$response->assertSee('sandstorm-anon: false');
putenv('SANDSTORM=0');
}
/**
* Set up test
*/
protected function setUp(): void
2017-12-26 10:33:53 -06:00
{
parent::setUp();
Route::middleware(Sandstorm::class)->any(
'/_test/sandstorm', function () {
return view('test.test');
}
);
}
2018-03-04 08:14:29 -06:00
}