Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add tests for latest fixes #512

Merged
merged 3 commits into from Jun 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 74 additions & 6 deletions tests/JWTTest.php
Expand Up @@ -76,6 +76,9 @@ public function testValidToken()
$this->assertSame($decoded->message, 'abc');
}

/**
* @runInSeparateProcess
*/
public function testValidTokenWithLeeway()
{
JWT::$leeway = 60;
Expand All @@ -86,9 +89,11 @@ public function testValidTokenWithLeeway()
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
$this->assertSame($decoded->message, 'abc');
JWT::$leeway = 0;
}

/**
* @runInSeparateProcess
*/
public function testExpiredTokenWithLeeway()
{
JWT::$leeway = 60;
Expand All @@ -100,7 +105,6 @@ public function testExpiredTokenWithLeeway()
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
$this->assertSame($decoded->message, 'abc');
JWT::$leeway = 0;
}

public function testValidTokenWithNbf()
Expand All @@ -116,6 +120,9 @@ public function testValidTokenWithNbf()
$this->assertSame($decoded->message, 'abc');
}

/**
* @runInSeparateProcess
*/
public function testValidTokenWithNbfLeeway()
{
JWT::$leeway = 60;
Expand All @@ -126,9 +133,11 @@ public function testValidTokenWithNbfLeeway()
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
$this->assertSame($decoded->message, 'abc');
JWT::$leeway = 0;
}

/**
* @runInSeparateProcess
*/
public function testInvalidTokenWithNbfLeeway()
{
JWT::$leeway = 60;
Expand All @@ -139,9 +148,45 @@ public function testInvalidTokenWithNbfLeeway()
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$this->expectException(BeforeValidException::class);
JWT::decode($encoded, new Key('my_key', 'HS256'));
JWT::$leeway = 0;
}

public function testValidTokenWithNbfIgnoresIat()
{
$payload = [
'message' => 'abc',
'nbf' => time() - 20, // time in the future
'iat' => time() + 20, // time in the past
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
$this->assertEquals('abc', $decoded->message);
}

public function testValidTokenWithNbfMicrotime()
{
$payload = [
'message' => 'abc',
'nbf' => microtime(true), // use microtime
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
$this->assertEquals('abc', $decoded->message);
}

public function testInvalidTokenWithNbfMicrotime()
{
$this->expectException(BeforeValidException::class);
$payload = [
'message' => 'abc',
'nbf' => microtime(true) + 20, // use microtime in the future
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
JWT::decode($encoded, new Key('my_key', 'HS256'));
}

/**
* @runInSeparateProcess
*/
public function testValidTokenWithIatLeeway()
{
JWT::$leeway = 60;
Expand All @@ -152,9 +197,11 @@ public function testValidTokenWithIatLeeway()
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
$this->assertSame($decoded->message, 'abc');
JWT::$leeway = 0;
}

/**
* @runInSeparateProcess
*/
public function testInvalidTokenWithIatLeeway()
{
JWT::$leeway = 60;
Expand All @@ -165,7 +212,28 @@ public function testInvalidTokenWithIatLeeway()
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$this->expectException(BeforeValidException::class);
JWT::decode($encoded, new Key('my_key', 'HS256'));
JWT::$leeway = 0;
}

public function testValidTokenWithIatMicrotime()
{
$payload = [
'message' => 'abc',
'iat' => microtime(true), // use microtime
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
$decoded = JWT::decode($encoded, new Key('my_key', 'HS256'));
$this->assertEquals('abc', $decoded->message);
}

public function testInvalidTokenWithIatMicrotime()
{
$this->expectException(BeforeValidException::class);
$payload = [
'message' => 'abc',
'iat' => microtime(true) + 20, // use microtime in the future
];
$encoded = JWT::encode($payload, 'my_key', 'HS256');
JWT::decode($encoded, new Key('my_key', 'HS256'));
}

public function testInvalidToken()
Expand Down