【问题标题】:Can't authenticate for REST testing无法对 REST 测试进行身份验证
【发布时间】:2017-10-27 01:50:36
【问题描述】:

我在 xampp 上设置了以下虚拟主机名:reporting.dev

public function userIsAuthenticated(\ApiTester $I)
    {
        $I->amGoingTo('Check Authentication works');
        // i've tried both of these ways to authenticate
        $I->amHttpAuthenticated('my_email', 'my_password');
        $I->haveHttpHeader('Authorization', 'Basic super_long_token');
        $I->sendGET(Url::toRoute('/api/reports', true)); 
        $I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK); // 200
        $I->deleteHeader('Authorization');
    }

以下请求在邮递员http://reporting.dev/api/reports 中使用基本身份验证标头和上述测试中的相同令牌。

这是我的 api 套件配置:

class_name: ApiTester
modules:
    enabled:
      - REST:
          depends: PhpBrowser
          #url: /api/
          part: Json
      - Yii2:
          part: [orm, fixtures]
          configFile: 'config/web.php'

我正在使用 Yii2,如果我从我的 api 控制器中删除将身份验证确定为基本身份验证的行为函数,我会得到 200 响应和预期的 json。

public function behaviors()
  {
      $behaviors = parent::behaviors();
      $behaviors['authenticator'] = [
          'class' => HttpBasicAuth::className(),
          'except' => [], 
      ];
      return $behaviors;
  }

所以我不确定我还能在这里做什么或为什么我没有通过身份验证

【问题讨论】:

  • amHttpAuthenticated 的 PhpBrowser 方法(以及使用 PhpBrowser 时的 REST)设置 Authorization 标头,因此使用 haveHttpHeader 设置它是多余的,可能会导致问题。
  • 我删除了有关冲突模块的 cmets,因为您启用了正确的部分。
  • @Naktibalda 那么你有什么建议,我已经分别尝试了它们,我得到了 401s
  • echo base64_decode('token');并检查您是否收到相同的电子邮件和密码

标签: yii2 codeception


【解决方案1】:

我遇到的问题是我的数据库中的身份验证令牌不是 base64 编码的。

public function userIsAuthenticated(\ApiTester $I)
{
    $I->amGoingTo('Check Authentication works');
    // fixed now 
    $I->haveHttpHeader('Authorization', 'Basic ' . base64_encode('Basic super_long_db_auth_token'));
    $I->sendGET(Url::toRoute('/api/reports', true)); 
    $I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK); // 200
    $I->deleteHeader('Authorization');
}

【讨论】:

  • header可以这样表示Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1len.wikipedia.org/wiki/Basic_access_authentication#Client_side
  • 我知道。这是标题如何表示的一部分Basic token
  • 不,我在 base64_encoded 标记前面有 'Basic ' 这个词。因为需要。没有它,测试就会失败。这就是我引用维基文章的原因。邮递员跨多种语言构建的所有请求也以此为前缀:-)
猜你喜欢
  • 2013-12-29
  • 2011-08-04
  • 2016-04-18
  • 2016-01-08
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
  • 2013-08-23
  • 1970-01-01
相关资源
最近更新 更多