-
-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathAppCheckTest.php
More file actions
81 lines (61 loc) · 2.52 KB
/
AppCheckTest.php
File metadata and controls
81 lines (61 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
declare(strict_types=1);
namespace Kreait\Firebase\Tests\Integration;
use Kreait\Firebase\AppCheck\VerifyAppCheckTokenResponse;
use Kreait\Firebase\Contract\AppCheck;
use Kreait\Firebase\Contract\AppCheckWithReplayProtection;
use Kreait\Firebase\Tests\IntegrationTestCase;
/**
* @internal
*/
final class AppCheckTest extends IntegrationTestCase
{
public AppCheck&AppCheckWithReplayProtection $appCheck;
protected function setUp(): void
{
parent::setUp();
if (self::$appId === null) {
$this->markTestSkipped('AppCheck tests require an App ID');
}
$this->appCheck = self::$factory->createAppCheck();
}
public function testCreateTokenWithDefaultTtl(): void
{
$token = $this->appCheck->createToken(self::$appId);
$this->assertSame('3600s', $token->ttl);
}
public function testCreateTokenWithCustomTtl(): void
{
$token = $this->appCheck->createToken(self::$appId, ['ttl' => 1800]);
$this->assertSame('1800s', $token->ttl);
}
public function testVerifyToken(): void
{
$token = $this->appCheck->createToken(self::$appId);
$response = $this->appCheck->verifyToken($token->token);
$this->assertSame(self::$appId, $response->appId);
$this->assertSame(self::$appId, $response->token->app_id);
}
public function testVerifyTokenWithReplayProtection(): void
{
$token = $this->appCheck->createToken(self::$appId);
$firstVerification = $this->appCheck->verifyTokenWithReplayProtection($token->token);
$secondVerification = $this->appCheck->verifyTokenWithReplayProtection($token->token);
// Replay-consumption state may not be visible immediately, so retry briefly.
if ($secondVerification->alreadyConsumed !== true) {
for ($attempt = 0; $attempt < 3; ++$attempt) {
sleep(1);
$secondVerification = $this->appCheck->verifyTokenWithReplayProtection($token->token);
if ($secondVerification->alreadyConsumed === true) {
break;
}
$secondVerification = null;
}
}
$this->assertSame(self::$appId, $firstVerification->appId);
$this->assertSame(self::$appId, $firstVerification->token->app_id);
$this->assertFalse($firstVerification->alreadyConsumed);
$this->assertInstanceOf(VerifyAppCheckTokenResponse::class, $secondVerification);
$this->assertTrue($secondVerification->alreadyConsumed);
}
}