[英]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
測試。 因為那將是acceptance
或functional
測試。 其中一個比較流行的工具是代碼 。 它與phantomjs或selenium結合使用可以模擬瀏覽器會話並獲得完整的用戶體驗渲染。
每個文檔可從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.