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\TestCase; |
7
|
|
|
use App\Containers\User\Models\User; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AssignUserToRoleTest. |
11
|
|
|
* |
12
|
|
|
* @author Mahmoud Zalt <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class AssignUserToRoleTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
protected $endpoint = 'post@roles/assign'; |
18
|
|
|
|
19
|
|
|
protected $access = [ |
20
|
|
|
'roles' => 'admin', |
21
|
|
|
'permissions' => '', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
public function testAssignUserToRole_() |
25
|
|
|
{ |
26
|
|
|
$randomUser = factory(User::class)->create(); |
27
|
|
|
|
28
|
|
|
$role = factory(Role::class)->create(); |
29
|
|
|
|
30
|
|
|
$data = [ |
31
|
|
|
'roles_ids' => [$role->getHashedKey()], |
32
|
|
|
'user_id' => $randomUser->getHashedKey(), |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
// send the HTTP request |
36
|
|
|
$response = $this->makeCall($data); |
37
|
|
|
|
38
|
|
|
// assert response status is correct |
39
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
40
|
|
|
|
41
|
|
|
$responseContent = $this->getResponseContent($response); |
42
|
|
|
|
43
|
|
|
$this->assertEquals($data['user_id'], $responseContent->data->id); |
44
|
|
|
|
45
|
|
|
$this->assertEquals($data['roles_ids'][0], $responseContent->data->roles->data[0]->id); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function testAssignUserToRoleWithRealId_() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$randomUser = factory(User::class)->create(); |
51
|
|
|
|
52
|
|
|
$role = factory(Role::class)->create(); |
53
|
|
|
|
54
|
|
|
$data = [ |
55
|
|
|
'roles_ids' => [$role->id], // testing against real ID's |
56
|
|
|
'user_id' => $randomUser->id, // testing against real ID's |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
// send the HTTP request |
60
|
|
|
$response = $this->makeCall($data); |
61
|
|
|
|
62
|
|
|
// assert response status is correct. Note: this will return 200 if `HASH_ID=false` in the .env |
63
|
|
|
if(\Config::get('hello.hash-id')){ |
64
|
|
|
$this->assertEquals('400', $response->getStatusCode()); |
65
|
|
|
|
66
|
|
|
$this->assertResponseContainKeyValue([ |
67
|
|
|
'message' => 'Only Hashed ID\'s allowed (user_id).', |
68
|
|
|
], $response); |
69
|
|
|
}else{ |
70
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testAssignUserToManyRoles_() |
76
|
|
|
{ |
77
|
|
|
$randomUser = factory(User::class)->create(); |
78
|
|
|
|
79
|
|
|
$role1 = factory(Role::class)->create(); |
80
|
|
|
$role2 = factory(Role::class)->create(); |
81
|
|
|
|
82
|
|
|
$data = [ |
83
|
|
|
'roles_ids' => [ |
84
|
|
|
$role1->getHashedKey(), |
85
|
|
|
$role2->getHashedKey(), |
86
|
|
|
], |
87
|
|
|
'user_id' => $randomUser->getHashedKey(), |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
// send the HTTP request |
91
|
|
|
$response = $this->makeCall($data); |
92
|
|
|
|
93
|
|
|
// assert response status is correct |
94
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
95
|
|
|
|
96
|
|
|
$responseContent = $this->getResponseContent($response); |
97
|
|
|
|
98
|
|
|
$this->assertTrue(count($responseContent->data->roles->data) > 1); |
99
|
|
|
|
100
|
|
|
$this->assertEquals($data['roles_ids'][0], $responseContent->data->roles->data[0]->id); |
101
|
|
|
|
102
|
|
|
$this->assertEquals($data['roles_ids'][1], $responseContent->data->roles->data[1]->id); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
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.