-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLimitWikiAccessTest.php
More file actions
83 lines (70 loc) · 2.34 KB
/
LimitWikiAccessTest.php
File metadata and controls
83 lines (70 loc) · 2.34 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
<?php
namespace Tests\Jobs;
use App\User;
use App\Wiki;
use App\WikiManager;
use Illuminate\Http\Request;
use Tests\TestCase;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Testing\RefreshDatabase;
class LimitWikiAccessTest extends TestCase
{
use RefreshDatabase;
public function setUp(): void
{
parent::setUp();
Route::middleware('limit_wiki_access')->get('/endpoint', function (Request $request) {
return response()->json([
'wiki_id' => $request->attributes->get('wiki')->id
]);
});
}
public function tearDown(): void
{
parent::tearDown();
}
private function createWikiAndUser(): array
{
$wiki = Wiki::factory()->create();
$user = User::factory()->create(['verified' => true]);
WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);
return array($wiki, $user);
}
private function getURI(Wiki $wiki): string
{
return "/endpoint?wiki={$wiki->id}";
}
public function testSuccess(): void
{
[$wiki, $user] = $this->createWikiAndUser();
$this->actingAs($user)
->json('GET', $this->getURI($wiki))
->assertStatus(200)
->assertJson(['wiki_id' => $wiki->id]);
}
public function testFailOnWrongWikiManager(): void
{
$userWiki = Wiki::factory()->create();
$otherWiki = Wiki::factory()->create();
$user = User::factory()->create(['verified' => true]);
WikiManager::factory()->create(['wiki_id' => $userWiki->id, 'user_id' => $user->id]);
$this->actingAs($user)->json('GET', $this->getURI($otherWiki))->assertStatus(403);
}
public function testFailOnDeletedWiki(): void
{
[$wiki, $user] = $this->createWikiAndUser();
$wiki->wikiManagers()->delete();
$wiki->delete();
$this->actingAs($user)->json('GET', $this->getURI($wiki))->assertStatus(404);
}
public function testFailOnMissingWiki(): void
{
[$wiki, $user] = $this->createWikiAndUser();
$this->actingAs($user)->json('GET', '/endpoint')->assertStatus(422);
}
public function testFailOnMissingUser(): void
{
[$wiki, $user] = $this->createWikiAndUser();
$this->json('GET', $this->getURI($wiki))->assertStatus(403);
}
}