我正在测试我的 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);