Skip to content

Commit

Permalink
fix: properly cast response when uploading a new file version using s…
Browse files Browse the repository at this point in the history
…ession (#810)
  • Loading branch information
mwwoda committed Mar 2, 2022
1 parent 8021b8e commit 73d877f
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 17 deletions.
14 changes: 14 additions & 0 deletions Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions Box.V2.Test/Box.V2.Test.csproj
Expand Up @@ -47,6 +47,9 @@
<None Update="Fixtures\BoxFileRequest\GetFileRequest200.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Fixtures\BoxFiles\UploadNewVersionUsingSession200.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Fixtures\BoxMetadata\ExecuteMetadataQuery200.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
7 changes: 3 additions & 4 deletions Box.V2.Test/BoxFilesManagerTest.cs
Expand Up @@ -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<BoxFile>(It.IsAny<IBoxRequest>()))
.Returns(Task.FromResult<IBoxResponse<BoxFile>>(new BoxResponse<BoxFile>()
Handler.Setup(h => h.ExecuteAsync<BoxCollection<BoxFile>>(It.IsAny<IBoxRequest>()))
.Returns(Task.FromResult<IBoxResponse<BoxCollection<BoxFile>>>(new BoxResponse<BoxCollection<BoxFile>>()
{
Status = ResponseStatus.Success,
ContentString = responseString
ContentString = LoadFixtureFromJson("Fixtures/BoxFiles/UploadNewVersionUsingSession200.json")
}));

var fakeStream = new Mock<System.IO.Stream>();
Expand Down
81 changes: 81 additions & 0 deletions 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
}
22 changes: 9 additions & 13 deletions Box.V2/Managers/BoxFilesManager.cs
Expand Up @@ -367,17 +367,7 @@ public async Task<BoxUploadPartResponse> UploadPartAsync(Uri uploadPartUri, stri
/// <returns> The complete BoxFile object. </returns>
public async Task<BoxFile> 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<BoxCollection<BoxFile>>(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);
}

/// <summary>
Expand All @@ -388,6 +378,11 @@ public async Task<BoxFile> CommitSessionAsync(Uri commitSessionUrl, string sha,
/// <param name="sessionPartsInfo">Parts info for the uploaded parts.</param>
/// <returns> The complete BoxFile object. </returns>
public async Task<BoxFile> CommitFileVersionSessionAsync(Uri commitSessionUrl, string sha, BoxSessionParts sessionPartsInfo)
{
return await CommitSessionInternalAsync(commitSessionUrl, sha, sessionPartsInfo).ConfigureAwait(false);
}

private async Task<BoxFile> CommitSessionInternalAsync(Uri commitSessionUrl, string sha, BoxSessionParts sessionPartsInfo)
{
BoxRequest request = new BoxRequest(commitSessionUrl)
.Method(RequestMethod.Post)
Expand All @@ -396,9 +391,10 @@ public async Task<BoxFile> CommitFileVersionSessionAsync(Uri commitSessionUrl, s

request.ContentType = Constants.RequestParameters.ContentTypeJson;

var response = await ToResponseAsync<BoxFile>(request).ConfigureAwait(false);
var response = await ToResponseAsync<BoxCollection<BoxFile>>(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();
}

/// <summary>
Expand Down

0 comments on commit 73d877f

Please sign in to comment.