贝哈特 - 拉拉维尔 - CAS 登录用户功能测试



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

  Scenario: Authentication
    Given I am on the homepage
    And I press "Login" button
    Then I should see "Login Successfully"

但是,它没有像我预期的那样工作。知道吗?

非常感谢

这是我的

一位同事找到并成功实施的解决方案

app/Http/CASAuth.php

<?php
namespace AppHttpMiddleware;
use Closure;
use IlluminateContractsAuthGuard;
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  IlluminateHttpRequest $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);
    }
}

features/bootstrap/FeatureContext.php

/**
     * @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()));
    }

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"

这应该有效。

最新更新