Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelJ2324 committed Aug 15, 2017
2 parents a3242ab + 789e48f commit a1e75a2
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 18 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
],
"require": {
"php": ">=5.3.0",
"michaelj2324/php-rest-client": "~1.0"
"michaelj2324/php-rest-client": ">=1.3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0|~5.0"
Expand Down
12 changes: 9 additions & 3 deletions src/Auth/SugarOAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

class SugarOAuthController extends AbstractOAuth2Controller
{
protected static $_OAUTH_HEADER = 'OAuth-Token';

protected static $_TOKEN_VALUE = '%s';
protected static $_AUTH_HEADER = 'OAuth-Token';

protected static $_DEFAULT_GRANT_TYPE = self::OAUTH_RESOURCE_OWNER_GRANT;

Expand All @@ -26,6 +24,14 @@ class SugarOAuthController extends AbstractOAuth2Controller
'platform' => 'api'
);

/**
* @inheritdoc
*/
protected function getAuthHeaderValue()
{
return $this->token['access_token'];
}

/**
* @inheritdoc
* Load Stored Token based on Credentials
Expand Down
19 changes: 19 additions & 0 deletions src/Client/Sugar7API.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,24 @@ public function logout(){
return $this->getAuth()->logout();
}

/**
* Check if authenticated, and attempt Refresh/Login if not
* @return bool
* @codeCoverageIgnore
*/
public function isAuthenticated()
{
$Auth = $this->getAuth();
if ($Auth){
if (!$Auth->isAuthenticated()){
if (!$this->refreshToken()){
return $this->login();
}
}
} else {
return FALSE;
}
return TRUE;
}

}
64 changes: 50 additions & 14 deletions src/Endpoint/Abstracts/AbstractSugarBeanEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

namespace Sugarcrm\REST\Endpoint\Abstracts;

use MRussell\Http\Request\AbstractRequest;
use MRussell\Http\Request\JSON;
use MRussell\Http\Request\RequestInterface;
use MRussell\Http\Response\ResponseInterface;
use MRussell\REST\Endpoint\JSON\ModelEndpoint;
use Sugarcrm\REST\Endpoint\Data\FilterData;
Expand Down Expand Up @@ -160,31 +160,52 @@ public function getModule(){
}

/**
* Auto configure uploads
* @param AbstractRequest $Request
* @return AbstractRequest
* Configure Uploads on Request
* @inheritdoc
* @codeCoverageIgnore
*/
protected function configureRequest(AbstractRequest $Request)
protected function configureRequest(RequestInterface $Request)
{
$Request = parent::configureRequest($Request);
return $this->configureUploads($Request);
}

/**
* Configure the Uploads Data on the Request Object
* @param RequestInterface $Request
* @return RequestInterface
*/
protected function configureUploads(RequestInterface $Request)
{
$Request->setUpload($this->upload);
if ($Request->getUpload()){
if (!empty($this->_files)){
foreach($this->_files as $key => $properties){
$Request->addFile($key,$properties['path'],$properties['mimeType'],$properties['filename']);
}
}
return $Request;
}

/**
* Add a reset for Upload Settings
* @inheritdoc
* @codeCoverageIgnore
*/
protected function configureResponse(ResponseInterface $Response)
{
$this->resetUploads();
return parent::configureResponse($Response);
}

/**
* Reset the Upload Settings back to defaults
*/
protected function resetUploads(){
if ($this->upload){
//Reset file uploads after uploading
$this->upload = FALSE;
$this->_files = array();
$this->getData()->reset();
}
return parent::configureResponse($Response);
$this->upload = FALSE;
$this->_files = array();
}

/**
Expand Down Expand Up @@ -368,11 +389,11 @@ public function attachFile($fileField,$filePath,$deleteOnFail = false,$mimeType
{
$this->setCurrentAction(self::BEAN_ACTION_ATTACH_FILE,array($fileField));
$this->configureFileUploadData($deleteOnFail);
$this->_files[$fileField] = array(
$this->addFile($fileField,array(
'path' => $filePath,
'mimeType' => $mimeType,
'filename' => $uploadName
);
));
return $this->execute();
}

Expand All @@ -395,11 +416,11 @@ public function tempFile($fileField,$filePath,$deleteOnFail = false,$mimeType =
$this->set($idKey,'temp');
$this->setCurrentAction(self::BEAN_ACTION_TEMP_FILE_UPLOAD,array($fileField));
$this->configureFileUploadData($deleteOnFail);
$this->_files[$fileField] = array(
$this->addFile($fileField,array(
'path' => $filePath,
'mimeType' => $mimeType,
'filename' => $uploadName
);
));
return $this->execute();
}

Expand All @@ -419,4 +440,19 @@ protected function configureFileUploadData($deleteOnFail = FALSE){
$this->setData($data);
}

/**
* Add a file to the internal Files array to be added to the Request
* @param $name
* @param array $properties
* @return $this
*/
protected function addFile($name,array $properties)
{
if (isset($properties['path'])){
$this->upload = TRUE;
$this->_files[$name] = $properties;
}
return $this;
}

}
14 changes: 14 additions & 0 deletions tests/Auth/SugarOAuthControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Sugarcrm\REST\Tests\Auth;

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


