-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathLinkAuthorizationTest.php
More file actions
213 lines (188 loc) · 7.15 KB
/
LinkAuthorizationTest.php
File metadata and controls
213 lines (188 loc) · 7.15 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
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
<?php
namespace Tests\Feature\AuthPage;
use App\Models\Url;
use Illuminate\Support\Facades\Hash;
use PHPUnit\Framework\Attributes as PHPUnit;
use Tests\Support\Helper;
use Tests\TestCase;
#[PHPUnit\Group('auth-page')]
#[PHPUnit\Group('link-page')]
class LinkAuthorizationTest extends TestCase
{
/**
* Test that an authorized user can access the edit page.
*
* @see \App\Http\Controllers\LinkController::edit()
*/
#[PHPUnit\Test]
public function canAccessEditLinkPage(): void
{
$url = Url::factory()->create();
$response = $this->actingAs($url->author)
->get(route('link.edit', $url->keyword));
$response->assertOk();
}
#[PHPUnit\DataProvider('canAccessAnotherUsersEditLinkPageProvider')]
#[PHPUnit\Test]
public function canAccessAnotherUsersEditLinkPage($url, $actingAs, $expectedStatus): void
{
$url = $url($this);
$response = $this->actingAs($actingAs($this))
->get(route('link.edit', $url->keyword));
$response->assertStatus($expectedStatus);
}
public static function canAccessAnotherUsersEditLinkPageProvider(): array
{
return [
'Admin can access another users link edit page' => [
'url' => fn(TestCase $tc) => Url::factory()->create(),
'actingAs' => fn(TestCase $tc) => $tc->adminUser(),
'expectedStatus' => 200,
],
'Admin can access guest users link edit page' => [
'url' => fn(TestCase $tc) => Url::factory()->guest()->create(),
'actingAs' => fn(TestCase $tc) => $tc->adminUser(),
'expectedStatus' => 200,
],
'Basic users cant access other users link edit page' => [
'url' => fn(TestCase $tc) => Url::factory()->create(),
'actingAs' => fn(TestCase $tc) => $tc->basicUser(),
'expectedStatus' => 403,
],
];
}
#[PHPUnit\DataProvider('canUpdateAnotherUsersLinkProvider')]
#[PHPUnit\Test]
public function canUpdateAnotherUsersLink($url, $actingAs, $expectedUpdate): void
{
$url = $url($this);
$newLongUrl = 'https://phpunit.readthedocs.io/en/9.1';
$response = $this->actingAs($actingAs($this))
->from(route('link.edit', $url->keyword))
->post(
route('link.update', $url->keyword),
Helper::updateLinkData($url, ['long_url' => $newLongUrl]),
);
if ($expectedUpdate) {
$response
->assertRedirectToRoute('link.edit', $url->keyword)
->assertSessionHas('flash_success');
$this->assertSame($newLongUrl, $url->fresh()->destination);
} else {
$response->assertForbidden();
$this->assertNotSame($newLongUrl, $url->fresh()->destination);
}
}
public static function canUpdateAnotherUsersLinkProvider(): array
{
return [
'Admin can update another users link' => [
'url' => fn(TestCase $tc) => Url::factory()->create(),
'actingAs' => fn(TestCase $tc) => $tc->adminUser(),
'expectedUpdate' => true,
],
'Admin can update guest users link' => [
'url' => fn(TestCase $tc) => Url::factory()->guest()->create(),
'actingAs' => fn(TestCase $tc) => $tc->adminUser(),
'expectedUpdate' => true,
],
'Basic user cant update other users link' => [
'url' => fn(TestCase $tc) => Url::factory()->create(),
'actingAs' => fn(TestCase $tc) => $tc->basicUser(),
'expectedUpdate' => false,
],
];
}
/**
* Test that an admin user can delete another user's link.
*
* @see \App\Http\Controllers\LinkController::delete()
*/
#[PHPUnit\Test]
public function delete_AnotherUsersLink_ByAdmin_WillBeOk(): void
{
$url = Url::factory()->create();
$response = $this->actingAs($this->adminUser())
->from(route('dboard.allurl'))
->get(route('link.delete.fromTable', $url->keyword));
$response->assertRedirectToRoute('dboard.allurl')
->assertSessionHas('flash_success');
$this->assertCount(0, Url::all());
}
/**
* Normal users can't delete other users' URLs.
*
* @see \App\Http\Controllers\LinkController::delete()
*/
#[PHPUnit\Test]
public function delete_AnotherUsersLink_ByBasicUser_WillBeForbidden(): void
{
$url = Url::factory()->create();
$response = $this->actingAs($this->basicUser())
->from(route('dboard.allurl'))
->get(route('link.delete', $url->keyword));
$response->assertForbidden();
$this->assertCount(1, Url::all());
}
#[PHPUnit\Test]
public function password_store_adminCanAccessAll()
{
$url = Url::factory()->create();
$this->actingAs($this->adminUser())
->post(route('link.password.store', $url), [
'password' => 'password',
'password_confirmation' => 'password',
]);
$this->assertNotNull($url->fresh()->password);
}
#[PHPUnit\Test]
public function password_store_otherUserCantAccess()
{
$url = Url::factory()->create();
$this->actingAs($this->basicUser())
->post(route('link.password.store', $url), [
'password' => 'password',
'password_confirmation' => 'password',
])
->assertForbidden();
$this->assertNull($url->fresh()->password);
}
#[PHPUnit\Test]
public function password_update_adminCanAccessAll()
{
$url = Url::factory()->create(['password' => 'password']);
$this->actingAs($this->adminUser())
->post(route('link.password.update', $url), [
'password' => 'new-password',
'password_confirmation' => 'new-password',
]);
$this->assertTrue(Hash::check('new-password', $url->fresh()->password));
}
#[PHPUnit\Test]
public function password_update_otherUserCantAccess()
{
$url = Url::factory()->create(['password' => 'password']);
$this->actingAs($this->basicUser())
->post(route('link.password.update', $url), [
'password' => 'new-password',
'password_confirmation' => 'new-password',
]);
$this->assertFalse(Hash::check('new-password', $url->fresh()->password));
}
#[PHPUnit\Test]
public function password_delete_adminCanAccessAll()
{
$url = Url::factory()->create(['password' => 'password']);
$this->actingAs($this->adminUser())
->delete(route('link.password.delete', $url));
$this->assertNull($url->fresh()->password);
}
#[PHPUnit\Test]
public function password_delete_otherUserCantAccess()
{
$url = Url::factory()->create(['password' => 'password']);
$this->actingAs($this->basicUser())
->get(route('link.password.delete', $url));
$this->assertNotNull($url->fresh()->password);
}
}