-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPermissionsTest.php
More file actions
125 lines (101 loc) · 4.53 KB
/
PermissionsTest.php
File metadata and controls
125 lines (101 loc) · 4.53 KB
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
<?php
use App\User;
use App\Role;
use App\Permission;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PermissionsTest extends TestCase
{
//use DatabaseMigrations;
/** @test */
public function cant_access_if_unauthenticated()
{
//Check if the Dashboard is Shown
$this->visit('/')->see('A glimpse to the future of the Store Plugin');
//Check if WebPanel cant be accessed if unauthorized
$this->visit('/webpanel')->see('Sign in');
}
/** @test */
public function can_access_if_authenticated()
{//Login with the default admin user
$user = User::where('name', 'admin')->first();
$this->actingAs($user);
//Check if WebPanel can be accessed
$this->visit('/webpanel')->see('Dashboard');
}
/** @test */
public function cant_access_if_unauthorized()
{
//Create the Temp User and the Temp Group
$user_role = $this->create_temp_user_and_role_if_not_exists();
$user = $user_role[0];
$role = $user_role[1];
$this->remove_permissions_from_role($role);
//Login with the user
$this->actingAs($user);
//Check if Dashboard can be accessed
$this->visit('/webpanel')->see('Dashboard');
//Check if Unauthorized
$this->visit('/webpanel/store/items')->see('You do not have the permission WebPanelStoreItemsView that is required to perform this action');
$this->visit('/webpanel/store/categories')->see('You do not have the permission WebPanelStoreCategoriesView that is required to perform this action');
$this->visit('/webpanel/store/users')->see('You do not have the permission WebPanelStoreUsersView that is required to perform this action');
$this->visit('/webpanel/store/servers')->see('You do not have the permission WebPanelStoreServersView that is required to perform this action');
$this->visit('/webpanel/store/tools')->see('You do not have the permission WebPanelStoreToolsView that is required to perform this action');
//Assign permissions to group
$this->add_permissions_to_role($role);
//Check if Authorized
$this->visit('/webpanel/store/items')->see('Items');
$this->visit('/webpanel/store/categories')->see('Categories');
$this->visit('/webpanel/store/users')->see('Users');
$this->visit('/webpanel/store/servers')->see('Servers');
$this->visit('/webpanel/store/tools')->see('Tools');
//Delete user and role
$this->delete_user_role($user, $role);
}
private function create_temp_user_and_role_if_not_exists()
{
//Check if the user and group exists
$user = User::where('name', 'temp')->first();
$role = Role::where('name', 'temp')->first();
// Check if user exists, if not create a user
if ($user == null) {
$user = new User;
$user->name = "temp";
$user->password = bcrypt("temp");
$user->save();
}
if ($role == null) {
$role = new Role;
$role->name = "temp";
$role->display_name = "temp";
$role->description = "A Temp Role";
$role->save();
}
$user->roles()->sync(array($role->id));
//Return the existing user and group if they exist
return array($user, $role);
}
private function delete_user_role(User $user, Role $role)
{
//Remove all permissions then delete the user and the roles
$role->perms()->sync(array());
$user->delete();
$role->delete();
}
private function add_permissions_to_role(Role $role)
{
//Get the permissions and assign them to the Role
$perm_str_itm_view = Permission::where('name', 'WebPanelStoreItemsView')->first();
$perm_str_cat_view = Permission::where('name', 'WebPanelStoreCategoriesView')->first();
$perm_str_usr_view = Permission::where('name', 'WebPanelStoreUsersView')->first();
$perm_str_srv_view = Permission::where('name', 'WebPanelStoreServersView')->first();
$perm_str_tol_view = Permission::where('name', 'WebPanelStoreToolsView')->first();
$role->attachPermissions(array($perm_str_itm_view, $perm_str_cat_view, $perm_str_usr_view, $perm_str_srv_view, $perm_str_tol_view));
}
private function remove_permissions_from_role(Role $role)
{
$role->perms()->sync(array());
}
}