friendica/tests/src/Security/TwoFactor/Factory/TrustedBrowserTest.php

86 lines
2.4 KiB
PHP
Raw Normal View History

<?php
2022-01-07 00:30:59 +01:00
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Test\src\Security\TwoFactor\Factory;
use Friendica\Security\TwoFactor\Factory\TrustedBrowser;
2021-04-01 21:19:45 +02:00
use Friendica\Test\MockedTest;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings;
use Psr\Log\NullLogger;
2021-04-01 21:19:45 +02:00
class TrustedBrowserTest extends MockedTest
{
public function testCreateFromTableRowSuccess()
{
$factory = new TrustedBrowser(new NullLogger());
$row = [
'cookie_hash' => Strings::getRandomHex(),
'uid' => 42,
'user_agent' => 'PHPUnit',
'created' => DateTimeFormat::utcNow(),
2022-06-25 14:45:33 +02:00
'trusted' => true,
'last_used' => null,
];
$trustedBrowser = $factory->createFromTableRow($row);
$this->assertEquals($row, $trustedBrowser->toArray());
}
public function testCreateFromTableRowMissingData()
{
$this->expectException(\TypeError::class);
$factory = new TrustedBrowser(new NullLogger());
$row = [
'cookie_hash' => null,
'uid' => null,
'user_agent' => null,
'created' => null,
2022-06-25 14:45:33 +02:00
'trusted' => true,
'last_used' => null,
];
$trustedBrowser = $factory->createFromTableRow($row);
$this->assertEquals($row, $trustedBrowser->toArray());
}
public function testCreateForUserWithUserAgent()
{
$factory = new TrustedBrowser(new NullLogger());
2022-06-25 23:06:42 +02:00
$uid = 42;
$userAgent = 'PHPUnit';
2022-06-25 14:45:33 +02:00
$trustedBrowser = $factory->createForUserWithUserAgent($uid, $userAgent, true);
$this->assertNotEmpty($trustedBrowser->cookie_hash);
$this->assertEquals($uid, $trustedBrowser->uid);
$this->assertEquals($userAgent, $trustedBrowser->user_agent);
2022-06-25 14:45:33 +02:00
$this->assertTrue($trustedBrowser->trusted);
$this->assertNotEmpty($trustedBrowser->created);
}
}