ZoneLockdownTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: junade
  5. * Date: 04/09/2017
  6. * Time: 21:23
  7. */
  8. class ZoneLockdownTest extends TestCase
  9. {
  10. public function testListLockdowns()
  11. {
  12. $response = $this->getPsr7JsonResponseForFixture('Endpoints/listLockdowns.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/lockdowns'),
  19. $this->equalTo([
  20. 'page' => 1,
  21. 'per_page' => 20,
  22. ])
  23. );
  24. $zones = new \Cloudflare\API\Endpoints\ZoneLockdown($mock);
  25. $result = $zones->listLockdowns('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 testAddLockdown()
  33. {
  34. $config = new \Cloudflare\API\Configurations\ZoneLockdown();
  35. $config->addIP('1.2.3.4');
  36. $response = $this->getPsr7JsonResponseForFixture('Endpoints/addLockdown.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/lockdowns'),
  43. $this->equalTo([
  44. 'urls' => ['api.mysite.com/some/endpoint*'],
  45. 'id' => '372e67954025e0ba6aaa6d586b9e0b59',
  46. 'description' => 'Restrict access to these endpoints to requests from a known IP address',
  47. 'configurations' => $config->getArray(),
  48. ])
  49. );
  50. $zoneLockdown = new \Cloudflare\API\Endpoints\ZoneLockdown($mock);
  51. $zoneLockdown->createLockdown(
  52. '023e105f4ecef8ad9ca31a8372d0c353',
  53. ['api.mysite.com/some/endpoint*'],
  54. $config,
  55. '372e67954025e0ba6aaa6d586b9e0b59',
  56. 'Restrict access to these endpoints to requests from a known IP address'
  57. );
  58. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $zoneLockdown->getBody()->result->id);
  59. }
  60. public function testGetRecordDetails()
  61. {
  62. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getRecordDetails.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/lockdowns/372e67954025e0ba6aaa6d586b9e0b59')
  69. );
  70. $lockdown = new \Cloudflare\API\Endpoints\ZoneLockdown($mock);
  71. $result = $lockdown->getLockdownDetails('023e105f4ecef8ad9ca31a8372d0c353', '372e67954025e0ba6aaa6d586b9e0b59');
  72. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->id);
  73. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $lockdown->getBody()->result->id);
  74. }
  75. public function testUpdateLockdown()
  76. {
  77. $config = new \Cloudflare\API\Configurations\ZoneLockdown();
  78. $config->addIP('1.2.3.4');
  79. $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateLockdown.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/lockdowns/372e67954025e0ba6aaa6d586b9e0b59'),
  86. $this->equalTo([
  87. 'urls' => ['api.mysite.com/some/endpoint*'],
  88. 'id' => '372e67954025e0ba6aaa6d586b9e0b59',
  89. 'description' => 'Restrict access to these endpoints to requests from a known IP address',
  90. 'configurations' => $config->getArray(),
  91. ])
  92. );
  93. $zoneLockdown = new \Cloudflare\API\Endpoints\ZoneLockdown($mock);
  94. $zoneLockdown->updateLockdown(
  95. '023e105f4ecef8ad9ca31a8372d0c353',
  96. '372e67954025e0ba6aaa6d586b9e0b59',
  97. ['api.mysite.com/some/endpoint*'],
  98. $config,
  99. 'Restrict access to these endpoints to requests from a known IP address'
  100. );
  101. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $zoneLockdown->getBody()->result->id);
  102. }
  103. public function testDeleteLockdown()
  104. {
  105. $config = new \Cloudflare\API\Configurations\ZoneLockdown();
  106. $config->addIP('1.2.3.4');
  107. $response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteLockdown.json');
  108. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  109. $mock->method('delete')->willReturn($response);
  110. $mock->expects($this->once())
  111. ->method('delete')
  112. ->with(
  113. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/lockdowns/372e67954025e0ba6aaa6d586b9e0b59')
  114. );
  115. $zoneLockdown = new \Cloudflare\API\Endpoints\ZoneLockdown($mock);
  116. $zoneLockdown->deleteLockdown('023e105f4ecef8ad9ca31a8372d0c353', '372e67954025e0ba6aaa6d586b9e0b59');
  117. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $zoneLockdown->getBody()->result->id);
  118. }
  119. }