0

我需要一些关于如何在 Cake 2.3 中设置单元测试来测试 OAuth 登录的建议。我正在使用thomseddon/cakephp-oauth-server插件。注意:我已经查看了诸如CakePHP 2.3 - Unit testing User Login之类的示例,但我仍然对如何使用该插件进行 OAuth 测试感到困惑。任何帮助表示赞赏。

以下是我目前在单元测试中的内容。还不是很考验。

/**
* testOAuthLogin method
* Tests that OAuth login works
* @return void
*/
public function testOAuthLogin(){

    $data = array(
        'response_type' => 'code',
        'client_id' => getenv('THREE_SCALE_APP_ID'),
        'User' => array(
            'username' => TEST_USERNAME,
            'passwd' => TEST_PASSWORD
        )
    );
    $result = $this->testAction('/oauth/login', array(
        'data' => $data, 
        'method' => 'post'
    ));
    debug($result);
}

这将返回:

{"error":"invalid_client","error_description":"No client id supplied"}
4

1 回答 1

1

我能够弄清楚这一点。我只需要为 User 和 AccessToken 设置适当的装置。然后我必须确保这些是通过 $fixtures 导入我正在测试的控制器中的。

我的 AccessTokenFixture 示例:

<?php
App::uses('OAuthComponent', 'OAuth.Controller/Component'); 
/**
 * AccessTokenFixture
 *
 */
class AccessTokenFixture extends CakeTestFixture {

/**
 * Fields
 *
 * @var array
 */
    public $fields = array(
        'oauth_token' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 40, 'key' => 'primary', 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
        'client_id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
        'user_id' => array('type' => 'integer', 'null' => false, 'default' => null),
        'expires' => array('type' => 'integer', 'null' => false, 'default' => null),
        'scope' => array('type' => 'string', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
        'indexes' => array(
            'PRIMARY' => array('column' => 'oauth_token', 'unique' => 1)
        ),
        'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'MyISAM')
    );

/**
 * init method
 * @return void
 */
    public function init() {
        $this->records = array(
            array(
                'oauth_token' => OAuthComponent::hash('SAMPLE_ACCESS_TOKEN'),
                'client_id' => 'YOUR_CLIENT_ID',
                'user_id' => 1,
                'expires' => 1367263611232323,
                'scope' => ''
            ),
            array(
                'oauth_token' => OAuthComponent::hash('SAMPLE_ACCESS_TOKEN'),
                'client_id' => 'YOUR_CLIENT_ID',
                'user_id' => 2,
                'expires' => 13672640632323323,
                'scope' => ''
            )
        );
        parent::init();
    }

}
于 2013-06-10T02:49:17.553 回答