summaryrefslogtreecommitdiffstats
path: root/tests/lib
diff options
context:
space:
mode:
authorRoeland Jago Douma <rullzer@users.noreply.github.com>2019-11-28 08:49:57 +0100
committerGitHub <noreply@github.com>2019-11-28 08:49:57 +0100
commit4173d9d74993bfae8d9c7dce5b983cad1b133751 (patch)
treebc234c62e53fe95d7ae4ec0d90507526f1a7f2a8 /tests/lib
parent62dc32019146bdc186e110bde23dd210633acbe7 (diff)
parentbde624b07423de4a6b9e3aaae6371cd4f886c2de (diff)
downloadnextcloud-server-4173d9d74993bfae8d9c7dce5b983cad1b133751.tar.gz
nextcloud-server-4173d9d74993bfae8d9c7dce5b983cad1b133751.zip
Merge pull request #17625 from nextcloud/enh/noid/direct-editing
Direct editing API to allow file editing using a one-time token
Diffstat (limited to 'tests/lib')
-rw-r--r--tests/lib/DirectEditing/ManagerTest.php172
1 files changed, 172 insertions, 0 deletions
diff --git a/tests/lib/DirectEditing/ManagerTest.php b/tests/lib/DirectEditing/ManagerTest.php
new file mode 100644
index 00000000000..9a56c3307e0
--- /dev/null
+++ b/tests/lib/DirectEditing/ManagerTest.php
@@ -0,0 +1,172 @@
+<?php
+
+namespace Test\DirectEditing;
+
+use OC\DirectEditing\Manager;
+use OC\Files\Node\File;
+use OCP\AppFramework\Http\DataResponse;
+use OCP\AppFramework\Http\NotFoundResponse;
+use OCP\AppFramework\Http\Response;
+use OCP\DirectEditing\ACreateEmpty;
+use OCP\DirectEditing\IEditor;
+use OCP\DirectEditing\IToken;
+use OCP\Files\Folder;
+use OCP\Files\IRootFolder;
+use OCP\IDBConnection;
+use OCP\IUserSession;
+use OCP\Security\ISecureRandom;
+use PHPUnit\Framework\MockObject\MockObject;
+use Test\TestCase;
+
+class CreateEmpty extends ACreateEmpty {
+
+ public function getId(): string {
+ return 'createEmpty';
+ }
+
+ public function getName(): string {
+ return 'create empty file';
+ }
+
+ public function getExtension(): string {
+ return '.txt';
+ }
+
+ public function getMimetype(): string {
+ return 'text/plain';
+ }
+}
+
+class Editor implements IEditor {
+
+ public function getId(): string {
+ return 'testeditor';
+ }
+
+ public function getName(): string {
+ return 'Test editor';
+ }
+
+ public function getMimetypes(): array {
+ return [ 'text/plain' ];
+ }
+
+
+ public function getMimetypesOptional(): array {
+ return [];
+ }
+
+ public function getCreators(): array {
+ return [
+ new CreateEmpty()
+ ];
+ }
+
+ public function isSecure(): bool {
+ return false;
+ }
+
+
+ public function open(IToken $token): Response {
+ return new DataResponse('edit page');
+ }
+}
+
+/**
+ * Class ManagerTest
+ *
+ * @package Test\DirectEditing
+ * @group DB
+ */
+class ManagerTest extends TestCase {
+
+ private $manager;
+ /**
+ * @var Editor
+ */
+ private $editor;
+ /**
+ * @var MockObject|ISecureRandom
+ */
+ private $random;
+ /**
+ * @var IDBConnection
+ */
+ private $connection;
+ /**
+ * @var MockObject|IUserSession
+ */
+ private $userSession;
+ /**
+ * @var MockObject|IRootFolder
+ */
+ private $rootFolder;
+ /**
+ * @var MockObject|Folder
+ */
+ private $userFolder;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->editor = new Editor();
+
+ $this->random = $this->createMock(ISecureRandom::class);
+ $this->connection = \OC::$server->getDatabaseConnection();
+ $this->userSession = $this->createMock(IUserSession::class);
+ $this->rootFolder = $this->createMock(IRootFolder::class);
+ $this->userFolder = $this->createMock(Folder::class);
+
+
+ $this->rootFolder->expects($this->any())
+ ->method('getUserFolder')
+ ->willReturn($this->userFolder);
+
+ $this->manager = new Manager(
+ $this->random, $this->connection, $this->userSession, $this->rootFolder
+ );
+
+ $this->manager->registerDirectEditor($this->editor);
+ }
+
+ public function testEditorRegistration() {
+ $this->assertEquals($this->manager->getEditors(), ['testeditor' => $this->editor]);
+ }
+
+
+ public function testCreateToken() {
+ $expectedToken = 'TOKEN' . time();
+ $file = $this->createMock(File::class);
+ $file->expects($this->any())
+ ->method('getId')
+ ->willReturn(123);
+ $this->random->expects($this->once())
+ ->method('generate')
+ ->willReturn($expectedToken);
+ $this->userFolder->expects($this->once())
+ ->method('newFile')
+ ->willReturn($file);
+ $token = $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
+ $this->assertEquals($token, $expectedToken);
+ }
+
+ public function testCreateTokenAccess() {
+ $expectedToken = 'TOKEN' . time();
+ $file = $this->createMock(File::class);
+ $file->expects($this->any())
+ ->method('getId')
+ ->willReturn(123);
+ $this->random->expects($this->once())
+ ->method('generate')
+ ->willReturn($expectedToken);
+ $this->userFolder->expects($this->once())
+ ->method('newFile')
+ ->willReturn($file);
+ $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
+ $firstResult = $this->manager->edit($expectedToken);
+ $secondResult = $this->manager->edit($expectedToken);
+ $this->assertInstanceOf(DataResponse::class, $firstResult);
+ $this->assertInstanceOf(NotFoundResponse::class, $secondResult);
+ }
+
+}