簡體   English   中英

如何使用Laravel測試授權重定向?

[英]How to test an authorization redirect with Laravel?

我手動測試了我想要的場景:

管理員用戶可以轉到該站點的/codes部分。 普通用戶被重定向(302)回到/dashboard並有一條消息Sorry you are not allowed there當他們轉到/qr時, Sorry you are not allowed there

手動測試通過,但是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; 聲明。

您有兩種選擇:

  • 更好地為用戶工廠創建測試,就像它可以創建您想要的用戶類型
  • 用戶生成后用調試器中斷以手動檢查它

我建議你創建測試,因為你現在懷疑,並且將來有可能意外地破壞工廠代碼,因為它不是那么明顯。

另外: 單元測試不是user experience測試。 因為那將是acceptancefunctional測試。 其中一個比較流行的工具是代碼 它與phantomjsselenium結合使用可以模擬瀏覽器會話並獲得完整的用戶體驗渲染。

每個文檔可從http://codeception.com/docs/01-Introduction docs獲得:

驗收測試 :從用戶的角度來看,驗收測試可以涵蓋標准但復雜的場景。 通過驗收測試,您可以確信用戶在所有已定義的方案中都不會出錯。

功能測試 :'模擬Web請求的功能測試($ _GET和$ _POST變量)並將其發送到返回HTML響應的應用程序中。

單元測試 :'在將它們耦合在一起之前測試代碼片段也非常重要。 通過這種方式,您可以確保某些深度隱藏的功能仍然有效,即使功能或驗收測試未涵蓋這些功能。 這也證明您生成了穩定且可測試的代碼。

我建議使用Authenticate a User Instance Auth::login($user); 有關詳細信息,請閱讀此處

此方法適用於Laravel 5.x及更高版本

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM