Skip to content

Commit

Permalink
add get_token and save_token cache functions (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Coutadeur committed Sep 13, 2024
1 parent c643451 commit 29b32a6
Show file tree
Hide file tree
Showing 2 changed files with 238 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Ltb/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,52 @@ function verify_form_token($formtoken)
}


# Get a token from the cache
# return the content of the content (can be an array, a string)
function get_token($tokenid)
{
$cached_token = $this->cache->getItem($tokenid);
$cached_token_content = $cached_token->get();

if($cached_token->isHit())
{
return $cached_token_content;
}
else
{
return null;
}
}

# Save a token to the cache
function save_token($content, $tokenid = null, $cache_token_expiration = null)
{
$msg = "";
if(is_null($tokenid))
{
$tokenid = hash('sha256', bin2hex(random_bytes(16)));
$msg .= "Generated cache entry with id: $tokenid";
}
else
{
$msg .= "Saving existing cache entry with id: $tokenid";
}

$cached_token = $this->cache->getItem($tokenid);
$cached_token->set( $content );

if(!is_null($cache_token_expiration))
{
$cached_token->expiresAfter($cache_token_expiration);
$msg .= ", valid for $cache_token_expiration s";
}

$this->cache->save($cached_token);
error_log($msg);

return $tokenid;
}

}

?>
192 changes: 192 additions & 0 deletions tests/Ltb/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,196 @@ function($token) use(&$generated_token) {
"Error: expected 'invalidformtoken', but received result: '$result' in verify_form_token");

}

public function test_get_token_ok(): void
{

$tokenid = "e712b08e55f8977e2b9ecad35d5180ed24345e76607413411e90df66b9538fa1";
$token_content = "test";

$cacheInstance = new \Ltb\Cache(
"testCache",
0,
null
);

$cacheInstance->cache = Mockery::mock('FilesystemAdapter');
$cacheInstance->cache->shouldreceive('getItem')
->andReturnUsing(
function($token) use(&$token_content) {
$cacheItem = Mockery::mock('CacheItem');

$cacheItem->shouldreceive('isHit')
->andReturn(true);

$cacheItem->shouldreceive('get')
->andReturn($token_content);

return $cacheItem;
}
);

$result = $cacheInstance->get_token($tokenid);
$this->assertEquals($token_content,
$result,
"Unexpected token content: '$result' sent by get_token");

}

public function test_get_token_ko(): void
{

$tokenid = "e712b08e55f8977e2b9ecad35d5180ed24345e76607413411e90df66b9538fa1";
$token_content = "test";

$cacheInstance = new \Ltb\Cache(
"testCache",
0,
null
);

$cacheInstance->cache = Mockery::mock('FilesystemAdapter');
$cacheInstance->cache->shouldreceive('getItem')
->andReturnUsing(
function($token) use(&$token_content) {
$cacheItem = Mockery::mock('CacheItem');

$cacheItem->shouldreceive('isHit')
->andReturn(false);

$cacheItem->shouldreceive('get')
->andReturn(null);

return $cacheItem;
}
);

$result = $cacheInstance->get_token($tokenid);
$this->assertEquals(null,
$result,
"Unexpected not null token content: '$result' sent by get_token");

}

public function test_save_token_new(): void
{

$tokenid = "";
$token_content = [
'param1' => 'value1',
'param2' => 'value2'
];
$cache_token_expiration = 3600;

$cacheInstance = new \Ltb\Cache(
"testCache",
0,
null
);

$cacheInstance->cache = Mockery::mock('FilesystemAdapter');
$cacheInstance->cache->shouldreceive('getItem')
->andReturnUsing(
function($token) use(&$tokenid, &$token_content, &$cache_token_expiration) {
$tokenid = $token;
$cacheItem = Mockery::mock('CacheItem');

$cacheItem->shouldreceive('set')
->with($token_content);

$cacheItem->shouldreceive('expiresAfter')
->with($cache_token_expiration);

return $cacheItem;
}
);

$cacheInstance->cache->shouldreceive('save');

$result = $cacheInstance->save_token($token_content, null, $cache_token_expiration);
$this->assertEquals($tokenid,
$result,
"Bad token id sent by save_token function");

}

public function test_save_token_existing(): void
{

$tokenid = "e712b08e55f8977e2b9ecad35d5180ed24345e76607413411e90df66b9538fa1";
$token_content = [
'par1' => 'val1',
'par2' => 'val2'
];
$cache_token_expiration = 3600;

$cacheInstance = new \Ltb\Cache(
"testCache",
0,
null
);

$cacheInstance->cache = Mockery::mock('FilesystemAdapter');
$cacheInstance->cache->shouldreceive('getItem')
->with($tokenid)
->andReturnUsing(
function($token) use(&$tokenid, &$token_content, &$cache_token_expiration) {
$cacheItem = Mockery::mock('CacheItem');

$cacheItem->shouldreceive('set')
->with($token_content);

$cacheItem->shouldreceive('expiresAfter')
->with($cache_token_expiration);

return $cacheItem;
}
);

$cacheInstance->cache->shouldreceive('save');

$result = $cacheInstance->save_token($token_content, $tokenid, $cache_token_expiration);
$this->assertEquals($tokenid,
$result,
"Bad token id sent by save_token function");

}

public function test_save_token_existing_noexpiration(): void
{

$tokenid = "e712b08e55f8977e2b9ecad35d5180ed24345e76607413411e90df66b9538fa1";
$token_content = [
'par1' => 'val1',
'par2' => 'val2'
];

$cacheInstance = new \Ltb\Cache(
"testCache",
0,
null
);

$cacheInstance->cache = Mockery::mock('FilesystemAdapter');
$cacheInstance->cache->shouldreceive('getItem')
->with($tokenid)
->andReturnUsing(
function($token) use(&$tokenid, &$token_content) {
$cacheItem = Mockery::mock('CacheItem');

$cacheItem->shouldreceive('set')
->with($token_content);

return $cacheItem;
}
);

$cacheInstance->cache->shouldreceive('save');

$result = $cacheInstance->save_token($token_content, $tokenid);
$this->assertEquals($tokenid,
$result,
"Bad token id sent by save_token function");

}
}

0 comments on commit 29b32a6

Please sign in to comment.