Skip to content

Commit

Permalink
fix identation and improve documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronGilMartinez committed Dec 13, 2024
1 parent 11fa6fd commit 37f36d1
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 108 deletions.
12 changes: 5 additions & 7 deletions tests/src/Kernel/Controller/WopiControllerContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@
class WopiControllerContentTest extends WopiControllerTestBase {

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

$this->assertResponse(
Response::HTTP_OK,
Expand Down
50 changes: 21 additions & 29 deletions tests/src/Kernel/Controller/WopiControllerInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class WopiControllerInfoTest extends WopiControllerTestBase {

/**
* Tests a successful response.
* Tests a request with correct parameters.
*/
public function testSuccess(): void {
$request = $this->createRequest(
Expand Down Expand Up @@ -58,15 +58,13 @@ public function testSuccess(): void {
}

/**
* Tests a successful response with write operation.
* Tests a request with correct parameters using write permission in payload.
*/
public function testSuccessWrite(): void {
$request = $this->createRequest(
[
'id' => $this->media->id(),
'access_token' => $this->getAccessToken(write: TRUE),
]
);
$request = $this->createRequest([
'id' => $this->media->id(),
'access_token' => $this->getAccessToken(write: TRUE),
]);

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

Expand All @@ -91,48 +89,42 @@ public function testSuccessWrite(): void {
}

/**
* Tests response with NULL media.
* Tests a request using deleted media.
*/
public function testAccessDeniedMedia(): void {
$this->media->delete();

$request = $this->createRequest(
[
'id' => '1',
'access_token' => $this->getAccessToken(),
]
);
$request = $this->createRequest([
'id' => '1',
'access_token' => $this->getAccessToken(),
]);

$this->assertAccessDeniedResponse($request);
}

/**
* Tests response with a bad UID.
* Tests a request using bad UID in payload.
*/
public function testAccessDeniedUser(): void {
$request = $this->createRequest(
[
'id' => $this->media->id(),
'access_token' => $this->getAccessToken(uid: 5),
]
);
$request = $this->createRequest([
'id' => $this->media->id(),
'access_token' => $this->getAccessToken(uid: 5),
]);

$this->assertAccessDeniedResponse($request);
}

/**
* Tests response with rwite operation and without permission to edit.
* Tests a request with write operation and without permission to edit.
*/
public function testAccessDeniedWrite(): void {
$this->user = $this->createUser(['access content']);
$this->setCurrentUser($this->user);

$request = $this->createRequest(
[
'id' => $this->media->id(),
'access_token' => $this->getAccessToken(write: TRUE),
]
);
$request = $this->createRequest([
'id' => $this->media->id(),
'access_token' => $this->getAccessToken(write: TRUE),
]);

$this->assertAccessDeniedResponse($request);
$this->assertTrue($this->logger->hasRecord('Token and user permissions do not match.'), 'error');
Expand Down
111 changes: 51 additions & 60 deletions tests/src/Kernel/Controller/WopiControllerSaveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,130 +29,121 @@ class WopiControllerSaveTest extends WopiControllerTestBase {
const METHOD = 'POST';

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

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

$this->assertResponse(
Response::HTTP_OK,
json_encode([
'LastModifiedTime' => $mtime->format('c'),
]),
'application/json',
$request
Response::HTTP_OK,
json_encode([
'LastModifiedTime' => $mtime->format('c'),
]),
'application/json',
$request
);
$this->assertTrue($this->logger->hasRecord('Save reason: Saved by Collabora Online'), 'error');
}

/**
* Tests a successful response with timestamp in the header.
* Tests a request with correct parameters and timestamp value in the header.
*/
public function testSuccessTimestamp(): void {
$request = $this->createRequest(
[
'id' => $this->media->id(),
'access_token' => $this->getAccessToken(write: TRUE),
]
);
$request = $this->createRequest([
'id' => $this->media->id(),
'access_token' => $this->getAccessToken(write: TRUE),
]);

$mtime = date_create_immutable_from_format('U', (string) $this->file->getChangedTime());
$request->headers->set('x-cool-wopi-timestamp', $mtime->format(\DateTimeInterface::ATOM));

$this->assertResponse(
Response::HTTP_OK,
json_encode([
'LastModifiedTime' => $mtime->format('c'),
]),
'application/json',
$request
Response::HTTP_OK,
json_encode([
'LastModifiedTime' => $mtime->format('c'),
]),
'application/json',
$request
);
$this->assertTrue($this->logger->hasRecord('Save reason: Saved by Collabora Online'), 'error');
}

/**
* Tests a successful response adding all reasons in the log entry.
* Tests a request with correct parameters and all parameters in header.
*/
public function testSuccessLogReasons(): void {
$request = $this->createRequest(
[
'id' => $this->media->id(),
'access_token' => $this->getAccessToken(write: TRUE),
]
);
public function testSuccessAllParameters(): void {
$request = $this->createRequest([
'id' => $this->media->id(),
'access_token' => $this->getAccessToken(write: TRUE),
]);

$mtime = date_create_immutable_from_format('U', (string) $this->file->getChangedTime());
$request->headers->set('x-cool-wopi-timestamp', $mtime->format(format: \DateTimeInterface::ATOM));
$request->headers->set('x-cool-wopi-ismodifiedbyuser', 'true');
$request->headers->set('x-cool-wopi-isautosave', 'true');
$request->headers->set('x-cool-wopi-isexitsave', 'true');

$this->assertResponse(
Response::HTTP_OK,
json_encode([
'LastModifiedTime' => $mtime->format('c'),
]),
Response::HTTP_OK,
json_encode([
'LastModifiedTime' => $mtime->format('c'),
]),
'application/json',
$request
);
$this->assertTrue($this->logger->hasRecord('Save reason: Saved by Collabora Online (Modified by user, Autosaved, Save on Exit)'), 'error');
}

/**
* Tests response with conflicting timestamp.
* Tests a request with conflicting timestamp.
*/
public function testConflictFile(): void {
$request = $this->createRequest(
[
'id' => $this->media->id(),
'access_token' => $this->getAccessToken(write: TRUE),
]
);
$request = $this->createRequest([
'id' => $this->media->id(),
'access_token' => $this->getAccessToken(write: TRUE),
]);

// Set a time in the future to force the error.
$mtime = date_create_immutable_from_format('U', (string) ($this->file->getChangedTime() + 1000));
$request->headers->set('x-cool-wopi-timestamp', $mtime->format(\DateTimeInterface::ATOM));

$this->assertResponse(
Response::HTTP_CONFLICT,
json_encode(['COOLStatusCode' => 1010]),
json_encode([
'COOLStatusCode' => 1010,
]),
'application/json',
$request
);
}

/**
* Tests response with NULL media.
* Tests a request using deleted media.
*/
public function testAccessDeniedMedia(): void {
$this->media->delete();

$request = $this->createRequest(
[
'id' => '1',
'access_token' => $this->getAccessToken(write: TRUE),
]
);
$request = $this->createRequest([
'id' => '1',
'access_token' => $this->getAccessToken(write: TRUE),
]);

$this->assertAccessDeniedResponse($request);
}

/**
* Tests response with a bad UID.
* Tests a request using bad UID in payload.
*/
public function testAccessDeniedUser(): void {
$request = $this->createRequest(
[
'id' => $this->media->id(),
'access_token' => $this->getAccessToken(write: TRUE, uid: 5),
]
);
$request = $this->createRequest([
'id' => $this->media->id(),
'access_token' => $this->getAccessToken(write: TRUE, uid: 5),
]);

$this->assertAccessDeniedResponse($request);
}
Expand Down
25 changes: 13 additions & 12 deletions tests/src/Kernel/Controller/WopiControllerTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,10 @@ protected function setUp(): void {
* Tests response with a bad access token.
*/
public function testAccessDeniedToken(): void {
$request = $this->createRequest(
[
'id' => $this->media->id(),
'access_token' => 'a',
]
);
$request = $this->createRequest([
'id' => $this->media->id(),
'access_token' => 'a',
]);

$this->assertResponse(
Response::HTTP_FORBIDDEN,
Expand Down Expand Up @@ -134,12 +132,15 @@ protected function getAccessToken(?int $fid = NULL, ?int $uid = NULL, bool $writ
$transcoder = \Drupal::service(JwtTranscoderInterface::class);
$expire_timestamp = gettimeofday(TRUE) + 1000;

return $transcoder->encode([
'fid' => $fid ?? $this->file->id(),
'uid' => $uid ?? $this->user->id(),
'wri' => $write,
'exp' => $expire_timestamp,
], $expire_timestamp);
return $transcoder->encode(
[
'fid' => $fid ?? $this->file->id(),
'uid' => $uid ?? $this->user->id(),
'wri' => $write,
'exp' => $expire_timestamp,
],
$expire_timestamp
);
}

/**
Expand Down

0 comments on commit 37f36d1

Please sign in to comment.