Completed
Push — master ( 224127...d5dc6c )
by Alejandro
28s
created

updateReturnsResponseAsIs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Rest\Authentication\Plugin;
5
6
use PHPUnit\Framework\TestCase;
7
use Prophecy\Prophecy\ObjectProphecy;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Shlinkio\Shlink\Rest\Authentication\Plugin\ApiKeyHeaderPlugin;
10
use Shlinkio\Shlink\Rest\Exception\VerifyAuthenticationException;
11
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
12
use Zend\Diactoros\Response;
13
use Zend\Diactoros\ServerRequestFactory;
14
use Zend\I18n\Translator\Translator;
15
16
class ApiKeyHeaderPluginTest extends TestCase
17
{
18
    /**
19
     * @var ApiKeyHeaderPlugin
20
     */
21
    private $plugin;
22
    /**
23
     * @var ObjectProphecy
24
     */
25
    private $apiKeyService;
26
27
    public function setUp()
28
    {
29
        $this->apiKeyService = $this->prophesize(ApiKeyServiceInterface::class);
30
        $this->plugin = new ApiKeyHeaderPlugin($this->apiKeyService->reveal(), Translator::factory([]));
31
    }
32
33
    /**
34
     * @test
35
     */
36
    public function verifyThrowsExceptionWhenApiKeyIsNotValid()
37
    {
38
        $apiKey = 'abc-ABC';
39
        $check = $this->apiKeyService->check($apiKey)->willReturn(false);
40
        $check->shouldBeCalledTimes(1);
41
42
        $this->expectException(VerifyAuthenticationException::class);
43
        $this->expectExceptionMessage('Provided API key does not exist or is invalid');
44
45
        $this->plugin->verify($this->createRequest($apiKey));
46
    }
47
48
    /**
49
     * @test
50
     */
51
    public function verifyDoesNotThrowExceptionWhenApiKeyIsValid()
52
    {
53
        $apiKey = 'abc-ABC';
54
        $check = $this->apiKeyService->check($apiKey)->willReturn(true);
55
56
        $this->plugin->verify($this->createRequest($apiKey));
57
58
        $check->shouldHaveBeenCalledTimes(1);
59
    }
60
61
    /**
62
     * @test
63
     */
64
    public function updateReturnsResponseAsIs()
65
    {
66
        $apiKey = 'abc-ABC';
67
        $response = new Response();
68
69
        $returnedResponse = $this->plugin->update($this->createRequest($apiKey), $response);
70
71
        $this->assertSame($response, $returnedResponse);
72
    }
73
74
    private function createRequest(string $apiKey): ServerRequestInterface
75
    {
76
        return ServerRequestFactory::fromGlobals()->withHeader(ApiKeyHeaderPlugin::HEADER_NAME, $apiKey);
77
    }
78
}
79