Skip to content

Commit

Permalink
WAC-1030: Initial commit to update dandb to support auto login
Browse files Browse the repository at this point in the history
WAC-1030 fix unit tests for new auth code endpoints

WAC-1030: Updating OWL OAuth2 calls to support JSON Request Format

* WAC-1030: Updating OWL OAuth2 calls to support JSON Request Format + user-token in header

* WAC-1030: Removing print_r

* WAC-1030: Making userToken as default param for OWL Request

* WAC=1030: making usertoken as optional in formatJsonRequest

* WAC-1030: Mocking Owl user token
  • Loading branch information
parakhkunal authored and Brian Anderson committed Jan 10, 2017
1 parent 5515ec9 commit 9ca1519
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 8 deletions.
31 changes: 31 additions & 0 deletions src/Credibility/DandB/DandB.php
Expand Up @@ -445,4 +445,35 @@ public function userTokenStatus($userToken)
));
}

/**
* Return the user token from OAuth2 auth code
*
* @see DandB::authCode
* @param $authCode
* @return mixed json
*/
public function getUserTokenFromAuthCode($authCode)
{
return $this->requester->runJsonPost('/v1/oauth2/token/authorization_code', array(
'code' => $authCode
));
}

/**
* Return the auth code appended in the redirect url by providing the user token
* @param $clientId
* @param $redirectUrl
* @param $state
* @param $userToken
* @return mixed json
*/
public function getAuthCodeFromUserToken($clientId, $redirectUrl, $state, $userToken)
{
return $this->requester->runJsonPost('/v1/oauth2/authorize/code', array(
'client_id' => $clientId,
"redirect_uri" => $redirectUrl,
"state" => $state
), $userToken);
}

}
46 changes: 45 additions & 1 deletion src/Credibility/DandB/Requester.php
Expand Up @@ -56,12 +56,30 @@ public function runPost($uri, $data = array())
return $this->formatRequest('POST', $uri, $requestData);
}

public function runJsonPost($uri, $data = array(), $userToken = null)
{
$requestData = array(
'body' => json_encode($data)
);
return $this->formatJsonRequest('POST', $uri, $requestData, $userToken);
}

public function formatJsonRequest($method, $uri, $data = array(), $userToken = null)
{
if(is_null($this->accessToken)) {
$this->accessToken = $this->getAccessToken();
}
$requestParams = $this->createJsonRequestParams($data, $userToken);

return $this->executeJson($method, $uri, $requestParams);
}

public function formatRequest($method, $uri, $data = array())
{
if(is_null($this->accessToken)) {
$this->accessToken = $this->getAccessToken();
}
$requestParams = $this->createRequestParams($data, $this->accessToken);
$requestParams = $this->createRequestParams($data);

return $this->execute($method, $uri, $requestParams);
}
Expand All @@ -75,6 +93,15 @@ public function execute($method, $uri, $data)
return new Response($response);
}

public function executeJson($method, $uri, $data)
{
$request = $this->guzzleClient->createRequest($method, $uri, $data);
/** @var ResponseInterface $response */
/** @noinspection PhpVoidFunctionResultUsedInspection */
$response = $this->guzzleClient->send($request);
return $response->json();
}

public function createRequestParams($data)
{
$header = array('headers' => array());
Expand All @@ -90,6 +117,23 @@ public function createRequestParams($data)
}
}

public function createJsonRequestParams($data, $userToken)
{
$header = array('headers' => array());
$header['headers']['x-access-token'] = $this->accessToken;
$header['headers']['Content-Type'] = 'application/json';
if($userToken !== null)
$header['headers']['user-token'] = $userToken;

$requestParams = array_merge(
$header,
$data
);

return $requestParams;

}

public function getAccessToken()
{
if($this->accessToken) {
Expand Down
45 changes: 43 additions & 2 deletions tests/DandBTest.php
Expand Up @@ -14,8 +14,6 @@ class DandBTest extends PHPUnit_Framework_TestCase
/** @var MockInterface */
protected $mockRequester;

private $testAccessToken;

public function setUp()
{
$this->mockRequester = m::mock('Credibility\DandB\Requester');
Expand Down Expand Up @@ -409,6 +407,40 @@ public function testBusinessSearchByNameAddressWithZip()

$this->dandb->businessSearchByNameAddress($name, $state, null, $city);
}

public function testGetUserTokenFromAuthCode()
{
$authCode = '61b7ae10ff4a22d82ef6b45dedc4182aebcfee28';

$this->setMockRequesterExpectations('runJsonPost',
'/v1/oauth2/token/authorization_code', array(
'code' => $authCode
)
);

$this->dandb->getUserTokenFromAuthCode($authCode);
}


public function testGetAuthCodeFromUserToken()
{
$clientId = 'credrev';
$redirectUrl = 'https://dashboard-qa.malibucoding.com/services/v1/from-external';
$state = 'test';
$userToken = 'test-token';

$this->setJsonMockRequesterExpectations('runJsonPost',
'/v1/oauth2/authorize/code', array(
'client_id' => $clientId,
'redirect_uri' => $redirectUrl,
'state' => $state
),
$userToken
);

$this->dandb->getAuthCodeFromUserToken($clientId, $redirectUrl, $state, $userToken);
}

private function setMockRequesterExpectations(
$mockRequesterRequest,
$owlEndpoint,
Expand All @@ -418,4 +450,13 @@ private function setMockRequesterExpectations(
->once()->withArgs([$owlEndpoint, $args]);
}

private function setJsonMockRequesterExpectations(
$mockRequesterRequest,
$owlEndpoint,
array $args,
$userToken
) {
$this->mockRequester->shouldReceive($mockRequesterRequest)
->once()->withArgs([$owlEndpoint, $args, $userToken]);
}
}
21 changes: 16 additions & 5 deletions tests/RequesterTest.php
Expand Up @@ -210,15 +210,21 @@ public function testRunGet()
public function testRunPost()
{
$testArray = $this->setHttpMethodExpectations();
$mockAccessToken = 'test-token';

$response = $this->requester->runPost('test-uri', $testArray, $mockAccessToken);
$response = $this->requester->runPost('test-uri', $testArray);

$this->assertInstanceOf('Credibility\DandB\Response', $response);
$this->assertTrue($response->isValid());
$this->assertArrayHasKey('business_name', $response->getResponseData());
}

public function testRunJsonPost()
{
$data = '{"test": "data"}';
$testArray = $this->setHttpMethodAndReturnData($data);
$response = $this->requester->runJsonPost('test-uri', $testArray);
$this->assertJson($response);
}

protected function getMockResponseInterface($data)
{
$mockResponse = m::mock('GuzzleHttp\Message\ResponseInterface');
Expand Down Expand Up @@ -270,8 +276,13 @@ private function setHttpMethodExpectations()
)
)
);
return $this->setHttpMethodAndReturnData($testArray);
}

private function setHttpMethodAndReturnData($data)
{
$this->setMockAccessTokenCall();
$mockResponse = $this->getMockResponseInterface($testArray);
$mockResponse = $this->getMockResponseInterface($data);
$mockRequest = $this->getMockRequestInterface();

$this->mockGuzzle->shouldReceive('createRequest')
Expand All @@ -280,7 +291,7 @@ private function setHttpMethodExpectations()
$this->mockGuzzle->shouldReceive('send')
->with($mockRequest)
->andReturn($mockResponse);
return $testArray;
return $data;
}

private function setCacheExpectations($hasCacheData, $cacheData = null, $dataToCache = null)
Expand Down

0 comments on commit 9ca1519

Please sign in to comment.