Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
WebhookRequestValidatorTest.php
Go to the documentation of this file.
1 <?php
7 
12 
16 class WebhookRequestValidatorTest extends \PHPUnit\Framework\TestCase
17 {
21  private $model;
22 
26  private $config;
27 
31  private $decoder;
32 
36  protected function setUp()
37  {
38  $this->config = $this->getMockBuilder(Config::class)
39  ->disableOriginalConstructor()
40  ->getMock();
41 
42  $this->decoder = $this->getMockBuilder(DecoderInterface::class)
43  ->getMockForAbstractClass();
44 
45  $this->model = new WebhookRequestValidator(
46  $this->config,
47  $this->decoder
48  );
49  }
50 
60  public function testValidateSuccess($body, $topic, $hash, $callConfigCount)
61  {
62  $this->config->expects($this->exactly($callConfigCount))
63  ->method('getApiKey')
64  ->willReturn('GpFZZnxGgIxuI8BazSm3v6eGK');
65 
66  $this->decoder->expects($this->once())
67  ->method('decode')
68  ->with($body)
69  ->willReturn(['status' => "DISMISSED", 'orderId' => '19418']);
70 
71  $webhookRequest = $this->createWebhookRequest($body, $topic, $hash);
72 
73  $this->assertTrue(
74  $this->model->validate($webhookRequest)
75  );
76  }
77 
83  public function validateSuccessDataProvider()
84  {
85  return [
86  1 => [
87  'body' => '{ status: "DISMISSED", orderId: "19418" }',
88  'topic' => 'cases/creation',
89  'hash' => 'KWR8Bzu3tinEpDviw1opWSMJGFqfpA79nNGp0TEYM6Q=',
90  'callConfigCount' => 1
91  ],
92  2 => [
93  'body' => '{ status: "DISMISSED", orderId: "19418" }',
94  'topic' => 'cases/test',
95  'hash' => '6npAahliNbzYo/Qi4+g+JeqPhLFgg19sIbuxDLmvobw=',
96  'callConfigCount' => 0
97  ]
98  ];
99  }
100 
107  public function testValidationTopicFails($topic)
108  {
109  $body = '{ status: "DISMISSED", orderId: "19418" }';
110  $hash = 'KWR8Bzu3tinEpDviw1opWSMJGFqfpA79nNGp0TEYM6Q=';
111 
112  $this->config->expects($this->never())
113  ->method('getApiKey');
114 
115  $this->decoder->expects($this->never())
116  ->method('decode');
117 
118  $webhookRequest = $this->createWebhookRequest($body, $topic, $hash);
119 
120  $this->assertFalse(
121  $this->model->validate($webhookRequest),
122  'Negative webhook event topic value validation fails'
123  );
124  }
125 
130  {
131  return [
132  ['wrong topic' => 'bla-bla-topic'],
133  ['empty topic' => '']
134  ];
135  }
136 
143  public function testValidationBodyFails($body)
144  {
145  $topic = 'cases/creation';
146  $hash = 'KWR8Bzu3tinEpDviw1opWSMJGFqfpA79nNGp0TEYM6Q=';
147  $webhookRequest = $this->createWebhookRequest($body, $topic, $hash);
148 
149  $this->config->expects($this->never())
150  ->method('getApiKey');
151 
152  if (empty($body)) {
153  $this->decoder->expects($this->once())
154  ->method('decode')
155  ->with($body)
156  ->willReturn('');
157  } else {
158  $this->decoder->expects($this->once())
159  ->method('decode')
160  ->with($body)
161  ->willThrowException(new \Exception('Error'));
162  }
163 
164  $this->assertFalse(
165  $this->model->validate($webhookRequest),
166  'Negative webhook request body validation fails'
167  );
168  }
169 
174  {
175  return [
176  ['Empty request body' => ''],
177  ['Bad request body' => '{ bad data}']
178  ];
179  }
180 
184  public function testValidationHashFails()
185  {
186  $topic = 'cases/creation';
187  $body = '{ status: "DISMISSED", orderId: "19418" }';
188  $hash = 'wrong hash';
189  $webhookRequest = $this->createWebhookRequest($body, $topic, $hash);
190 
191  $this->config->expects($this->once())
192  ->method('getApiKey')
193  ->willReturn('GpFZZnxGgIxuI8BazSm3v6eGK');
194 
195  $this->decoder->expects($this->once())
196  ->method('decode')
197  ->with($body)
198  ->willReturn(['status' => "DISMISSED", 'orderId' => '19418']);
199 
200  $this->assertFalse(
201  $this->model->validate($webhookRequest),
202  'Negative webhook hash validation fails'
203  );
204  }
205 
214  private function createWebhookRequest($body, $topic, $hash)
215  {
216  $webhookRequest = $this->getMockBuilder(WebhookRequest::class)
217  ->disableOriginalConstructor()
218  ->getMock();
219  $webhookRequest->expects($this->once())
220  ->method('getBody')
221  ->willReturn($body);
222  $webhookRequest->expects($this->once())
223  ->method('getEventTopic')
224  ->willReturn($topic);
225  $webhookRequest->expects($this->once())
226  ->method('getHash')
227  ->willReturn($hash);
228 
229  return $webhookRequest;
230  }
231 }