From 41a8f45b93a55fe3e72f0700c15ac04db498d839 Mon Sep 17 00:00:00 2001
From: Roeland Jago Douma <roeland@famdouma.nl>
Date: Fri, 22 May 2015 14:42:57 +0200
Subject: OCS Share API link shares now always have an url

To make sure clients have the correct and latest link add an url field
to all OCS Share API return statements on public link shares.

* Added unit tests
---
 apps/files_sharing/api/local.php | 10 +++++++++
 apps/files_sharing/tests/api.php | 45 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/apps/files_sharing/api/local.php b/apps/files_sharing/api/local.php
index 03812800c17..706cac24ff9 100644
--- a/apps/files_sharing/api/local.php
+++ b/apps/files_sharing/api/local.php
@@ -70,6 +70,10 @@ class Local {
 					}
 					$share['icon'] = substr(\OC_Helper::mimetypeIcon($share['mimetype']), 0, -3) . 'svg';
 				}
+
+				if (!is_null($share['token'])) {
+					$share['url'] = \OC::$server->getURLGenerator()->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share['token']]);
+				}
 			}
 			return new \OC_OCS_Result($shares);
 		}
@@ -142,6 +146,12 @@ class Local {
 		if ($shares === null || empty($shares)) {
 			return new \OC_OCS_Result(null, 404, 'share doesn\'t exist');
 		} else {
+			foreach ($shares as &$share) {
+				if (!is_null($share['token'])) {
+					$share['url'] = \OC::$server->getURLGenerator()->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share['token']]);
+				}
+			}
+
 			return new \OC_OCS_Result($shares);
 		}
 	}
diff --git a/apps/files_sharing/tests/api.php b/apps/files_sharing/tests/api.php
index 44c6b1dc4b9..f203a4e3f81 100644
--- a/apps/files_sharing/tests/api.php
+++ b/apps/files_sharing/tests/api.php
@@ -307,6 +307,51 @@ class Test_Files_Sharing_Api extends TestCase {
 				\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
 	}
 
+	/**
+	 * @medium
+	 * @depends testCreateShare
+	 */
+	function testPublicLinkUrl() {
+		// simulate a post request
+		$_POST['path'] = $this->folder;
+		$_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
+
+		$result = \OCA\Files_Sharing\API\Local::createShare([]);
+		$this->assertTrue($result->succeeded());
+		$data = $result->getData();
+
+		// check if we have a token
+		$this->assertTrue(is_string($data['token']));
+		$id = $data['id'];
+
+		// check for correct link
+		$url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
+		$this->assertEquals($url, $data['url']);
+
+		// check for link in getall shares
+		$result = \OCA\Files_Sharing\API\Local::getAllShares([]);
+		$this->assertTrue($result->succeeded());
+		$data = $result->getData();
+		$this->assertEquals($url, current($data)['url']);
+
+		// check for path
+		$_GET['path'] = $this->folder;
+		$result = \OCA\Files_Sharing\API\Local::getAllShares([]);
+		$this->assertTrue($result->succeeded());
+		$data = $result->getData();
+		$this->assertEquals($url, current($data)['url']);
+
+		// check in share id
+		$result = \OCA\Files_Sharing\API\Local::getShare(['id' => $id]);
+		$this->assertTrue($result->succeeded());
+		$data = $result->getData();
+		$this->assertEquals($url, current($data)['url']);
+
+		//Clean up share
+		$fileinfo = $this->view->getFileInfo($this->folder);
+		\OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
+	}
+
 	/**
 	 * @medium
 	 * @depends testCreateShare
-- 
cgit v1.2.3