jingcai-php/vendor/tymon/jwt-auth/tests/Middleware/AuthenticateTest.php

103 lines
3.3 KiB
PHP
Executable File

<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test\Middleware;
use Mockery;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
use Tymon\JWTAuth\Http\Middleware\Authenticate;
use Tymon\JWTAuth\Http\Parser\Parser;
use Tymon\JWTAuth\Test\Stubs\UserStub;
class AuthenticateTest extends AbstractMiddlewareTest
{
/**
* @var \Tymon\JWTAuth\Http\Middleware\Authenticate
*/
protected $middleware;
public function setUp(): void
{
parent::setUp();
$this->middleware = new Authenticate($this->auth);
}
/** @test */
public function it_should_authenticate_a_user()
{
$parser = Mockery::mock(Parser::class);
$parser->shouldReceive('hasToken')->once()->andReturn(true);
$this->auth->shouldReceive('parser')->andReturn($parser);
$this->auth->parser()->shouldReceive('setRequest')->once()->with($this->request)->andReturn($this->auth->parser());
$this->auth->shouldReceive('parseToken->authenticate')->once()->andReturn(new UserStub);
$this->middleware->handle($this->request, function () {
//
});
}
/** @test */
public function it_should_throw_an_unauthorized_exception_if_token_not_provided()
{
$this->expectException(UnauthorizedHttpException::class);
$parser = Mockery::mock(Parser::class);
$parser->shouldReceive('hasToken')->once()->andReturn(false);
$this->auth->shouldReceive('parser')->andReturn($parser);
$this->auth->parser()->shouldReceive('setRequest')->once()->with($this->request)->andReturn($this->auth->parser());
$this->middleware->handle($this->request, function () {
//
});
}
/** @test */
public function it_should_throw_an_unauthorized_exception_if_token_invalid()
{
$this->expectException(UnauthorizedHttpException::class);
$parser = Mockery::mock(Parser::class);
$parser->shouldReceive('hasToken')->once()->andReturn(true);
$this->auth->shouldReceive('parser')->andReturn($parser);
$this->auth->parser()->shouldReceive('setRequest')->once()->with($this->request)->andReturn($this->auth->parser());
$this->auth->shouldReceive('parseToken->authenticate')->once()->andThrow(new TokenInvalidException);
$this->middleware->handle($this->request, function () {
//
});
}
/** @test */
public function it_should_throw_an_unauthorized_exception_if_user_not_found()
{
$this->expectException(UnauthorizedHttpException::class);
$parser = Mockery::mock(Parser::class);
$parser->shouldReceive('hasToken')->once()->andReturn(true);
$this->auth->shouldReceive('parser')->andReturn($parser);
$this->auth->parser()->shouldReceive('setRequest')->once()->with($this->request)->andReturn($this->auth->parser());
$this->auth->shouldReceive('parseToken->authenticate')->once()->andReturn(false);
$this->middleware->handle($this->request, function () {
//
});
}
}