Skip to content

Commit

Permalink
Added more tests to JwtAuthMiddleware class to increase code coverage…
Browse files Browse the repository at this point in the history
… to 100%.
  • Loading branch information
RobDWaller committed May 1, 2019
1 parent 646ab28 commit d7fe9f9
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions tests/JwtAuthMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use PsrJwt\Factory\Jwt;
use PsrJwt\JwtAuthMiddleware;
use PsrJwt\Auth\Authenticate;
use PsrJwt\Auth\Auth;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Server\MiddlewareInterface;
use Mockery as m;
use ReflectionMethod;

class JwtAuthMiddlewareTest extends TestCase
{
Expand Down Expand Up @@ -73,6 +75,49 @@ public function testProcess()
$this->assertSame('Ok', $result->getReasonPhrase());
}

/**
* @covers PsrJwt\JwtAuthMiddleware::process
* @uses PsrJwt\JwtAuthMiddleware
* @uses PsrJwt\Auth\Authenticate
* @uses PsrJwt\Auth\Auth
* @uses PsrJwt\Factory\Jwt
* @uses PsrJwt\Parser\Parse
* @uses PsrJwt\Validation\Validate
* @uses PsrJwt\Parser\Bearer
* @uses PsrJwt\Parser\Body
* @uses PsrJwt\Parser\Cookie
* @uses PsrJwt\Parser\Query
*/
public function testProcessFail()
{
$request = m::mock(ServerRequestInterface::class);
$request->shouldReceive('getCookieParams')
->once()
->andReturn(['car' => 'park']);
$request->shouldReceive('getQueryParams')
->once()
->andReturn(['farm' => 'yard']);
$request->shouldReceive('getParsedBody')
->twice()
->andReturn(['gary' => 'barlow']);
$request->shouldReceive('getHeader')
->with('authorization')
->once()
->andReturn([]);

$handler = m::mock(RequestHandlerInterface::class);

$authenticate = new Authenticate('jwt', 'Secret123!456$');

$process = new JwtAuthMiddleware($authenticate);

$result = $process->process($request, $handler);

$this->assertInstanceOf(ResponseInterface::class, $result);
$this->assertSame(400, $result->getStatusCode());
$this->assertSame('Bad Request: JSON Web Token not set.', $result->getReasonPhrase());
}

/**
* @covers PsrJwt\JwtAuthMiddleware::__invoke
* @uses PsrJwt\JwtAuthMiddleware::__construct
Expand Down Expand Up @@ -159,6 +204,28 @@ public function testInvokeFail()
$this->assertSame('Unauthorized: Signature is invalid.', $result->getReasonPhrase());
}

/**
* @covers PsrJwt\JwtAuthMiddleware::failResponse
* @uses PsrJwt\JwtAuthMiddleware
* @uses PsrJwt\Auth\Auth
* @uses PsrJwt\Auth\Authenticate
*/
public function testFailResponse()
{
$authenticate = new Authenticate('jwt', 'secret');
$auth = new Auth(400, 'Bad Request');

$middleware = new JwtAuthMiddleware($authenticate);

$method = new ReflectionMethod(JwtAuthMiddleware::class, 'failResponse');
$method->setAccessible(true);
$result = $method->invokeArgs($middleware, [$auth]);

$this->assertInstanceOf(ResponseInterface::class, $result);
$this->assertSame(400, $result->getStatusCode());
$this->assertSame('Bad Request', $result->getReasonPhrase());
}

public function tearDown()
{
m::close();
Expand Down

0 comments on commit d7fe9f9

Please sign in to comment.