Skip to content

Commit

Permalink
chore: add tests for latest fixes (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Jun 30, 2023
1 parent 3936842 commit 299105a
Showing 1 changed file with 74 additions and 6 deletions.
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

0 comments on commit 299105a

Please sign in to comment.