Completed
Branch master (558225)
by Nate
07:00 queued 02:12
created

HubSpotTest::testScopes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php namespace Flipbox\OAuth2\Client\Test\Provider;
2
3
use Flipbox\OAuth2\Client\Provider\HubSpot;
4
use Mockery as m;
5
6
class HubSpotTest extends \PHPUnit_Framework_TestCase
7
{
8
9
    /** @var HubSpot */
10
    protected $provider;
11
12
    protected function setUp()
13
    {
14
        $this->provider = new HubSpot([
15
            'clientId' => 'mock_client_id',
16
            'clientSecret' => 'mock_secret',
17
            'redirectUri' => 'none',
18
        ]);
19
    }
20
21
    public function tearDown()
22
    {
23
        m::close();
24
        parent::tearDown();
25
    }
26
27
    /** @test */
28
    public function testAuthorizationUrl()
29
    {
30
        $url = $this->provider->getAuthorizationUrl();
31
        $uri = parse_url($url);
32
        parse_str($uri['query'], $query);
33
34
        $this->assertArrayHasKey('client_id', $query);
35
        $this->assertArrayHasKey('redirect_uri', $query);
36
        $this->assertArrayHasKey('state', $query);
37
        $this->assertArrayHasKey('scope', $query);
38
        $this->assertArrayHasKey('response_type', $query);
39
        $this->assertArrayHasKey('approval_prompt', $query);
40
        $this->assertNotNull($this->provider->getState());
41
    }
42
43
    /** @test */
44
    public function testScopes()
45
    {
46
        $options = ['scope' => [uniqid(), uniqid()]];
47
48
        $url = $this->provider->getAuthorizationUrl($options);
49
50
        $scopeString = implode(' ', $options['scope']);
51
        $scopeQuery = http_build_query(['scope' => $scopeString], null, '&', \PHP_QUERY_RFC3986);
52
53
        $this->assertContains($scopeQuery, $url);
54
    }
55
56
    /** @test */
57
    public function testGetAuthorizationUrl()
58
    {
59
        $url = $this->provider->getAuthorizationUrl();
60
        $uri = parse_url($url);
61
62
        $this->assertEquals('/oauth/authorize', $uri['path']);
63
    }
64
65
    /** @test */
66
    public function testGetBaseAccessTokenUrl()
67
    {
68
        $params = [];
69
70
        $url = $this->provider->getBaseAccessTokenUrl($params);
71
        $uri = parse_url($url);
72
73
        $this->assertEquals('/oauth/v1/token', $uri['path']);
74
    }
75
76
    /** @test */
77
    public function testGetAccessToken()
78
    {
79
        $response = m::mock('Psr\Http\Message\ResponseInterface');
80
        $response->shouldReceive('getBody')->andReturn('{"access_token":"mock_access_token", "scope":"repo,gist", "token_type":"bearer"}');
81
        $response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
82
        $response->shouldReceive('getStatusCode')->andReturn(200);
83
84
        $client = m::mock('GuzzleHttp\ClientInterface');
85
        $client->shouldReceive('send')->times(1)->andReturn($response);
86
        $this->provider->setHttpClient($client);
87
88
        $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
89
90
        $this->assertEquals('mock_access_token', $token->getToken());
91
        $this->assertNull($token->getExpires());
92
        $this->assertNull($token->getRefreshToken());
93
        $this->assertNull($token->getResourceOwnerId());
94
    }
95
96
    /** @test */
97
    public function testUserData()
98
    {
99
100
        $params = [
101
            'token' => uniqid() . uniqid(),
102
            'user' => uniqid() . '@' . uniqid() . '.com',
103
            'hub_domain' => uniqid() . '.com',
104
            'scopes' => [
105
                'contacts',
106
                'content',
107
                'oauth'
108
            ],
109
            'hub_id' => rand(6, 10),
110
            'app_id' => rand(6, 10),
111
            'expires_in' => rand(6, 10),
112
            'user_id' => rand(6, 10),
113
            'token_type' => 'access'
114
        ];
115
116
        $postResponse = m::mock('Psr\Http\Message\ResponseInterface');
117
        $postResponse->shouldReceive('getBody')->andReturn('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&otherKey={1234}');
118
        $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'application/x-www-form-urlencoded']);
119
        $postResponse->shouldReceive('getStatusCode')->andReturn(200);
120
121
        $userResponse = m::mock('Psr\Http\Message\ResponseInterface');
122
        $userResponse->shouldReceive('getBody')->andReturn(json_encode($params));
123
        $userResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
124
        $userResponse->shouldReceive('getStatusCode')->andReturn(200);
125
126
        $client = m::mock('GuzzleHttp\ClientInterface');
127
        $client->shouldReceive('send')
128
            ->times(2)
129
            ->andReturn($postResponse, $userResponse);
130
        $this->provider->setHttpClient($client);
131
132
        $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
133
        $user = $this->provider->getResourceOwner($token);
134
135
        $this->assertEquals($params['hub_id'], $user->getHubId());
136
        $this->assertEquals($params['hub_id'], $user->toArray()['hub_id']);
137
        $this->assertEquals($params['user_id'], $user->getId());
138
        $this->assertEquals($params['user_id'], $user->toArray()['user_id']);
139
        $this->assertEquals($params['app_id'], $user->getAppId());
140
        $this->assertEquals($params['app_id'], $user->toArray()['app_id']);
141
        $this->assertEquals($params['hub_domain'], $user->getDomain());
142
        $this->assertEquals($params['hub_domain'], $user->toArray()['hub_domain']);
143
        $this->assertEquals($params['user'], $user->getEmail());
144
        $this->assertEquals($params['user'], $user->toArray()['user']);
145
    }
146
147
    /**
148
     * @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException
149
     **/
150
    public function testExceptionThrownWhenErrorObjectReceived()
151
    {
152
        $status = rand(400, 600);
153
        $postResponse = m::mock('Psr\Http\Message\ResponseInterface');
154
        $postResponse->shouldReceive('getBody')->andReturn('{"message": "Validation Failed","errors": [{"resource": "Issue","field": "title","code": "missing_field"}]}');
155
        $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
156
        $postResponse->shouldReceive('getStatusCode')->andReturn($status);
157
158
        $client = m::mock('GuzzleHttp\ClientInterface');
159
        $client->shouldReceive('send')
160
            ->times(1)
161
            ->andReturn($postResponse);
162
        $this->provider->setHttpClient($client);
163
        $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
0 ignored issues
show
Unused Code introduced by
$token is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
164
    }
165
166
    /**
167
     * @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException
168
     **/
169
    public function testExceptionThrownWhenOAuthErrorReceived()
170
    {
171
        $status = 200;
172
        $postResponse = m::mock('Psr\Http\Message\ResponseInterface');
173
        $postResponse->shouldReceive('getBody')->andReturn('{"error": "bad_verification_code","error_description": "The code passed is incorrect or expired.","error_uri": "https://developer.github.com/v3/oauth/#bad-verification-code"}');
174
        $postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
175
        $postResponse->shouldReceive('getStatusCode')->andReturn($status);
176
177
        $client = m::mock('GuzzleHttp\ClientInterface');
178
        $client->shouldReceive('send')
179
            ->times(1)
180
            ->andReturn($postResponse);
181
        $this->provider->setHttpClient($client);
182
        $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
0 ignored issues
show
Unused Code introduced by
$token is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
183
    }
184
}
185