|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Github\Tests\Api\Organization; |
| 6 | + |
| 7 | +use Github\Api\Organization\OrganizationRoles; |
| 8 | +use Github\Tests\Api\TestCase; |
| 9 | + |
| 10 | +class OrganizationRolesTest extends TestCase |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @test |
| 14 | + */ |
| 15 | + public function shouldGetAllOrganizationRoles() |
| 16 | + { |
| 17 | + $expectedValue = [ |
| 18 | + 'total_count' => 1, |
| 19 | + 'roles' => [[ |
| 20 | + 'id' => 1, |
| 21 | + 'name' => 'all_repo_admin', |
| 22 | + 'description' => 'Grants admin access to all repositories in the organization.', |
| 23 | + 'permissions' => [], |
| 24 | + 'organization' => null, |
| 25 | + 'created_at' => '2023-01-01T00:00:00Z', |
| 26 | + 'updated_at' => '2023-01-01T00:00:00Z', |
| 27 | + 'source' => 'Predefined', |
| 28 | + 'base_role' => 'admin', |
| 29 | + ]], |
| 30 | + ]; |
| 31 | + |
| 32 | + $api = $this->getApiMock(); |
| 33 | + $api->expects($this->once()) |
| 34 | + ->method('get') |
| 35 | + ->with('/orgs/acme/organization-roles') |
| 36 | + ->will($this->returnValue($expectedValue)); |
| 37 | + |
| 38 | + $this->assertEquals($expectedValue, $api->all('acme')); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @test |
| 43 | + */ |
| 44 | + public function shouldShowSingleOrganizationRole() |
| 45 | + { |
| 46 | + $expectedValue = [ |
| 47 | + 'id' => 1, |
| 48 | + 'name' => 'all_repo_admin', |
| 49 | + 'description' => 'Grants admin access to all repositories in the organization.', |
| 50 | + 'permissions' => [], |
| 51 | + 'organization' => null, |
| 52 | + 'created_at' => '2023-01-01T00:00:00Z', |
| 53 | + 'updated_at' => '2023-01-01T00:00:00Z', |
| 54 | + 'source' => 'Predefined', |
| 55 | + 'base_role' => 'admin', |
| 56 | + ]; |
| 57 | + |
| 58 | + $api = $this->getApiMock(); |
| 59 | + $api->expects($this->once()) |
| 60 | + ->method('get') |
| 61 | + ->with('/orgs/acme/organization-roles/1') |
| 62 | + ->will($this->returnValue($expectedValue)); |
| 63 | + |
| 64 | + $this->assertEquals($expectedValue, $api->show('acme', 1)); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @test |
| 69 | + */ |
| 70 | + public function shouldGetAllTeamsWithRole() |
| 71 | + { |
| 72 | + $expectedValue = [['name' => 'Acme Admins']]; |
| 73 | + |
| 74 | + $api = $this->getApiMock(); |
| 75 | + $api->expects($this->once()) |
| 76 | + ->method('get') |
| 77 | + ->with('/orgs/acme/organization-roles/1/teams') |
| 78 | + ->will($this->returnValue($expectedValue)); |
| 79 | + |
| 80 | + $this->assertEquals($expectedValue, $api->listTeamsWithRole('acme', 1)); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @test |
| 85 | + */ |
| 86 | + public function shouldAssignRoleToTeam() |
| 87 | + { |
| 88 | + $api = $this->getApiMock(); |
| 89 | + $api->expects($this->once()) |
| 90 | + ->method('put') |
| 91 | + ->with('/orgs/acme/organization-roles/teams/acme-admins/1') |
| 92 | + ->will($this->returnValue('')); |
| 93 | + |
| 94 | + $api->assignRoleToTeam('acme', 1, 'acme-admins'); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @test |
| 99 | + */ |
| 100 | + public function shouldRemoveRoleFromTeam() |
| 101 | + { |
| 102 | + $api = $this->getApiMock(); |
| 103 | + $api->expects($this->once()) |
| 104 | + ->method('delete') |
| 105 | + ->with('/orgs/acme/organization-roles/teams/acme-admins/1') |
| 106 | + ->will($this->returnValue('')); |
| 107 | + |
| 108 | + $api->removeRoleFromTeam('acme', 1, 'acme-admins'); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @test |
| 113 | + */ |
| 114 | + public function shouldRemoveAllRolesFromTeam() |
| 115 | + { |
| 116 | + $api = $this->getApiMock(); |
| 117 | + $api->expects($this->once()) |
| 118 | + ->method('delete') |
| 119 | + ->with('/orgs/acme/organization-roles/teams/acme-admins') |
| 120 | + ->will($this->returnValue('')); |
| 121 | + |
| 122 | + $api->removeAllRolesFromTeam('acme', 'acme-admins'); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @test |
| 127 | + */ |
| 128 | + public function shouldGetAllUsersWithRole() |
| 129 | + { |
| 130 | + $expectedValue = [['username' => 'Admin']]; |
| 131 | + |
| 132 | + $api = $this->getApiMock(); |
| 133 | + $api->expects($this->once()) |
| 134 | + ->method('get') |
| 135 | + ->with('/orgs/acme/organization-roles/1/users') |
| 136 | + ->will($this->returnValue($expectedValue)); |
| 137 | + |
| 138 | + $this->assertEquals($expectedValue, $api->listUsersWithRole('acme', 1)); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @test |
| 143 | + */ |
| 144 | + public function shouldAssignRoleToUser() |
| 145 | + { |
| 146 | + $api = $this->getApiMock(); |
| 147 | + $api->expects($this->once()) |
| 148 | + ->method('put') |
| 149 | + ->with('/orgs/acme/organization-roles/users/admin/1') |
| 150 | + ->will($this->returnValue('')); |
| 151 | + |
| 152 | + $api->assignRoleToUser('acme', 1, 'admin'); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * @test |
| 157 | + */ |
| 158 | + public function shouldRemoveRoleFromUser() |
| 159 | + { |
| 160 | + $api = $this->getApiMock(); |
| 161 | + $api->expects($this->once()) |
| 162 | + ->method('delete') |
| 163 | + ->with('/orgs/acme/organization-roles/users/admin/1') |
| 164 | + ->will($this->returnValue('')); |
| 165 | + |
| 166 | + $api->removeRoleFromUser('acme', 1, 'admin'); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @test |
| 171 | + */ |
| 172 | + public function shouldRemoveAllRolesFromUser() |
| 173 | + { |
| 174 | + $api = $this->getApiMock(); |
| 175 | + $api->expects($this->once()) |
| 176 | + ->method('delete') |
| 177 | + ->with('/orgs/acme/organization-roles/users/admin') |
| 178 | + ->will($this->returnValue('')); |
| 179 | + |
| 180 | + $api->removeAllRolesFromUser('acme', 'admin'); |
| 181 | + } |
| 182 | + |
| 183 | + protected function getApiClass(): string |
| 184 | + { |
| 185 | + return OrganizationRoles::class; |
| 186 | + } |
| 187 | +} |
0 commit comments