diff --git a/src/Endpoints/RulesLists.php b/src/Endpoints/RulesLists.php new file mode 100755 index 0000000..6ef83d1 --- /dev/null +++ b/src/Endpoints/RulesLists.php @@ -0,0 +1,103 @@ +adapter = $adapter; + } + + public function getLists(string $accountId) + { + $response = $this->adapter->get('/accounts/' . $accountId . '/rules/lists'); + $this->body = json_decode($response->getBody()); + + return (object)['result' => $this->body->result]; + } + + public function getListDetails(string $accountId, string $listId) + { + $response = $this->adapter->get('/accounts/' . $accountId . '/rules/lists/' . $listId); + $this->body = json_decode($response->getBody()); + + return $this->body->result; + } + + public function getListItems(string $accountId, string $listId, string $search = '', int $itemsPerPage = 20, string $cursor = '') + { + + $options = [ + 'per_page' => $itemsPerPage, + ]; + + if ($search) { + $options['search'] = $search; + } + + if ($cursor) { + $options['cursor'] = $cursor; + } + + $response = $this->adapter->get('/accounts/' . $accountId . '/rules/lists/' . $listId . '/items', $options); + $this->body = json_decode($response->getBody()); + + return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; + } + + public function createList(string $accountId, string $kind, string $name, string $description = '') + { + $options = [ + 'kind' => $kind, + 'name' => $name, + ]; + + if ($description) { + $options['description'] = $description; + } + + $response = $this->adapter->post('/accounts/' . $accountId . '/rules/lists', $options); + $this->body = json_decode($response->getBody()); + + return $this->body->result; + } + + public function createListItem(string $accountId, string $listId, array $ip) + { + $options = []; + foreach ($ip as $ipAddress) { + $options['ip'] = $ipAddress; + } + + $response = $this->adapter->post('/accounts/' . $accountId . '/rules/lists/' . $listId . '/items', $options); + $this->body = json_decode($response->getBody()); + + return $this->body->result; + } + + public function deleteListItem(string $accountId, string $listId, string $item = '') + { + + $response = $this->adapter->delete('/accounts/' . $accountId . '/rules/lists/' . $listId . '/items' . ($item ? '/' . $item : '')); + $this->body = json_decode($response->getBody()); + + return $this->body->result; + } + + public function getOperationStatus(string $accountId, string $operationId) + { + $response = $this->adapter->get('/accounts/' . $accountId . '/rules/lists/bulk_operations/' . $operationId); + $this->body = json_decode($response->getBody()); + + return $this->body->result; + } + +} diff --git a/tests/Endpoints/RulesListsTest.php b/tests/Endpoints/RulesListsTest.php new file mode 100755 index 0000000..34a0884 --- /dev/null +++ b/tests/Endpoints/RulesListsTest.php @@ -0,0 +1,172 @@ +getPsr7JsonResponseForFixture('Endpoints/createRulesList.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists'), + $this->equalTo([ + 'kind' => 'ip', + 'name' => 'ip-allowlist', + ]) + ); + + $rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock); + $result = $rulesLists->createList('01a7362d577a6c3019a474fd6f485823', 'ip', 'ip-allowlist'); + + $this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $result->id); + $this->assertEquals('ip', $result->kind); + $this->assertEquals('ip-allowlist', $result->name); + + $this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $rulesLists->getBody()->result->id); + } + + public function testGetRulesLists() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/listRulesLists.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists') + ); + + $rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock); + $result = $rulesLists->getLists('01a7362d577a6c3019a474fd6f485823'); + + $this->assertObjectHasAttribute('result', $result); + $this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $result->result[0]->id); + + $this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $rulesLists->getBody()->result[0]->id); + } + + public function testGetRulesListDetails() + { + + $response = $this->getPsr7JsonResponseForFixture('Endpoints/getRulesListDetails.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e') + ); + + $rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock); + $result = $rulesLists->getListDetails('01a7362d577a6c3019a474fd6f485823', '2c0fc9fa937b11eaa1b71c4d701ab86e'); + + $this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $result->id); + $this->assertEquals('ip', $result->kind); + + $this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $rulesLists->getBody()->result->id); + } + + public function testGetRulesListItems() + { + + $response = $this->getPsr7JsonResponseForFixture('Endpoints/getRulesListItems.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e/items'), + $this->equalTo([ + 'per_page' => 20, + ]) + ); + + $rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock); + $result = $rulesLists->getListItems('01a7362d577a6c3019a474fd6f485823', '2c0fc9fa937b11eaa1b71c4d701ab86e'); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('result_info', $result); + + $this->assertEquals('10.0.0.1', $result->result[0]); + $this->assertEquals('10.0.0.1', $rulesLists->getBody()->result[0]); + } + + public function testCreateRulesListItem() { + + $response = $this->getPsr7JsonResponseForFixture('Endpoints/createRulesListItem.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e/items') + ); + + $rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock); + $result = $rulesLists->createListItem('01a7362d577a6c3019a474fd6f485823', '2c0fc9fa937b11eaa1b71c4d701ab86e', [ + '10.0.0.1' + ]); + + $this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $result->operation_id); + $this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $rulesLists->getBody()->result->operation_id); + } + + public function testDeleteRulesListItem() { + + $response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteRulesListItem.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('delete')->willReturn($response); + + $mock->expects($this->once()) + ->method('delete') + ->with( + $this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e/items') + ); + + $rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock); + $result = $rulesLists->deleteListItem('01a7362d577a6c3019a474fd6f485823', '2c0fc9fa937b11eaa1b71c4d701ab86e'); + + $this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $result->operation_id); + $this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $rulesLists->getBody()->result->operation_id); + } + + public function testGetOperationStatus() { + + $response = $this->getPsr7JsonResponseForFixture('Endpoints/getOperationStatus.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/bulk_operations/4da8780eeb215e6cb7f48dd981c4ea02') + ); + + $rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock); + $result = $rulesLists->getOperationStatus('01a7362d577a6c3019a474fd6f485823', '4da8780eeb215e6cb7f48dd981c4ea02'); + + $this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $result->id); + $this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $rulesLists->getBody()->result->id); + } + +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/createRulesList.json b/tests/Fixtures/Endpoints/createRulesList.json new file mode 100755 index 0000000..21e4136 --- /dev/null +++ b/tests/Fixtures/Endpoints/createRulesList.json @@ -0,0 +1,25 @@ +{ + "success": true, + "errors": [ + { + "code": 1000, + "message": "message" + } + ], + "messages": [ + { + "code": 1000, + "message": "message" + } + ], + "result": { + "id": "2c0fc9fa937b11eaa1b71c4d701ab86e", + "created_on": "2020-01-01T08:00:00Z", + "description": "", + "kind": "ip", + "modified_on": "2020-01-10T14:00:00Z", + "name": "ip-allowlist", + "num_items": 10, + "num_referencing_filters": 2 + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/createRulesListItem.json b/tests/Fixtures/Endpoints/createRulesListItem.json new file mode 100755 index 0000000..f7b1ccb --- /dev/null +++ b/tests/Fixtures/Endpoints/createRulesListItem.json @@ -0,0 +1,18 @@ +{ + "errors": [ + { + "code": 1000, + "message": "message" + } + ], + "messages": [ + { + "code": 1000, + "message": "message" + } + ], + "result": { + "operation_id": "4da8780eeb215e6cb7f48dd981c4ea02" + }, + "success": true +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/deleteRulesListItem.json b/tests/Fixtures/Endpoints/deleteRulesListItem.json new file mode 100755 index 0000000..ed500e1 --- /dev/null +++ b/tests/Fixtures/Endpoints/deleteRulesListItem.json @@ -0,0 +1,18 @@ +{ + "success": true, + "errors": [ + { + "code": 1000, + "message": "message" + } + ], + "messages": [ + { + "code": 1000, + "message": "message" + } + ], + "result": { + "operation_id": "4da8780eeb215e6cb7f48dd981c4ea02" + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/getOperationStatus.json b/tests/Fixtures/Endpoints/getOperationStatus.json new file mode 100755 index 0000000..83be39a --- /dev/null +++ b/tests/Fixtures/Endpoints/getOperationStatus.json @@ -0,0 +1,21 @@ +{ + "errors": [ + { + "code": 1000, + "message": "message" + } + ], + "messages": [ + { + "code": 1000, + "message": "message" + } + ], + "result": { + "id": "4da8780eeb215e6cb7f48dd981c4ea02", + "status": "pending", + "completed": "2020-01-01T08:00:00Z", + "error": "This list is at the maximum number of items" + }, + "success": true +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/getRulesListDetails.json b/tests/Fixtures/Endpoints/getRulesListDetails.json new file mode 100755 index 0000000..aab5e02 --- /dev/null +++ b/tests/Fixtures/Endpoints/getRulesListDetails.json @@ -0,0 +1,25 @@ +{ + "success": true, + "errors": [ + { + "code": 1000, + "message": "message" + } + ], + "messages": [ + { + "code": 1000, + "message": "message" + } + ], + "result": { + "id": "2c0fc9fa937b11eaa1b71c4d701ab86e", + "created_on": "2020-01-01T08:00:00Z", + "description": "This is a note", + "kind": "ip", + "modified_on": "2020-01-10T14:00:00Z", + "name": "list1", + "num_items": 10, + "num_referencing_filters": 2 + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/getRulesListItems.json b/tests/Fixtures/Endpoints/getRulesListItems.json new file mode 100755 index 0000000..dad0bb5 --- /dev/null +++ b/tests/Fixtures/Endpoints/getRulesListItems.json @@ -0,0 +1,24 @@ +{ + "success": true, + "errors": [ + { + "code": 1000, + "message": "message" + } + ], + "messages": [ + { + "code": 1000, + "message": "message" + } + ], + "result": [ + "10.0.0.1" + ], + "result_info": { + "cursors": { + "after": "yyy", + "before": "xxx" + } + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/listRulesLists.json b/tests/Fixtures/Endpoints/listRulesLists.json new file mode 100755 index 0000000..517d7c3 --- /dev/null +++ b/tests/Fixtures/Endpoints/listRulesLists.json @@ -0,0 +1,27 @@ +{ + "success": true, + "errors": [ + { + "code": 1000, + "message": "message" + } + ], + "messages": [ + { + "code": 1000, + "message": "message" + } + ], + "result": [ + { + "id": "2c0fc9fa937b11eaa1b71c4d701ab86e", + "created_on": "2020-01-01T08:00:00Z", + "kind": "ip", + "modified_on": "2020-01-10T14:00:00Z", + "name": "list1", + "num_items": 10, + "description": "This is a note", + "num_referencing_filters": 2 + } + ] +} \ No newline at end of file