Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | namespace Tests\Providers\Time; |
| 4 | |
| 5 | use PHPUnit\Framework\TestCase; |
| 6 | use Tests\MightNotMakeAssertions; |
| 7 | use RobThree\Auth\TwoFactorAuthException; |
| 8 | use RobThree\Auth\TwoFactorAuth; |
| 9 | |
| 10 | class ITimeProviderTest extends TestCase |
| 11 | { |
| 12 | use MightNotMakeAssertions; |
| 13 | |
| 14 | /** |
| 15 | * @return void |
| 16 | */ |
| 17 | public function testEnsureCorrectTimeDoesNotThrowForCorrectTime() |
| 18 | { |
| 19 | $tpr1 = new TestTimeProvider(123); |
| 20 | $tpr2 = new TestTimeProvider(128); |
| 21 | |
| 22 | $tfa = new TwoFactorAuth('Test', 6, 30, 'sha1', null, null, $tpr1); |
| 23 | $tfa->ensureCorrectTime(array($tpr2)); // 128 - 123 = 5 => within default leniency |
| 24 | |
| 25 | $this->noAssertionsMade(); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @return void |
| 30 | */ |
| 31 | public function testEnsureCorrectTimeThrowsOnIncorrectTime() |
| 32 | { |
| 33 | $tpr1 = new TestTimeProvider(123); |
| 34 | $tpr2 = new TestTimeProvider(124); |
| 35 | |
| 36 | $tfa = new TwoFactorAuth('Test', 6, 30, 'sha1', null, null, $tpr1); |
| 37 | |
| 38 | $this->expectException(TwoFactorAuthException::class); |
| 39 | |
| 40 | $tfa->ensureCorrectTime(array($tpr2), 0); // We force a leniency of 0, 124-123 = 1 so this should throw |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @return void |
| 45 | */ |
| 46 | public function testEnsureDefaultTimeProviderReturnsCorrectTime() |
| 47 | { |
| 48 | $tfa = new TwoFactorAuth('Test', 6, 30, 'sha1'); |
| 49 | $tfa->ensureCorrectTime(array(new TestTimeProvider(time())), 1); // Use a leniency of 1, should the time change between both time() calls |
| 50 | |
| 51 | $this->noAssertionsMade(); |
| 52 | } |
| 53 | } |