ControllerPermissionTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Models\AdminPermission;
  4. use App\Models\AdminRole;
  5. use Illuminate\Support\Facades\Route;
  6. use Tests\AdminTestCase;
  7. use Illuminate\Foundation\Testing\WithFaker;
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9. use Tests\Traits\RequestActions;
  10. class ControllerPermissionTest extends AdminTestCase
  11. {
  12. use RefreshDatabase;
  13. use RequestActions;
  14. protected $resourceName = 'test-resources';
  15. protected function setUp(): void
  16. {
  17. parent::setUp();
  18. $this->login();
  19. $this->checkPermission(true);
  20. $this->prepare();
  21. }
  22. protected function prepare()
  23. {
  24. Route::prefix('admin-api')
  25. ->middleware([
  26. 'admin',
  27. 'auth:admin',
  28. 'admin.permission',
  29. ])
  30. ->as('admin.')
  31. ->group(function () {
  32. Route::middleware('admin.permission:check,with-args')
  33. ->group(function () {
  34. Route::get('test-resources/with-args', 'Tests\Controllers\DummyAdminController@withArgs');
  35. });
  36. Route::get('test-resources/check', 'Tests\Controllers\DummyAdminController@check');
  37. Route::get('test-resources/pass-through', 'Tests\Controllers\DummyAdminController@passThrough');
  38. Route::resource('test-resources', 'Tests\Controllers\DummyAdminController');
  39. });
  40. }
  41. protected function bindRole($role = [], $permissions = [])
  42. {
  43. $permissions = array_map(function ($attributes) {
  44. return factory(AdminPermission::class)->create($attributes);
  45. }, $permissions);
  46. $role = factory(AdminRole::class)->create($role);
  47. $role->permissions()->attach(collect($permissions)->pluck('id'));
  48. $this->user->roles()->attach($role->id);
  49. }
  50. protected function bindPermission($attributes = [])
  51. {
  52. $this->user->permissions()->create(factory(AdminPermission::class)->create($attributes)->toArray());
  53. }
  54. public function testNoPermission()
  55. {
  56. $res = $this->getResources();
  57. $res->assertStatus(403);
  58. }
  59. public function testExactMethodAndPath()
  60. {
  61. $this->bindPermission([
  62. 'http_method' => ['GET'],
  63. 'http_path' => '/test-resources',
  64. ]);
  65. $this->user->permissions()->attach(1);
  66. $res = $this->getResources();
  67. $res->assertStatus(200);
  68. }
  69. public function testAnyMethod()
  70. {
  71. $this->bindPermission([
  72. 'http_method' => [],
  73. 'http_path' => '/test-resources',
  74. ]);
  75. $res = $this->getResources();
  76. $res->assertStatus(200);
  77. $res = $this->storeResource();
  78. $res->assertStatus(201);
  79. }
  80. public function testSpecifyMethodInPath()
  81. {
  82. $this->bindPermission([
  83. 'http_method' => ['GET'],
  84. 'http_path' => 'POST:/test-resources',
  85. ]);
  86. $res = $this->storeResource();
  87. $res->assertStatus(201);
  88. }
  89. public function testIsAdministrator()
  90. {
  91. $this->bindRole([
  92. 'slug' => 'administrator',
  93. ], [
  94. [
  95. 'slug' => '*',
  96. 'http_method' => [],
  97. 'http_path' => '*',
  98. ],
  99. ]);
  100. $res = $this->getResources();
  101. $res->assertStatus(200);
  102. $res = $this->storeResource();
  103. $res->assertStatus(201);
  104. }
  105. public function testPermissionsInRole()
  106. {
  107. $this->bindRole([], [
  108. [
  109. 'http_method' => ['GET'],
  110. 'http_path' => '/test-resources',
  111. ],
  112. [
  113. 'http_method' => ['POST'],
  114. 'http_path' => '/test-resources',
  115. ],
  116. ]);
  117. $res = $this->getResources();
  118. $res->assertStatus(200);
  119. $res = $this->storeResource();
  120. $res->assertStatus(201);
  121. }
  122. public function testPermissionCheckInMethod()
  123. {
  124. $this->bindPermission([
  125. 'http_path' => '',
  126. 'http_method' => '',
  127. 'slug' => 'check',
  128. ]);
  129. $res = $this->get('/admin-api/test-resources/check');
  130. $res->assertStatus(200);
  131. }
  132. public function testPermissionCheckWithArgs()
  133. {
  134. $this->bindPermission([
  135. 'slug' => 'with-args',
  136. ]);
  137. $res = $this->get('/admin-api/test-resources/with-args');
  138. $res->assertStatus(200);
  139. }
  140. public function testUseUrlWhitelist()
  141. {
  142. $res = $this->get('/admin-api/test-resources/pass-through');
  143. $res->assertStatus(200);
  144. }
  145. }