3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\Security\TwoFactor\Factory;
24 use Friendica\Security\TwoFactor\Factory\TrustedBrowser;
25 use Friendica\Test\MockedTest;
26 use Friendica\Util\DateTimeFormat;
27 use Friendica\Util\Strings;
28 use Psr\Log\NullLogger;
30 class TrustedBrowserTest extends MockedTest
32 public function testCreateFromTableRowSuccess()
34 $factory = new TrustedBrowser(new NullLogger());
37 'cookie_hash' => Strings::getRandomHex(),
39 'user_agent' => 'PHPUnit',
40 'created' => DateTimeFormat::utcNow(),
45 $trustedBrowser = $factory->createFromTableRow($row);
47 $this->assertEquals($row, $trustedBrowser->toArray());
50 public function testCreateFromTableRowMissingData()
52 $this->expectException(\TypeError::class);
54 $factory = new TrustedBrowser(new NullLogger());
57 'cookie_hash' => null,
65 $trustedBrowser = $factory->createFromTableRow($row);
67 $this->assertEquals($row, $trustedBrowser->toArray());
70 public function testCreateForUserWithUserAgent()
72 $factory = new TrustedBrowser(new NullLogger());
75 $userAgent = 'PHPUnit';
77 $trustedBrowser = $factory->createForUserWithUserAgent($uid, $userAgent, true);
79 $this->assertNotEmpty($trustedBrowser->cookie_hash);
80 $this->assertEquals($uid, $trustedBrowser->uid);
81 $this->assertEquals($userAgent, $trustedBrowser->user_agent);
82 $this->assertTrue($trustedBrowser->trusted);
83 $this->assertNotEmpty($trustedBrowser->created);