Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulius Härtl <jus@bitgrid.net>2019-11-14 18:19:58 +0300
committerJulius Härtl <jus@bitgrid.net>2019-11-27 16:36:20 +0300
commitc8d3e32190d8672c9fc42cd30a63182623006431 (patch)
tree3f9cdcdae66c437a90b784cd577e129a33cd17a4 /tests/lib/DirectEditing
parent103c6fb39eab36063b16767abb33da0395b2b9c8 (diff)
Direct editing token tests
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'tests/lib/DirectEditing')
-rw-r--r--tests/lib/DirectEditing/ManagerTest.php152
1 files changed, 152 insertions, 0 deletions
diff --git a/tests/lib/DirectEditing/ManagerTest.php b/tests/lib/DirectEditing/ManagerTest.php
new file mode 100644
index 00000000000..b2e58efd8e2
--- /dev/null
+++ b/tests/lib/DirectEditing/ManagerTest.php
@@ -0,0 +1,152 @@
+<?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\AppFramework\Http\TemplateResponse;
+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 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';
+ }
+}
+
+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 \PHPUnit\Framework\MockObject\MockObject
+ */
+ private $random;
+
+ 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);
+ }
+
+}