我正在测试我的API,并且有一个OAuth的登录方法,我似乎无法正确测试,因为它在POST时不会读取变量的$ _GET值。
登录方法。基本上$ 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 :(得分:0)
模拟getAuthorizeParams()函数以返回所需的值。
要模拟的OAuth内部的函数是带有'methods'键的字符串数组,即:
$this->generate(
'OAuth',
array(
'methods' => array(
'getAuthorizeParams'
),
'components' => array(
...
)
)
);
然后在调用testAction()
方法之前:
$this->OAuth->expects($this->once())
->method('getAuthorizeParams')
->will($this->returnValue($YOUR_EXPECTED_VALUE));