【问题标题】:CakePHP Controller Testing with the Security Component使用安全组件进行 CakePHP 控制器测试
【发布时间】:2013-05-29 05:18:08
【问题描述】:

考虑这段代码:

控制器代码

<?php
App::uses('AppController', 'Controller');

class UsersController extends AppController {

    public $components = array(
        'Security',
        'Session'
    );

    public function example() {
        if ($this->request->is('post')) {
            $this->set('some_var', true);
        }
    }
}

查看代码

<?php

echo $this->Form->create();
echo $this->Form->input('name');
echo $this->Form->end('Submit');

由于我有安全组件,以任何方式篡改表单(例如向其添加字段)都会导致请求成为黑洞。我想测试一下:

测试代码

<?php

class UsersControllerTest extends ControllerTestCase {

    public function testExamplePostValidData() {
        $this->Controller = $this->generate('Users', array(
            'components' => array(
                'Security'
            )
        ));

        $data = array(
            'User' => array(
                'name' => 'John Doe'
            )
        );

        $this->testAction('/users/example', array('data' => $data, 'method' => 'post'));
        $this->assertTrue($this->vars['some_var']);
    }

    public function testExamplePostInvalidData() {
        $this->Controller = $this->generate('Users', array(
            'components' => array(
                'Security'
            )
        ));

        $data = array(
            'User' => array(
                'name' => 'John Doe',
                'some_field' => 'The existence of this should cause the request to be black-holed.'
            )
        );

        $this->testAction('/users/example', array('data' => $data, 'method' => 'post'));
        $this->assertTrue($this->vars['some_var']);
    }
}

第二个测试testExamplePostInvalidData 应该失败,因为some_field$data 数组中,但它通过了!我做错了什么?

【问题讨论】:

    标签: cakephp testing


    【解决方案1】:

    通过在 ->testAction 的数据中添加“some_field”,安全组件将假定该字段是您的应用程序的一部分(因为它来自您的代码,而不是 POST 数组),因此它不会被视为“黑客尝试”。

    检查黑洞有点复杂。但是 Cake 核心测试已经测试了黑洞功能,所以如果这些测试通过了,你就不需要在你的应用中检查它。

    如果您坚持,请查看核心 Cake 测试以获取指导:

    具体来说:

    /**
     * test that validatePost fails if any of its required fields are missing.
     *
     * @return void
     */
    public function testValidatePostFormHacking() {
        $this->Controller->Security->startup($this->Controller);
        $key = $this->Controller->params['_Token']['key'];
        $unlocked = '';
    
        $this->Controller->request->data = array(
            'Model' => array('username' => 'nate', 'password' => 'foo', 'valid' => '0'),
            '_Token' => compact('key', 'unlocked')
        );
        $result = $this->Controller->Security->validatePost($this->Controller);
        $this->assertFalse($result, 'validatePost passed when fields were missing. %s');
    }
    

    文件中有更多示例:
    https://github.com/cakephp/cakephp/blob/master/lib/Cake/Test/Case/Controller/Component/SecurityComponentTest.php

    【讨论】:

    • 这个答案让我想到了仅检查视图在 GET 上返回的内容并查看它是否包含不应编辑的字段的可能性。然后,知道安全组件已启用,我知道我是安全的。但我认为我的测试仍然有意义,因为测试不关心你的实现是什么;测试只关心结果。因此,发布不应编辑的字段的结果应该导致错误,无论是否从安全组件生成(测试不关心)。但我认为这个答案就足够了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 2012-06-12
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    相关资源
    最近更新 更多