Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
enh(api): indicate missing file owner in create response
Files without an owner such as federated shares

cannot receive attachments

as the attachment would need to be stored in the owners user folder.

Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed May 13, 2025
commit eb8868ce57ec8498e4f39502bb2bf9fa3f0a8a23
3 changes: 3 additions & 0 deletions lib/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ public function create(?int $fileId = null, ?string $filePath = null, ?string $b
$lockInfo = null;
}

$hasOwner = $file->getOwner() !== null;

if (!$readOnly) {
$isLocked = $this->documentService->lock($file->getId());
if (!$isLocked) {
Expand All @@ -155,6 +157,7 @@ public function create(?int $fileId = null, ?string $filePath = null, ?string $b
'content' => $content,
'documentState' => $documentState,
'lock' => $lockInfo,
'hasOwner' => $hasOwner,
]);
}

Expand Down
78 changes: 78 additions & 0 deletions tests/unit/Service/ApiServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace OCA\Text\Tests;

use OCA\Text\Db\Document;
use OCA\Text\Service\ApiService;
use OCA\Text\Service\ConfigService;
use OCA\Text\Service\DocumentService;
use OCA\Text\Service\EncodingService;
use OCA\Text\Service\SessionService;
use OCP\IL10N;
use OCP\IRequest;
use Psr\Log\LoggerInterface;

class ApiServiceTest extends \PHPUnit\Framework\TestCase {
private ApiService $apiService;

private IRequest $request;
private ConfigService $configService;
private SessionService $sessionService;
private DocumentService $documentService;
private EncodingService $encodingService;
private LoggerInterface $loggerInterface;
private IL10N $l10n;
private string $userId;

public function setUp(): void {
$this->request = $this->createMock(IRequest::class);
$this->configService = $this->createMock(ConfigService::class);
$this->sessionService = $this->createMock(SessionService::class);
$this->documentService = $this->createMock(DocumentService::class);
$this->encodingService = $this->createMock(EncodingService::class);
$this->loggerInterface = $this->createMock(LoggerInterface::class);
$this->l10n = $this->createMock(IL10N::class);
$this->userId = 'admin';

$document = new Document();
$document->setId(123);
$this->documentService->method('getDocument')->willReturn($document);
$this->documentService->method('isReadOnly')->willReturn(false);

$this->apiService = new ApiService(
$this->request,
$this->configService,
$this->sessionService,
$this->documentService,
$this->encodingService,
$this->loggerInterface,
$this->l10n,
$this->userId,
null,
);
}

public function testCreateNewSession() {
$file = $this->mockFile(1234, 'admin');
$this->documentService->method('getFileById')->willReturn($file);
$actual = $this->apiService->create(1234);
self::assertTrue($actual->getData()['hasOwner']);
}

public function testCreateNewSessionWithoutOwner() {
$file = $this->mockFile(1234, null);
$this->documentService->method('getFileById')->willReturn($file);
$actual = $this->apiService->create(1234);
self::assertFalse($actual->getData()['hasOwner']);
}

private function mockFile(int $id, ?string $owner) {
$file = $this->createMock(\OCP\Files\File::class);
$storage = $this->createMock(\OCP\Files\Storage\IStorage::class);
$file->method('getStorage')->willReturn($storage);
$file->method('getId')->willReturn($id);
$file->method('getOwner')->willReturn($owner);
return $file;
}

}