Laravel测试对路由的授权,中间件auth问题

时间:2018-08-06 11:51:45

标签: php laravel testing phpunit laravel-5.6

与我同在,是测试的新手。

因此,我具有中间件的当前结构:

class IsActive
{
    public function handle($request, Closure $next)
    {
        if (self::active()) {
            return $next($request);
        }
        Auth::guard('web')->logout();
        return redirect('/');
    }

    protected function active()
    {
        if (Auth::guard('web')->check()) {
            $state = Auth::guard('web')->user()->state;
            if ($state == 'enabled') {
                return true;
            }
        }
        return false;
    }
}

class IsAdmin extends IsActive
{
    public function handle($request, Closure $next)
    {
        if (parent::active()) {
            if (Auth::guard('web')->check()) {
                $role = Auth::guard('web')->user()->role->name;
                if ($role == 'admin' || $role == 'superAdmin') {
                    return $next($request);
                }
            }
            return redirect('/');
        }
        Auth::guard('web')->logout();
        return redirect('/');
    }
}

这是路线

Route::group(['middleware' => 'admin'], function () {
    Route::get('/', 'ReportingController@reportingPage');
});
//Its registered on RouteServiceProvider that these routes require IsActive and this specific route needs for the admin

在报表控制器中,我有了此功能

public function reportingPage(Request $request) {
    return view('reporting.reporting');
}

到目前为止很好。如果用户具有访问权限,那么很好,如果没有访问权限,它将被重定向。

现在要进行测试,我想验证管理员是否可以查看它(未经授权的人不能查看,但是对于这个问题,我试图找出管理员)

protected $user;

public function setUp()
{
    parent::setUp();
    $this->user = new \App\Models\User(['role_id'    => 6, //admin
                                        'name'       => 'Admin',
                                        'user'       => 'admin',
                                        'email'      => 'admin@admin.com',
                                        'state'      => 'enabled',
                                        'deleted_at' => null]);
    $this->be($this->user);
}

public function testCanAccessReportingPage()
{
    $this->get("/reporting/")->assertStatus(200);
}

Error

这是我遇到的错误,我正在尝试弄清楚该过程。显然我做错了什么或忘记了什么,因此非常感谢有关如何测试此授权的提示和想法。

编辑:

在删除了涉及重构和一些尝试的答案之后,我确定Auth::guard('web')->user()有一个用户,Auth::guard('web')->user()->role()在测试环境中为空。我已经通过加载关系解决了问题,但是我现在遇到的问题是:为什么这会在测试环境中发生?

2 个答案:

答案 0 :(得分:1)

我稍微更改了您的代码。删除了重复的代码,并为用户角色$user->load('role')添加了延迟加载的请求。尝试

class IsAdmin
{
    public function handle($request, Closure $next)
    {
        //no need to check active again, because you said IsActive is already applied in RouteServiceProvider
        $user = Auth::guard('web')->user();

        $user->load('role'); //lazy eager load

        $role = $user->role->name; 

        if ($role == 'admin' || $role == 'superAdmin') {
            return $next($request);
        }

        Auth::guard('web')->logout();
        return redirect('/');
    }
}

答案 1 :(得分:0)

可以在$ this上下文中不使用创建的用户(模拟)。

如果您查看文档https://laravel.com/docs/5.6/http-tests#session-and-authentication,则会看到方法

$this->actingAs($user)

完整示例:

<?php

use App\User;

class ExampleTest extends TestCase
{
    public function testApplication()
    {
        $user = factory(User::class)->create();

        $response = $this->actingAs($user)
                         ->withSession(['foo' => 'bar'])
                         ->get('/');
    }
}
相关问题