|
3 | 3 | namespace Binaryk\LaravelRestify\Tests\Controllers; |
4 | 4 |
|
5 | 5 | use Binaryk\LaravelRestify\Tests\Fixtures\Company\Company; |
| 6 | +use Binaryk\LaravelRestify\Tests\Fixtures\Company\CompanyPolicy; |
| 7 | +use Binaryk\LaravelRestify\Tests\Fixtures\User\User; |
6 | 8 | use Binaryk\LaravelRestify\Tests\IntegrationTest; |
| 9 | +use Illuminate\Support\Facades\Gate; |
7 | 10 |
|
8 | 11 | class RepositoryAttachControllerTest extends IntegrationTest |
9 | 12 | { |
@@ -63,4 +66,64 @@ public function test_after_attach_a_user_to_company_number_of_users_increased() |
63 | 66 | $usersFromCompany = $this->getJson('/restify-api/users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users'); |
64 | 67 | $this->assertCount(1, $usersFromCompany->json('data')); |
65 | 68 | } |
| 69 | + |
| 70 | + public function test_policy_to_attach_a_user_to_a_company() |
| 71 | + { |
| 72 | + Gate::policy(Company::class, CompanyPolicy::class); |
| 73 | + |
| 74 | + $user = $this->mockUsers(2)->first(); |
| 75 | + $company = factory(Company::class)->create(); |
| 76 | + $this->authenticate( |
| 77 | + factory(User::class)->create() |
| 78 | + ); |
| 79 | + |
| 80 | + $_SERVER['allow_attach_users'] = false; |
| 81 | + |
| 82 | + $this->postJson('restify-api/companies/'.$company->id.'/attach/users', [ |
| 83 | + 'users' => $user->id, |
| 84 | + 'is_admin' => true, |
| 85 | + ]) |
| 86 | + ->assertForbidden(); |
| 87 | + |
| 88 | + $_SERVER['allow_attach_users'] = true; |
| 89 | + |
| 90 | + $this->postJson('restify-api/companies/'.$company->id.'/attach/users', [ |
| 91 | + 'users' => $user->id, |
| 92 | + 'is_admin' => true, |
| 93 | + ]) |
| 94 | + ->assertCreated(); |
| 95 | + } |
| 96 | + |
| 97 | + public function test_policy_to_detach_a_user_to_a_company() |
| 98 | + { |
| 99 | + Gate::policy(Company::class, CompanyPolicy::class); |
| 100 | + |
| 101 | + $user = $this->mockUsers(2)->first(); |
| 102 | + $company = factory(Company::class)->create(); |
| 103 | + $this->authenticate( |
| 104 | + factory(User::class)->create() |
| 105 | + ); |
| 106 | + |
| 107 | + $this->postJson('restify-api/companies/'.$company->id.'/attach/users', [ |
| 108 | + 'users' => $user->id, |
| 109 | + 'is_admin' => true, |
| 110 | + ]) |
| 111 | + ->assertCreated(); |
| 112 | + |
| 113 | + $_SERVER['allow_detach_users'] = false; |
| 114 | + |
| 115 | + $this->postJson('restify-api/companies/'.$company->id.'/detach/users', [ |
| 116 | + 'users' => $user->id, |
| 117 | + 'is_admin' => true, |
| 118 | + ]) |
| 119 | + ->assertForbidden(); |
| 120 | + |
| 121 | + $_SERVER['allow_detach_users'] = true; |
| 122 | + |
| 123 | + $this->postJson('restify-api/companies/'.$company->id.'/detach/users', [ |
| 124 | + 'users' => $user->id, |
| 125 | + 'is_admin' => true, |
| 126 | + ]) |
| 127 | + ->assertNoContent(); |
| 128 | + } |
66 | 129 | } |
0 commit comments