<?php

namespace Tests\Browser\Auth;

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\Browser;
use Tests\Browser\Traits\AuthTrait;
use Tests\DuskTestCase;

class LoginTest extends DuskTestCase
{
    use DatabaseMigrations;
    use AuthTrait;

    public function testShouldFailLoginOnBadCredentials()
    {
        $this->browse(function (Browser $browser) {
            $browser->visit('/admin/login')
                ->assertSee('Username')
                ->type('username', 'admin')
                ->type('password', '123456789')
                ->click('#login_btn')
                ->waitForText('These credentials do not match our records.')
                ->assertPathIs('/admin/login')
                ->assertSee('These credentials do not match our records.');

            $browser->visit('/admin/login')
                ->type('username', 'admin')
                ->type('password', '')
                ->click('#login_btn')
                ->assertPathIs('/admin/login')
                ->assertSee('Username');

            $browser->visit('/admin/login')
                ->type('username', 'banana')
                ->type('password', '12345678')
                ->click('#login_btn')
                ->waitForText('These credentials do not match our records.')
                ->assertPathIs('/admin/login')
                ->assertSee('These credentials do not match our records.');
        });
    }

    public function testShouldLoginWithSuccessOnCorrectCredentials()
    {
        $this->browse(function (Browser $browser) {
            $browser->visit('/admin/login')
                    ->assertSee('Username')
                    ->type('username', 'admin')
                    ->type('password', '12345678')
                    ->click('#login_btn')
                    ->waitForText('This is my body content.')
                    ->assertPathIs('/admin')
                    ->assertSee('This is my body content.');
        });
    }

    public function testShouldRedirectToLoginPageIfNotAuthenticated()
    {
        $this->browse(function (Browser $browser) {
            $this->logoutFromAdminBackend();
            $browser->visit('/admin/content/form/page')
                ->assertPathIs('/admin/login');
        });
    }
}
