-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectTest.php
207 lines (174 loc) · 6.64 KB
/
ProjectTest.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
<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Base\Models\User;
use Laravel\Passport\Passport;
use App\Project\Models\Project;
use App\Base\Exceptions\UserIsNotMember;
use Illuminate\Support\Facades\Notification;
use Illuminate\Auth\Access\AuthorizationException;
class ProjectTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
$this->project = factory(Project::class)->create();
}
/** @test */
public function user_can_see_public_projects_and_projects_of_which_user_is_member()
{
$project = factory(Project::class)->create(['owner_id' => $this->user->id]);
$this->actingAs($this->user);
resolve('Authorization')->setupDefaultPermissions($project);
$project->members()->save($this->user);
$this->json('GET', 'projects/')->assertJsonFragment([
'status' => 'success',
'name' => $project->name,
]);
Passport::actingAs($this->user);
$this->json('GET', 'api/projects/')->assertJsonFragment([
'status' => 'success',
'name' => $project->name,
]);
}
/** @test */
public function user_with_permission_can_see_project_page()
{
// $this->user_with_permission_can_create_project();
// $id = Project::where('name', 'New Project')->first()->id;
// $this->actingAs($this->user)
// ->json('GET', 'projects/' . $id)
// ->assertJsonFragment([
// 'status' => 'success',
// 'name' => 'New Project',
// ]);
}
/** @test */
public function user_without_permission_cant_see_project_page()
{
// $this->expectException(AuthorizationException::class);
// $user = factory(User::class)->create(['role_id' => 5]);
// $this->user_with_permission_can_create_project();
// $id = Project::where('name', 'New Project')->first()->id;
// $this->actingAs($user)->get('projects/' . $id)->assertSee('New Project');
}
/** @test */
public function user_with_permission_can_create_project()
{
$this->actingAs($this->user)->post('projects', [
'name' => 'New Project',
'description' => 'Description for new project',
])->assertJsonFragment([
'status' => 'success',
'name' => 'New Project',
]);
$this->assertDatabaseHas('projects', ['name' => 'New Project', 'description' => 'Description for new project', 'owner_id' => $this->user->id]);
}
/** @test */
public function user_without_permission_cant_create_project()
{
$this->expectException(AuthorizationException::class);
$user = factory('App\Base\Models\User')->create(['role_id' => 5]);
$this->actingAs($user)->post('projects', [
'name' => 'New Project',
'description' => 'Description for new project',
]);
}
/** @test */
public function add_user_to_project()
{
Notification::fake();
$user = factory('App\Base\Models\User')->create();
$this->actingAs($this->user)->post('/members', [
'user_id' => $user->id,
'group_type' => 'project',
'group_id' => $this->project->id,
])->assertJson([
'status' => 'success',
'message' => 'User added to the project',
'user' => [
'id' => $user->id,
'name' => $user->name,
'username' => $user->username,
'avatar' => $user->avatar,
],
]);
}
/** @test */
public function user_with_permission_can_delete_a_project()
{
$this->user_with_permission_can_create_project();
$id = Project::where('name', 'New Project')->first()->id;
$this->actingAs($this->user)->delete('/projects/' . $id)
->assertJsonFragment([
'status' => 'success',
'message' => 'The project has been deleted',
]);
}
/** @test */
public function user_without_permission_cant_delete_a_project()
{
$this->expectException(AuthorizationException::class);
$user = factory(User::class)->create(['role_id' => 5]);
$this->user_with_permission_can_create_project();
$id = Project::where('name', 'New Project')->first()->id;
$this->actingAs($user)->delete('projects/' . $id);
}
/** @test */
public function remove_user_from_project()
{
$this->add_user_to_project();
$this->assertCount(1, $this->project->members);
$user = $this->project->members->first();
$this->actingAs($this->user)->delete('/members', [
'user_id' => $user->id,
'group_type' => 'project',
'group_id' => $this->project->id,
])->assertJson([
'status' => 'success',
'message' => 'User removed from the project',
'user' => [
'id' => $user->id,
'name' => $user->name,
'username' => $user->username,
'avatar' => $user->avatar,
],
]);
$this->assertEmpty($this->project->fresh()->members);
}
/** @test */
public function cannot_remove_user_from_project_if_not_a_member()
{
$this->expectException(UserIsNotMember::class);
$user = factory('App\Base\Models\User')->create();
$this->actingAs($this->user)
->delete('/members', [
'user_id' => $user->id,
'group_type' => 'project',
'group_id' => $this->project->id,
])->dump();
}
/** @test */
public function admin_can_make_project_public()
{
$this->actingAs($this->user)
->post('public-projects/' . $this->project->id)
->assertJsonFragment([
'status' => 'success',
'message' => localize('project.Project has been made public'),
]);
$this->assertDatabaseHas('projects', ['id' => $this->project->id, 'public' => true]);
}
/** @test */
public function admin_can_make_project_private()
{
$this->actingAs($this->user)
->post('public-projects/' . $this->project->id);
$this->delete('public-projects/' . $this->project->id)
->assertJsonFragment([
'status' => 'success',
'message' => localize('project.Project has been made private'),
]);
$this->assertDatabaseHas('projects', ['id' => $this->project->id, 'public' => false]);
}
}