From 73d877ff679b5999ea50cdfa68f14b0e2169ea65 Mon Sep 17 00:00:00 2001 From: Mateusz Woda Date: Wed, 2 Mar 2022 09:35:18 +0100 Subject: [PATCH] fix: properly cast response when uploading a new file version using session (#810) --- .../BoxFilesManagerIntegrationTest.cs | 14 ++++ Box.V2.Test/Box.V2.Test.csproj | 3 + Box.V2.Test/BoxFilesManagerTest.cs | 7 +- .../UploadNewVersionUsingSession200.json | 81 +++++++++++++++++++ Box.V2/Managers/BoxFilesManager.cs | 22 +++-- 5 files changed, 110 insertions(+), 17 deletions(-) create mode 100644 Box.V2.Test/Fixtures/BoxFiles/UploadNewVersionUsingSession200.json diff --git a/Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs b/Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs index 76777c661..b0c5f39ba 100644 --- a/Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs +++ b/Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs @@ -235,6 +235,20 @@ public async Task UploadBigFileInSession_ShouldUploadTheFile_OnlyIfCommitIsCalle await DeleteFile(uploadedFile.Id); } + [TestMethod] + public async Task UploadNewVersionOfBigFileInSession_ShouldUploadNewVersionOfFile_WhenFileAlreadyExists() + { + var file = await CreateSmallFile(); + + long fileSize = 20000000; + MemoryStream fileInMemoryStream = CreateFileInMemoryStream(fileSize); + + var response = await UserClient.FilesManager.UploadNewVersionUsingSessionAsync(fileInMemoryStream, file.Id); + + Assert.AreEqual(file.Id, response.Id); + Assert.AreNotEqual(file.FileVersion.Id, response.FileVersion.Id); + } + private int GetNumberOfParts(long totalSize, long partSize) { if (partSize == 0) diff --git a/Box.V2.Test/Box.V2.Test.csproj b/Box.V2.Test/Box.V2.Test.csproj index 0a104fe73..a55c3abaa 100644 --- a/Box.V2.Test/Box.V2.Test.csproj +++ b/Box.V2.Test/Box.V2.Test.csproj @@ -47,6 +47,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/Box.V2.Test/BoxFilesManagerTest.cs b/Box.V2.Test/BoxFilesManagerTest.cs index b6d6dc63c..640635ef3 100644 --- a/Box.V2.Test/BoxFilesManagerTest.cs +++ b/Box.V2.Test/BoxFilesManagerTest.cs @@ -42,12 +42,11 @@ public async Task UploadNewVersionUsingSessionAsync_ValidResponse() })); - var responseString = "{ \"type\": \"file\", \"id\": \"5000948880\", \"sequence_id\": \"3\", \"etag\": \"3\", \"sha1\": \"134b65991ed521fcfe4724b7d814ab8ded5185dc\", \"name\": \"new name.jpg\", \"description\": \"a picture of tigers\", \"size\": 629644, \"path_collection\": { \"total_count\": 2, \"entries\": [ { \"type\": \"folder\", \"id\": \"0\", \"sequence_id\": null, \"etag\": null, \"name\": \"All Files\" }, { \"type\": \"folder\", \"id\": \"11446498\", \"sequence_id\": \"1\", \"etag\": \"1\", \"name\": \"Pictures\" } ] }, \"created_at\": \"2012-12-12T10:55:30-08:00\", \"modified_at\": \"2012-12-12T11:04:26-08:00\", \"created_by\": { \"type\": \"user\", \"id\": \"17738362\", \"name\": \"sean rose\", \"login\": \"sean@box.com\" }, \"modified_by\": { \"type\": \"user\", \"id\": \"17738362\", \"name\": \"sean rose\", \"login\": \"sean@box.com\" }, \"owned_by\": { \"type\": \"user\", \"id\": \"17738362\", \"name\": \"sean rose\", \"login\": \"sean@box.com\" }, \"shared_link\": { \"url\": \"https://www.box.com/s/rh935iit6ewrmw0unyul\", \"download_url\": \"https://www.box.com/shared/static/rh935iit6ewrmw0unyul.jpeg\", \"vanity_url\": null, \"is_password_enabled\": false, \"unshared_at\": null, \"download_count\": 0, \"preview_count\": 0, \"access\": \"open\", \"permissions\": { \"can_download\": true, \"can_preview\": true } }, \"parent\": { \"type\": \"folder\", \"id\": \"11446498\", \"sequence_id\": \"1\", \"etag\": \"1\", \"name\": \"Pictures\" }, \"item_status\": \"active\", \"tags\": [ \"important\", \"needs review\" ] }"; - Handler.Setup(h => h.ExecuteAsync(It.IsAny())) - .Returns(Task.FromResult>(new BoxResponse() + Handler.Setup(h => h.ExecuteAsync>(It.IsAny())) + .Returns(Task.FromResult>>(new BoxResponse>() { Status = ResponseStatus.Success, - ContentString = responseString + ContentString = LoadFixtureFromJson("Fixtures/BoxFiles/UploadNewVersionUsingSession200.json") })); var fakeStream = new Mock(); diff --git a/Box.V2.Test/Fixtures/BoxFiles/UploadNewVersionUsingSession200.json b/Box.V2.Test/Fixtures/BoxFiles/UploadNewVersionUsingSession200.json new file mode 100644 index 000000000..90ef75e8e --- /dev/null +++ b/Box.V2.Test/Fixtures/BoxFiles/UploadNewVersionUsingSession200.json @@ -0,0 +1,81 @@ +{ + "entries": [ + { + "id": 5000948880, + "etag": 1, + "type": "file", + "sequence_id": 3, + "name": "Contract.pdf", + "sha1": "85136C79CBF9FE36BB9D05D0639C70C265C18D37", + "file_version": { + "id": 12345, + "type": "file_version", + "sha1": "134b65991ed521fcfe4724b7d814ab8ded5185dc" + }, + "description": "Contract for Q1 renewal", + "size": 629644, + "path_collection": { + "total_count": 1, + "entries": [ + { + "id": 12345, + "etag": 1, + "type": "folder", + "sequence_id": 3, + "name": "Contracts" + } + ] + }, + "created_at": "2012-12-12T10:53:43-08:00", + "modified_at": "2012-12-12T10:53:43-08:00", + "trashed_at": "2012-12-12T10:53:43-08:00", + "purged_at": "2012-12-12T10:53:43-08:00", + "content_created_at": "2012-12-12T10:53:43-08:00", + "content_modified_at": "2012-12-12T10:53:43-08:00", + "created_by": { + "id": 11446498, + "type": "user", + "name": "Aaron Levie", + "login": "ceo@example.com" + }, + "modified_by": { + "id": 11446498, + "type": "user", + "name": "Aaron Levie", + "login": "ceo@example.com" + }, + "owned_by": { + "id": 11446498, + "type": "user", + "name": "Aaron Levie", + "login": "ceo@example.com" + }, + "shared_link": { + "url": "https://www.box.com/s/vspke7y05sb214wjokpk", + "download_url": "https://www.box.com/shared/static/rh935iit6ewrmw0unyul.jpeg", + "vanity_url": "https://acme.app.box.com/v/my_url/", + "vanity_name": "my_url", + "access": "open", + "effective_access": "company", + "effective_permission": "can_download", + "unshared_at": "2018-04-13T13:53:23-07:00", + "is_password_enabled": true, + "permissions": { + "can_download": true, + "can_preview": true + }, + "download_count": 3, + "preview_count": 3 + }, + "parent": { + "id": 12345, + "etag": 1, + "type": "folder", + "sequence_id": 3, + "name": "Contracts" + }, + "item_status": "active" + } + ], + "total_count": 1 +} diff --git a/Box.V2/Managers/BoxFilesManager.cs b/Box.V2/Managers/BoxFilesManager.cs index 8cecc6ea2..918285543 100644 --- a/Box.V2/Managers/BoxFilesManager.cs +++ b/Box.V2/Managers/BoxFilesManager.cs @@ -367,17 +367,7 @@ public async Task UploadPartAsync(Uri uploadPartUri, stri /// The complete BoxFile object. public async Task CommitSessionAsync(Uri commitSessionUrl, string sha, BoxSessionParts sessionPartsInfo) { - BoxRequest request = new BoxRequest(commitSessionUrl) - .Method(RequestMethod.Post) - .Header(Constants.RequestParameters.Digest, "sha=" + sha) - .Payload(_converter.Serialize(sessionPartsInfo)); - - request.ContentType = Constants.RequestParameters.ContentTypeJson; - - var response = await ToResponseAsync>(request).ConfigureAwait(false); - - // We can only commit one file at a time, so return the first entry - return response.ResponseObject.Entries.FirstOrDefault(); + return await CommitSessionInternalAsync(commitSessionUrl, sha, sessionPartsInfo).ConfigureAwait(false); } /// @@ -388,6 +378,11 @@ public async Task CommitSessionAsync(Uri commitSessionUrl, string sha, /// Parts info for the uploaded parts. /// The complete BoxFile object. public async Task CommitFileVersionSessionAsync(Uri commitSessionUrl, string sha, BoxSessionParts sessionPartsInfo) + { + return await CommitSessionInternalAsync(commitSessionUrl, sha, sessionPartsInfo).ConfigureAwait(false); + } + + private async Task CommitSessionInternalAsync(Uri commitSessionUrl, string sha, BoxSessionParts sessionPartsInfo) { BoxRequest request = new BoxRequest(commitSessionUrl) .Method(RequestMethod.Post) @@ -396,9 +391,10 @@ public async Task CommitFileVersionSessionAsync(Uri commitSessionUrl, s request.ContentType = Constants.RequestParameters.ContentTypeJson; - var response = await ToResponseAsync(request).ConfigureAwait(false); + var response = await ToResponseAsync>(request).ConfigureAwait(false); - return response.ResponseObject; + // We can only commit one file at a time, so return the first entry + return response.ResponseObject.Entries.FirstOrDefault(); } ///