Expanding tests, default Session test revealed errors.

master
Brady McDonough 2 years ago
parent 8f49c8815d
commit 1313a4b6da

@ -14,7 +14,7 @@ class Base implements SessionInterface
public function live(): bool public function live(): bool
{ {
return session_status() === PHP_SESSION_ACTIVE; return \session_status() === PHP_SESSION_NONE || \session_status() === PHP_SESSION_ACTIVE;
} }
public function tryStore(string $key, mixed $val): bool public function tryStore(string $key, mixed $val): bool

@ -0,0 +1,56 @@
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use BradyMcD\TAATP\Session\Base as BaseSession;
final class SessionTest extends TestCase
{
public function testSessionIsLive(): void
{
$session = new BaseSession();
$this->assertTrue($session->live());
}
public function testPreservesValues(): void
{
$key = "testVar";
$value = "someData";
$session = new BaseSession();
$session->tryStore($key, $value);
$this->assertSame($session->get($key), $value);
}
public function testTryStoreNeverOverwrites(): void
{
$key = "test";
$value1 = "someImportantData";
$value2 = "DataArrivingLate";
$session = new BaseSession();
$this->assertTrue($session->tryStore($key, $value1));
$this->assertTrue(!$session->tryStore($key, $value2));
$this->assertSame($session->get($key), $value1);
}
public function testStoreOverwrites(): void
{
$key = "test";
$value1 = "initialData";
$value2 = "overwriteData";
$session = new BaseSession();
$session->store($key, $value1);
$session->store($key, $value2);
$this->assertSame($session->get($key), $value2);
}
}
?>

@ -4,19 +4,34 @@ use PHPUnit\Framework\TestCase;
use BradyMcD\TAATP\URI\Otpauth; use BradyMcD\TAATP\URI\Otpauth;
// For more information about the specification, refer to google's documentation on github.
// https://github.com/google/google-authenticator/wiki/Key-Uri-Format
/** @SuppressWarnings(PHPMD.StaticAccess)*/ /** @SuppressWarnings(PHPMD.StaticAccess)*/
final class URITest extends TestCase final class URITest extends TestCase
{ {
public function testRejectsInvalidUri(): void
{
$string = 'totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=MD5&digits=8&period=60';
$this->expectException(\InvalidArgumentException::class);
Otpauth::fromString($string);
}
public function testRejectsMissingSecretInformation(): void
{
$string = 'otpauth://totp/ACME%20Co:john.doe@email.com?issuer=ACME%20Co&algorithm=MD5&digits=8&period=60';
$this->expectException(\InvalidArgumentException::class);
Otpauth::fromString($string);
}
public function testPreservesAllUriFields(): void public function testPreservesAllUriFields(): void
{ {
// Sourced from Google's otpauth URI specification
// https://github.com/google/google-authenticator/wiki/Key-Uri-Format
$string = 'otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=MD5&digits=8&period=60'; $string = 'otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=MD5&digits=8&period=60';
$urlComponents = [ "scheme" => "otpauth",
"host" => "totp",
"path" => "ACME%20Co:john.doe@email.com/",
"query" => "secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30",];
$queryComponents = [ "secret" => "HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ", $queryComponents = [ "secret" => "HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ",
"issuer" => "ACME Co", "issuer" => "ACME Co",
"algorithm" => "MD5", "algorithm" => "MD5",

Loading…
Cancel
Save