|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit; |
| 4 | + |
| 5 | +use Tests\TestCase; |
| 6 | +use Illuminate\Foundation\Testing\WithFaker; |
| 7 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 8 | +use Illuminate\Foundation\Testing\DatabaseMigrations; |
| 9 | +use User; |
| 10 | +use Auth; |
| 11 | +use App\Admin; |
| 12 | +use DB; |
| 13 | +use Session; |
| 14 | +use Hash; |
| 15 | + |
| 16 | +class AuthAdminTest extends TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * A basic test example. |
| 20 | + * |
| 21 | + * @return void |
| 22 | + */ |
| 23 | + public function test_unathorized_user() |
| 24 | + { |
| 25 | + // => redirect to login page and get 302 status |
| 26 | + $response = $this->get('/panel'); |
| 27 | + $response->assertStatus(302) |
| 28 | + ->assertRedirect('/panel/login'); |
| 29 | + } |
| 30 | + |
| 31 | + public function test_login_page() |
| 32 | + { |
| 33 | + $response = $this->get('/panel/login'); |
| 34 | + $response->assertStatus(200); |
| 35 | + } |
| 36 | + |
| 37 | + public function test_fail_login() |
| 38 | + { |
| 39 | + $response = $this->withHeaders([])->json('POST', '/panel/login', [ |
| 40 | + 'email' => 'admin@change.me', |
| 41 | + 'password' => 'wrong-password' |
| 42 | + ]); |
| 43 | + $response->assertStatus(302) |
| 44 | + ->assertRedirect('/panel/login'); |
| 45 | + } |
| 46 | + |
| 47 | + public function test_admin_logining() |
| 48 | + { |
| 49 | + $response = $this->withHeaders([])->json('POST', '/panel/login', [ |
| 50 | + 'email' => 'admin@change.me', |
| 51 | + 'password' => 12345 |
| 52 | + ]); |
| 53 | + $response->assertStatus(302) |
| 54 | + ->assertRedirect('/panel'); |
| 55 | + } |
| 56 | + |
| 57 | + public function test_logout() |
| 58 | + { |
| 59 | + ### After do logout -> redirect to login page |
| 60 | + // Do login |
| 61 | + Auth::guard('panel')->loginUsingId(1, true); // 1 => admin@change.me |
| 62 | + // is authorized |
| 63 | + $response = $this->get('/panel'); |
| 64 | + $response->assertStatus(200); |
| 65 | + // Do logout |
| 66 | + $response = $this->get('/panel/logout'); |
| 67 | + // check redirect |
| 68 | + $response->assertStatus(302) |
| 69 | + ->assertRedirect('/panel/login'); |
| 70 | + // test an unauthorize page |
| 71 | + $response = $this->get('/panel'); |
| 72 | + $response->assertStatus(302) |
| 73 | + ->assertRedirect('/panel/login'); |
| 74 | + } |
| 75 | + |
| 76 | + public function test_add_new_admin () { |
| 77 | + $email = 'fake@fake.fake'; |
| 78 | + $data = [ |
| 79 | + 'email' => $email, |
| 80 | + 'password' => 'password', |
| 81 | + 'first_name' => 'test', |
| 82 | + 'last_name' => 'test', |
| 83 | + 'roles' => ['1'], // Super user |
| 84 | + 'save' => '1' |
| 85 | + ]; |
| 86 | + $adminCount = DB::table('admins')->where('email', $email) |
| 87 | + ->count(); |
| 88 | + $this->assertTrue($adminCount == 0); |
| 89 | + // if ($adminCount) { |
| 90 | + // DB::table('admins')->where('email', $email) |
| 91 | + // ->delete(); |
| 92 | + // } |
| 93 | + Auth::guard('panel')->loginUsingId(1, true); |
| 94 | + $response = $this->call('POST', '/panel/Admin/edit?insert=1', $data); |
| 95 | + $adminCount = DB::table('admins')->where('email', $email) |
| 96 | + ->count(); |
| 97 | + $this->assertTrue($adminCount == 1); |
| 98 | + } |
| 99 | + |
| 100 | + public function test_delete_admin () { |
| 101 | + Session::start(); |
| 102 | + $email = 'fake@fake.fake'; |
| 103 | + $data = [ |
| 104 | + '_method' => 'DELETE', |
| 105 | + '_token' => csrf_token() |
| 106 | + ]; |
| 107 | + $admin = DB::table('admins')->where('email', $email) |
| 108 | + ->first(); |
| 109 | + Auth::guard('panel')->loginUsingId(1, true); |
| 110 | + $response = $this->call('POST', "/panel/Admin/edit?do_delete=$admin->id", $data) |
| 111 | + ->assertStatus(200); |
| 112 | + $adminCount = DB::table('admins')->where('email', $email) |
| 113 | + ->count(); |
| 114 | + $this->assertTrue($adminCount == 0); |
| 115 | + } |
| 116 | + |
| 117 | + public function test_edit_admin () |
| 118 | + { |
| 119 | + Session::start(); |
| 120 | + $email = 'admin@change.me'; |
| 121 | + Auth::guard('panel')->loginUsingId(1, true); |
| 122 | + // Change admin information |
| 123 | + $data = [ |
| 124 | + 'email' => $email, |
| 125 | + 'first_name' => 'ftest', |
| 126 | + 'last_name' => 'ltest', |
| 127 | + 'password' => 12345, |
| 128 | + 'roles' => ['1'], |
| 129 | + '_method' => 'PATCH', |
| 130 | + '_token' => csrf_token(), |
| 131 | + "save" => "1" |
| 132 | + ]; |
| 133 | + $response = $this->call('POST', "/panel/Admin/edit?update=1", $data) |
| 134 | + ->assertStatus(200); |
| 135 | + |
| 136 | + $admin = DB::table('admins')->where('email', $email) |
| 137 | + ->first(); |
| 138 | + |
| 139 | + $this->assertTrue($admin->first_name == 'ftest' |
| 140 | + && $admin->last_name == 'ltest' |
| 141 | + && $admin->password == Hash::check('12345', $admin->password)); |
| 142 | + } |
| 143 | +} |
0 commit comments