首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >功能测试失败

功能测试失败
EN

Stack Overflow用户
提问于 2015-12-01 17:37:08
回答 1查看 325关注 0票数 2

我正在尝试对防火墙后面的路由进行功能测试。我不确定我做错了什么,但是路由admin/dashboard的测试失败了。有什么想法吗?

代码语言:javascript
复制
<?php

namespace AppBundle\Tests;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

class ApplicationAvailabilityFunctionalTest extends WebTestCase
{

    private $client;

    public function setUp()
    {
        $this->client = self::createClient();
    }

    /**
     * @dataProvider urlProvider
     */
    public function testPageIsSuccessful($url)
    {

        $this->client->request('GET', $url);

        $this->assertTrue($this->client->getResponse()->isSuccessful());
    }

    public function urlProvider()
    {
        $this->logIn();

        return array(
            array('/'),
            array('/admin/login'),
            array('/admin/dashboard'),
        );
    }

    public function logIn()
    {

        $this->client = self::createClient();
        $session = $this->client->getContainer()->get('session');

        $firewall = 'our_db_provider';
        $token = new UsernamePasswordToken('admin', 'admin', $firewall, array('ROLE_ADMIN'));
        $session->set('_security_'.$firewall, serialize($token));
        $session->save();

        $cookie = new Cookie($session->getName(), $session->getId());
        $this->client->getCookieJar()->set($cookie);
    }
}

//更新

下面是我得到的错误

代码语言:javascript
复制
1) AppBundle\Tests\ApplicationAvailabilityFunctionalTest::testPageIsSuccessful with data set #2 ('/admin/dashboard')
Failed asserting that false is true.

/Users/me/Projects/cms/src/AppBundle/Tests/ApplicationAvailabilityFunctionalTest.php:27

//更新2

下面是$token变量的转储

Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken {#488 -credentials: null -providerKey: "security" -user: "admin" -roles: array:1 [ 0 => Symfony\Component\Security\Core\Role\Role {#487 -role: "ROLE_ADMIN" } ] -authenticated: true -attributes: [] }

//更新3

代码语言:javascript
复制
`security:
    encoders:
        AppBundle\Entity\Admin\User:
            algorithm: bcrypt
    providers:
        our_db_provider:
            entity:
               class: AppBundle\Entity\Admin\User
               property: username
    access_control:
        - { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, roles: ROLE_ADMIN }
    firewalls:
        default:
            anonymous: ~
            http_basic: ~
            form_login:
               login_path: /admin/login
               check_path: /admin/login_check
               csrf_provider: security.csrf.token_manager
            logout:
               path:   /admin/logout
               target: /admin/login
            provider: our_db_provider
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            anonymous: ~`
EN

回答 1

Stack Overflow用户

发布于 2015-12-01 17:50:22

该路由不是公共路由

失败的测试位于可能受身份验证保护的/admin/dashboard路由上,因此服务器响应不会成功(200 OK),而是(403访问拒绝或302重定向)

因此您必须以不同方式测试您的路由:该路由是受保护的,因此请检查403或重定向到登录页面

查看有关How to Simulate Authentication with a Token in a Functional Test的文档

并测试通过身份验证的用户是否正确地看到该页面

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34017065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档