Skip to content

Commit

Permalink
Merge pull request #14 from Treggats/refactor/tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mazedlx authored Mar 20, 2023
2 parents 4af1d28 + 830fa76 commit 0c4d3af
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 148 deletions.
7 changes: 2 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"minimum-stability": "stable",
"require": {
"php": "^7.1|^8.0|^8.1",
"php": "^7.4|^8.0|^8.1",
"illuminate/http": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0",
"illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0"
},
Expand All @@ -29,10 +29,7 @@
"autoload": {
"psr-4": {
"Mazedlx\\FeaturePolicy\\": "src"
},
"files": [
"src/helpers.php"
]
}
},
"autoload-dev": {
"psr-4": {
Expand Down
5 changes: 3 additions & 2 deletions src/AddFeaturePolicyHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Mazedlx\FeaturePolicy\Policies\Policy;

class AddFeaturePolicyHeaders
{
Expand All @@ -13,8 +14,8 @@ public function handle(Request $request, Closure $next, $customPolicyClass = nul
$response = $next($request);

$this->getPolicies($customPolicyClass)
->filter->shouldBeApplied($request, $response)
->each->applyTo($response);
->filter(fn (Policy $policy) => $policy->shouldBeApplied($request, $response))
->each(fn (Policy $policy) => $policy->applyTo($response));

return $response;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidFeaturePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public static function create($class): self
{
$className = get_class($class);

return new self("The Feature-Policy class '{$className}' is nit valid. A valid policy extends " . Policy::class);
return new self("The Feature-Policy class '{$className}' is not valid. A valid policy extends " . Policy::class);
}
}
1 change: 0 additions & 1 deletion src/helpers.php

This file was deleted.

145 changes: 45 additions & 100 deletions tests/AddFeaturePolicyHeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,51 @@

use Mazedlx\FeaturePolicy\Value;
use Mazedlx\FeaturePolicy\Directive;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\Facades\Route;
use Mazedlx\FeaturePolicy\Policies\Policy;
use Symfony\Component\HttpFoundation\HeaderBag;
use Mazedlx\FeaturePolicy\AddFeaturePolicyHeaders;
use Mazedlx\FeaturePolicy\Exceptions\InvalidFeaturePolicy;

class AddFeaturePolicyHeaderTest extends TestCase
class AddFeaturePolicyHeadersTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

app(Kernel::class)->pushMiddleware(AddFeaturePolicyHeaders::class);

Route::get('test-route', function () {
return 'ok';
});
}

/** @test */
public function it_sets_the_default_feature_policy_headers()
public function it_sets_the_default_feature_policy_headers(): void
{
$headers = $this->getResponseHeaders();
$permissionPolicyHeader = $this->get('test-route')
->assertSuccessful()
->headers
->get('Permissions-Policy');

$this->assertStringContainsString('geolocation=self', $headers->get('Permissions-Policy'));
$this->assertStringContainsString('geolocation=self', $permissionPolicyHeader);
}

/** @test */
public function it_wont_set_headers_if_it_is_not_enabled_in_the_config()
public function it_wont_set_headers_if_it_is_not_enabled_in_the_config(): void
{
config([
'feature-policy.enabled' => false,
]);

$headers = $this->getResponseHeaders();

$this->assertNull($headers->get('Permissions-Policy'));
$this->get('test-route')->assertHeaderMissing('Permissions-Policy');
}

/** @test */
public function it_throws_an_invalid_policy_class_exception_when_using_an_invalid_policy()
public function it_throws_an_invalid_policy_class_exception_when_using_an_invalid_policy(): void
{
$this->withoutExceptionHandling();

$invalidPolicyClassName = get_class(new class {
});

config(['feature-policy.policy' => $invalidPolicyClassName]);
config(['feature-policy.policy' => self::class]);

$this->expectException(InvalidFeaturePolicy::class);

$this->getResponseHeaders();
$this->get('test-route')->assertOk();
}

/** @test */
public function it_accepts_multiple_values_for_the_same_directive()
public function it_accepts_multiple_values_for_the_same_directive(): void
{
$policy = new class extends Policy {
public function configure()
public function configure(): void
{
$this->addDirective(Directive::CAMERA, 'src-1')
->addDirective(Directive::CAMERA, 'src-2')
Expand All @@ -74,136 +59,108 @@ public function configure()

config(['feature-policy.policy' => get_class($policy)]);

$headers = $this->getResponseHeaders();

$this->assertEquals(
'camera=("src-1" "src-2"),fullscreen=("src-3" "src-4")',
$headers->get('Permissions-Policy')
);
$this->get('test-route')
->assertHeader('Permissions-Policy', 'camera=("src-1" "src-2"),fullscreen=("src-3" "src-4")');
}

/** @test */
public function it_can_add_multiple_values_for_the_same_directive_in_one_go()
public function it_can_add_multiple_values_for_the_same_directive_in_one_go(): void
{
$policy = new class extends Policy {
public function configure()
public function configure(): void
{
$this->addDirective(Directive::CAMERA, ['src-1', 'src-2']);
}
};

config(['feature-policy.policy' => get_class($policy)]);

$headers = $this->getResponseHeaders();

$this->assertEquals(
'camera=("src-1" "src-2")',
$headers->get('Permissions-Policy')
);
$this->get('test-route')
->assertHeader('Permissions-Policy', 'camera=("src-1" "src-2")');
}

/** @test */
public function it_doesnt_quotes_special_directive_values()
public function it_doesnt_quotes_special_directive_values(): void
{
$policy = new class extends Policy {
public function configure()
public function configure(): void
{
$this->addDirective(Directive::CAMERA, [Value::SELF]);
}
};

config(['feature-policy.policy' => get_class($policy)]);

$headers = $this->getResponseHeaders();

$this->assertEquals(
'camera=self',
$headers->get('Permissions-Policy')
);
$this->get('test-route')
->assertHeader('Permissions-Policy', 'camera=self');
}

/** @test */
public function it_can_add_values_from_a_space_separated_string()
public function it_can_add_values_from_a_space_separated_string(): void
{
$policy = new class extends Policy {
public function configure()
public function configure(): void
{
$this->addDirective(Directive::CAMERA, 'src-1 ' . Value::SELF . ' src-2');
}
};

config(['feature-policy.policy' => get_class($policy)]);

$headers = $this->getResponseHeaders();

$this->assertEquals(
'camera=("src-1" self "src-2")',
$headers->get('Permissions-Policy')
);
$this->get('test-route')
->assertHeader('Permissions-Policy', 'camera=("src-1" self "src-2")');
}

/** @test */
public function it_will_not_add_duplicate_values()
public function it_will_not_add_duplicate_values(): void
{
$policy = new class extends Policy {
public function configure()
public function configure(): void
{
$this->addDirective(Directive::CAMERA, [Value::SELF, Value::SELF]);
}
};

config(['feature-policy.policy' => get_class($policy)]);

$headers = $this->getResponseHeaders();

$this->assertEquals(
'camera=self',
$headers->get('Permissions-Policy')
);
$this->get('test-route')
->assertHeader('Permissions-Policy', 'camera=self');
}

/** @test */
public function it_will_render_none_value()
public function it_will_render_none_value(): void
{
$policy = new class extends Policy {
public function configure()
public function configure(): void
{
$this->addDirective(Directive::CAMERA, [Value::NONE]);
}
};

config(['feature-policy.policy' => get_class($policy)]);

$headers = $this->getResponseHeaders();

$this->assertEquals(
'camera=()',
$headers->get('Permissions-Policy')
);
$this->get('test-route')
->assertHeader('Permissions-Policy', 'camera=()');
}

/** @test */
public function it_will_render_all_value()
public function it_will_render_all_value(): void
{
$policy = new class extends Policy {
public function configure()
public function configure(): void
{
$this->addDirective(Directive::CAMERA, [Value::ALL]);
}
};

config(['feature-policy.policy' => get_class($policy)]);

$headers = $this->getResponseHeaders();

$this->assertEquals(
'camera=*',
$headers->get('Permissions-Policy')
);
$this->get('test-route')
->assertHeader('Permissions-Policy', 'camera=*');
}

/** @test */
public function a_route_middleware_will_overwrite_a_global_middleware_for_a_given_route()
public function a_route_middleware_will_overwrite_a_global_middleware_for_a_given_route(): void
{
$this->withoutExceptionHandling();

Expand All @@ -214,22 +171,10 @@ public function configure()
}
};

Route::get('other-route', function () {
return 'ok';
})->middleware(AddFeaturePolicyHeaders::class . ':' . get_class($customPolicy));

$headers = $this->getResponseHeaders('other-route');

$this->assertEquals(
'fullscreen="custom-policy"',
$headers->get('Permissions-Policy')
);
}
Route::get('other-route', static fn () => 'ok')
->middleware(AddFeaturePolicyHeaders::class . ':' . get_class($customPolicy));

protected function getResponseHeaders(string $url = 'test-route'): HeaderBag
{
return $this->get($url)
->assertSuccessful()
->headers;
$this->get('other-route')
->assertHeader('Permissions-Policy', 'fullscreen="custom-policy"');
}
}
2 changes: 1 addition & 1 deletion tests/DirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class DirectiveTest extends TestCase
{
/** @test */
public function it_can_determine_if_a_directive_is_valid()
public function it_can_determine_if_a_directive_is_valid(): void
{
$this->assertTrue(Directive::isValid(Directive::GEOLOCATION));

Expand Down
38 changes: 0 additions & 38 deletions tests/MiddlewareTest.php

This file was deleted.

14 changes: 14 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@

namespace Mazedlx\FeaturePolicy\Tests;

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\Facades\Route;
use Mazedlx\FeaturePolicy\AddFeaturePolicyHeaders;
use Orchestra\Testbench\TestCase as Orchestra;
use Mazedlx\FeaturePolicy\FeaturePolicyServiceProvider;

abstract class TestCase extends Orchestra
{
public function setUp(): void
{
parent::setUp();

app(Kernel::class)->pushMiddleware(AddFeaturePolicyHeaders::class);

Route::get('test-route', function () {
return 'ok';
});
}

protected function getPackageProviders($app)
{
return [
Expand Down

0 comments on commit 0c4d3af

Please sign in to comment.