<commit_before><?php

namespace Bitbucket\Tests\API\Http\Listener;

use Bitbucket\API\Api;
use Bitbucket\API\Repositories;
use Bitbucket\Tests\API as Tests;
use Bitbucket\API\Http\Listener\OAuth2Listener;
use Buzz\Message\Response;

/**
 * @author  <NAME>.    <<EMAIL>>
 */
class OAuth2ListenerTest extends Tests\TestCase
{
    /**
     * @expectedException \Bitbucket\API\Exceptions\ForbiddenAccessException
     */
    public function testGetAccessTokenShouldFailWithInvalidJson()
    {
        $oauth_params = array(
            'client_id'         => 'aaa',
            'client_secret'     => 'bbb'
        );

        $response = new Response();
        $response->setContent('{"bla": "boo}');

        $httpClient = $this->getHttpClientMock();
        $httpClient->expects($this->any())
            ->method('post')
            ->with(OAuth2Listener::ENDPOINT_ACCESS_TOKEN, array(
                'grant_type'    => 'client_credentials',
                'client_id'     => $oauth_params['client_id'],
                'client_secret' => $oauth_params['client_secret'],
                'scope'         => ''
            ))
            ->will($this->returnValue($response))
        ;

        $repositories = new Repositories(array(), $this->getHttpClient());
        $repositories->getClient()->addListener(
            new OAuth2Listener($oauth_params, $httpClient)
        );

        $repositories->all('my_account');
    }

    /**
     * @expectedException \Bitbucket\API\Exceptions\ForbiddenAccessException
     */
    public function testGetAccessTokenFail()
    {
        $oauth_params = array(
            'client_id'         => 'aaa',
            'client_secret'     => 'bbb'
        );

        $repositories = new Repositories(array(), $this->getHttpClient());
        $repositories->getClient()->addListener(
            new OAuth2Listener($oauth_params)
        );

        $repositories->all('my_account');
    }

    public function testOauth2ListenerDoesNotReplaceExistingBearer()
    {
        $oauth_params = array(
            'client_id'         => 'aaa',
            'client_secret'     => 'bbb'
        );

        $repositories = new Repositories(array(), $this->getHttpClient());
        $repositories->getClient()->addListener(
            new OAuth2Listener($oauth_params, $this->getHttpClient())
        );

        $repositories->getClient()->request('something', array(), 'GET', array(
            'Authorization' => 'Bearer secret'<commit_msg>[Test] Use mocked HTTP client in testGetAccessTokenFail<commit_after><?php

namespace Bitbucket\Tests\API\Http\Listener;

use Bitbucket\API\Api;
use Bitbucket\API\Repositories;
use Bitbucket\Tests\API as Tests;
use Bitbucket\API\Http\Listener\OAuth2Listener;
use Buzz\Message\Response;

/**
 * @author  <NAME>.    <<EMAIL>>
 */
class OAuth2ListenerTest extends Tests\TestCase
{
    /**
     * @expectedException \Bitbucket\API\Exceptions\ForbiddenAccessException
     */
    public function testGetAccessTokenShouldFailWithInvalidJson()
    {
        $oauth_params = array(
            'client_id'         => 'aaa',
            'client_secret'     => 'bbb'
        );

        $response = new Response();
        $response->setContent('{"bla": "boo}');

        $httpClient = $this->getHttpClientMock();
        $httpClient->expects($this->any())
            ->method('post')
            ->with(OAuth2Listener::ENDPOINT_ACCESS_TOKEN, array(
                'grant_type'    => 'client_credentials',
                'client_id'     => $oauth_params['client_id'],
                'client_secret' => $oauth_params['client_secret'],
                'scope'         => ''
            ))
            ->will($this->returnValue($response))
        ;

        $repositories = new Repositories(array(), $this->getHttpClient());
        $repositories->getClient()->addListener(
            new OAuth2Listener($oauth_params, $httpClient)
        );

        $repositories->all('my_account');
    }

    /**
     * @expectedException \Bitbucket\API\Exceptions\ForbiddenAccessException
     */
    public function testGetAccessTokenFail()
    {
        $response = new Response();
        $response->setContent('{"error_description": "Invalid OAuth client credentials", "error": "unauthorized_client"}');

        $oauth_params = array(
            'client_id'         => 'aaa',
            'client_secret'     => 'bbb'
        );

        $httpClient = $this->getHttpClientMock();
        $httpClient->expects($this->any())
            ->method('POST')
            ->with(OAuth2Listener::ENDPOINT_ACCESS_TOKEN,
                array(
                    'grant_type'    => 'client_credentials',
                    'client_id'     => $oauth_params['client_id'],
                    'client_secret' => $oauth_params['client_secret'],
                    'scope'         => ''
                ))
            ->will($this->returnValue($response))
        ;

        $repositories = new Repositories(array(), $this->getHttpClient());
        $repositories->getClient()->addListener(
            new OAuth2Listener($oauth_params, $httpClient)
        );

        $repositories->all('my_account');
    }

    public function testOauth2ListenerDoesNotReplaceExistingBearer()
    {
        $oauth_params = array(
            'client_id'         => 'aaa',
            'client_secret'     => 'bbb'
        );

        $repositories = new Repositories(array(), $this->getHttpClient());
        $repositories->getClient()->addListener(
            new OAuth2Listener($oauth_params, $this->getHttpClient())
        );

        $repositories->getClient()->request('something', array(), 'GET', array(
            'Authorization' => 'Bearer secret'
{'fraction_non_alphanumeric': 0.1423806179775281, 'fraction_numerical': 0.0026334269662921348, 'mean_word_length': 2.2628865979381443, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6035123', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1591, 'n_words': 238}