summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/tests/unit/Common/Service/BuilderTest.php
blob: 09e9918ad5c889a1faa0060239ddc22336ef377b (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php

namespace OpenCloud\Test\Common\Service;

use GuzzleHttp\ClientInterface;
use OpenCloud\Common\Service\Builder;
use OpenCloud\Identity\v2\Models\Token;
use OpenCloud\Identity\v2\Service as IdentityV2;
use OpenCloud\Identity\v3\Service as IdentityV3;
use OpenCloud\Compute\v2\Service as ComputeV2;
use OpenCloud\Test\Common\Auth\FakeToken;
use OpenCloud\Test\TestCase;
use Prophecy\Argument;

class BuilderTest extends TestCase
{
    private $builder;
    private $opts;

    public function setUp()
    {
        $this->builder = new Builder([]);

        $this->opts = [
            'username' => '1',
            'password' => '2',
            'tenantId' => '3',
            'authUrl' => '4',
            'region' => '5',
            'catalogName' => '6',
            'catalogType' => '7',
        ];
    }

    /**
     * @expectedException \Exception
     */
    public function test_it_throws_exception_if_username_is_missing()
    {
        $this->builder->createService('Compute', 2, []);
    }

    /**
     * @expectedException \Throwable
     */
    public function test_it_throws_exception_if_password_is_missing()
    {
        $this->builder->createService('Compute', 2, ['username' => 1]);
    }

    /**
     * @expectedException \Throwable
     */
    public function test_it_throws_exception_if_both_tenantId_and_tenantName_is_missing()
    {
        $this->builder->createService('Compute', 2, [
            'username' => 1, 'password' => 2, 'authUrl' => 4, 'region' => 5, 'catalogName' => 6, 'catalogType' => 7,
        ]);
    }

    /**
     * @expectedException \Throwable
     */
    public function test_it_throws_exception_if_authUrl_is_missing()
    {
        $this->builder->createService('Compute', 2, ['username' => 1, 'password' => 2, 'tenantId' => 3]);
    }

    /**
     * @expectedException \Throwable
     */
    public function test_it_throws_exception_if_region_is_missing()
    {
        $this->builder->createService('Compute', 2, [
            'username' => 1, 'password' => 2, 'tenantId' => 3, 'authUrl' => 4,
        ]);
    }

    /**
     * @expectedException \Throwable
     */
    public function test_it_throws_exception_if_catalogName_is_missing()
    {
        $this->builder->createService('Compute', 2, [
            'username' => 1, 'password' => 2, 'tenantId' => 3, 'authUrl' => 4,
        ]);
    }

    /**
     * @expectedException \Throwable
     */
    public function test_it_throws_exception_if_catalogType_is_missing()
    {
        $this->builder->createService('Compute', 2, [
            'username' => 1, 'password' => 2, 'tenantId' => 3, 'authUrl' => 4, 'region' => 5, 'catalogName' => 6,
        ]);
    }

//    public function test_it_builds_services_with_custom_identity_service()
//    {
//        $this->rootFixturesDir = dirname(dirname(__DIR__)) . '/Identity/v2/';
//
//        $token = $this->prophesize(FakeToken::class)->reveal();
//        $service = $this->prophesize(IdentityService::class);
//        $service->authenticate(Argument::type('array'))->shouldBeCalled()->willReturn([$token, '']);
//
//        $this->opts += [
//            'identityService' => $service->reveal(),
//            'catalogName'     => 'nova',
//            'catalogType'     => 'compute',
//            'region'          => 'RegionOne',
//        ];
//
//        $service = $this->builder->createService('Compute', 2, $this->opts);
//        $this->assertInstanceOf(ComputeV2::class, $service);
//    }

    private function setupHttpClient()
    {
        $this->rootFixturesDir = dirname(dirname(__DIR__)) . '/Identity/v3/';

        $response = $this->getFixture('token-get');

        $expectedJson = [
            'auth' => [
                'identity' => [
                    'methods'  => ['password'],
                    'password' => ['user' => ['id' => '0ca8f6', 'password' => 'secretsecret']]
                ]
            ]
        ];

        $httpClient = $this->prophesize(ClientInterface::class);
        $httpClient->request('POST', 'tokens', ['json' => $expectedJson])->shouldBeCalled()->willReturn($response);

        return $httpClient;
    }

    public function it_builds_services_with_default_identity()
    {
        $httpClient = $this->setupHttpClient();

        $options = [
            'httpClient'  => $httpClient->reveal(),
            'catalogName' => 'nova',
            'catalogType' => 'compute',
            'region'      => 'RegionOne',
            'user'        => [
                'id'       => '0ca8f6',
                'password' => 'secretsecret',
            ]
        ];

        $service = $this->builder->createService('Compute', 2, $options);
        $this->assertInstanceOf(ComputeV2::class, $service);
    }

//    public function test_it_does_not_authenticate_when_creating_identity_services()
//    {
//        $this->assertInstanceOf(IdentityV3::class, $this->builder->createService('Identity', 3, [
//            'authUrl'    => 'foo.com',
//        ]));
//    }
}