This repository has been archived by the owner on Sep 10, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: refs #951. Add authorize endpoint testing for oauth module
Includes some style and documentation fixes
- Loading branch information
1 parent
de73e66
commit 6b84fc5
Showing
12 changed files
with
247 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
add_subdirectory( controllers ) | ||
|
||
add_midas_style_test( StyleOauthControllers ${CMAKE_SOURCE_DIR}/modules/oauth/controllers ) | ||
add_midas_style_test( StyleOauthModels ${CMAKE_SOURCE_DIR}/modules/oauth/models ) | ||
add_midas_style_test( StyleOauthNotification ${CMAKE_SOURCE_DIR}/modules/oauth/Notification.php ) |
130 changes: 130 additions & 0 deletions
130
modules/oauth/tests/controllers/AuthorizeControllerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
/*========================================================================= | ||
MIDAS Server | ||
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved. | ||
69328 Lyon, FRANCE. | ||
See Copyright.txt for details. | ||
This software is distributed WITHOUT ANY WARRANTY; without even | ||
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | ||
PURPOSE. See the above copyright notices for more information. | ||
=========================================================================*/ | ||
|
||
/** test oauth authorize controller */ | ||
class OauthAuthorizeControllerTest extends ControllerTestCase | ||
{ | ||
/** set up tests*/ | ||
public function setUp() | ||
{ | ||
$this->setupDatabase(array('default')); //core dataset | ||
$this->setupDatabase(array('default'), 'oauth'); | ||
$this->enabledModules = array('api', 'oauth'); | ||
$this->_models = array('User'); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
/** | ||
* Helper function to get test that each paramter in the array is required | ||
*/ | ||
private function _testParamsRequired($uri, $params, $userDao = null) | ||
{ | ||
foreach($params as $key => $value) | ||
{ | ||
$localParams = $params; //copy array | ||
unset($localParams[$key]); | ||
$this->resetAll(); | ||
$this->params = $localParams; | ||
$this->getRequest()->setMethod('GET'); | ||
$this->dispatchUri($uri, $userDao, true); | ||
$this->assertEquals($this->getResponse()->getHttpResponseCode(), 400); //IETF spec dictates we must send BAD_REQUEST response | ||
} | ||
} | ||
|
||
/** | ||
* Tests the login screen used by the user to authorize the client | ||
*/ | ||
public function testLoginScreen() | ||
{ | ||
$_SERVER['HTTPS'] = true; //must set this to trick the action into thinking we're using SSL | ||
$params = array( | ||
'client_id' => '1000', | ||
'response_type' => 'code', | ||
'redirect_uri' => 'http://google.com'); | ||
$this->_testParamsRequired('/oauth/authorize', $params); | ||
|
||
$scopes = array(MIDAS_API_PERMISSION_SCOPE_READ_USER_INFO, | ||
MIDAS_API_PERMISSION_SCOPE_WRITE_USER_INFO, | ||
MIDAS_API_PERMISSION_SCOPE_READ_DATA); | ||
$this->resetAll(); | ||
$this->params = $params; | ||
$this->params['state'] = 'my_state_value'; | ||
$this->params['scope'] = JsonComponent::encode($scopes); | ||
$this->dispatchUrI('/oauth/authorize', null); | ||
$this->assertQueryCount('ul.scopeList li', count($scopes)); | ||
$scopeMap = Zend_Registry::get('permissionScopeMap'); | ||
|
||
foreach($scopes as $scope) | ||
{ | ||
$this->assertQueryContentContains('ul.scopeList li', $scopeMap[$scope]); | ||
} | ||
} | ||
|
||
/** | ||
* Test the submission of the login form, authorizing the client | ||
*/ | ||
public function testSubmitAction() | ||
{ | ||
$user = $this->User->load(1); | ||
$this->User->changePassword($user, 'myPassword'); //easiest way to set the password | ||
$params = array( | ||
'client_id' => '1000', | ||
'login' => $user->getEmail(), | ||
'password' => 'wrongPass', | ||
'redirect_uri' => 'http://google.com'); | ||
$this->_testParamsRequired('/oauth/authorize/submit', $params); | ||
|
||
$scopes = array(MIDAS_API_PERMISSION_SCOPE_READ_USER_INFO, | ||
MIDAS_API_PERMISSION_SCOPE_WRITE_USER_INFO, | ||
MIDAS_API_PERMISSION_SCOPE_READ_DATA); | ||
|
||
// Test with incorrect password | ||
$this->resetAll(); | ||
$this->params = $params; | ||
$this->params['state'] = 'my_state_value'; | ||
$this->params['scope'] = JsonComponent::encode($scopes); | ||
$this->params['allowOrDeny'] = 'Allow'; | ||
$this->dispatchUrI('/oauth/authorize/submit', null); | ||
$json = JsonComponent::decode($this->getBody()); | ||
$this->assertEquals($json['status'], 'error'); | ||
$this->assertEquals($json['message'], 'Invalid username or password'); | ||
|
||
// Test user denying the request | ||
$this->resetAll(); | ||
$this->params = $params; | ||
$this->params['state'] = 'my_state_value'; | ||
$this->params['scope'] = JsonComponent::encode($scopes); | ||
$this->params['allowOrDeny'] = 'Deny'; | ||
$this->dispatchUrI('/oauth/authorize/submit', null); | ||
$json = JsonComponent::decode($this->getBody()); | ||
$this->assertEquals($json['status'], 'ok'); | ||
$this->assertEquals($json['redirect'], $params['redirect_uri'].'?error=access_denied&state='.$this->params['state']); | ||
|
||
// Test user allowing the request | ||
$this->resetAll(); | ||
$this->params = $params; | ||
$this->params['state'] = 'my_state_value'; | ||
$this->params['scope'] = JsonComponent::encode($scopes); | ||
$this->params['allowOrDeny'] = 'Allow'; | ||
$this->params['password'] = 'myPassword'; | ||
$this->dispatchUrI('/oauth/authorize/submit', null); | ||
|
||
$codeModel = MidasLoader::loadModel('Code', 'oauth'); | ||
$codeDaos = $codeModel->getByUser($user); | ||
$codeDao = end($codeDaos); | ||
|
||
$json = JsonComponent::decode($this->getBody()); | ||
$this->assertEquals($json['status'], 'ok'); | ||
$this->assertEquals($json['redirect'], $params['redirect_uri'].'?code='.$codeDao->getCode().'&state='.$this->params['state']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
add_midas_test( OauthAuthorizeController AuthorizeControllerTest.php ) | ||
add_midas_test( OauthClientController ClientControllerTest.php ) | ||
add_midas_test( OauthTokenController TokenControllerTest.php ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/*========================================================================= | ||
MIDAS Server | ||
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved. | ||
69328 Lyon, FRANCE. | ||
See Copyright.txt for details. | ||
This software is distributed WITHOUT ANY WARRANTY; without even | ||
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | ||
PURPOSE. See the above copyright notices for more information. | ||
=========================================================================*/ | ||
|
||
/** test oauth client controller */ | ||
class OauthClientControllerTest extends ControllerTestCase | ||
{ | ||
/** set up tests*/ | ||
public function setUp() | ||
{ | ||
$this->setupDatabase(array('default')); //core dataset | ||
$this->setupDatabase(array('default'), 'oauth'); | ||
$this->enabledModules = array('api', 'oauth'); | ||
$this->_models = array('User'); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
/** | ||
* TODO stub | ||
*/ | ||
public function testStub() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/*========================================================================= | ||
MIDAS Server | ||
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved. | ||
69328 Lyon, FRANCE. | ||
See Copyright.txt for details. | ||
This software is distributed WITHOUT ANY WARRANTY; without even | ||
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | ||
PURPOSE. See the above copyright notices for more information. | ||
=========================================================================*/ | ||
|
||
/** test oauth token controller */ | ||
class OauthTokenControllerTest extends ControllerTestCase | ||
{ | ||
/** set up tests*/ | ||
public function setUp() | ||
{ | ||
$this->setupDatabase(array('default')); //core dataset | ||
$this->setupDatabase(array('default'), 'oauth'); | ||
$this->enabledModules = array('api', 'oauth'); | ||
$this->_models = array('User'); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
/** | ||
* TODO stub | ||
*/ | ||
public function testStub() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<dataset> | ||
|
||
<oauth_client | ||
client_id="1000" | ||
name="dummy" | ||
secret="12345" | ||
owner_id="1" | ||
creation_date="2011-11-14 11:11:11" | ||
/> | ||
<oauth_code | ||
code_id="1000" | ||
client_id="1000" | ||
user_id="1" | ||
code="xyz" | ||
scopes="[0]" | ||
creation_date="2011-12-14 11:11:11" | ||
expiration_date="2011-12-14 11:11:11" | ||
/> | ||
<oauth_token | ||
token_id="1000" | ||
client_id="1000" | ||
user_id="1" | ||
token="abcd" | ||
scopes="[0]" | ||
type="0" | ||
creation_date="2011-12-14 11:11:11" | ||
expiration_date="2011-12-14 11:11:11" | ||
/> | ||
|
||
</dataset> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo;foo |