Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert DkanDocsTest to BTB #4245

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions modules/common/src/Controller/OpenApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

/**
* Serves openapi spec for dataset-related endpoints.
*
* @codeCoverageIgnore
*/
class OpenApiController implements ContainerInjectionInterface {
use JsonResponseTrait;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
<?php

namespace Drupal\Tests\common\Functional;
namespace Drupal\Tests\common\Functional\Controller;

use Drupal\common\Controller\OpenApiController;
use Drupal\Core\Serialization\Yaml;
use weitzman\DrupalTestTraits\ExistingSiteBase;

class DkanDocsTest extends ExistingSiteBase {
use Drupal\Tests\BrowserTestBase;

/**
* @coversDefaultClass \Drupal\common\Controller\OpenApiController
*
* @group dkan
* @group common
* @group functional
* @group btb
*/
class OpenApiControllerTest extends BrowserTestBase {

protected static $modules = [
'common',
'datastore',
'harvest',
'node',
];

protected $defaultTheme = 'stark';

public function testGetVersions() {
$controller = $this->getController();
Expand All @@ -23,60 +40,56 @@ public function testGetCompleteJson() {
$data = json_decode($response->getContent(), TRUE);

// Basic auth is included.
$this->assertTrue(isset($data["components"]["securitySchemes"]["basic_auth"]));
$this->assertTrue(isset($data['components']['securitySchemes']['basic_auth']));

// Some sample paths from different modules are there
$this->assertArrayHasKey('/api/1/datastore/imports', $data["paths"]);
$this->assertArrayHasKey('/api/1/harvest/plans/{plan_id}', $data["paths"]);
$this->assertArrayHasKey('/api/1/metastore/schemas/{schema_id}', $data["paths"]);
$this->assertArrayHasKey('/api/1/datastore/imports', $data['paths']);
$this->assertArrayHasKey('/api/1/harvest/plans/{plan_id}', $data['paths']);
$this->assertArrayHasKey('/api/1/metastore/schemas/{schema_id}', $data['paths']);
}


public function testGetCompleteYaml() {
$controller = $this->getController();
$response = $controller->getComplete('yaml');
$this->assertEquals(200, $response->getStatusCode());
$data = Yaml::decode($response->getContent(), TRUE);

// Basic auth is included.
$this->assertTrue(isset($data["components"]["securitySchemes"]["basic_auth"]));
$this->assertTrue(isset($data['components']['securitySchemes']['basic_auth']));

// Some sample paths from different modules are there
$this->assertArrayHasKey('/api/1/datastore/imports', $data["paths"]);
$this->assertArrayHasKey('/api/1/harvest/plans/{plan_id}', $data["paths"]);
$this->assertArrayHasKey('/api/1/metastore/schemas/{schema_id}', $data["paths"]);
$this->assertArrayHasKey('/api/1/datastore/imports', $data['paths']);
$this->assertArrayHasKey('/api/1/harvest/plans/{plan_id}', $data['paths']);
$this->assertArrayHasKey('/api/1/metastore/schemas/{schema_id}', $data['paths']);
}

public function testGetNoAuth() {
$controller = $this->getController(["authentication" => "false"]);
$controller = $this->getController(['authentication' => 'false']);
$response = $controller->getComplete();

// Simulate an authentication=false parameter in the request stack.
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getContent(), TRUE);

// Basic auth is excluded.
$this->assertFalse(isset($data["components"]["securitySchemes"]["basic_auth"]));
$this->assertFalse(isset($data['components']['securitySchemes']['basic_auth']));

// Authorized paths are not there.
$this->assertArrayNotHasKey('/api/1/datastore/imports', $data["paths"]);
$this->assertArrayNotHasKey('/api/1/harvest/plans/{plan_id}', $data["paths"]);
$this->assertArrayNotHasKey('/api/1/datastore/imports', $data['paths']);
$this->assertArrayNotHasKey('/api/1/harvest/plans/{plan_id}', $data['paths']);
// Public paths still there.
$this->assertArrayHasKey('/api/1/metastore/schemas/{schema_id}', $data["paths"]);
$this->assertArrayHasKey('/api/1/metastore/schemas/{schema_id}', $data['paths']);
}

private function getController(array $params = []) {
$requestStack = \Drupal::service('request_stack');

if (!empty($params)) {
if ($params) {
/** @var \Symfony\Component\HttpFoundation\RequestStack $requestStack */
$requestStack = $this->container->get('request_stack');
$request = $requestStack->pop()->duplicate($params);
$requestStack->push($request);
}

return new OpenApiController(
$requestStack,
\Drupal::service('dkan.common.docs_generator')
);
return OpenApiController::create($this->container);
}

}