2013-07-28 76 views
0

我正在测试我的API,并且有一个OAuth登录方法,我无法正确测试,因为它在POSTing时不会读取变量的$ _GET值。是否可以模拟CakePHP测试(PHP单元)的方法的变量值?

登录方法。基本上$ OAuthParams在使用POST时不会被设置,因为getAuthorizeParams()使用$ _GET。 所以我想填充/模拟$ _GET或$ OAuthParams。

public function login() { 
    $OAuthParams = $this->OAuth->getAuthorizeParams(); 
    if ($this->request->is('post')) { 
     $this->validateRequest(); 
     $loginData = array('User' => array(
       'username' => $this->request->data['User']['username'], 
       'passwd' => $this->request->data['User']['passwd'] 
      )); 
     //Attempted login 
     if ($this->Auth->login($loginData['User'])) { 
      unset($loginData); 
      $userData = $this->User->find('first',array(
       'conditions' => array(
        'User.username' => $this->request->data['User']['username'] 
       ), 
       'fields' => array('username','name','id','banned','active','role','private'), 
       'recursive' => -1 
      )); 

      $this->Session->write('Auth.User',$userData['User']); //Update the session 

      //Write this to session so we can log them out after authenticating 
      $this->Session->write('OAuth.logout', true); 

      //Write the auth params to the session for later 
      $this->Session->write('OAuth.params', $OAuthParams); 

      //Off we go 
      return $this->redirect(array('action' => 'authorize')); 
     } else { 
      $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth'); 
     } 
    } 
    $appName = $this->applicationDetails['name']; 
    $this->set(compact('OAuthParams','appName')); 
} 

目前的测试方法。

/** 
* testAccessToken method 
* 1. Get the authorization code (/oauth/authorize?response_type=code&client_id=CLIENT_ID&client_secret=CLIENT_SECRET) 
* 2. Retrieve the token (/oauth/token?grant_type=authorization_code&code=CODE_RETURNED&client_id=CLIENT_ID&client_secret=CLIENT_SECRET) 
* 3. Parse the token from the returned data 
* @return void 
*/ 
public function testAccessToken(){ 
    //@link http://stackoverflow.com/questions/8183396/dealing-with-security-component-in-a-cakephp-2-test-case 
    $this->OAuth = $this->generate('OAuth', array(
     'components' => array(
      'Security' => array('_validatePost'), 
     ) 
    )); 
    $this->OAuth->Security->expects($this->any()) 
     ->method('_validatePost') 
     ->will($this->returnValue(true)); 

    $get_data = array(
     'response_type' => 'code', 
     'client_id' => getenv('THREE_SCALE_APP_ID'), 
     'client_secret' => getenv('THREE_SCALE_APP_KEY'), 
    ); 
    $post_data = array(
     'User' => array(
      'username' => 'test_user_1', 
      'passwd' => 'tester' 
     ) 
    ); 

    $resultPost = $this->testAction(
     '/oauth/login', 
     array(
      'method' => 'post', 
      'data' => $post_data 
     ) 
    ); 
    debug($resultPost); 

    $data_for_code = array(
     'accept' => 'Yep', 
     'Authorize' => array(
      'client_id' => getenv('THREE_SCALE_APP_ID'), 
      'client_secret' => getenv('THREE_SCALE_APP_KEY'), 
      'response_type' => 'code', 
      'state' => '', 
      'scope' => '' 
     ) 
    ); 
    $code = $this->testAction(
     '/oauth/authorize', 
     array(
      'data' => $data_for_code, 
      'method' => 'post' 
     ) 
    ); 
    debug($code); 
    /*$data_for_token = array(
     'grant_type' => 'authorization_code', 
     'code' => $code, 
     'client_id' => getenv('THREE_SCALE_APP_ID'), 
     'client_secret' => getenv('THREE_SCALE_APP_KEY') 
    ); 
    $token = $this->testAction(
     '/oauth/token', 
     array(
      'data' => $data_for_token, 
      'method' => 'post' 
     ) 
    );*/ 
    //debug($token); 
} 

试过以下没有运气。

$this->OAuth = $this->generate(
    'OAuth', 
    array(
     'components' => array(
      'Security' => array('_validatePost'), 
      'Auth' => array('loggedIn','user') 
     ), 
     'methods' => array(
      'getAuthorizeParams' 
     ) 
    ) 
); 
$data = array(
    'response_type' => 'code', 
    'client_id' => getenv('THREE_SCALE_APP_ID'), 
    'client_secret' => getenv('THREE_SCALE_APP_KEY') 
); 
$this->OAuth->expects($this->any()) 
    ->method('getAuthorizeParams') 
    ->will($this->returnValue($data)); 

$loginData = array(
    'User' => array(
     'client_id' => getenv('THREE_SCALE_APP_ID'), 
     'client_secret' => getenv('THREE_SCALE_APP_KEY'), 
     'response_type' => 'code', 
     'state' => '', 
     'scope' => '', 
     'redirect_uri' => 'https://myurl.com/api/myCallbackMethod', 
     'username' => 'test_user_1', 
     'passwd' => 'tester' 
    ) 
); 

//test login action 
$resultPost = $this->testAction(
    '/oauth/login?response_type=code&client_id=' . getenv('THREE_SCALE_APP_ID') . '&client_secret=' . getenv('THREE_SCALE_APP_KEY'), 
    array(
     'method' => 'post', 
     'data' => $loginData 
    ) 
); 
debug($resultPost); 

$data = array(
    'accept' => 'Yep', 
    'Authorize' => array(
     'client_id' => getenv('THREE_SCALE_APP_ID'), 
     'client_secret' => getenv('THREE_SCALE_APP_KEY'), 
     'response_type' => 'code', 
     'state' => '', 
     'scope' => '' 
    ) 
); 

$result = $this->testAction(
     '/oauth/authorize', 
     array(
      'data' => $data, 
      'method' => 'post' 
     ) 
    ); 
debug($result); 
+0

嘲笑'getAuthorizeParams()'函数返回你想要的值。 – Derek

+0

在CakePHP 2中还有一个错误,它在单元测试中没有正确使用安全组件。确保你使用的是2.3.6或更高版本。 – Derek

+0

是的,我在想同样的,但我试图嘲笑getAuthorizeParams(),但我仍然得到{“错误”:“invalid_client”,“error_description”:“没有客户端ID提供”}。感谢您的安全提示,我已升级到版本2.3.8。 –

回答

0

模拟getAuthorizeParams()函数返回所需的值。

要嘲笑里面的OAuth功能是一个字符串数组与“方法”键,即:

$this->generate(
    'OAuth', 
    array(
     'methods' => array(
      'getAuthorizeParams' 
     ), 
     'components' => array(
      ... 
     ) 
    ) 
); 

然后你打电话给你testAction()方法之前:

$this->OAuth->expects($this->once()) 
    ->method('getAuthorizeParams') 
    ->will($this->returnValue($YOUR_EXPECTED_VALUE)); 
+0

仍然没有运气与此。:( –

+0

究竟是什么做错了/什么是意想不到的行为?任何错误消息?你确定该函数将被调用的数据你正在传递? – Derek

+0

错误是“{”error“:”invalid_client“,”error_description“:”没有提供客户端ID“}。代码以供参考,我不得不自定义它以适应Threescale。基本上Threescale正在处理所有的密钥,并且我只是在它们被验证时将它们放在本地数据库中。可能比你需要的多一点知道,但owell。再次感谢您的帮助。https://github.com/robksawyer/ cakephp的-OAuth的服务器/斑点/主/控制器/ OAuthController.php –