Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jun 13, 2019
1 parent 1ebfe27 commit 32bd0c1
Showing 1 changed file with 45 additions and 7 deletions.
52 changes: 45 additions & 7 deletions tests/WebhookControllerTest.php
Expand Up @@ -3,13 +3,24 @@
namespace Spatie\WebhookClient\Tests;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Route;
use Spatie\WebhookClient\Models\WebhookCall;
use Spatie\WebhookClient\Tests\TestClasses\ProcessWebhookJobTestClass;

class WebhookControllerTest extends TestCase
{
/** @var array */
private $payload;

/** @var string */
private $signature;

/** @var array */
private $headers;

public function setUp(): void
{
parent::setUp();
Expand All @@ -20,19 +31,21 @@ public function setUp(): void
Route::webhooks('incoming-webhooks');

Queue::fake();

Event::fake();

$this->payload = ['a' => 1];

$this->headers = [
config('webhook-client.0.signature_header_name') => $this->determineSignature($this->payload)
];
}

/** @test */
public function it_can_process_a_webhook_request()
{
$payload = ['a' => 1];

$headers = [
config('webhook-client.0.signature_header_name') => $this->determineSignature($payload)
];

$this
->postJson('incoming-webhooks', $payload, $headers)
->postJson('incoming-webhooks', $this->payload, $this->headers)
->assertSuccessful();

$this->assertCount(1, WebhookCall::get());
Expand All @@ -44,8 +57,20 @@ public function it_can_process_a_webhook_request()
$this->assertEquals(1, $job->webhookCall->id);
return true;
});
}

/** @test */
public function a_request_with_an_invalid_payload_will_not_get_processed()
{
$headers = $this->headers;
$headers['Signature'] .= 'invalid';

$this
->postJson('incoming-webhooks', $this->payload, $headers)
->assertStatus(Response::HTTP_INTERNAL_SERVER_ERROR);

$this->assertCount(0, WebhookCall::get());
Queue::assertNothingPushed();
}

private function determineSignature(array $payload): string
Expand All @@ -54,5 +79,18 @@ private function determineSignature(array $payload): string

return hash_hmac('sha256', json_encode($payload), $secret);
}

/**
* @return array
*/
protected function getValidPayloadAndHeaders(): array
{
$payload = ['a' => 1];

$headers = [
config('webhook-client.0.signature_header_name') => $this->determineSignature($payload)
];
return [$payload, $headers];
}
}

0 comments on commit 32bd0c1

Please sign in to comment.