Skip to content

Commit

Permalink
fixes #505 - value must not be empty error when using dynamodb storage.
Browse files Browse the repository at this point in the history
Adding test for dynamodb storage.
Default scope should be null when none is set in the database.

Removing immediate return true in dynamodb test.
  • Loading branch information
jun authored and jun committed Mar 4, 2015
1 parent 5ebe653 commit 0786a36
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/OAuth2/Storage/DynamoDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function setAccessToken($access_token, $client_id, $user_id, $expires, $s
$expires = date('Y-m-d H:i:s', $expires);

$clientData = compact('access_token', 'client_id', 'user_id', 'expires', 'scope');
$clientData = array_filter($clientData, function ($value) { return !is_null($value); });
$clientData = array_filter($clientData, function ($value) { return !empty($value); });

$result = $this->client->putItem(array(
'TableName' => $this->config['access_token_table'],
Expand Down Expand Up @@ -208,7 +208,7 @@ public function setAuthorizationCode($authorization_code, $client_id, $user_id,
$expires = date('Y-m-d H:i:s', $expires);

$clientData = compact('authorization_code', 'client_id', 'user_id', 'redirect_uri', 'expires', 'id_token', 'scope');
$clientData = array_filter($clientData, function ($value) { return !is_null($value); });
$clientData = array_filter($clientData, function ($value) { return !empty($value); });

$result = $this->client->putItem(array(
'TableName' => $this->config['code_table'],
Expand Down Expand Up @@ -309,7 +309,7 @@ public function setRefreshToken($refresh_token, $client_id, $user_id, $expires,
$expires = date('Y-m-d H:i:s', $expires);

$clientData = compact('refresh_token', 'client_id', 'user_id', 'expires', 'scope');
$clientData = array_filter($clientData, function ($value) { return !is_null($value); });
$clientData = array_filter($clientData, function ($value) { return !empty($value); });

$result = $this->client->putItem(array(
'TableName' => $this->config['refresh_token_table'],
Expand Down Expand Up @@ -411,7 +411,7 @@ public function getDefaultScope($client_id = null)
$defaultScope[] = $item['scope']['S'];
}

return implode(' ', $defaultScope);
return empty($defaultScope) ? null : implode(' ', $defaultScope);
}

return null;
Expand Down
5 changes: 5 additions & 0 deletions test/OAuth2/Storage/AccessTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@ public function testSetAccessToken(AccessTokenInterface $storage)
$this->assertEquals($token['client_id'], 'client ID2');
$this->assertEquals($token['user_id'], 'SOMEOTHERID');
$this->assertEquals($token['expires'], $expires);

// add token with scope having an empty string value
$expires = time() + 42;
$success = $storage->setAccessToken('newtoken', 'client ID', 'SOMEOTHERID', $expires, '');
$this->assertTrue($success);
}
}
5 changes: 5 additions & 0 deletions test/OAuth2/Storage/AuthorizationCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public function testSetAuthorizationCode(AuthorizationCodeInterface $storage)
$this->assertEquals($code['user_id'], 'SOMEOTHERID');
$this->assertEquals($code['redirect_uri'], 'http://example.org');
$this->assertEquals($code['expires'], $expires);

// add new code with scope having an empty string value
$expires = time() + 20;
$success = $storage->setAuthorizationCode('newcode', 'client ID', 'SOMEUSERID', 'http://example.com', $expires, '');
$this->assertTrue($success);
}

/** @dataProvider provideStorage */
Expand Down
41 changes: 41 additions & 0 deletions test/OAuth2/Storage/DynamoDBTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace OAuth2\Storage;

class DynamoDBTest extends BaseTest
{
public function testGetDefaultScope()
{
$client = $this->getMockBuilder('\Aws\DynamoDb\DynamoDbClient')
->disableOriginalConstructor()
->setMethods(array('query'))
->getMock();

$return = $this->getMockBuilder('\Guzzle\Service\Resource\Model')
->setMethods(array('count', 'toArray'))
->getMock();

$data = array(
'Items' => array(),
'Count' => 0,
'ScannedCount'=> 0
);

$return->expects($this->once())
->method('count')
->will($this->returnValue(count($data)));

$return->expects($this->once())
->method('toArray')
->will($this->returnValue($data));


// should return null default scope if none is set in database
$client->expects($this->once())
->method('query')
->will($this->returnValue($return));

$storage = new DynamoDB($client);
$this->assertNull($storage->getDefaultScope());
}
}
5 changes: 5 additions & 0 deletions test/OAuth2/Storage/RefreshTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,10 @@ public function testSetRefreshToken(RefreshTokenInterface $storage)
$this->assertEquals($token['client_id'], 'client ID');
$this->assertEquals($token['user_id'], 'SOMEUSERID');
$this->assertEquals($token['expires'], $expires);

// add token with scope having an empty string value
$expires = time() + 20;
$success = $storage->setRefreshToken('refreshtoken2', 'client ID', 'SOMEUSERID', $expires, '');
$this->assertTrue($success);
}
}

0 comments on commit 0786a36

Please sign in to comment.