cakephp 控制器测试 - 如何测试需要授权的操作?

发布于 2024-10-18 09:01:35 字数 370 浏览 3 评论 0原文

标题几乎说明了一切。我想测试例如 UsersController::admin_index() 操作,但需要授权用户访问此位置,因此当我运行测试时,它会将我发送到登录页面,甚至当我手动登录时,没有进行任何测试。

那么如何在不编辑实际授权码的情况下强制蛋糕跳过授权呢?

顺便说一句,如果有帮助的话,我的 testAdminIndex() 代码如下所示:

function testAdminIndex() {     
    $result = $this->testAction('/admin/users/index');      
    debug($result); 
}

The title pretty much says it all. I would like to test e.g. UsersController::admin_index() action, but it's required for the user to be authorized to access this location, therefore when I run the test it sends me to the login page and even when I log in manully, no tests are done.

So how can I force cake to skip authorization without editing the actual authorization code?

btw, in case it helps, my testAdminIndex() code looks like this:

function testAdminIndex() {     
    $result = $this->testAction('/admin/users/index');      
    debug($result); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光的暖冬 2024-10-25 09:01:35

这里有一篇文章介绍了这个主题...

http: //mark-story.com/posts/view/testing-cakephp-controllers-the-hard-way

建议在为经过身份验证的用户添加会话值后完全绕过“testAction”并手动执行请求。示例如下...

function testAdminEdit() {
    $this->Posts->Session->write('Auth.User', array(
        'id' => 1,
        'username' => 'markstory',
    ));
    $this->Posts->data = array(
        'Post' => array(
            'id' => 2,
            'title' => 'Best article Evar!',
            'body' => 'some text',
        ),
        'Tag' => array(
            'Tag' => array(1,2,3),
        )
    );
    $this->Posts->params = Router::parse('/admin/posts/edit/2');
    $this->Posts->beforeFilter();
    $this->Posts->Component->startup($this->Posts);
    $this->Posts->admin_edit();
}

There's an article that covers the subject here ...

http://mark-story.com/posts/view/testing-cakephp-controllers-the-hard-way

The recommendation is to bypass "testAction" completely and manually perform the request, after adding session values for the authenticated user. The example is as follows ...

function testAdminEdit() {
    $this->Posts->Session->write('Auth.User', array(
        'id' => 1,
        'username' => 'markstory',
    ));
    $this->Posts->data = array(
        'Post' => array(
            'id' => 2,
            'title' => 'Best article Evar!',
            'body' => 'some text',
        ),
        'Tag' => array(
            'Tag' => array(1,2,3),
        )
    );
    $this->Posts->params = Router::parse('/admin/posts/edit/2');
    $this->Posts->beforeFilter();
    $this->Posts->Component->startup($this->Posts);
    $this->Posts->admin_edit();
}
孤独患者 2024-10-25 09:01:35

这是在 cakephp 测试文档上。

http://book.cakephp.org /3.0/en/development/testing.html#testing-actions-that-require-authentication

测试需要身份验证的操作
如果您使用 AuthComponent,则需要删除 AuthComponent 用于验证用户身份的会话数据。您可以使用 IntegrationTestCase 中的辅助方法来执行此操作。假设您有一个包含 add 方法的 ArticlesController,并且该 add 方法需要身份验证,您可以编写以下测试:

public function testAddUnauthenticatedFails()
{
    // No session data set.
    $this->get('/articles/add');

    $this->assertRedirect(['controller' => 'Users', 'action' => 'login']);
}

public function testAddAuthenticated()
{
    // Set session data
    $this->session([
        'Auth' => [
            'User' => [
                'id' => 1,
                'username' => 'testing',
                // other keys.
            ]
        ]
    ]);
    $this->get('/articles/add');

    $this->assertResponseOk();
    // Other assertions.
}

我使用了这个,

// Set session data
$this->session(['Auth.User.id' => 1]);

我实际上有角色,所以我的解决方案如下所示:

public function testDisplay()
{
 $this->session(['Auth.User.id' => 1, 'Auth.User.role' => 'admin']);

    $this->get('/pages/home');
    $this->assertResponseOk();
    $this->assertResponseContains('CakePHP');
    $this->assertResponseContains('<html>');
}

This is on the cakephp testing documentation.

http://book.cakephp.org/3.0/en/development/testing.html#testing-actions-that-require-authentication

Testing Actions That Require Authentication
If you are using AuthComponent you will need to stub out the session data that AuthComponent uses to validate a user’s identity. You can use helper methods in IntegrationTestCase to do this. Assuming you had an ArticlesController that contained an add method, and that add method required authentication, you could write the following tests:

public function testAddUnauthenticatedFails()
{
    // No session data set.
    $this->get('/articles/add');

    $this->assertRedirect(['controller' => 'Users', 'action' => 'login']);
}

public function testAddAuthenticated()
{
    // Set session data
    $this->session([
        'Auth' => [
            'User' => [
                'id' => 1,
                'username' => 'testing',
                // other keys.
            ]
        ]
    ]);
    $this->get('/articles/add');

    $this->assertResponseOk();
    // Other assertions.
}

I used this

// Set session data
$this->session(['Auth.User.id' => 1]);

I actually have roles so my solution looks like this:

public function testDisplay()
{
 $this->session(['Auth.User.id' => 1, 'Auth.User.role' => 'admin']);

    $this->get('/pages/home');
    $this->assertResponseOk();
    $this->assertResponseContains('CakePHP');
    $this->assertResponseContains('<html>');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文