<commit_before><?php
/**
 *
 * User: <NAME>
 * Date: 16.5.12
 * Time: 11:46
 *
 */

namespace Keboola\Test\Common;

use Keboola\StorageApi\ClientException;
use Keboola\Test\StorageApiTestCase;
use Keboola\Csv\CsvFile;
use Keboola\StorageApi\Options\Components\ListComponentsOptions;
use Keboola\StorageApi\Client;

class TokensTest extends StorageApiTestCase
{

    protected $_inBucketId;
    protected $_outBucketId;

    public function setUp()
    {
        parent::setUp();
        $this->_initEmptyTestBuckets();

        $this->_outBucketId = $this->getTestBucketId(self::STAGE_OUT);
        $this->_inBucketId = $this->getTestBucketId(self::STAGE_IN);

        $this->_initTokens();
    }

    protected function _initTokens()
    {
        foreach ($this->_client->listTokens() as $token) {
            if ($token['isMasterToken']) {
                continue;
            }
            $this->_client->dropToken($token['id']);
        }
    }

    protected function _clearComponents()
    {
        $components = new \Keboola\StorageApi\Components($this->_client);
        foreach ($components->listComponents() as $component) {
            foreach ($component['configurations'] as $configuration) {
                $components->deleteConfiguration($component['id'], $configuration['id']);
            }
        }

        // erase all deleted configurations
        foreach ($components->listComponents((new ListComponentsOptions())->setIsDeleted(true)) as $component) {
            foreach ($component['configurations'] as $configuration) {
                $components->deleteConfiguration($component['id'], $configuration['id']);
            }
        }
    }

    public function testTokenProperties()
    {
        $token = $this->_client->verifyToken();
        $this->assertArrayHasKey('created', $token);
        $this->assertArrayHasKey('refreshed', $token);
        $this->assertArrayHasKey('description', $token);
        $this->assertArrayHasKey('id', $token);
        $this->assertTrue($token['isMasterToken']);
        $this->assertTrue($token['canManageBuckets']);
        $this->assertTrue($token['canReadAllFileUploads']);
        $this->assertFalse($token['isDisabled']);
        $this->assertNotEmpty($token['bucketPermissions']);
        $this->assertArrayHasKey('owner', $token);
        $this->assertArrayHasKey('admin', $token);

        $owner = $token['owner'];
        $this->assertInternalType('integer', $owner['dataSizeBytes']);
        $this->assertInternalType('integer', $owner['rowsCount']);
        $this->assertInternalType('boolean', $owner['hasRedshift']);

        $this->assertArrayHasKey('limits', $owner);
        $this->assertArrayHasKey('metrics', $owner);
        $this->assertArrayHasKey('defaultBackend', $owner);

        $firstLimit = reset($owner['limits']);
        $limitKeys = array_keys($owner['limits']);
        $this->assertArrayHasKey('name', $firstLimit);
        $this->assertArrayHasKey('value', $firstLimit);
        $this->assertInternalType('int', $firstLimit['value']);
        $this->assertEquals($firstLimit['name'], $limitKeys[0]);

        $tokens = $this->_client->listTokens();
        foreach ($tokens as $currentToken) {
            if ($currentToken['id'] != $token['id']) {
                continue;
            }

            $this->assertArrayHasKey('admin', $currentToken);

            $admin = $currentToken['admin'];
            $this->assertArrayHasKey('id', $admin);
            $this->assertArrayHasKey('name', $admin);
            $this->assertArrayHasKey('features', $admin);
            return;
        }

        $this->fail("Token $token[id] not present in list");
    }

    public function testKeenReadTokensRetrieve()
    {
        $keen = $this->_client->getKeenReadCredentials();
        $this->assertArrayHasKey('keenToken', $keen);
        $this->assertNotEmpty($keen['keenToken']);
        $this->assertNotEmpty($keen['projectId']);
    }

