Skip to content

Commit

Permalink
Add test for save action, move token test to base.
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronGilMartinez committed Dec 13, 2024
1 parent e1900f2 commit 78d4009
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 30 deletions.
5 changes: 2 additions & 3 deletions tests/src/Kernel/Controller/WopiControllerContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@
use Symfony\Component\HttpFoundation\Response;

/**
* Tests the WopiController content.
* Tests the 'content' action in WopiController.
*
* @see \Drupal\collabora_online\Controller\WopiController::wopiGetFile()
*/
class WopiControllerContentTest extends WopiControllerTestBase {

/**
* Tests a successful response for check file info.
* Tests a successful response.
*/
public function testSuccess(): void {
$request = $this->createRequest(
[
'id' => $this->media->id(),
'action' => 'content',
'access_token' => $this->getAccessToken(),
]
);
Expand Down
27 changes: 1 addition & 26 deletions tests/src/Kernel/Controller/WopiControllerInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Symfony\Component\HttpFoundation\Response;

/**
* Tests the info operation in WopiController.
* Tests the 'info' action in WopiController.
*
* @see \Drupal\collabora_online\Controller\WopiController::wopiCheckFileInfo()
*/
Expand All @@ -30,7 +30,6 @@ public function testSuccess(): void {
$request = $this->createRequest(
[
'id' => $this->media->id(),
'action' => 'info',
'access_token' => $this->getAccessToken(),
]
);
Expand Down Expand Up @@ -65,7 +64,6 @@ public function testSuccessWrite(): void {
$request = $this->createRequest(
[
'id' => $this->media->id(),
'action' => 'info',
'access_token' => $this->getAccessToken(write: TRUE),
]
);
Expand All @@ -92,26 +90,6 @@ public function testSuccessWrite(): void {
);
}

/**
* Tests response with a bad access token.
*/
public function testAccessDeniedToken(): void {
$request = $this->createRequest(
[
'id' => $this->media->id(),
'action' => 'info',
'access_token' => 'a',
]
);

$this->assertResponse(
Response::HTTP_FORBIDDEN,
'Authentication failed.',
'text/plain',
$request
);
}

/**
* Tests response with NULL media.
*/
Expand All @@ -121,7 +99,6 @@ public function testAccessDeniedMedia(): void {
$request = $this->createRequest(
[
'id' => '1',
'action' => 'info',
'access_token' => $this->getAccessToken(),
]
);
Expand All @@ -141,7 +118,6 @@ public function testAccessDeniedUser(): void {
$request = $this->createRequest(
[
'id' => $this->media->id(),
'action' => 'info',
'access_token' => $this->getAccessToken(uid: 5),
]
);
Expand All @@ -164,7 +140,6 @@ public function testAccessDeniedWrite(): void {
$request = $this->createRequest(
[
'id' => $this->media->id(),
'action' => 'info',
'access_token' => $this->getAccessToken(write: TRUE),
]
);
Expand Down
61 changes: 61 additions & 0 deletions tests/src/Kernel/Controller/WopiControllerSaveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* Copyright the Collabora Online contributors.
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

declare(strict_types=1);

namespace Drupal\Tests\collabora_online\Kernel\Controller;

use Symfony\Component\HttpFoundation\Response;

/**
* Tests the 'save' action in WopiController.
*
* @see \Drupal\collabora_online\Controller\WopiController::wopiPutFile()
*/
class WopiControllerSaveTest extends WopiControllerTestBase {

/**
* {@inheritDoc}
*/
const METHOD = 'POST';

/**
* Tests a successful response.
*/
public function testSuccess(): void {
$request = $this->createRequest(
[
'id' => $this->media->id(),
'access_token' => $this->getAccessToken(),
]
);

$mtime = date_create_immutable_from_format('U', (string) $this->file->getChangedTime());

$this->assertResponse(
Response::HTTP_OK,
json_encode([
'LastModifiedTime' => $mtime->format('c'),
]),
$this->file->getMimeType(),
$request
);
}

/**
* {@inheritDoc}
*/
protected function getRequestUri(): string {
return '/cool/wopi/files/' . $this->media->id() . '/contents';
}

}
24 changes: 23 additions & 1 deletion tests/src/Kernel/Controller/WopiControllerTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
use Drupal\Tests\collabora_online\Kernel\CollaboraKernelTestBase;
use Drupal\user\UserInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Base class to test controller responses in Collabora.
* Base class to test Wopi controller requests in Collabora.
*
* Contains structure and common tests.
*/
abstract class WopiControllerTestBase extends CollaboraKernelTestBase {

Expand Down Expand Up @@ -94,6 +97,25 @@ protected function setUp(): void {
$this->setCurrentUser($this->user);
}

/**
* Tests response with a bad access token.
*/
public function testAccessDeniedToken(): void {
$request = $this->createRequest(
[
'id' => $this->media->id(),
'access_token' => 'a',
]
);

$this->assertResponse(
Response::HTTP_FORBIDDEN,
'Authentication failed.',
'text/plain',
$request
);
}

/**
* Retrieves an encoded access token.
*
Expand Down

0 comments on commit 78d4009

Please sign in to comment.