Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelJ2324 committed Jul 6, 2017
2 parents de5cb63 + d12bf27 commit 820f0f1
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/Auth/SugarOAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,69 @@ class SugarOAuthController extends AbstractOAuth2Controller
'platform' => 'api'
);

/**
* @inheritdoc
* Load Stored Token based on Credentials
*/
public function setCredentials(array $credentials)
{
parent::setCredentials($credentials);
if (!empty($this->credentials)){
$token = $this->getStoredToken($this->credentials);
if ($token !== NULL){
$token = json_decode($token,TRUE);
if (is_array($token)){
$this->setToken($token);
}
}
}
return $this;
}

/**
* @inheritdoc
*/
public function updateCredentials(array $credentials){
$current = array_replace($this->getCredentials(),$credentials);
return $this->setCredentials($current);
}

/**
* @inheritdoc
* @codeCoverageIgnore
*/
public function authenticate()
{
$return = parent::authenticate();
if ($return){
$this->storeToken($this->getCredentials(),$this->getToken());
}
return $return;
}

/**
* @inheritdoc
* @codeCoverageIgnore
*/
public function logout()
{
$return = parent::logout();
if ($return){
$this->removeStoredToken($this->getCredentials());
}
return $return;
}

/**
* @inheritdoc
* @codeCoverageIgnore
*/
public function refresh()
{
$return = parent::refresh();
if ($return){
$this->storeToken($this->getCredentials(),$this->getToken());
}
return $return;
}
}
39 changes: 39 additions & 0 deletions src/Storage/SugarStaticStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,43 @@
class SugarStaticStorage extends StaticStorage
{
protected $namespace = 'sugarapi';

protected function formatKey($key){
$return = '';
if (is_array($key)){
if (isset($key['server'])){
$return .= $key['server']."_";
}
if (isset($key['client_id'])){
$return .= $key['client_id']."_";
}
if (isset($key['platform'])){
$return .= $key['platform'];
}
}else {
$return = $key;
}
return $return;
}

public function store($key, $value)
{
$key = $this->formatKey($key);
if (is_array($value) || is_object($value)){
$value = json_encode($value);
}
return parent::store($key,$value);
}

public function remove($key)
{
$key = $this->formatKey($key);
return parent::remove($key);
}

public function get($key)
{
$key = $this->formatKey($key);
return parent::get($key);
}
}
33 changes: 33 additions & 0 deletions tests/Auth/SugarOAuthControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Sugarcrm\REST\Tests\Auth;

use Sugarcrm\REST\Auth\SugarOAuthController;
use Sugarcrm\REST\Storage\SugarStaticStorage;


/**
Expand Down Expand Up @@ -34,6 +35,38 @@ public function tearDown()
parent::tearDown();
}

/**
* @covers ::setCredentials
*/
public function testSetCredentials(){
$Auth = new SugarOAuthController();
$Storage = new SugarStaticStorage();
$Auth->setStorageController($Storage);
$this->assertEquals($Auth,$Auth->setCredentials(array(
'username' => 'admin',
'password' => '',
'client_id' => 'sugar',
'client_secret' => '',
'platform' => 'api'
)));
$this->assertEmpty($Auth->getToken());
$Storage->store($Auth->getCredentials(),array(
'access_token' => '1234',
'refresh_token' => '5678',
));
$this->assertEquals($Auth,$Auth->setCredentials(array(
'username' => 'admin',
'password' => '',
'client_id' => 'sugar',
'client_secret' => '',
'platform' => 'api'
)));
$this->assertEquals(array(
'access_token' => '1234',
'refresh_token' => '5678',
),$Auth->getToken());
}

/**
* @covers ::updateCredentials
*/
Expand Down
93 changes: 93 additions & 0 deletions tests/Storage/SugarStaticStorageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* ©[2017] SugarCRM Inc. Licensed by SugarCRM under the Apache 2.0 license.
*/

namespace Sugarcrm\REST\Tests\Storage;

use Sugarcrm\REST\Storage\SugarStaticStorage;


/**
* Class SugarStaticStorageTest
* @package Sugarcrm\REST\Tests\Storage
* @coversDefaultClass Sugarcrm\REST\Storage\SugarStaticStorage
* @group SugarStaticStorageTest
*/
class SugarStaticStorageTest extends \PHPUnit_Framework_TestCase
{

protected $token = array(
'access_token' => '1234'
);

public static function setUpBeforeClass()
{
//Add Setup for static properties here
}

public static function tearDownAfterClass()
{
//Add Tear Down for static properties here
}

public function setUp()
{
parent::setUp();
}

public function tearDown()
{
parent::tearDown();
}

/**
* @covers ::store
* @covers ::get
* @covers ::remove
* @covers ::formatKey
*
*/
public function testStorage(){
$Storage = new SugarStaticStorage();
$this->assertEquals(true,$Storage->store(array(
'server' => 'test.sugarondemand.com'
),$this->token));
$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_');
$this->assertEquals(NULL,$token);


$this->assertEquals(true,$Storage->store(array(
'client_id' => 'test',
'platform' => 'base'
),$this->token));
$token = $Storage->get('test_base');
$this->assertEquals(json_encode($this->token),$token);
$this->assertEquals(true,$Storage->store(array(
'server' => 'test.sugarondemand.com',
'platform' => 'base'
),$this->token));
$token = $Storage->get('test.sugarondemand.com_base');
$this->assertEquals(json_encode($this->token),$token);
$this->assertEquals(true,$Storage->store('foobar',$this->token));
$token = $Storage->get('foobar');
$this->assertEquals(json_encode($this->token),$token);

$this->assertEquals(true,$Storage->remove(array(
'server' => 'test.sugarondemand.com',
'platform' => 'base'
)));
$this->assertEquals(true,$Storage->remove('test_base'));
$token = $Storage->get(array(
'server' => 'test.sugarondemand.com',
'platform' => 'base'
));
$this->assertEquals(NULL,$token);
}

}

0 comments on commit 820f0f1

Please sign in to comment.