    public function testInvalidToken()
    {
        $invalidToken = '<PASSWORD>';

        try {
            $client = new \Keboola\StorageApi\Client(array(
                'token' => $invalidToken,
                'url' => STORAGE_API_URL,
            ));<commit_msg>tests - token list, features not listed<commit_after><?php
/**
 *
 * User: <NAME>
 * Date: 16.5.12
 * Time: 11:46
 *
 */

namespace Keboola\Test\Common;

use Keboola\StorageApi\ClientException;
use Keboola\Test\StorageApiTestCase;
use Keboola\Csv\CsvFile;
use Keboola\StorageApi\Options\Components\ListComponentsOptions;
use Keboola\StorageApi\Client;

class TokensTest extends StorageApiTestCase
{

    protected $_inBucketId;
    protected $_outBucketId;

    public function setUp()
    {
        parent::setUp();
        $this->_initEmptyTestBuckets();

        $this->_outBucketId = $this->getTestBucketId(self::STAGE_OUT);
        $this->_inBucketId = $this->getTestBucketId(self::STAGE_IN);

        $this->_initTokens();
    }

    protected function _initTokens()
    {
        foreach ($this->_client->listTokens() as $token) {
            if ($token['isMasterToken']) {
                continue;
            }
            $this->_client->dropToken($token['id']);
        }
    }

    protected function _clearComponents()
    {
        $components = new \Keboola\StorageApi\Components($this->_client);
        foreach ($components->listComponents() as $component) {
            foreach ($component['configurations'] as $configuration) {
                $components->deleteConfiguration($component['id'], $configuration['id']);
            }
        }

        // erase all deleted configurations
        foreach ($components->listComponents((new ListComponentsOptions())->setIsDeleted(true)) as $component) {
            foreach ($component['configurations'] as $configuration) {
                $components->deleteConfiguration($component['id'], $configuration['id']);
            }
        }
    }

    public function testTokenProperties()
    {
        $token = $this->_client->verifyToken();
        $this->assertArrayHasKey('created', $token);
        $this->assertArrayHasKey('refreshed', $token);
        $this->assertArrayHasKey('description', $token);
        $this->assertArrayHasKey('id', $token);
        $this->assertTrue($token['isMasterToken']);
        $this->assertTrue($token['canManageBuckets']);
        $this->assertTrue($token['canReadAllFileUploads']);
        $this->assertFalse($token['isDisabled']);
        $this->assertNotEmpty($token['bucketPermissions']);
        $this->assertArrayHasKey('owner', $token);
        $this->assertArrayHasKey('admin', $token);

        $owner = $token['owner'];
        $this->assertInternalType('integer', $owner['dataSizeBytes']);
        $this->assertInternalType('integer', $owner['rowsCount']);
        $this->assertInternalType('boolean', $owner['hasRedshift']);

        $this->assertArrayHasKey('limits', $owner);
        $this->assertArrayHasKey('metrics', $owner);
        $this->assertArrayHasKey('defaultBackend', $owner);

        $firstLimit = reset($owner['limits']);
        $limitKeys = array_keys($owner['limits']);
        $this->assertArrayHasKey('name', $firstLimit);
        $this->assertArrayHasKey('value', $firstLimit);
        $this->assertInternalType('int', $firstLimit['value']);
        $this->assertEquals($firstLimit['name'], $limitKeys[0]);

        $tokens = $this->_client->listTokens();
        foreach ($tokens as $currentToken) {
            if ($currentToken['id'] != $token['id']) {
                continue;
            }

            $this->assertArrayHasKey('admin', $currentToken);

            $admin = $currentToken['admin'];
            $this->assertArrayHasKey('id', $admin);
            $this->assertArrayHasKey('name', $admin);
            return;
        }

        $this->fail("Token $token[id] not present in list");
    }

    public function testKeenReadTokensRetrieve()
    {
        $keen = $this->_client->getKeenReadCredentials();
        $this->assertArrayHasKey('keenToken', $keen);
        $this->assertNotEmpty($keen['keenToken']);
        $this->assertNotEmpty($keen['projectId']);
    }

    public function testInvalidToken()
    {
        $invalidToken = '<PASSWORD>';

        try {
            $client = new \Keboola\StorageApi\Client(array(
                'token' => $invalidToken,
                'url' => STORAGE_API_URL,
            ));
{'fraction_non_alphanumeric': 0.1609907120743034, 'fraction_numerical': 0.0023815194093831865, 'mean_word_length': 2.8580615525953146, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 112, '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': '636868', 'n_tokens_mistral': 2359, 'n_tokens_neox': 2318, 'n_words': 355}