Completed
Push — master ( 9ab4b9...065cdd )
by Alejandro
09:44
created

authenticationExceptionsReturnErrorResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 9.4285
1
<?php
2
namespace ShlinkioTest\Shlink\Rest\Action;
3
4
use PHPUnit_Framework_TestCase as TestCase;
5
use Prophecy\Prophecy\ObjectProphecy;
6
use Shlinkio\Shlink\Rest\Action\AuthenticateAction;
7
use Shlinkio\Shlink\Rest\Authentication\JWTService;
8
use Shlinkio\Shlink\Rest\Entity\ApiKey;
9
use Shlinkio\Shlink\Rest\Service\ApiKeyService;
10
use Zend\Diactoros\Response;
11
use Zend\Diactoros\ServerRequestFactory;
12
use Zend\I18n\Translator\Translator;
13
14
class AuthenticateActionTest extends TestCase
15
{
16
    /**
17
     * @var AuthenticateAction
18
     */
19
    protected $action;
20
    /**
21
     * @var ObjectProphecy
22
     */
23
    protected $apiKeyService;
24
    /**
25
     * @var ObjectProphecy
26
     */
27
    protected $jwtService;
28
29
    public function setUp()
30
    {
31
        $this->apiKeyService = $this->prophesize(ApiKeyService::class);
32
        $this->jwtService = $this->prophesize(JWTService::class);
33
        $this->action = new AuthenticateAction(
34
            $this->apiKeyService->reveal(),
35
            $this->jwtService->reveal(),
36
            Translator::factory([])
37
        );
38
    }
39
40
    /**
41
     * @test
42
     */
43
    public function notProvidingAuthDataReturnsError()
44
    {
45
        $resp = $this->action->__invoke(ServerRequestFactory::fromGlobals(), new Response());
46
        $this->assertEquals(400, $resp->getStatusCode());
47
    }
48
49
    /**
50
     * @test
51
     */
52
    public function properApiKeyReturnsTokenInResponse()
53
    {
54
        $this->apiKeyService->getByKey('foo')->willReturn((new ApiKey())->setId(5))
55
                                             ->shouldBeCalledTimes(1);
56
57
        $request = ServerRequestFactory::fromGlobals()->withParsedBody([
58
            'apiKey' => 'foo',
59
        ]);
60
        $response = $this->action->__invoke($request, new Response());
61
        $this->assertEquals(200, $response->getStatusCode());
62
63
        $response->getBody()->rewind();
64
        $this->assertTrue(strpos($response->getBody()->getContents(), '"token"') > 0);
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function invalidApiKeyReturnsErrorResponse()
71
    {
72
        $this->apiKeyService->getByKey('foo')->willReturn((new ApiKey())->setEnabled(false))
73
                                             ->shouldBeCalledTimes(1);
74
75
        $request = ServerRequestFactory::fromGlobals()->withParsedBody([
76
            'apiKey' => 'foo',
77
        ]);
78
        $response = $this->action->__invoke($request, new Response());
79
        $this->assertEquals(401, $response->getStatusCode());
80
    }
81
}
82