File size: 1,733 Bytes
e4f4821
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

require_once __DIR__.'/../../Base.php';

use Kanboard\Core\Http\OAuth2;

class OAuth2Test extends Base
{
    public function testAuthUrl()
    {
        $oauth = new OAuth2($this->container);
        $oauth->createService('A', 'B', 'C', 'D', 'E', array('f', 'g'));
        $state = $oauth->getState();
        $this->assertEquals('D?response_type=code&client_id=A&redirect_uri=C&scope=f+g&state='.$state, $oauth->getAuthorizationUrl());
    }

    public function testAuthHeader()
    {
        $oauth = new OAuth2($this->container);
        $oauth->createService('A', 'B', 'C', 'D', 'E', array('f', 'g'));

        $oauth->setAccessToken('foobar', 'BeaRer');
        $this->assertEquals('Authorization: Bearer foobar', $oauth->getAuthorizationHeader());

        $oauth->setAccessToken('foobar', 'unknown');
        $this->assertEquals('', $oauth->getAuthorizationHeader());
    }

    public function testAccessToken()
    {
        $oauth = new OAuth2($this->container);

        $params = array(
            'code' => 'something',
            'client_id' => 'A',
            'client_secret' => 'B',
            'redirect_uri' => 'C',
            'grant_type' => 'authorization_code',
            'state' => $oauth->getState(),
        );

        $response = json_encode(array(
            'token_type' => 'bearer',
            'access_token' => 'plop',
        ));

        $this->container['httpClient']
            ->expects($this->once())
            ->method('postForm')
            ->with('E', $params, array('Accept: application/json'))
            ->will($this->returnValue($response));

        $oauth->createService('A', 'B', 'C', 'D', 'E', array('f', 'g'));
        $oauth->getAccessToken('something');
    }
}