Behat - Laravel - CAS登录用户功能测试

时间:2017-03-26 06:46:09

标签: php laravel cas behat

我在Laravel项目中使用Behat编写功能。 我的方案只是检查用户是通过CAS系统成功登录还是失败。

Benchmark summary:
Time units : milliseconds 
 expr n.eval  min lw.qu median mean up.qu   max total relative
purrr    100 25.2  29.8   37.5 43.4  44.2 159.0  4340      1.1
 rcpp    100 19.0  27.9   34.3 35.8  37.2  93.8  3580      1.0

然而,它没有像我预期的那样工作。 有什么想法吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

这是我的一位同事发现并成功实施的解决方案

应用程序/ HTTP / CASAuth.php



<?php
namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class MiamiCASAuth
{

    protected $auth;
    protected $cas;

    public function __construct(Guard $auth)
    {
        if(strcmp(env(“APP_ENV”),“acceptance”) !== 0) {
          $this->auth = $auth;
          $this->cas = app(‘cas’);
        }
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(strcmp(env("APP_ENV"),"acceptance") !== 0) {
            if ($this->cas->isAuthenticated()) {
                // Store the user credentials in a Laravel managed session
                session()->put('cas_user', $this->cas->user());

            } else {
                if ($request->ajax()) {
                    return response('Unauthorized.', 401);
                }
                $this->cas->authenticate();
            }
        }

        return $next($request);
    }
}
&#13;
&#13;
&#13;

特征/引导/ FeatureContext.php

&#13;
&#13;
/**
     * @Given /^I am logged in as "([^"]*)"$/
     */
    public function iAmLoggedInAs($id)
    {
        if (Session::isStarted()) {
            Session::regenerate(true);
        } else {
            Session::start();
        }

        // Set server session
      
        Session::put('cas_user', $id);
        
        // Set client cookie
        $minkSession = $this->getSession();
        $minkSession->setCookie(Session::getName(), Crypt::encrypt(Session::getId()));
    }
&#13;
&#13;
&#13;

&#13;
&#13;
Scenario: login
Given I am logged in as "trinhdh"
And I am on the homepage
Then the response should contain "Logged in as: Trinh Daniel"
&#13;
&#13;
&#13;

这应该有效。