如何使用 Laravel 测试授权重定向?

问题描述 投票:0回答:4

我已经手动测试了我想要的场景:

管理员用户可以访问网站的

/codes
部分。 普通用户会被重定向 (302) 返回
/dashboard
,并在转到
Sorry you are not allowed there
时收到一条消息
/qr

手动测试通过,但laravel测试失败。

我正在使用

laravel 5.1

管理员用户测试:

public function testAdminViewCodes()
    {
        //create an admin user
        $user = factory(App\User::class, 'admin')->create();

        $this->actingAs($user)
            ->visit('/codes')
            ->seePageIs('/codes')
            ->see('Codes');
    }

普通用户测试:

    public function testNormalViewCodesFail()
    {
        //create a normal user
        $normal_user = factory(App\User::class)->create();

        //TODO: Fix this failing test FFS

        $this->actingAs($normal_user)
             ->visit('/qr')
             ->seePageIs('/dashboard')
             ->see('Sorry you are not allowed there');
}

测试结果;

There was 1 failure:

1) AdminTest::testNormalViewQRCodesFail
Did not land on expected page [http://localhost/dashboard].

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/dashboard'
+'http://localhost/codes'

我认为工厂可能有问题,似乎总是创建一个管理员用户:

$factory->define(App\User::class, function (Faker\Generator $faker) {
    return [
        'email' => $faker->email,
        'password' => bcrypt(str_random(10)),
        'remember_token' => str_random(10),
        'is_admin' => false,
    ];
});

$factory->defineAs(App\User::class, 'admin', function ($faker) use ($factory) {
    $user = $factory->raw(App\User::class);

    return array_merge($user, ['is_admin' => true]);
});

我很抱歉这个问题太长了,但还有另一个相关问题。我正在使用

middleware
来测试用户是否是管理员:

<?php

namespace RMS\Http\Middleware;

use Closure;

class IsAdminMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (app()->env === 'testing') {
            return $next($request);
        }

        if (! $request->user()->isAdmin()) {
          return redirect()->route('dashboard')
              ->with('message', 'Sorry you are not allowed there');
        }

        return $next($request);
    }
}

Kernel.php

protected $routeMiddleware = [
        'auth' => \RMS\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \RMS\Http\Middleware\RedirectIfAuthenticated::class,
        'isadmin' => \RMS\Http\Middleware\IsAdminMiddleware::class,
    ];

并应用于路线:

Route::group(['middleware' => ['auth', 'isadmin']], function()
{
    Route::resource('user', 'UserController');
});

中间件是否被忽略?我确定不添加

use WithoutMiddleware;
声明。

php laravel unit-testing laravel-5 laravel-5.1
4个回答
8
投票

您有两个选择:

  • 更好地为用户工厂创建测试,就像它可以创建你想要的用户类型
  • 用户生成后使用调试器进行中断以手动检查它

我建议您创建测试,因为您现在有疑问,并且将来有可能意外地破坏工厂代码,因为它不是那么明显。


8
投票

顺便说一句:单元测试并不意味着

user experience
测试。为此,将进行
acceptance
functional
测试。更流行的工具之一是codeception。它与 phantomjsselenium 结合可以模拟浏览器会话并获得完整的用户体验渲染。

每个文档可在 http://codeception.com/docs/01-Introduction 文档:

验收测试:'验收测试可以涵盖从用户角度来看的标准但复杂的场景。通过验收测试,您可以确信用户遵循所有定义的场景不会出现错误。”

功能测试:“功能测试模拟 Web 请求($_GET 和 $_POST 变量)并将其发送到返回 HTML 响应的应用程序。 '

单元测试:“在将代码片段耦合在一起之前对其进行测试也非常重要。通过这种方式,您可以确保某些隐藏得很深的功能仍然有效,即使它没有被功能或验收测试覆盖。这也证明您生成了稳定且可测试的代码。'


4
投票

我建议使用“验证用户实例”

Auth::login($user);
了解更多详细信息,请阅读此处

此方法对 Laravel 5.x 及更高版本有效


0
投票

测试fdsfdsffsdfdsfdsfds 法斯夫 dsf dsfds F 乙肝病毒 VC BDDF 广东省

© www.soinside.com 2019 - 2024. All rights reserved.