From 61e2a65a28ebdd0fae11e2975be19fb7835bc75e Mon Sep 17 00:00:00 2001
From: Swikriti Tripathi <swikriti808@gmail.com>
Date: Tue, 23 Aug 2022 17:05:12 +0545
Subject: [PATCH] Add api tests for tus upload when quota is set

---
 .../features/apiSpaces/tusUpload.feature      |  23 ++++
 .../features/bootstrap/SpacesContext.php      |   7 +-
 .../features/bootstrap/TusContext.php         | 106 ++++++++++++------
 tests/acceptance/filesForUpload/lorem.txt     |   7 ++
 4 files changed, 103 insertions(+), 40 deletions(-)
 create mode 100644 tests/acceptance/features/apiSpaces/tusUpload.feature
 create mode 100644 tests/acceptance/filesForUpload/lorem.txt

diff --git a/tests/acceptance/features/apiSpaces/tusUpload.feature b/tests/acceptance/features/apiSpaces/tusUpload.feature
new file mode 100644
index 00000000000..1ba9c294845
--- /dev/null
+++ b/tests/acceptance/features/apiSpaces/tusUpload.feature
@@ -0,0 +1,23 @@
+@api @skipOnOcV10
+Feature: upload resources using TUS protocol
+  As a user
+  I want to be able to upload files
+  So that I can store and share files between multiple client systems
+
+    Background:
+      Given these users have been created with default attributes and without skeleton files:
+        | username |
+        | Alice    |
+      And the administrator has given "Alice" the role "Space Admin" using the settings api
+
+
+  Scenario: upload a file within the set quota to a project space 
+    Given user "Alice" has created a space "Project Jupiter" of type "project" with quota "10000"
+    When user "Alice" uploads a file "lorem.txt" via TUS inside of the space "Project Jupiter" using the WebDAV API
+    Then the HTTP status code should be "204"
+  
+ 
+  Scenario: upload a file bigger than the set quota to a project space 
+    Given user "Alice" has created a space "Project Jupiter" of type "project" with quota "10"
+    When user "Alice" creates a new TUS resource "lorem.txt" for the space "Project Jupiter" using the WebDAV API
+    Then the HTTP status code should be "507"
\ No newline at end of file
diff --git a/tests/acceptance/features/bootstrap/SpacesContext.php b/tests/acceptance/features/bootstrap/SpacesContext.php
index 590b085a7c2..c5367412049 100644
--- a/tests/acceptance/features/bootstrap/SpacesContext.php
+++ b/tests/acceptance/features/bootstrap/SpacesContext.php
@@ -466,9 +466,9 @@ public function deleteAllSpacesOfTheType(string $driveType): void {
 	 */
 	public function deleteAllPersonalSpaces(): void {
 		$query = "\$filter=driveType eq personal";
-		$createdUsers= $this->featureContext->getCreatedUsers();
+		$createdUsers = $this->featureContext->getCreatedUsers();
 
-		foreach($createdUsers as $user) {
+		foreach ($createdUsers as $user) {
 			$this->theUserListsAllHisAvailableSpacesUsingTheGraphApi(
 				$user["actualUsername"],
 				$query
@@ -586,7 +586,6 @@ public function sendCreateSpaceRequest(
 		array $headers = []
 	): ResponseInterface {
 		$fullUrl = $this->baseUrl . "/graph/v1.0/drives/";
-
 		return HttpRequestHelper::post($fullUrl, $xRequestId, $user, $password, $headers, $body);
 	}
 
@@ -3027,7 +3026,7 @@ public function forUserSpaceShouldContainLinks(
 		string $fileName = ''
 	): void {
 		$body = '';
-		if (!empty ($fileName)) {
+		if (!empty($fileName)) {
 			$body = $this->getFileId($user, $spaceName, $fileName);
 		} else {
 			$space = $this->getSpaceByName($user, $spaceName);
diff --git a/tests/acceptance/features/bootstrap/TusContext.php b/tests/acceptance/features/bootstrap/TusContext.php
index 03c29f6194e..5b01a659b5f 100644
--- a/tests/acceptance/features/bootstrap/TusContext.php
+++ b/tests/acceptance/features/bootstrap/TusContext.php
@@ -74,47 +74,55 @@ public function before(BeforeScenarioScope $scope): void {
 	 * @throws GuzzleException
 	 */
 	public function uploadFileViaTus(string $user, string $resource, string $spaceName): void {
-		$resourceLocation = $this->getResourceLocation($user, $resource, $spaceName);
-		$file = \fopen($this->acceptanceTestsDirLocation() . $resource, 'r');
-
-		$this->featureContext->setResponse(
-			HttpRequestHelper::sendRequest(
-				$resourceLocation,
-				"",
-				'HEAD',
-				$user,
-				$this->featureContext->getPasswordForUser($user),
-				[],
-				""
-			)
-		);
-		$this->featureContext->theHTTPStatusCodeShouldBe(200, "Expected response status code should be 200");
-
-		$this->featureContext->setResponse(
-			HttpRequestHelper::sendRequest(
-				$resourceLocation,
-				"",
-				'PATCH',
-				$user,
-				$this->featureContext->getPasswordForUser($user),
-				["Tus-Resumable" => "1.0.0", "Upload-Offset" => 0, 'Content-Type' => 'application/offset+octet-stream'],
-				$file
-			)
-		);
+		$this->createTusResource($user, $resource, $spaceName);
+		$this->featureContext->theHTTPStatusCodeShouldBe(201, "Expected response status code should be 201");
+		$this->uploadResourceThroughTUS($user, $resource);
 		$this->featureContext->theHTTPStatusCodeShouldBe(204, "Expected response status code should be 204");
 	}
+	
+	/**
+	 * @When /^user "([^"]*)" uploads a file "([^"]*)" via TUS inside of the space "([^"]*)" using the WebDAV API$/
+	 *
+	 * @param string $user
+	 * @param string $resource
+	 * @param string $spaceName
+	 *
+	 * @return void
+	 *
+	 * @throws Exception
+	 * @throws GuzzleException
+	 */
+	public function userUploadsAFileViaTusInsideOfTheSpaceUsingTheWebdavApi(string $user, string $resource, string $spaceName) {
+		$this->createTusResource($user, $resource, $spaceName);
+		$this->featureContext->theHTTPStatusCodeShouldBe(201, "Expected response status code should be 201");
+		$this->uploadResourceThroughTUS($user, $resource);
+	}
+   
+	/**
+	 * @When /^user "([^"]*)" creates a new TUS resource "([^"]*)" for the space "([^"]*)" using the WebDAV API$/
+	 *
+	 * @param string $user
+	 * @param string $resource
+	 * @param string $spaceName
+	 *
+	 * @return void
+	 * @throws Exception|GuzzleException
+	 */
+	public function userCreatesANewTusResourceForTheSpaceUsingTheWebdavApi(string $user, string $resource, string $spaceName):void {
+		$this->createTusResource($user, $resource, $spaceName);
+	}
 
 	/**
-	 * send POST and return the url of the resource location in the response header
+	 * send POST request to create the TUS resource
 	 *
 	 * @param string $user
 	 * @param string $resource
 	 * @param string $spaceName
 	 *
-	 * @return string
+	 * @return void
 	 * @throws Exception|GuzzleException
 	 */
-	public function getResourceLocation(string $user, string $resource, string $spaceName): string {
+	public function createTusResource(string $user, string $resource, string $spaceName): void {
 		$space = $this->spacesContext->getSpaceByName($user, $spaceName);
 		$fullUrl = $this->baseUrl . "/remote.php/dav/spaces/" . $space["id"];
 
@@ -126,7 +134,6 @@ public function getResourceLocation(string $user, string $resource, string $spac
 			"Upload-Metadata" => $tusEndpoint . ',' . $fileName,
 			"Upload-Length" => filesize($this->acceptanceTestsDirLocation() . $resource)
 		];
-
 		$this->featureContext->setResponse(
 			HttpRequestHelper::post(
 				$fullUrl,
@@ -137,12 +144,39 @@ public function getResourceLocation(string $user, string $resource, string $spac
 				''
 			)
 		);
-		$this->featureContext->theHTTPStatusCodeShouldBe(201, "Expected response status code should be 201");
+	}
 
-		$locationHeader = $this->featureContext->getResponse()->getHeader('Location');
-		if (\sizeof($locationHeader) > 0) {
-			return $locationHeader[0];
+	public function uploadResourceThroughTUS(string $user, string $resource) {
+		$resourceLocation = $this->featureContext->getResponse()->getHeader('Location');
+		if (\sizeof($resourceLocation) > 0) {
+			$resourceLocation = $resourceLocation[0];
+		} else {
+			throw new \Exception(__METHOD__ . " Location header could not be found");
 		}
-		throw new \Exception(__METHOD__ . " Location header could not be found");
+		$file = \fopen($this->acceptanceTestsDirLocation() . $resource, 'r');
+
+		$this->featureContext->setResponse(
+			HttpRequestHelper::sendRequest(
+				$resourceLocation,
+				"",
+				'HEAD',
+				$user,
+				$this->featureContext->getPasswordForUser($user),
+				[],
+				""
+			)
+		);
+		$this->featureContext->theHTTPStatusCodeShouldBe(200, "Expected response status code should be 200");
+		$this->featureContext->setResponse(
+			HttpRequestHelper::sendRequest(
+				$resourceLocation,
+				"",
+				'PATCH',
+				$user,
+				$this->featureContext->getPasswordForUser($user),
+				["Tus-Resumable" => "1.0.0", "Upload-Offset" => 0, 'Content-Type' => 'application/offset+octet-stream'],
+				$file
+			)
+		);
 	}
 }
diff --git a/tests/acceptance/filesForUpload/lorem.txt b/tests/acceptance/filesForUpload/lorem.txt
new file mode 100644
index 00000000000..9d3b7a654c1
--- /dev/null
+++ b/tests/acceptance/filesForUpload/lorem.txt
@@ -0,0 +1,7 @@
+Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium,
+totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae
+dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit,
+sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est
+qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora
+incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum
+exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur?
\ No newline at end of file