WAFTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: junade
  5. * Date: 23/10/2017
  6. * Time: 13:34
  7. */
  8. class WAFTest extends TestCase
  9. {
  10. public function testgetPackages()
  11. {
  12. $response = $this->getPsr7JsonResponseForFixture('Endpoints/listPackages.json');
  13. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  14. $mock->method('get')->willReturn($response);
  15. $mock->expects($this->once())
  16. ->method('get')
  17. ->with(
  18. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/waf/packages'),
  19. $this->equalTo([
  20. 'page' => 1,
  21. 'per_page' => 20,
  22. 'match' => 'all',
  23. 'order' => 'status',
  24. 'direction' => 'desc'
  25. ])
  26. );
  27. $waf = new \Cloudflare\API\Endpoints\WAF($mock);
  28. $result = $waf->getPackages('023e105f4ecef8ad9ca31a8372d0c353', 1, 20, 'status', 'desc');
  29. $this->assertObjectHasAttribute('result', $result);
  30. $this->assertObjectHasAttribute('result_info', $result);
  31. $this->assertEquals('a25a9a7e9c00afc1fb2e0245519d725b', $result->result[0]->id);
  32. $this->assertEquals(1, $result->result_info->page);
  33. $this->assertEquals('a25a9a7e9c00afc1fb2e0245519d725b', $waf->getBody()->result[0]->id);
  34. }
  35. public function testgetPackageInfo()
  36. {
  37. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getPackageInfo.json');
  38. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  39. $mock->method('get')->willReturn($response);
  40. $mock->expects($this->once())
  41. ->method('get')
  42. ->with(
  43. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/waf/packages/a25a9a7e9c00afc1fb2e0245519d725b')
  44. );
  45. $waf = new \Cloudflare\API\Endpoints\WAF($mock);
  46. $result = $waf->getPackageInfo('023e105f4ecef8ad9ca31a8372d0c353', 'a25a9a7e9c00afc1fb2e0245519d725b');
  47. $this->assertEquals('a25a9a7e9c00afc1fb2e0245519d725b', $result->id);
  48. $this->assertEquals('a25a9a7e9c00afc1fb2e0245519d725b', $waf->getBody()->result->id);
  49. }
  50. public function testgetRules()
  51. {
  52. $response = $this->getPsr7JsonResponseForFixture('Endpoints/listPackageRules.json');
  53. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  54. $mock->method('get')->willReturn($response);
  55. $mock->expects($this->once())
  56. ->method('get')
  57. ->with(
  58. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/waf/packages/a25a9a7e9c00afc1fb2e0245519d725b/rules'),
  59. $this->equalTo([
  60. 'page' => 1,
  61. 'per_page' => 20,
  62. 'match' => 'all',
  63. 'order' => 'status',
  64. 'direction' => 'desc'
  65. ])
  66. );
  67. $waf = new \Cloudflare\API\Endpoints\WAF($mock);
  68. $result = $waf->getRules('023e105f4ecef8ad9ca31a8372d0c353', 'a25a9a7e9c00afc1fb2e0245519d725b', 1, 20, 'status', 'desc');
  69. $this->assertObjectHasAttribute('result', $result);
  70. $this->assertObjectHasAttribute('result_info', $result);
  71. $this->assertEquals('92f17202ed8bd63d69a66b86a49a8f6b', $result->result[0]->id);
  72. $this->assertEquals(1, $result->result_info->page);
  73. $this->assertEquals('92f17202ed8bd63d69a66b86a49a8f6b', $waf->getBody()->result[0]->id);
  74. }
  75. public function testgetRuleInfo()
  76. {
  77. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getPackageRuleInfo.json');
  78. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  79. $mock->method('get')->willReturn($response);
  80. $mock->expects($this->once())
  81. ->method('get')
  82. ->with(
  83. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/waf/packages/a25a9a7e9c00afc1fb2e0245519d725b/rules/f939de3be84e66e757adcdcb87908023')
  84. );
  85. $waf = new \Cloudflare\API\Endpoints\WAF($mock);
  86. $result = $waf->getRuleInfo('023e105f4ecef8ad9ca31a8372d0c353', 'a25a9a7e9c00afc1fb2e0245519d725b', 'f939de3be84e66e757adcdcb87908023');
  87. $this->assertEquals('f939de3be84e66e757adcdcb87908023', $result->id);
  88. $this->assertEquals('f939de3be84e66e757adcdcb87908023', $waf->getBody()->result->id);
  89. }
  90. public function testupdateRule()
  91. {
  92. $response = $this->getPsr7JsonResponseForFixture('Endpoints/updatePackageRule.json');
  93. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  94. $mock->method('patch')->willReturn($response);
  95. $details = [
  96. 'mode' => 'on',
  97. ];
  98. $mock->expects($this->once())
  99. ->method('patch')
  100. ->with(
  101. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/waf/packages/a25a9a7e9c00afc1fb2e0245519d725b/rules/f939de3be84e66e757adcdcb87908023'),
  102. $this->equalTo($details)
  103. );
  104. $waf = new \Cloudflare\API\Endpoints\WAF($mock);
  105. $result = $waf->updateRule('023e105f4ecef8ad9ca31a8372d0c353', 'a25a9a7e9c00afc1fb2e0245519d725b', 'f939de3be84e66e757adcdcb87908023', 'on');
  106. $this->assertEquals('f939de3be84e66e757adcdcb87908023', $result->id);
  107. foreach ($details as $property => $value) {
  108. $this->assertEquals($result->{ $property }, $value);
  109. }
  110. $this->assertEquals('f939de3be84e66e757adcdcb87908023', $waf->getBody()->result->id);
  111. }
  112. public function getGroups()
  113. {
  114. $response = $this->getPsr7JsonResponseForFixture('Endpoints/listPackageGroups.json');
  115. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  116. $mock->method('get')->willReturn($response);
  117. $mock->expects($this->once())
  118. ->method('get')
  119. ->with(
  120. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/waf/packages/a25a9a7e9c00afc1fb2e0245519d725b/groups'),
  121. $this->equalTo([
  122. 'page' => 1,
  123. 'per_page' => 20,
  124. 'match' => 'all',
  125. 'order' => 'status',
  126. 'direction' => 'desc'
  127. ])
  128. );
  129. $waf = new \Cloudflare\API\Endpoints\WAF($mock);
  130. $result = $waf->getGroups('023e105f4ecef8ad9ca31a8372d0c353', 'a25a9a7e9c00afc1fb2e0245519d725b', 1, 20, 'status', 'desc');
  131. $this->assertObjectHasAttribute('result', $result);
  132. $this->assertObjectHasAttribute('result_info', $result);
  133. $this->assertEquals('de677e5818985db1285d0e80225f06e5', $result->result[0]->id);
  134. $this->assertEquals(1, $result->result_info->page);
  135. $this->assertEquals('de677e5818985db1285d0e80225f06e5', $waf->getBody()->result[0]->id);
  136. }
  137. public function testgetGroupInfo()
  138. {
  139. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getPackageGroupInfo.json');
  140. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  141. $mock->method('get')->willReturn($response);
  142. $mock->expects($this->once())
  143. ->method('get')
  144. ->with(
  145. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/waf/packages/a25a9a7e9c00afc1fb2e0245519d725b/groups/de677e5818985db1285d0e80225f06e5')
  146. );
  147. $waf = new \Cloudflare\API\Endpoints\WAF($mock);
  148. $result = $waf->getGroupInfo('023e105f4ecef8ad9ca31a8372d0c353', 'a25a9a7e9c00afc1fb2e0245519d725b', 'de677e5818985db1285d0e80225f06e5');
  149. $this->assertEquals('de677e5818985db1285d0e80225f06e5', $result->id);
  150. $this->assertEquals('de677e5818985db1285d0e80225f06e5', $waf->getBody()->result->id);
  151. }
  152. public function testupdateGroup()
  153. {
  154. $response = $this->getPsr7JsonResponseForFixture('Endpoints/updatePackageGroup.json');
  155. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  156. $mock->method('patch')->willReturn($response);
  157. $details = [
  158. 'mode' => 'off',
  159. ];
  160. $mock->expects($this->once())
  161. ->method('patch')
  162. ->with(
  163. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/waf/packages/a25a9a7e9c00afc1fb2e0245519d725b/groups/de677e5818985db1285d0e80225f06e5'),
  164. $this->equalTo($details)
  165. );
  166. $waf = new \Cloudflare\API\Endpoints\WAF($mock);
  167. $result = $waf->updateGroup('023e105f4ecef8ad9ca31a8372d0c353', 'a25a9a7e9c00afc1fb2e0245519d725b', 'de677e5818985db1285d0e80225f06e5', 'off');
  168. $this->assertEquals('de677e5818985db1285d0e80225f06e5', $result->id);
  169. foreach ($details as $property => $value) {
  170. $this->assertEquals($result->{ $property }, $value);
  171. }
  172. $this->assertEquals('de677e5818985db1285d0e80225f06e5', $waf->getBody()->result->id);
  173. }
  174. }