diff --git a/composer.json b/composer.json index 1010f1b..a91c7b9 100644 --- a/composer.json +++ b/composer.json @@ -3,8 +3,11 @@ "description": "Chatwork SDK for Laravel", "type": "library", "require": { - "sun-asterisk/chatwork-php": "^0.1.1", - "illuminate/support": "^5.0" + "ext-json": "*", + "illuminate/http": "^5.0", + "illuminate/support": "^5.0", + "mockery/mockery": "^1.2", + "sun-asterisk/chatwork-php": "^0.1.1" }, "require-dev": { "phpunit/phpunit": "^8.3" diff --git a/src/ChatworkRequest.php b/src/ChatworkRequest.php new file mode 100644 index 0000000..df71ac7 --- /dev/null +++ b/src/ChatworkRequest.php @@ -0,0 +1,56 @@ +getContent(), true); + return $body['webhook_event_type']; + } + + /** + * @return string + */ + public function id() + { + $body = json_decode($this->getContent(), true); + return $body['webhook_setting_id']; + } + + /** + * @return int + */ + public function timestamp() + { + $body = json_decode($this->getContent(), true); + return $body['webhook_event_time']; + } + + /** + * @return array + */ + public function event() + { + $body = json_decode($this->getContent(), true); + return $body['webhook_event']; + } + + /** + * @param string $token + * @return bool + */ + public function verifySignature(string $token) + { + $signature = $this->header('X-ChatWorkWebhookSignature'); + $body = $this->getContent(); + return Webhook::verifySignature($token, $body, $signature); + } +} diff --git a/tests/ChatworkRequestTest.php b/tests/ChatworkRequestTest.php new file mode 100644 index 0000000..bbb3814 --- /dev/null +++ b/tests/ChatworkRequestTest.php @@ -0,0 +1,68 @@ +getRequest(); + $type = $request->type(); + $this->assertEquals($type, $except); + } + + public function testId() + { + $except = 12345; + $request = $this->getRequest(); + $id = $request->id(); + $this->assertEquals($id, $except); + } + + public function testTimestamp() + { + $except = 1498028130; + $request = $this->getRequest(); + $timestamp = $request->timestamp(); + $this->assertEquals($timestamp, $except); + } + + public function testEvent() + { + $body = require "Fixtures/request.php"; + $body = json_decode($body, true); + $except = $body['webhook_event']; + $request = $this->getRequest(); + $event = $request->event(); + $this->assertEquals($event, $except); + } + + public function testVerifySignatureReturnTrue() + { + $token = 'test'; + $signature = 'cJ9U13bNloO3GyDI4F2PSrSqSqjSndvyxLmaQSpkA1E='; + $request = $this->getRequest(); + $request->headers->set('X-ChatWorkWebhookSignature', $signature); + $this->assertTrue($request->verifySignature($token)); + } + + public function testVerifySignatureReturnFalse() + { + $token = 'test'; + $signature = 'test'; + $request = $this->getRequest(); + $request->headers->set('X-ChatWorkWebhookSignature', $signature); + $this->assertFalse($request->verifySignature($token)); + } +} diff --git a/tests/Fixtures/request.php b/tests/Fixtures/request.php new file mode 100644 index 0000000..5bff120 --- /dev/null +++ b/tests/Fixtures/request.php @@ -0,0 +1,18 @@ + "12345", + "webhook_event_type" => "mention_to_me", + "webhook_event_time" => 1498028130, + "webhook_event" => [ + "from_account_id" => 123456, + "to_account_id" => 1484814, + "room_id" => 567890123, + "message_id" => "789012345", + "body" => "[To=>1484814]Okazu là gì?", + "send_time" => 1498028125, + "update_time" => 0, + ], +]; + +return json_encode($body, true); diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..b7502e7 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,8 @@ +