summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/tests/unit/Common/Auth/AuthHandlerTest.php
blob: d0d63cf58a6b49372e69f9ca0318eb5ba8f3b94a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

namespace OpenCloud\Test\Common\Auth;

use GuzzleHttp\Psr7\Request;
use OpenCloud\Common\Auth\AuthHandler;
use OpenCloud\Common\Auth\Token;
use OpenCloud\Test\TestCase;
use Psr\Http\Message\RequestInterface;

class AuthHandlerTest extends TestCase
{
    const TOKEN_ID = 'tokenId';

    private $generator;
    private $handler;

    public function setUp()
    {
        $this->generator = function () {
            $token = $this->prophesize(FakeToken::class);
            $token->getId()->shouldBeCalled()->willReturn(self::TOKEN_ID);
            return $token->reveal();
        };

        $this->handler = function (RequestInterface $r) {
            return $r;
        };

        $this->handler = new AuthHandler($this->handler, $this->generator);
    }

    public function test_it_should_bypass_auth_http_requests()
    {
        // Fake a Keystone request
        $request = new Request('POST', 'https://my-openstack.org:5000/v2.0/tokens');

        $this->assertEquals($request, call_user_func_array($this->handler, [$request, []]));
    }

    public function test_it_should_generate_a_new_token_if_the_current_token_is_either_expired_or_not_set()
    {
        $token = $this->prophesize(Token::class);

        // force the mock token to indicate that its expired
        $token->getId()->willReturn('');
        $token->hasExpired()->willReturn(true);

        $request = new Request('GET', '');

        $handler = new AuthHandler($this->handler, $this->generator, $token->reveal());
        $handler($request, []);
    }
}

class FakeToken implements Token
{
    public function getId(): string
    {}

    public function hasExpired(): bool
    {}
}