This repository has been archived by the owner on Jul 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathUsersControllerTest.php
82 lines (70 loc) · 2.76 KB
/
UsersControllerTest.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
<?php
namespace Tests\Feature\Controllers\Admin;
use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\RefreshDatabase;
class UsersControllerTest extends TestCase
{
use RefreshDatabase;
public function test_member_can_not_manage_users()
{
$user = factory(User::class)->create();
$this->assertFalse($user->isAdmin());
// View List
$response = $this->actingAs($user)->get(route('admin::users.index'));
$response->assertRedirect(route('dashboard::index'));
// Create User
$tmpUser = factory(User::class)->make();
$response = $this->actingAs($user)->post(route('admin::users.store'),
array_merge($tmpUser->toarray(), [
'password' => 'secret',
'password_confirmation' => 'secret',
'_token' => csrf_token(),
]));
$this->assertDatabaseMissing('users', [
'email' => $tmpUser->email
]);
$response->assertRedirect(route('dashboard::index'));
// Delete User
$newUser = factory(User::class)->create();
$response = $this->actingAs($user)->delete(route('admin::users.destroy', $newUser->id));
$this->assertDatabaseHas('users', [
'id' => $newUser->id
]);
$response->assertRedirect(route('dashboard::index'));
}
public function test_admin_can_manage_users()
{
$user = factory(User::class, 'admin')->create();
$this->assertTrue($user->isAdmin());
// View List
$response = $this->actingAs($user)->get(route('admin::users.index'));
$response->assertStatus(200);
// Create User
$tmpUser = factory(User::class)->make();
$response = $this->actingAs($user)->post(route('admin::users.store'),
array_merge($tmpUser->toarray(), [
'password' => 'secret',
'password_confirmation' => 'secret',
'_token' => csrf_token(),
]));
$this->assertDatabaseHas('users', [
'email' => $tmpUser->email
]);
$response->assertRedirect(route('admin::users.index'));
// Delete User
$newUser = factory(User::class)->create();
$response = $this->actingAs($user)->delete(route('admin::users.destroy', $newUser->id));
$this->assertDatabaseMissing('users', [
'id' => $newUser->id
]);
$response->assertRedirect(route('admin::users.index'));
// Can not Delete Own User
$response = $this->actingAs($user)->delete(route('admin::users.destroy', $user->id));
$this->assertDatabaseHas('users', [
'id' => $user->id
]);
}
}