This commit is contained in:
James Brooks
2020-08-01 09:14:58 +01:00
parent dd1248a919
commit 90b450031f
8 changed files with 8 additions and 8 deletions
@@ -0,0 +1,32 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Providers;
use AltThree\TestBench\ServiceProviderTrait;
use CachetHQ\Cachet\Services\Dates\DateFactory;
use CachetHQ\Tests\Cachet\AbstractTestCase;
/**
* This is the app service provider test class.
*
* @author Graham Campbell <graham@alt-three.com>
* @author James Brooks <james@alt-three.com>
*/
class AppServiceProviderTest extends AbstractTestCase
{
use ServiceProviderTrait;
public function testDateFactoryIsInjectable()
{
$this->assertIsInjectable(DateFactory::class);
}
}
@@ -0,0 +1,25 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Providers;
use AltThree\TestBench\ServiceProviderTrait;
use CachetHQ\Tests\Cachet\AbstractTestCase;
/**
* This is the composer service provider test class.
*
* @author Graham Campbell <graham@alt-three.com>
*/
class ComposerServiceProviderTest extends AbstractTestCase
{
use ServiceProviderTrait;
}
@@ -0,0 +1,37 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Providers;
use AltThree\TestBench\ServiceProviderTrait;
use CachetHQ\Cachet\Settings\Cache;
use CachetHQ\Cachet\Settings\Repository;
use CachetHQ\Tests\Cachet\AbstractTestCase;
/**
* This is the config service provider test class.
*
* @author Graham Campbell <graham@alt-three.com>
*/
class ConfigServiceProviderTest extends AbstractTestCase
{
use ServiceProviderTrait;
public function testCacheIsInjectable()
{
$this->assertIsInjectable(Cache::class);
}
public function testRepositoryIsInjectable()
{
$this->assertIsInjectable(Repository::class);
}
}
@@ -0,0 +1,25 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Providers;
use AltThree\TestBench\ServiceProviderTrait;
use CachetHQ\Tests\Cachet\AbstractTestCase;
/**
* This is the console service provider test class.
*
* @author Graham Campbell <graham@alt-three.com>
*/
class ConsoleServiceProviderTest extends AbstractTestCase
{
use ServiceProviderTrait;
}
@@ -0,0 +1,41 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Providers;
use AltThree\TestBench\EventServiceProviderTrait;
use CachetHQ\Tests\Cachet\AbstractTestCase;
use Illuminate\Support\ServiceProvider;
use ReflectionClass;
/**
* This is the event service provider test class.
*
* @author Graham Campbell <graham@alt-three.com>
* @author James Brooks <james@alt-three.com>
*/
class EventServiceProviderTest extends AbstractTestCase
{
use EventServiceProviderTrait;
public function testIsAnEventServiceProvider()
{
$class = $this->getServiceProviderClass($this->app);
$reflection = new ReflectionClass($class);
$provider = new ReflectionClass(ServiceProvider::class);
$msg = "Expected class '$class' to be a service provider.";
$this->assertTrue($reflection->isSubclassOf($provider), $msg);
}
}
@@ -0,0 +1,56 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Providers;
use AltThree\TestBench\ServiceProviderTrait;
use CachetHQ\Cachet\Integrations\Contracts\Beacon;
use CachetHQ\Cachet\Integrations\Contracts\Credits;
use CachetHQ\Cachet\Integrations\Contracts\Feed;
use CachetHQ\Cachet\Integrations\Contracts\Releases;
use CachetHQ\Cachet\Integrations\Contracts\System;
use CachetHQ\Tests\Cachet\AbstractTestCase;
/**
* This is the integration service provider test class.
*
* @author James Brooks <james@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
*/
class IntegrationServiceProviderTest extends AbstractTestCase
{
use ServiceProviderTrait;
public function testBeaconIsInjectable()
{
$this->assertIsInjectable(Beacon::class);
}
public function testCreditsIsInjectable()
{
$this->assertIsInjectable(Credits::class);
}
public function testFeedIsInjectable()
{
$this->assertIsInjectable(Feed::class);
}
public function testSystemIsInjectable()
{
$this->assertIsInjectable(System::class);
}
public function testReleasesIsInjectable()
{
$this->assertIsInjectable(Releases::class);
}
}
@@ -0,0 +1,31 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Providers;
use AltThree\TestBench\ServiceProviderTrait;
use CachetHQ\Cachet\Repositories\Metric\MetricRepository;
use CachetHQ\Tests\Cachet\AbstractTestCase;
/**
* This is the repository service provider test class.
*
* @author Graham Campbell <graham@alt-three.com>
*/
class RepositoryServiceProviderTest extends AbstractTestCase
{
use ServiceProviderTrait;
public function testMetricRepositoryIsInjectable()
{
$this->assertIsInjectable(MetricRepository::class);
}
}
@@ -0,0 +1,217 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Providers;
use AltThree\TestBench\ServiceProviderTrait;
use CachetHQ\Cachet\Foundation\Providers\RouteServiceProvider;
use CachetHQ\Cachet\Http\Middleware\Authenticate;
use CachetHQ\Tests\Cachet\AbstractTestCase;
use Illuminate\Routing\Route;
use Illuminate\Routing\RouteCollection;
use Illuminate\Routing\Router;
/**
* This is the route service provider test class.
*
* @author Graham Campbell <graham@alt-three.com>
*/
class RouteServiceProviderTest extends AbstractTestCase
{
use ServiceProviderTrait;
/**
* The login routes should always be available regardless of the always authenticate setting.
*/
public function testWhenAlwaysAuthenticateIsEnabledLoginRoutesAreWhiteListed()
{
$loginRoutes = [
'core::get:auth.login',
'core::post:auth.login',
'core::post:auth.two-factor',
'core::get:auth.logout',
'core::get:signup.invite',
'core::post:signup.invite',
];
$this->assertRoutesDontHaveAuthMiddleware($loginRoutes, $this->bootRouter(true));
}
/**
* The setup routes should always be available regardless of the always authenticate setting.
*/
public function testWhenAlwaysAuthenticateIsEnabledSetupRoutesAreWhiteListed()
{
$loginRoutes = [
'core::get:setup',
'core::post:setup.step1',
'core::post:setup.step2',
'core::post:setup.step3',
];
$this->assertRoutesDontHaveAuthMiddleware($loginRoutes, $this->bootRouter(true));
}
/**
* It's possible to retrieve the cachet version, status and ping endpoints regardless of the
* always authenticate setting.
*/
public function testWhenAlwaysAuthenticateIsEnabledApiSystemRoutesAreWhiteListed()
{
$routeActions = [
'CachetHQ\Cachet\Http\Controllers\Api\GeneralController@ping',
'CachetHQ\Cachet\Http\Controllers\Api\GeneralController@version',
'CachetHQ\Cachet\Http\Controllers\Api\GeneralController@status',
];
$router = $this->bootRouter(true);
foreach ($routeActions as $routeAction) {
$route = $router->getRoutes()->getByAction($routeAction);
$this->assertInstanceOf(Route::class, $route);
$middleware = $route->gatherMiddleware();
$this->assertFalse(in_array('auth.api:true', $middleware, true));
}
}
/**
* When using always authenticate, normal graceful api routes will require full authentication.
*/
public function testWhenAlwaysAuthenticateIsEnabledApiRoutesAreHardAuthenticated()
{
$routeActions = [
'CachetHQ\Cachet\Http\Controllers\Api\ComponentController@index',
'CachetHQ\Cachet\Http\Controllers\Api\ComponentGroupController@index',
'CachetHQ\Cachet\Http\Controllers\Api\ComponentGroupController@show',
'CachetHQ\Cachet\Http\Controllers\Api\ComponentController@show',
'CachetHQ\Cachet\Http\Controllers\Api\IncidentController@index',
'CachetHQ\Cachet\Http\Controllers\Api\IncidentController@show',
'CachetHQ\Cachet\Http\Controllers\Api\IncidentUpdateController@index',
'CachetHQ\Cachet\Http\Controllers\Api\IncidentUpdateController@show',
'CachetHQ\Cachet\Http\Controllers\Api\MetricController@index',
'CachetHQ\Cachet\Http\Controllers\Api\MetricController@show',
'CachetHQ\Cachet\Http\Controllers\Api\MetricPointController@index',
'CachetHQ\Cachet\Http\Controllers\Api\ScheduleController@index',
'CachetHQ\Cachet\Http\Controllers\Api\ScheduleController@show',
];
$router = $this->bootRouter(true);
foreach ($routeActions as $routeAction) {
$route = $router->getRoutes()->getByAction($routeAction);
$this->assertInstanceOf(Route::class, $route);
$middleware = $route->gatherMiddleware();
$this->assertTrue(in_array('auth.api:true', $middleware, true));
}
}
/**
* When enabling the always authenticate setting, the core frontpage routes require authentication.
*/
public function testWhenAlwaysAuthenticateIsEnabledAllNormalRoutesAreAuthenticated()
{
$namedRoutes = [
'core::get:status-page',
'core::get:incident',
'core::get:schedule',
'core::get:metric',
'core::get:component_shield',
'core::get:subscribe',
'core::post:subscribe',
'core::get:subscribe.manage',
'core::post:subscribe.manage',
'core::get:subscribe.verify',
'core::get:subscribe.unsubscribe',
];
$this->assertRoutesHaveAuthMiddleware($namedRoutes, $this->bootRouter(true));
}
/**
* This test asserts that when always authenticate is disabled, you are allowed to visit the frontpage
* routes without enforced authentication.
*/
public function testWhenAlwaysAuthenticateIsDisabledAllNormalRoutesAreUnauthenticated()
{
$namedRoutes = [
'core::get:status-page',
'core::get:incident',
'core::get:schedule',
'core::get:metric',
'core::get:component_shield',
'core::get:subscribe',
'core::post:subscribe',
'core::get:subscribe.manage',
'core::post:subscribe.manage',
'core::get:subscribe.verify',
'core::get:subscribe.unsubscribe',
];
$this->assertRoutesDontHaveAuthMiddleware($namedRoutes, $this->bootRouter(false));
}
/**
* A helper method that will execute the RouteProvider's map function and return a clean router.
*
* @param bool $alwaysAuthenticate
*
* @return Router
*/
private function bootRouter($alwaysAuthenticate)
{
$this->app->config->set('setting.always_authenticate', $alwaysAuthenticate);
$router = $this->app->make(Router::class);
$router->setRoutes(new RouteCollection());
$routeServiceProvider = new RouteServiceProvider($this->app);
$routeServiceProvider->map($router);
return $router;
}
/**
* Assertion helper that asserts if the authentication middleware has not been injected onto
* the collection of named routes.
*
* @param array $routeNames
* @param Router $router
*/
private function assertRoutesDontHaveAuthMiddleware(array $routeNames, Router $router)
{
foreach ($routeNames as $routeName) {
$route = $router->getRoutes()->getByName($routeName);
$this->assertInstanceOf(Route::class, $route);
$middleware = $route->gatherMiddleware();
$this->assertFalse(in_array(Authenticate::class, $middleware, true));
}
}
/**
* Assertion helper that asserts if the authentication middleware has been injected onto
* the collection of named routes.
*
* @param array $routeNames
* @param Router $router
*/
private function assertRoutesHaveAuthMiddleware(array $routeNames, Router $router)
{
foreach ($routeNames as $routeName) {
$route = $router->getRoutes()->getByName($routeName);
$this->assertInstanceOf(Route::class, $route);
$middleware = $route->gatherMiddleware();
$this->assertTrue(in_array(Authenticate::class, $middleware, true));
}
}
}