-
Notifications
You must be signed in to change notification settings - Fork 0
/
TagTest.php
134 lines (113 loc) · 4.11 KB
/
TagTest.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
<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Base\Models\Tag;
use App\Project\Models\Project;
use App\TaskManager\Models\Task;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Auth\Access\AuthorizationException;
class TagTest extends TestCase
{
/** @test */
public function user_with_permission_can_create_tag()
{
$this->actingAs($this->user)->post('tags', [
'label' => 'dummy',
])->assertJsonFragment([
'status' => 'success',
'message' => 'Tag has been created',
'label' => 'dummy',
]);
$this->assertDatabaseHas('tags', ['label' => 'dummy']);
}
/** @test */
public function user_without_permission_can_not_create_tag()
{
$this->expectException(AuthorizationException::class);
$user = factory(\App\Base\Models\User::class)->create(['role_id' => 5]);
$this->actingAs($user)->post('tags', [
'label' => 'dummy',
]);
}
/** @test */
public function user_can_add_a_existing_tag_to_a_task()
{
$project = factory(Project::class)->create(['owner_id' => $this->user->id]);
$task = factory(Task::class)->create(['taskable_type' => 'project', 'taskable_id' => $project->id]);
$tag = factory(Tag::class)->create();
$this->actingAs($this->user);
resolve('Authorization')->setupDefaultPermissions($project);
$project->members()->save($this->user);
$this->post("tasks/{$task->id}/tags", [
'labels' => $tag->id,
'group_type' => $task->taskable_type,
'group_id' => $task->taskable_id,
])->assertJsonFragment([
'status' => 'success',
'message' => 'Tag has been added to the task',
]);
$this->assertDatabaseHas('task_tags', [
'task_id' => $task->id,
'tag_id' => $tag->id,
]);
}
/** @test */
public function user_with_permission_can_detach_a_tag_from_a_task()
{
$project = factory(Project::class)->create(['owner_id' => $this->user->id]);
$task = factory(Task::class)->create(['taskable_type' => 'project', 'taskable_id' => $project->id]);
$tag = factory(Tag::class)->create();
$this->actingAs($this->user);
resolve('Authorization')->setupDefaultPermissions($project);
$project->members()->save($this->user);
$task->tags()->attach($tag);
$this->delete("tasks/{$task->id}/tags/{$tag->id}", [
'group_type' => $task->taskable_type,
'group_id' => $task->taskable_id,
])
->assertJsonFragment([
'status' => 'success',
'message' => 'Tag has been deleted from the task',
]);
$this->assertDatabaseMissing('task_tags', [
'task_id' => $task->id,
'tag_id' => $tag->id,
]);
}
/** @test */
public function user_without_permission_can_not_detach_a_tag_from_a_task()
{
$this->expectException(AuthorizationException::class);
$user = factory(\App\Base\Models\User::class)->create(['role_id' => 5]);
$tag = factory(Tag::class)->create();
$task = factory(Task::class)->create();
$task->tags()->attach($tag);
$this->actingAs($user)->delete("tasks/{$task->id}/tags/{$tag->id}", [
'group_type' => $task->taskable_type,
'group_id' => $task->taskable_id,
]);
}
/** @test */
public function authenticated_user_can_access_all_tags()
{
$tag = factory(Tag::class)->create();
$this->actingAs($this->user)
->get('/tags')
->assertJsonFragment([
'status' => 'success',
'total' => 1,
'tags' => [
[
'id' => $tag->id,
'label' => $tag->label,
],
],
]);
}
/** @test */
public function guest_user_cant_access_all_tags()
{
$this->expectException(AuthenticationException::class);
$this->get('/tags');
}
}