Skip to content

Commit 9d39f0e

Browse files
committed
feat: webhook chatwork request
1 parent 5532ce0 commit 9d39f0e

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

src/ChatworkRequest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace SunAsterisk\Chatwork\Laravel;
4+
5+
use SunAsterisk\Chatwork\Helpers\Webhook;
6+
use Illuminate\Http\Request;
7+
8+
class ChatworkRequest extends Request
9+
{
10+
/**
11+
* @return string
12+
*/
13+
public function type()
14+
{
15+
$body = json_decode($this->getContent(), true);
16+
return $body['webhook_event_type'];
17+
}
18+
19+
/**
20+
* @return string
21+
*/
22+
public function id()
23+
{
24+
$body = json_decode($this->getContent(), true);
25+
return $body['webhook_setting_id'];
26+
}
27+
28+
/**
29+
* @return int
30+
*/
31+
public function timestamp()
32+
{
33+
$body = json_decode($this->getContent(), true);
34+
return $body['webhook_event_time'];
35+
}
36+
37+
/**
38+
* @return array
39+
*/
40+
public function event()
41+
{
42+
$body = json_decode($this->getContent(), true);
43+
return $body['webhook_event'];
44+
}
45+
46+
/**
47+
* @param string $token
48+
* @return bool
49+
*/
50+
public function verifySignature(string $token)
51+
{
52+
$signature = $this->header('X-ChatWorkWebhookSignature');
53+
$body = $this->getContent();
54+
return Webhook::verifySignature($token, $body, $signature);
55+
}
56+
}

tests/ChatworkRequestTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
use SunAsterisk\Chatwork\Laravel\ChatworkRequest;
4+
5+
class ChatworkRequestTest extends TestCase
6+
{
7+
/**
8+
* @return ChatworkRequest
9+
*/
10+
protected function getRequest()
11+
{
12+
$body = require "Fixtures/request.php";
13+
$request = new ChatworkRequest([], [], [], [], [], [], $body);
14+
return $request;
15+
}
16+
17+
public function testType()
18+
{
19+
$except = "mention_to_me";
20+
$request = $this->getRequest();
21+
$type = $request->type();
22+
$this->assertEquals($type, $except);
23+
}
24+
25+
public function testId()
26+
{
27+
$except = 12345;
28+
$request = $this->getRequest();
29+
$id = $request->id();
30+
$this->assertEquals($id, $except);
31+
}
32+
33+
public function testTimestamp()
34+
{
35+
$except = 1498028130;
36+
$request = $this->getRequest();
37+
$timestamp = $request->timestamp();
38+
$this->assertEquals($timestamp, $except);
39+
}
40+
41+
public function testEvent()
42+
{
43+
$body = require "Fixtures/request.php";
44+
$body = json_decode($body, true);
45+
$except = $body['webhook_event'];
46+
$request = $this->getRequest();
47+
$event = $request->event();
48+
$this->assertEquals($event, $except);
49+
}
50+
51+
public function testVerifySignatureReturnTrue()
52+
{
53+
$token = 'test';
54+
$signature = 'cJ9U13bNloO3GyDI4F2PSrSqSqjSndvyxLmaQSpkA1E=';
55+
$request = $this->getRequest();
56+
$request->headers->set('X-ChatWorkWebhookSignature', $signature);
57+
$this->assertTrue($request->verifySignature($token));
58+
}
59+
60+
public function testVerifySignatureReturnFalse()
61+
{
62+
$token = 'test';
63+
$signature = 'test';
64+
$request = $this->getRequest();
65+
$request->headers->set('X-ChatWorkWebhookSignature', $signature);
66+
$this->assertFalse($request->verifySignature($token));
67+
}
68+
}

tests/Fixtures/request.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
$body = [
4+
"webhook_setting_id" => "12345",
5+
"webhook_event_type" => "mention_to_me",
6+
"webhook_event_time" => 1498028130,
7+
"webhook_event" => [
8+
"from_account_id" => 123456,
9+
"to_account_id" => 1484814,
10+
"room_id" => 567890123,
11+
"message_id" => "789012345",
12+
"body" => "[To=>1484814]Okazu là gì?",
13+
"send_time" => 1498028125,
14+
"update_time" => 0,
15+
],
16+
];
17+
18+
return json_encode($body, true);

tests/TestCase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase as BaseTestCase;
4+
5+
class TestCase extends BaseTestCase
6+
{
7+
8+
}

0 commit comments

Comments
 (0)