UARulesTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: junade
  5. * Date: 19/09/2017
  6. * Time: 15:19
  7. */
  8. class UARulesTest extends TestCase
  9. {
  10. public function testListRules()
  11. {
  12. $response = $this->getPsr7JsonResponseForFixture('Endpoints/listRules.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/ua_rules'),
  19. $this->equalTo([
  20. 'page' => 1,
  21. 'per_page' => 20
  22. ])
  23. );
  24. $zones = new \Cloudflare\API\Endpoints\UARules($mock);
  25. $result = $zones->listRules('023e105f4ecef8ad9ca31a8372d0c353');
  26. $this->assertObjectHasAttribute('result', $result);
  27. $this->assertObjectHasAttribute('result_info', $result);
  28. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->result[0]->id);
  29. $this->assertEquals(1, $result->result_info->page);
  30. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $zones->getBody()->result[0]->id);
  31. }
  32. public function testCreateRule()
  33. {
  34. $config = new \Cloudflare\API\Configurations\UARules();
  35. $config->addUA('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4');
  36. $response = $this->getPsr7JsonResponseForFixture('Endpoints/createRule.json');
  37. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  38. $mock->method('post')->willReturn($response);
  39. $mock->expects($this->once())
  40. ->method('post')
  41. ->with(
  42. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/ua_rules'),
  43. $this->equalTo([
  44. 'mode' => 'js_challenge',
  45. 'id' => '372e67954025e0ba6aaa6d586b9e0b59',
  46. 'description' => 'Prevent access from abusive clients identified by this UserAgent to mitigate DDoS attack',
  47. 'configurations' => $config->getArray(),
  48. ])
  49. );
  50. $rules = new \Cloudflare\API\Endpoints\UARules($mock);
  51. $rules->createRule(
  52. '023e105f4ecef8ad9ca31a8372d0c353',
  53. 'js_challenge',
  54. $config,
  55. '372e67954025e0ba6aaa6d586b9e0b59',
  56. 'Prevent access from abusive clients identified by this UserAgent to mitigate DDoS attack'
  57. );
  58. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $rules->getBody()->result->id);
  59. }
  60. public function getRuleDetails()
  61. {
  62. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getRuleDetails.json');
  63. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  64. $mock->method('get')->willReturn($response);
  65. $mock->expects($this->once())
  66. ->method('get')
  67. ->with(
  68. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/ua_rules/372e67954025e0ba6aaa6d586b9e0b59')
  69. );
  70. $lockdown = new \Cloudflare\API\Endpoints\UARules($mock);
  71. $result = $lockdown->getRuleDetails('023e105f4ecef8ad9ca31a8372d0c353', '372e67954025e0ba6aaa6d586b9e0b59');
  72. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->id);
  73. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $lockdown->getBody()->result->id);
  74. }
  75. public function testUpdateRule()
  76. {
  77. $config = new \Cloudflare\API\Configurations\UARules();
  78. $config->addUA('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4');
  79. $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateRule.json');
  80. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  81. $mock->method('put')->willReturn($response);
  82. $mock->expects($this->once())
  83. ->method('put')
  84. ->with(
  85. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/ua_rules/372e67954025e0ba6aaa6d586b9e0b59'),
  86. $this->equalTo([
  87. 'mode' => 'js_challenge',
  88. 'id' => '372e67954025e0ba6aaa6d586b9e0b59',
  89. 'description' => 'Restrict access to these endpoints to requests from a known IP address',
  90. 'configurations' => $config->getArray(),
  91. ])
  92. );
  93. $rules = new \Cloudflare\API\Endpoints\UARules($mock);
  94. $rules->updateRule(
  95. '023e105f4ecef8ad9ca31a8372d0c353',
  96. '372e67954025e0ba6aaa6d586b9e0b59',
  97. 'js_challenge',
  98. $config,
  99. 'Restrict access to these endpoints to requests from a known IP address'
  100. );
  101. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $rules->getBody()->result->id);
  102. }
  103. public function testDeleteRule()
  104. {
  105. $response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteRule.json');
  106. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  107. $mock->method('delete')->willReturn($response);
  108. $mock->expects($this->once())
  109. ->method('delete')
  110. ->with(
  111. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/ua_rules/372e67954025e0ba6aaa6d586b9e0b59')
  112. );
  113. $rules = new \Cloudflare\API\Endpoints\UARules($mock);
  114. $rules->deleteRule('023e105f4ecef8ad9ca31a8372d0c353', '372e67954025e0ba6aaa6d586b9e0b59');
  115. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $rules->getBody()->result->id);
  116. }
  117. }