/**
Expand Down Expand Up @@ -111,4 +113,16 @@ public function testUpdateCredentials(){
'platform' => array()
),$Auth->getCredentials());
}

/**
* @covers ::getAuthHeaderValue
*/
public function testAuthHeader()
{
$Auth = new SugarOAuthStub();
$Request = new JSON();
$this->assertEquals($Auth,$Auth->configureRequest($Request));
$headers = $Request->getHeaders();
$this->assertEquals('bar',$headers['OAuth-Token']);
}
}
62 changes: 62 additions & 0 deletions tests/Endpoint/AbstractSugarBeanEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use MRussell\Http\Request\JSON;
use Sugarcrm\REST\Endpoint\Module;
use Sugarcrm\REST\Tests\Stubs\Auth\SugarOAuthStub;


/**
Expand Down Expand Up @@ -354,5 +355,66 @@ public function testUpdateModel(){
'favorite' => 0
),$Bean->asArray());
}

/**
* @covers ::configureFileUploadData
*/
public function testConfigureFileUploadData(){
$Bean = new Module();
$Bean->setBaseUrl('http://localhost/rest/v10/');
$Auth = new SugarOAuthStub();
$Bean->setAuth($Auth);
$ReflectedEndpoint = new \ReflectionClass(get_class($Bean));
$configureFileUploadData = $ReflectedEndpoint->getMethod('configureFileUploadData');
$configureFileUploadData->setAccessible(true);
$configureFileUploadData->invoke($Bean,FALSE);
$this->assertEquals(array(
'format' => 'sugar-html-json',
'delete_if_fails' => FALSE,
),$Bean->getData()->asArray());
$configureFileUploadData->invoke($Bean,TRUE);
$this->assertEquals(array(
'format' => 'sugar-html-json',
'delete_if_fails' => TRUE,
'oauth_token' => 'bar'
),$Bean->getData()->asArray());
}

/**
* @covers ::attachFile
* @covers ::tempFile
* @covers ::addFile
* @covers ::resetUploads
* @covers ::configureUploads
*/
public function testFileAttachments()
{
$Bean = new Module();
$Bean->setBaseUrl('http://localhost/rest/v10/');
$Bean->set('id','12345a');
$Bean->setModule('Accounts');
$Reflection = new \ReflectionClass('Sugarcrm\REST\Endpoint\Abstracts\AbstractSugarBeanEndpoint');
$upload = $Reflection->getProperty('upload');
$upload->setAccessible(TRUE);
$_files = $Reflection->getProperty('_files');
$_files->setAccessible(TRUE);
$Auth = new SugarOAuthStub();
$Bean->setAuth($Auth);
$this->assertEquals($Bean,$Bean->attachFile('uploadfile',__FILE__));
$this->assertEquals(Module::BEAN_ACTION_ATTACH_FILE,$Bean->getCurrentAction());
$this->assertEquals(array(),$_files->getValue($Bean));
$this->assertEquals(FALSE,$upload->getValue($Bean));
$this->assertEmpty($Bean->getData()->asArray());
$rBody = $Bean->getRequest()->getBody();
$this->assertNotEmpty($rBody['uploadfile']);
$this->assertEquals($Bean,$Bean->tempFile('uploadfile',__FILE__));
$this->assertEquals(Module::BEAN_ACTION_TEMP_FILE_UPLOAD,$Bean->getCurrentAction());
$this->assertEquals(array(),$_files->getValue($Bean));
$this->assertEquals(FALSE,$upload->getValue($Bean));
$this->assertEmpty($Bean->getData()->asArray());
$this->assertEquals('temp',$Bean['id']);
$rBody = $Bean->getRequest()->getBody();
$this->assertNotEmpty($rBody['uploadfile']);
}
}

5 changes: 5 additions & 0 deletions tests/Stubs/Auth/SugarOAuthStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

class SugarOAuthStub extends SugarOAuthController
{
protected $token = array(
'access_token' => 'bar',
'refresh_token' => 'foo',
'expires_in' => '3600'
);

public function authenticate()
{
Expand Down

0 comments on commit a1e75a2

Please sign in to comment.