无效的CSRF令牌:Symfony 2

时间:2016-02-08 13:46:05

标签: php symfony behat

我正在使用Symfony 2.7应用程序上的behat工作。

这是一个非常简单的代码来测试注册表单,但由于CSRF令牌而失败。

我的配置有什么问题?

我该如何避免这个问题?

这是我的behat.yml:

default:
  suites:
    frontend_test_suite:
        type: symfony_bundle
        bundle: 'AppBundle'
  extensions:
    Behat\Symfony2Extension: ~
    Behat\MinkExtension:
      base_url: http://application.local/app_test.php
      browser_name: chrome
      sessions:
        default:
          symfony2: ~
      show_auto: true
      show_cmd: echo %s

这是我的FeatureContext的课程:

    <?php

    namespace MobileLeaves\Bundles\AppBundle\Features\Context;

    use Behat\Behat\Context\SnippetAcceptingContext;
    use Behat\MinkExtension\Context\MinkContext;
    use Behat\Symfony2Extension\Context\KernelDictionary;
    use Symfony\Component\Console\Input\StringInput;
    use Symfony\Component\Console\Output\BufferedOutput;
    use Symfony\Component\HttpKernel\KernelInterface;

    /**
     * Defines application features from the specific context.
     */
    class FeatureContext extends MinkContext implements SnippetAcceptingContext
    {

        use KernelDictionary;

        const DEBUG = true;  // make true if your tests have any troubles
        const LOADFIXTURES = true;  // activate when the project has fixtures

        /**
         * @var Application
         */
        protected $application;

        /**
         * Initializes context.
         *
         * Every scenario gets its own context instance.
         * You can also pass arbitrary arguments to the
         * context constructor through behat.yml.
         */
        public function __construct()
        {

        }

        /**
         * Clean the database (and schema).
         *
         * @Given /^database is clean$/
         */
        public function cleanDatabase()
        {
            print "\t\tC L E A N N I N G  -  D A T A B A S E \n";
            $this->application = new \Symfony\Bundle\FrameworkBundle\Console\Application($this->kernel);
            $this->application->setAutoExit(false);
            /*
             * In order to work-around this error:
             *    PDO Error - Invalid catalog name: 1046 No database selected
             * the sequence has been changed to prevent this error
             */
            //$this->runCommand("doctrine:database:drop", array("--force" => null));
            $this->runCommand('doctrine:database:create');
            $this->runCommand('doctrine:schema:drop', array('--force' => null));
            $this->runCommand('doctrine:schema:create');
            if (self::LOADFIXTURES) {
                $this->fixturesAreLoaded();
            }
            print "\t\tD A T A B A S E    -    C L E A N E D \n";
        }

        /**
         * @Given /^database is clean with \"(.*?)\" context loaded$/
         */
        public function cleanDatabaseWithContext($context)
        {
            $this->cleanDatabase($context);
        }

        /**
         * First, force logout, then go to the login page, fill the informations and finally go to requested page.
         *
         * @Given /^I am connected with "([^"]*)" and "([^"]*)" on "([^"]*)"$/
         *
         * @param string $login
         * @param string $rawPassword
         * @param string $url
         */
        public function iAmConnectedWithOn($login, $rawPassword, $url)
        {
            $this->visit('admin/logout');
            $this->visit('admin/login');
            $this->fillField('_username', $login);
            $this->fillField('_password', $rawPassword);
            $this->pressButton('_submit');
            $this->visit($url);
        }

        /**
         * @param $command
         * @param array $options
         */
        protected function runCommand($command, Array $options = array())
        {
            if (!self::DEBUG) {
                $options['-q'] = null;
            }
            $options['--env'] = 'test';
            foreach ($options as $option => $value) {
                $command .= ' '.$option.($value ? '='.$value : '');
            }
            $output = new BufferedOutput();
            $exitCode = $this->application->run(new StringInput($command), $output);

            if (self::DEBUG || $exitCode) {
                print sprintf("%s\t[exitCode=%d]\n%s", $command, $exitCode, $output->fetch());
            }
        }

        /**
         * @Given /^fixtures are loaded$/
         */
        public function fixturesAreLoaded()
        {

            $this->runCommand('doctrine:fixtures:load -n');
        }

        /**
         * {@inheritdoc}
         */
        public static function getAcceptedSnippetType()
        {
            return 'regex';
        }

        /**
         * Sets Kernel instance.
         *
         * @param KernelInterface $kernel
         */
        public function setKernel(KernelInterface $kernel) {
            $this->kernel = $kernel;
        }



    }

这是我的专题文件:

Feature: Free Registration

  Background:
    Given database is clean

  Scenario: Free user register
    Given I am on "/en/register"
    When I fill in the following:
      | fos_user_registration_form[email] | maurocasula@gmail.com |
      | fos_user_registration_form[profile][name] | NAME LASTNAME |
      | fos_user_registration_form[plainPassword][first] | pass |
      | fos_user_registration_form[plainPassword][second] | pass |
    And I press "_submit"
    And show last response
    Then I should see "app.registration.confirmed_free"
    And the response status code should be 200

1 个答案:

答案 0 :(得分:1)

最后我发现了问题。

注册页面中有两个表格。删除第二个形式的测试通过没有问题。

此致