Skip to content

Commit

Permalink
Merge pull request #32 from MichaelJ2324/2.0
Browse files Browse the repository at this point in the history
Updated Unit Tests + Tweaked Sudo
  • Loading branch information
geraldclark committed Sep 26, 2017
2 parents 3fd4ed6 + 5790cdd commit 78594e0
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 18 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
language: php

matrix:
include:
- php: 5.3
dist: precise
php:
- '5.3'
- '5.4'
- '5.5'
- '5.6'
Expand Down
22 changes: 15 additions & 7 deletions src/Auth/SugarOAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@ public function refresh()

/**
* Refreshes the OAuth 2 Token
* @param $user string
* @return bool
* @throws InvalidToken
*/
public function sudo($user,$data = array())
public function sudo($user)
{
if (isset($this->token['access_token'])) {
$Endpoint = $this->getActionEndpoint(self::ACTION_SUGAR_SUDO);

if ($Endpoint !== null) {
$Endpoint = $this->configureSudoEndpoint($Endpoint, $user,$data);
$Endpoint = $this->configureSudoEndpoint($Endpoint, $user);
$response = $Endpoint->execute()->getResponse();
if ($response->getStatus() == '200') {
//@codeCoverageIgnoreStart
Expand All @@ -143,13 +143,21 @@ public function sudo($user,$data = array())
return FALSE;
}

protected function configureSudoEndpoint(EndpointInterface $Endpoint,$user,$data = array())
/**
* Configure the Sudo Endpoint
* @param EndpointInterface $Endpoint
* @param $user
* @return EndpointInterface
*/
protected function configureSudoEndpoint(EndpointInterface $Endpoint,$user)
{
$Endpoint->setAuth($this);
$Endpoint->setOptions(array($user));
if (!empty($data)){
$Endpoint->setData($data);
}
$data = array();
$creds = $this->getCredentials();
$data['platform'] = $creds['platform'];
$data['client_id'] = $creds['client_id'];
$Endpoint->setData($data);
return $Endpoint;
}
}
11 changes: 4 additions & 7 deletions src/Client/Sugar7API.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,12 @@ public function logout(){

/**
* Helper method to Sudo to new user
* @param $user string
* @return bool
*/
public function sudo($user,$data=array())
public function sudo($user)
{
if (empty($data)){
$creds = $this->getAuth()->getCredentials();
$data['platform'] = $creds['platform'];
$data['client_id'] = $creds['client_id'];
}
return $this->getAuth()->sudo($user,$data);
return $this->getAuth()->sudo($user);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Storage/SugarStaticStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected function formatKey($key){
if (isset($key['sudo'])){
$return .= "sudo".$key['sudo'];
}
$return = rtrim($return,"_");
}else {
$return = $key;
}
Expand Down
37 changes: 37 additions & 0 deletions tests/Auth/SugarOAuthControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use MRussell\Http\Request\JSON;
use Sugarcrm\REST\Auth\SugarOAuthController;
use Sugarcrm\REST\Endpoint\OAuth2Sudo;
use Sugarcrm\REST\Storage\SugarStaticStorage;
use Sugarcrm\REST\Tests\Stubs\Auth\SugarOAuthStub;

Expand Down Expand Up @@ -37,6 +38,15 @@ public function tearDown()
parent::tearDown();
}

/**
* @covers ::__construct
*/
public function testConstructor()
{
$Auth = new SugarOAuthStub();
$this->assertEquals(true,in_array('sudo',$Auth->getActions()));
}

/**
* @covers ::setCredentials
*/
Expand Down Expand Up @@ -125,4 +135,31 @@ public function testAuthHeader()
$headers = $Request->getHeaders();
$this->assertEquals('bar',$headers['OAuth-Token']);
}

/**
* @covers ::sudo
* @covers ::configureSudoEndpoint
* @covers Sugarcrm\REST\Client\Sugar7API::sudo
*/
public function testSudo()
{
$Auth = new SugarOAuthStub();
$Auth->setCredentials(array(
'username' => 'system',
'password' => 'asdf',
'client_id' => 'sugar',
'client_secret' => '',
'platform' => 'api'
));
$EP = new OAuth2Sudo();
$EP->setBaseUrl('http://localhost/rest/v10');
$Auth->setActionEndpoint($Auth::ACTION_SUGAR_SUDO,$EP);
$Auth->sudo('max');
$request = $EP->getRequest();
$this->assertEquals('http://localhost/rest/v10/oauth2/sudo/max',$request->getURL());
$this->assertEquals(array(
'client_id' => 'sugar',
'platform' => 'api'
),$request->getBody());
}
}
11 changes: 11 additions & 0 deletions tests/Client/Sugar7APITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,15 @@ public function testLogout(){
$Client->setAuth($Auth);
$this->assertEquals(true,$Client->logout());
}

/**
* @covers ::sudo
*/
public function testSudo()
{
$Client = new Sugar7API('localhost');
$Auth = new SugarOAuthStub();
$Client->setAuth($Auth);
$this->assertEquals(false,$Client->sudo('max'));
}
}
9 changes: 9 additions & 0 deletions tests/Endpoint/AbstractSugarBeanEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,15 @@ public function testUpdateModel(){
'baz' => 'bar',
'favorite' => 0
),$Bean->asArray());

$Bean->reset();
$body->setValue($Bean->getResponse(),json_encode(array('record' => array('id' => '12345'),'filename' => array('guid' => 'test.txt'))));
$Bean->setCurrentAction(Module::BEAN_ACTION_TEMP_FILE_UPLOAD);
$updateModel->invoke($Bean);
$this->assertEquals(array(
'filename_guid' => '12345',
'filename' => 'test.txt'
),$Bean->asArray());
}

/**
Expand Down
13 changes: 11 additions & 2 deletions tests/Storage/SugarStaticStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public function testStorage(){
$this->assertEquals(true,$Storage->store(array(
'server' => 'test.sugarondemand.com'
),$this->token));
$token = $Storage->get('test.sugarondemand.com_');
$token = $Storage->get('test.sugarondemand.com');
$this->assertEquals($this->token,json_decode($token,TRUE));
$this->assertEquals(true,$Storage->remove(array(
'server' => 'test.sugarondemand.com'
)));
$token = $Storage->get('test.sugarondemand.com_');
$token = $Storage->get('test.sugarondemand.com');
$this->assertEquals(NULL,$token);


Expand Down Expand Up @@ -88,6 +88,15 @@ public function testStorage(){
'platform' => 'base'
));
$this->assertEquals(NULL,$token);

$this->assertEquals(true,$Storage->store(array(
'server' => 'test.sugarondemand.com',
'platform' => 'base',
'client_id' => 'foo',
'sudo' => 'max'
),$this->token));
$token = $Storage->get('test.sugarondemand.com_foo_base_sudomax');
$this->assertEquals(json_encode($this->token),$token);
}

}

0 comments on commit 78594e0

Please sign in to comment.