1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Containers\Authorization\UI\API\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use App\Containers\Authorization\Models\Role; |
6
|
|
|
use App\Containers\Authorization\Tests\ApiTestCase; |
7
|
|
|
use App\Containers\User\Models\User; |
8
|
|
|
use Illuminate\Support\Facades\Config; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class RevokeUserFromRoleTest. |
12
|
|
|
* |
13
|
|
|
* @group authorization |
14
|
|
|
* @group api |
15
|
|
|
* |
16
|
|
|
* @author Mahmoud Zalt <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class RevokeUserFromRoleTest extends ApiTestCase |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
protected $endpoint = '[email protected]/roles/revoke'; |
22
|
|
|
|
23
|
|
|
protected $access = [ |
24
|
|
|
'roles' => '', |
25
|
|
|
'permissions' => 'manage-admins-access', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @test |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
public function testRevokeUserFromRole_() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$roleA = factory(Role::class)->create(); |
34
|
|
|
|
35
|
|
|
$randomUser = factory(User::class)->create(); |
36
|
|
|
$randomUser->assignRole($roleA); |
37
|
|
|
|
38
|
|
|
$data = [ |
39
|
|
|
'roles_ids' => [$roleA->getHashedKey()], |
40
|
|
|
'user_id' => $randomUser->getHashedKey(), |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
// send the HTTP request |
44
|
|
|
$response = $this->makeCall($data); |
45
|
|
|
|
46
|
|
|
// assert response status is correct |
47
|
|
|
$response->assertStatus(200); |
48
|
|
|
|
49
|
|
|
$responseContent = $this->getResponseContentObject(); |
50
|
|
|
|
51
|
|
|
$this->assertEquals($data['user_id'], $responseContent->data->id); |
52
|
|
|
|
53
|
|
|
$this->assertDatabaseMissing('model_has_roles', [ |
54
|
|
|
'model_id' => $randomUser->id, |
55
|
|
|
'role_id' => $roleA->id, |
56
|
|
|
]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @test |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function testRevokeUserFromRoleWithRealId_() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$roleA = factory(Role::class)->create(); |
65
|
|
|
|
66
|
|
|
$randomUser = factory(User::class)->create(); |
67
|
|
|
$randomUser->assignRole($roleA); |
68
|
|
|
|
69
|
|
|
$data = [ |
70
|
|
|
'roles_ids' => [$roleA->id], |
71
|
|
|
'user_id' => $randomUser->id, |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
// send the HTTP request |
75
|
|
|
$response = $this->makeCall($data); |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
// assert response status is correct. Note: this will return 200 if `HASH_ID=false` in the .env |
79
|
|
|
if (Config::get('apiato.hash-id')){ |
80
|
|
|
$response->assertStatus(400); |
81
|
|
|
|
82
|
|
|
$this->assertResponseContainKeyValue([ |
83
|
|
|
'message' => 'Only Hashed ID\'s allowed.', |
84
|
|
|
]); |
85
|
|
|
}else{ |
86
|
|
|
$response->assertStatus(200); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @test |
93
|
|
|
*/ |
94
|
|
|
public function testRevokeUserFromManyRoles_() |
95
|
|
|
{ |
96
|
|
|
$roleA = factory(Role::class)->create(); |
97
|
|
|
$roleB = factory(Role::class)->create(); |
98
|
|
|
|
99
|
|
|
$randomUser = factory(User::class)->create(); |
100
|
|
|
$randomUser->assignRole($roleA); |
101
|
|
|
$randomUser->assignRole($roleB); |
102
|
|
|
|
103
|
|
|
$data = [ |
104
|
|
|
'roles_ids' => [$roleA->getHashedKey(), $roleB->getHashedKey()], |
105
|
|
|
'user_id' => $randomUser->getHashedKey(), |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
// send the HTTP request |
109
|
|
|
$response = $this->makeCall($data); |
110
|
|
|
|
111
|
|
|
// assert response status is correct |
112
|
|
|
$response->assertStatus(200); |
113
|
|
|
|
114
|
|
|
$this->assertDatabaseMissing('model_has_roles', [ |
115
|
|
|
'model_id' => $randomUser->id, |
116
|
|
|
'role_id' => $roleA->id, |
117
|
|
|
]); |
118
|
|
|
|
119
|
|
|
$this->assertDatabaseMissing('model_has_roles', [ |
120
|
|
|
'model_id' => $randomUser->id, |
121
|
|
|
'role_id' => $roleB->id, |
122
|
|
|
]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.