Laravel Unit test login of inactive user

10
October 11, 2018, at 5:50 PM

I would like to test the case when a user is marked as inactive in db, here is the method in LoginController

<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */
    use AuthenticatesUsers;
    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(['guest'])->except('logout'); //, 'checkup'
    }
    /**
     * The user has been authenticated.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  mixed $user
     * @return mixed
     */
    protected function authenticated(Request $request, $user)
    {
        if ($user->roles->count() === 0 ) {
            $this->guard()->logout();
            flash('There is no role specified please contact the system Administrator!')->error();
            return back()->withErrors(['There is no role specified']);
        }
    }
    /**
     * @param Request $request
     * @return array
     */
    protected function credentials(Request $request)
    {
        $credentials = $request->only($this->username(), 'password');
        $credentials['status'] = "A";
        return $credentials;
    }
}

therefore I create a fake user and I use the following test to cover the case

 $this->faker = Faker::create();
    $password = $this->faker->password;
    $user = factory(User::class)->create([
        'username' => $this->faker->username,
        'firstname' => $this->faker->firstName,
        'lastname' => $this->faker->lastName,
        'status'=> 'I',
        'password' => bcrypt($password),
        'email' => $this->faker->email
    ]);
    $response = $this->call('POST', '/login', [
       'email' => $user->email,
       'password' =>$password,
        '_token' => csrf_token()
    ]);
    $response->assertRedirect('/login');

but the user is getting logged in each time.

testing the same case with laravel dusk, will pass fine

public function testNotActiveUserLogin()
    {
        $faker = Faker\Factory::create();
        $password = $faker->password;
        $user = factory(User::class)->create([
            'username' => $faker->username,
            'firstname' => $faker->firstName,
            'lastname' => $faker->lastName,
            'status' => 'I',
            'password' => bcrypt($password),
            'email' => $faker->email
        ]);
        $this->browse(function (Browser $browser) use ($user, $password) {
            $browser->visit('/')
                ->type('email', $user->email)
                ->type('password', $password)
                ->press('Sign In')
                ->assertPathIs('/login');
        });
    }

What I miss in this case?

Answer 1

The user will always be seen as an active user because you overwrite the status after you get the user from the database with: $credentials['status'] = "A";. If you remove this statement the credentials should be correct.

READ ALSO
can not login when i add login with mobile functionality php

can not login when i add login with mobile functionality php

i have a login form in which user can login through mobile number or email or username now when i login through mobile number it works fine but when i login through username or email it says invalid how to make it works and when i remove the query for mobile...

43
Not found nagios index page

Not found nagios index page

I have installed Nagios on server following manual: http://linuxcallingblogspot

31
CakePHP 3.6: fetchAll()with conditions

CakePHP 3.6: fetchAll()with conditions

I would like to add search functionality in my viewThe problem is that I get data from raw sql like this so I dont know how to filter the results:

18