dq23171 2018-10-10 12:59
浏览 47

Laravel Unit测试非活动用户的登录

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?

  • 写回答

1条回答 默认 最新

  • duanbishai5271 2018-10-10 13:11
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多