FirewallTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. class FirewallTest extends TestCase
  3. {
  4. public function testCreatePageRules()
  5. {
  6. $response = $this->getPsr7JsonResponseForFixture('Endpoints/createFirewallRules.json');
  7. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  8. $mock->method('post')->willReturn($response);
  9. $mock->expects($this->once())
  10. ->method('post')
  11. ->with(
  12. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/rules'),
  13. $this->equalTo([
  14. [
  15. 'action' => 'block',
  16. 'description' => 'Foo',
  17. 'filter' => [
  18. 'expression' => 'http.cookie eq "foo"',
  19. 'paused' => false
  20. ],
  21. ],
  22. [
  23. 'action' => 'block',
  24. 'description' => 'Bar',
  25. 'filter' => [
  26. 'expression' => 'http.cookie eq "bar"',
  27. 'paused' => false
  28. ],
  29. ]
  30. ])
  31. );
  32. $firewall = new Cloudflare\API\Endpoints\Firewall($mock);
  33. $result = $firewall->createFirewallRules(
  34. '023e105f4ecef8ad9ca31a8372d0c353',
  35. [
  36. [
  37. 'filter' => [
  38. 'expression' => 'http.cookie eq "foo"',
  39. 'paused' => false
  40. ],
  41. 'action' => 'block',
  42. 'description' => 'Foo'
  43. ],
  44. [
  45. 'filter' => [
  46. 'expression' => 'http.cookie eq "bar"',
  47. 'paused' => false
  48. ],
  49. 'action' => 'block',
  50. 'description' => 'Bar'
  51. ],
  52. ]
  53. );
  54. $this->assertTrue($result);
  55. }
  56. public function testCreatePageRule()
  57. {
  58. $response = $this->getPsr7JsonResponseForFixture('Endpoints/createFirewallRule.json');
  59. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  60. $mock->method('post')->willReturn($response);
  61. $mock->expects($this->once())
  62. ->method('post')
  63. ->with(
  64. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/rules'),
  65. $this->equalTo([
  66. [
  67. 'action' => 'block',
  68. 'description' => 'Foobar',
  69. 'filter' => [
  70. 'expression' => 'http.cookie eq "foobar"',
  71. 'paused' => false
  72. ],
  73. 'paused' => false
  74. ]
  75. ])
  76. );
  77. $firewall = new Cloudflare\API\Endpoints\Firewall($mock);
  78. $options = new \Cloudflare\API\Configurations\FirewallRuleOptions();
  79. $options->setActionBlock();
  80. $result = $firewall->createFirewallRule(
  81. '023e105f4ecef8ad9ca31a8372d0c353',
  82. 'http.cookie eq "foobar"',
  83. $options,
  84. 'Foobar'
  85. );
  86. $this->assertTrue($result);
  87. }
  88. public function testListFirewallRules()
  89. {
  90. $response = $this->getPsr7JsonResponseForFixture('Endpoints/listFirewallRules.json');
  91. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  92. $mock->method('get')->willReturn($response);
  93. $mock->expects($this->once())
  94. ->method('get')
  95. ->with(
  96. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/rules'),
  97. $this->equalTo([
  98. 'page' => 1,
  99. 'per_page' => 50
  100. ])
  101. );
  102. $firewall = new Cloudflare\API\Endpoints\Firewall($mock);
  103. $result = $firewall->listFirewallRules('023e105f4ecef8ad9ca31a8372d0c353');
  104. $this->assertObjectHasAttribute('result', $result);
  105. $this->assertObjectHasAttribute('result_info', $result);
  106. $this->assertEquals('970b10321e3f4adda674c912b5f76591', $result->result[0]->id);
  107. }
  108. public function testDeleteFirewallRule()
  109. {
  110. $response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteFirewallRule.json');
  111. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  112. $mock->method('delete')->willReturn($response);
  113. $mock->expects($this->once())
  114. ->method('delete')
  115. ->with(
  116. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/rules/970b10321e3f4adda674c912b5f76591')
  117. );
  118. $firewall = new Cloudflare\API\Endpoints\Firewall($mock);
  119. $firewall->deleteFirewallRule('023e105f4ecef8ad9ca31a8372d0c353', '970b10321e3f4adda674c912b5f76591');
  120. }
  121. public function testUpdateFirewallRule()
  122. {
  123. $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateFirewallRule.json');
  124. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  125. $mock->method('put')->willReturn($response);
  126. $mock->expects($this->once())
  127. ->method('put')
  128. ->with(
  129. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/rules/970b10321e3f4adda674c912b5f76591'),
  130. $this->equalTo([
  131. 'id' => '970b10321e3f4adda674c912b5f76591',
  132. 'action' => 'block',
  133. 'description' => 'Foo',
  134. 'filter' => [
  135. 'id' => '5def9c4297e0466cb0736b838345d910',
  136. 'expression' => 'http.cookie eq "foo"',
  137. 'paused' => false
  138. ],
  139. 'paused' => false
  140. ])
  141. );
  142. $firewall = new Cloudflare\API\Endpoints\Firewall($mock);
  143. $options = new \Cloudflare\API\Configurations\FirewallRuleOptions();
  144. $options->setActionBlock();
  145. $result = $firewall->updateFirewallRule(
  146. '023e105f4ecef8ad9ca31a8372d0c353',
  147. '970b10321e3f4adda674c912b5f76591',
  148. '5def9c4297e0466cb0736b838345d910',
  149. 'http.cookie eq "foo"',
  150. $options,
  151. 'Foo'
  152. );
  153. $this->assertEquals('970b10321e3f4adda674c912b5f76591', $result->id);
  154. }
  155. }