|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Keboola\Test\Backend\Workspaces; |
| 6 | + |
| 7 | +use Exception; |
| 8 | +use Keboola\StorageApi\BranchAwareClient; |
| 9 | +use Keboola\StorageApi\Workspaces; |
| 10 | +use Keboola\TableBackendUtils\Escaping\Snowflake\SnowflakeQuote; |
| 11 | +use Keboola\Test\Backend\WorkspaceConnectionTrait; |
| 12 | +use Keboola\Test\Backend\Workspaces\Backend\WorkspaceBackendFactory; |
| 13 | +use Keboola\Test\ClientProvider\ClientProvider; |
| 14 | +use Keboola\Test\Utils\EventsQueryBuilder; |
| 15 | + |
| 16 | +class WorkspacesCredentialsTest extends ParallelWorkspacesTestCase |
| 17 | +{ |
| 18 | + use WorkspaceConnectionTrait; |
| 19 | + |
| 20 | + public function setUp(): void |
| 21 | + { |
| 22 | + parent::setUp(); |
| 23 | + |
| 24 | + $this->allowTestForBackendsOnly([ |
| 25 | + 'snowflake', |
| 26 | + 'bigquery', |
| 27 | + ], 'Workspace credentials are implemented only for Snowflake and BigQuery'); |
| 28 | + } |
| 29 | + |
| 30 | + public function testConnectByCredentials(): void |
| 31 | + { |
| 32 | + $this->skipTestForBackend(['bigquery'], 'BigQuery is WIP'); |
| 33 | + |
| 34 | + // create workspace and source table in workspace |
| 35 | + $workspace = $this->initTestWorkspace(forceRecreate: true); |
| 36 | + |
| 37 | + // connect to workspace after creation (we have credentials in response) |
| 38 | + $backend = WorkspaceBackendFactory::createWorkspaceBackend($workspace); |
| 39 | + |
| 40 | + // create dummy table to check it later by |
| 41 | + $backend->dropTableIfExists('test_Languages'); |
| 42 | + $backend->createTable('test_Languages', [ |
| 43 | + 'id' => 'integer', |
| 44 | + 'name' => 'string', |
| 45 | + ]); |
| 46 | + if ($workspace['connection']['backend'] === 'snowflake') { |
| 47 | + $backend->executeQuery(sprintf( |
| 48 | + 'INSERT INTO %s (%s, %s) VALUES (1, \'cz\'), (2, \'en\');', |
| 49 | + SnowflakeQuote::createQuotedIdentifierFromParts([ |
| 50 | + $workspace['connection']['schema'], |
| 51 | + 'test_Languages', |
| 52 | + ]), |
| 53 | + SnowflakeQuote::quoteSingleIdentifier('id'), |
| 54 | + SnowflakeQuote::quoteSingleIdentifier('name'), |
| 55 | + )); |
| 56 | + } else { |
| 57 | + // BigQuery |
| 58 | + $backend->executeQuery(sprintf( |
| 59 | + 'INSERT INTO %s.`test_Languages` (`id`, `name`) VALUES (1, \'cz\'), (2, \'en\');', |
| 60 | + $workspace['connection']['schema'], |
| 61 | + )); |
| 62 | + } |
| 63 | + |
| 64 | + $workspaces = new Workspaces($this->workspaceSapiClient); |
| 65 | + |
| 66 | + $this->initEvents($this->workspaceSapiClient); |
| 67 | + // credentials creation |
| 68 | + $assertCallback = function ($events) { |
| 69 | + $this->assertCount(1, $events); |
| 70 | + }; |
| 71 | + |
| 72 | + $query = new EventsQueryBuilder(); |
| 73 | + $query->setEvent('storage.workspaceCredentialsCreated')->setTokenId($this->tokenId); |
| 74 | + |
| 75 | + $workspaceCredentials = $workspaces->createCredentials($workspace['id']); |
| 76 | + $this->assertEventWithRetries($this->_client, $assertCallback, $query); |
| 77 | + $workspaceBackend = WorkspaceBackendFactory::createWorkspaceBackend($workspaceCredentials, true); |
| 78 | + |
| 79 | + $dbResult = $workspaceBackend->fetchAll('test_Languages'); |
| 80 | + |
| 81 | + $this->assertEquals( |
| 82 | + [ |
| 83 | + 0 => [1, 'cz'], |
| 84 | + 1 => [2, 'en'], |
| 85 | + ], |
| 86 | + $dbResult, |
| 87 | + ); |
| 88 | + |
| 89 | + // should just return the same credentials |
| 90 | + $query = new EventsQueryBuilder(); |
| 91 | + $query->setEvent('storage.workspaceCredentialsRetrieved')->setTokenId($this->tokenId); |
| 92 | + |
| 93 | + $retrievedCredentials = $workspaces->createCredentials($workspace['id']); |
| 94 | + $this->assertEventWithRetries($this->_client, $assertCallback, $query); |
| 95 | + $this->assertEquals($workspaceCredentials['connection']['privateKey'], $retrievedCredentials['connection']['privateKey']); |
| 96 | + |
| 97 | + // credential detail is working and returning a correct object |
| 98 | + $credentialsId = $workspaceCredentials['connection']['credentials']['id']; |
| 99 | + |
| 100 | + $query = new EventsQueryBuilder(); |
| 101 | + $query->setEvent('storage.workspaceCredentialsDetail')->setTokenId($this->tokenId); |
| 102 | + $workspaceCredentialsRefreshed = $workspaces->getCredentials($workspace['id'], $credentialsId); |
| 103 | + $this->assertEventWithRetries($this->_client, $assertCallback, $query); |
| 104 | + $this->assertEquals($workspaceCredentials['connection']['privateKey'], $workspaceCredentialsRefreshed['connection']['privateKey']); |
| 105 | + $workspaceBackendRefreshed = WOrkspaceBackendFactory::createWorkspaceBackend($workspaceCredentialsRefreshed, true); |
| 106 | + |
| 107 | + $dbResultRefreshed = $workspaceBackendRefreshed->fetchAll('test_Languages'); |
| 108 | + |
| 109 | + $this->assertEquals( |
| 110 | + [ |
| 111 | + 0 => [1, 'cz'], |
| 112 | + 1 => [2, 'en'], |
| 113 | + ], |
| 114 | + $dbResultRefreshed, |
| 115 | + ); |
| 116 | + |
| 117 | + $query = new EventsQueryBuilder(); |
| 118 | + $query->setEvent('storage.workspaceCredentialsDeleted')->setTokenId($this->tokenId); |
| 119 | + $workspaces->deleteCredentials($workspace['id'], $credentialsId); |
| 120 | + $this->assertEventWithRetries($this->_client, $assertCallback, $query); |
| 121 | + |
| 122 | + try { |
| 123 | + WorkspaceBackendFactory::createWorkspaceBackend($workspaceCredentialsRefreshed, true); |
| 124 | + $this->fail('Expected exception to be thrown.'); |
| 125 | + } catch (Exception $e) { |
| 126 | + $this->assertTrue(str_contains($e->getMessage(), 'JWT token is invalid.')); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments