Add trusted browser classes

- Added some tests
This commit is contained in:
Hypolite Petovan 2021-01-18 23:32:28 -05:00
parent b633d75e6c
commit 72bb3bce34
7 changed files with 356 additions and 0 deletions

View file

@ -0,0 +1,62 @@
<?php
namespace Friendica\Test\src\Security\TwoFactor\Factory;
use Friendica\Security\TwoFactor\Factory\TrustedBrowser;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Logger\VoidLogger;
use Friendica\Util\Strings;
class TrustedBrowserTest extends \PHPUnit_Framework_TestCase
{
public function testCreateFromTableRowSuccess()
{
$factory = new TrustedBrowser(new VoidLogger());
$row = [
'cookie_hash' => Strings::getRandomHex(),
'uid' => 42,
'user_agent' => 'PHPUnit',
'created' => DateTimeFormat::utcNow(),
'last_used' => null,
];
$trustedBrowser = $factory->createFromTableRow($row);
$this->assertEquals($row, $trustedBrowser->toArray());
}
public function testCreateFromTableRowMissingData()
{
$this->expectException(\TypeError::class);
$factory = new TrustedBrowser(new VoidLogger());
$row = [
'cookie_hash' => null,
'uid' => null,
'user_agent' => null,
'created' => null,
'last_used' => null,
];
$trustedBrowser = $factory->createFromTableRow($row);
$this->assertEquals($row, $trustedBrowser->toArray());
}
public function testCreateForUserWithUserAgent()
{
$factory = new TrustedBrowser(new VoidLogger());
$uid = 42;
$userAgent = 'PHPUnit';
$trustedBrowser = $factory->createForUserWithUserAgent($uid, $userAgent);
$this->assertNotEmpty($trustedBrowser->cookie_hash);
$this->assertEquals($uid, $trustedBrowser->uid);
$this->assertEquals($userAgent, $trustedBrowser->user_agent);
$this->assertNotEmpty($trustedBrowser->created);
}
}

View file

@ -0,0 +1,46 @@
<?php
namespace Friendica\Test\src\Security\TwoFactor\Model;
use Friendica\Security\TwoFactor\Model\TrustedBrowser;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings;
class TrustedBrowserTest extends \PHPUnit_Framework_TestCase
{
public function test__construct()
{
$hash = Strings::getRandomHex();
$trustedBrowser = new TrustedBrowser(
$hash,
42,
'PHPUnit',
DateTimeFormat::utcNow()
);
$this->assertEquals($hash, $trustedBrowser->cookie_hash);
$this->assertEquals(42, $trustedBrowser->uid);
$this->assertEquals('PHPUnit', $trustedBrowser->user_agent);
$this->assertNotEmpty($trustedBrowser->created);
}
public function testRecordUse()
{
$hash = Strings::getRandomHex();
$past = DateTimeFormat::utc('now - 5 minutes');
$trustedBrowser = new TrustedBrowser(
$hash,
42,
'PHPUnit',
$past,
$past
);
$trustedBrowser->recordUse();
$this->assertEquals($past, $trustedBrowser->created);
$this->assertGreaterThan($past, $trustedBrowser->last_used);
}
}