Skip to content

Commit

Permalink
Add Account Role Listings (#198)
Browse files Browse the repository at this point in the history
I want to list the roles availablew in an account so I can add a member
to the account
  • Loading branch information
phily245 authored Oct 13, 2021
1 parent 5024c2a commit fdfc656
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Endpoints/AccountRoles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Cloudflare\API\Endpoints;

use Cloudflare\API\Adapter\Adapter;
use Cloudflare\API\Traits\BodyAccessorTrait;
use stdClass;

class AccountRoles implements API
{
use BodyAccessorTrait;

/**
* @var Adapter
*/
private $adapter;

public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}

public function listAccountRoles(string $accountId): stdClass
{
$roles = $this->adapter->get('accounts/' . $accountId . '/roles');
$this->body = json_decode($roles->getBody());

return (object)[
'result' => $this->body->result,
'result_info' => $this->body->result_info,
];
}
}
32 changes: 32 additions & 0 deletions tests/Endpoints/AccountRolesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Endpoints;

use Cloudflare\API\Adapter\Adapter;
use Cloudflare\API\Endpoints\AccountRoles;
use TestCase;

class AccountRolesTest extends TestCase
{
public function testListAccountRoles()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/listAccountRoles.json');

$adapter = $this->getMockBuilder(Adapter::class)->getMock();
$adapter->method('get')->willReturn($response);

$adapter->expects($this->once())
->method('get')
->with($this->equalTo('accounts/023e105f4ecef8ad9ca31a8372d0c353/roles'));

$roles = new AccountRoles($adapter);
$result = $roles->listAccountRoles('023e105f4ecef8ad9ca31a8372d0c353');

$this->assertObjectHasAttribute('result', $result);
$this->assertObjectHasAttribute('result_info', $result);

$this->assertEquals('3536bcfad5faccb999b47003c79917fb', $result->result[0]->id);
$this->assertEquals(1, $result->result_info->page);
$this->assertEquals('3536bcfad5faccb999b47003c79917fb', $roles->getBody()->result[0]->id);
}
}
69 changes: 69 additions & 0 deletions tests/Fixtures/Endpoints/listAccountRoles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"success": true,
"errors": [],
"messages": [],
"result": [
{
"id": "3536bcfad5faccb999b47003c79917fb",
"name": "Account Administrator",
"description": "Administrative access to the entire Account",
"permissions": {
"analytics": {
"read": true,
"write": true
},
"billing": {
"read": true,
"write": true
},
"cache_purge": {
"read": true,
"write": true
},
"dns": {
"read": true,
"write": true
},
"dns_records": {
"read": true,
"write": true
},
"lb": {
"read": true,
"write": true
},
"logs": {
"read": true,
"write": true
},
"organization": {
"read": true,
"write": true
},
"ssl": {
"read": true,
"write": true
},
"waf": {
"read": true,
"write": true
},
"zones": {
"read": true,
"write": true
},
"zone_settings": {
"read": true,
"write": true
}
}
}
],
"result_info": {
"page": 1,
"per_page": 1,
"total_pages": 1,
"count": 1,
"total_count": 1
}
}

0 comments on commit fdfc656

Please sign in to comment.