-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
TestsAuthHelperTrait.php
225 lines (193 loc) · 5.77 KB
/
TestsAuthHelperTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
namespace Apiato\Core\Traits\TestsTraits\PhpUnit;
use App;
use App\Containers\Authentication\Tasks\ApiLoginThisUserObjectTask;
use App\Containers\User\Models\User;
use Artisan;
use Illuminate\Support\Facades\Hash;
/**
* Class TestsAuthHelperTrait.
*
* Tests helper for authentication and authorization.
*
* @author Mahmoud Zalt <mahmoud@zalt.me>
*/
trait TestsAuthHelperTrait
{
/**
* Logged in user object.
*
* @var User
*/
protected $testingUser;
/**
* Roles and permissions, to be attached on the user
*
* @var array
*/
protected $access = [
'permissions' => '',
'roles' => '',
];
/**
* Try to get the last logged in User, if not found then create new one.
* Note: if $userDetails are provided it will always create new user, even
* if another one was previously created during the execution of your test.
*
* By default Users will be given the Roles and Permissions found int he class
* `$access` property. But the $access parameter can be used to override the
* defined roles and permissions in the `$access` property of your class.
*
* @param null $access roles and permissions you'd like to provide this user with
* @param null $userDetails what to be attached on the User object
*
* @return \App\Containers\User\Models\User
*/
public function getTestingUser($userDetails = null, $access = null)
{
return is_null($userDetails) ? $this->findOrCreateTestingUser($userDetails, $access)
: $this->createTestingUser($userDetails, $access);
}
/**
* Same as `getTestingUser()` but always overrides the User Access
* (roles and permissions) with null. So the user can be used to test
* if unauthorized user tried to access your protected endpoint.
*
* @param null $userDetails
*
* @return \App\Containers\User\Models\User
*/
public function getTestingUserWithoutAccess($userDetails = null)
{
return $this->getTestingUser($userDetails, $this->getNullAccess());
}
/**
* @param $userDetails
* @param $access
*
* @return \App\Containers\User\Models\User
*/
private function findOrCreateTestingUser($userDetails, $access)
{
return $this->testingUser ? : $this->createTestingUser($userDetails, $access);
}
/**
* @param null $access
* @param null $userDetails
*
* @return User
*/
private function createTestingUser($userDetails = null, $access = null)
{
// "inject" the confirmed status, if userdetails are submitted
if(is_array($userDetails)) {
$defaults = [
'confirmed' => true,
];
$userDetails = array_merge($defaults, $userDetails);
}
// create new user
$user = $this->factoryCreateUser($userDetails);
// assign user roles and permissions based on the access property
$user = $this->setupTestingUserAccess($user, $access);
// authentication the user
$this->actingAs($user, 'api');
// set the created user
return $this->testingUser = $user;
}
/**
* @param null $userDetails
*
* @return User
*/
private function factoryCreateUser($userDetails = null)
{
return factory(User::class)->create($this->prepareUserDetails($userDetails));
}
/**
* @param null $userDetails
*
* @return array
*/
private function prepareUserDetails($userDetails = null)
{
$defaultUserDetails = [
'name' => $this->faker->name,
'email' => $this->faker->email,
'password' => 'testing-password',
];
// if no user detail provided, use the default details, to find the password or generate one before encoding it
return $this->prepareUserPassword($userDetails ? : $defaultUserDetails);;
}
/**
* @param $userDetails
*
* @return null
*/
private function prepareUserPassword($userDetails)
{
// get password from the user details or generate one
$password = isset($userDetails['password']) ? $userDetails['password'] : $this->faker->password;
// hash the password and set it back at the user details
$userDetails['password'] = Hash::make($password);
return $userDetails;
}
/**
* @return array|null
*/
private function getAccess()
{
return isset($this->access) ? $this->access : null;
}
/**
* @param $user
* @param $access
*
* @return mixed
*/
private function setupTestingUserAccess($user, $access = null)
{
$access = $access ? : $this->getAccess();
$user = $this->setupTestingUserPermissions($user, $access);
$user = $this->setupTestingUserRoles($user, $access);
return $user;
}
/**
* @param $user
* @param $access
*
* @return mixed
*/
private function setupTestingUserRoles($user, $access)
{
if (isset($access['roles']) && !empty($access['roles'])) {
if (!$user->hasRole($access['roles'])) {
$user->assignRole($access['roles']);
}
}
return $user;
}
/**
* @param $user
* @param $access
*
* @return mixed
*/
private function setupTestingUserPermissions($user, $access)
{
if (isset($access['permissions']) && !empty($access['permissions'])) {
$user->givePermissionTo($access['permissions']);
}
return $user;
}
/**
* @return array
*/
private function getNullAccess()
{
return [
'permissions' => null,
'roles' => null
];
}
}