RESTful Basic Auth的功能测试失败,Postman成功

时间:2017-05-15 13:33:57

标签: php symfony fosrestbundle

FOSRestBundle应用程序的功能测试失败,使用401代码,而具有相同凭据的Postman通过。测试db是sqlite,prod db是MySQL。存储的密码完全相同。

测试:

public function testBasicAuth() {
    $client = static::createClient([], ['PHP_AUTH_USER' => 'admin',
        'PHP_AUTH_PW' => '123Abc',]);

    $crawler = $client->request(
            'GET', '/basic/get_user/bborko@bogus.info'
    );
    $this->assertSame(200, $client->getResponse()->getStatusCode());
}

失败:

  

断言401与200相同。

控制器:

/**
 * @Route("/get_user/{email}", name="basic_get_user")
 * 
 * @return View
 */
public function getUserAction($email) {
    $em = $this->getDoctrine()->getManager();
    $data = $em->getRepository('AppBundle:Member')->findBy(['email' => $email]);

    if (!$data) {
        throw $this->createNotFoundException('Unable to find user entity');
    }
    $view = $this->view($data, 200)
            ->setTemplate("AppBundle:Users:getUsers.html.twig")
            ->setTemplateVar('user')
    ;

    return $this->handleView($view);
}

编辑:

注意:该应用程序还具有成功测试的API / API密钥路由。 security.yml:

...
    providers:
        fos_userbundle:
            id: fos_user.user_provider.username
        api_key_user_provider:
            id: api_key_user_provider
...
    firewalls:
        api:
            pattern: ^/api/
            stateless: true
            simple_preauth:
                authenticator: apikey_authenticator
            provider: api_key_user_provider
        basic:
            pattern: ^/basic/
            http_basic: 
                provider: fos_userbundle
            anonymous:    false
...
    access_control:
...
        - { path: ^/api/, role: ROLE_ADMIN }
        - { path: ^/basic/, role: ROLE_ADMIN }

邮差: Postman confi

1 个答案:

答案 0 :(得分:0)

如果您在标签中切换邮递员"标题"你应该看到标题"授权:{some token}"。它由Basic Auth发送给授权用户。你可以通过函数base64_encode("username:password")得到这个toke(冒号是必需的)。尝试通过此令牌授权您的用户进行测试