Skip to content

Commit

Permalink
fix a test and add more coverage for the getRemoteDump function.
Browse files Browse the repository at this point in the history
  • Loading branch information
holyfabi committed Feb 16, 2022
1 parent 3528d87 commit 41e677e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 16 deletions.
75 changes: 73 additions & 2 deletions Tests/RemoteDumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Orchestra\Testbench\TestCase;
use ReflectionClass;
use ReflectionMethod;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class RemoteDumpTest extends TestCase
{
Expand Down Expand Up @@ -64,14 +66,83 @@ public function htaccessIsInRequestHeaderWhenSpecified()
Config::set('protector.remoteEndpoint.htaccessLogin', '1234:1234');
Config::set('protector.routeMiddleware', []);

Http::fake();
// 401 Status shouldn't be the default behaviour, because while Sanctum is deactivated, we decrypt still every time.
$this->expectException(UnauthorizedHttpException::class);
$serverUrl = app('protector')->getServerUrl();

Http::fake([
$serverUrl => Http::response([], 401)
]);

app('protector')->getRemoteDump();
Http::assertSent(function ($request) {
return $request->hasHeader('Authorization', 'Basic ' . base64_encode('1234:1234'));
});
}

/**
* @test
*/
public function throwUnauthorizedExceptionIfUnauthorized()
{
Config::set('protector.remoteEndpoint.htaccessLogin', '1234:1234');
Config::set('protector.routeMiddleware', []);

$serverUrl = app('protector')->getServerUrl();
$statusCodes = [401, 403];

foreach ($statusCodes as $statusCode) {
$this->expectException(UnauthorizedHttpException::class);

Http::fake([
$serverUrl => Http::response([], $statusCode)
]);

app('protector')->getRemoteDump();
}
}

/**
* @test
*/
public function failOnUnknownRoute()
{
Config::set('protector.routeMiddleware', []);
Config::set('protector.remoteEndpoint.htaccessLogin', '1234:1234');

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

$serverUrl = app('protector')->getServerUrl();

Http::fake([
$serverUrl => Http::response([], 404)
]);

app('protector')->getRemoteDump();
}

/**
* @test
*/
public function checkForSuccessfulDecryption()
{
$message = env('PROTECTOR_DECRYPTED_MESSAGE');
$encryptedMessage = sodium_hex2bin(env('PROTECTOR_ENCRYPTED_MESSAGE'));

$serverUrl = app('protector')->getServerUrl();
$headers = ['Content-Disposition' => 'attachment; filename="HelloWorld.txt"'];
$disk = app('protector')->getDisk();

Http::fake([
$serverUrl => Http::response($encryptedMessage, 200, $headers)
]);

$destinationFilepath = app('protector')->getRemoteDump();

$this->assertFileExists($disk->path($destinationFilepath));
$this->assertEquals($message, $disk->get($destinationFilepath));
}

public function responseCodes()
{
return [
Expand Down Expand Up @@ -119,7 +190,7 @@ public function failIfLaravelSanctumIsActiveAndHtaccessIsDefined()
protected function getAccessibleReflectionMethod($method): ReflectionMethod
{
$reflectionProtector = new ReflectionClass(app('protector'));
$method = $reflectionProtector->getMethod($method);
$method = $reflectionProtector->getMethod($method);

$method->setAccessible(true);

Expand Down
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@
}
],
"require": {
"php": "^7.2|^8.0",
"ext-curl": "*",
"ext-json": "*",
"ext-openssl": "*",
"ext-sodium": "*",
"illuminate/support": "^7.0|^8.0",
"laravel/sanctum": "^2.4",
"php": "^7.2|^8.0"
"guzzlehttp/guzzle": "^7.4",
"illuminate/support": "^8.0|^9.0",
"laravel/framework": "^8.0|^9.0",
"laravel/sanctum": "^2.4"
},
"require-dev": {
"orchestra/testbench": "^5.0",
"phpunit/phpunit": "^8.0"
"orchestra/testbench": "^6.23",
"phpunit/phpunit": "^9.5"
},
"autoload": {
"psr-4": {
Expand Down
26 changes: 17 additions & 9 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@
<directory>Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<junit outputFile="build/report.junit.xml"/>
</logging>
<php>
<env name="PROTECTOR_PRIVATE_KEY" value="e195f1252346e31fe7b87e899f69a87d6fe99f38bd6e7c3cbdde411fcd9cc93e2c1d0ea6e0f8e207b38bef11bfcd5c0615c3cf4695876631b1da523a552b6022" />
<env name="PROTECTOR_PUBLIC_KEY" value="2c1d0ea6e0f8e207b38bef11bfcd5c0615c3cf4695876631b1da523a552b6022" />
<env name="PROTECTOR_ENCRYPTED_MESSAGE" value="c93e1c30857b0a5a36dddd11237ff65ef153144e0c400304cc80501da7e9b41a99c568fb34491629577ba4b5c0ec632e50067d61cf1ece27e1eabc" />
<env name="PROTECTOR_DECRYPTED_MESSAGE" value="hello world" />
</php>
</phpunit>

0 comments on commit 41e677e

Please sign in to comment.