Skip to content

Commit

Permalink
fix: Fix initialization of BoxAPIException object (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
arjankowski committed May 24, 2022
1 parent 6167ab5 commit a298f01
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ jobs:
VALIDATE_YAML: false
VALIDATE_JSCPD: false
VALIDATE_POWERSHELL: false
VALIDATE_EDITORCONFIG: false
DEFAULT_BRANCH: main
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
56 changes: 46 additions & 10 deletions Box.V2.Test/BoxFilesManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Box.V2.Exceptions;
using Box.V2.Managers;
using Box.V2.Models;
using Box.V2.Models.Request;
Expand All @@ -27,7 +28,7 @@ public BoxFilesManagerTest()
_filesManager = new BoxFilesManager(Config.Object, Service, Converter, AuthRepository);
}

[TestMethod]
[TestMethod]
public async Task UploadNewVersionUsingSessionAsync_ValidResponse()
{
var fileInMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes("whatever"));
Expand All @@ -42,12 +43,12 @@ public async Task UploadNewVersionUsingSessionAsync_ValidResponse()
}));


Handler.Setup(h => h.ExecuteAsync<BoxCollection<BoxFile>>(It.IsAny<IBoxRequest>()))
.Returns(Task.FromResult<IBoxResponse<BoxCollection<BoxFile>>>(new BoxResponse<BoxCollection<BoxFile>>()
{
Status = ResponseStatus.Success,
ContentString = LoadFixtureFromJson("Fixtures/BoxFiles/UploadNewVersionUsingSession200.json")
}));
Handler.Setup(h => h.ExecuteAsync<BoxCollection<BoxFile>>(It.IsAny<IBoxRequest>()))
.Returns(Task.FromResult<IBoxResponse<BoxCollection<BoxFile>>>(new BoxResponse<BoxCollection<BoxFile>>()
{
Status = ResponseStatus.Success,
ContentString = LoadFixtureFromJson("Fixtures/BoxFiles/UploadNewVersionUsingSession200.json")
}));

var fakeStream = new Mock<System.IO.Stream>();

Expand All @@ -56,7 +57,7 @@ public async Task UploadNewVersionUsingSessionAsync_ValidResponse()
Assert.AreEqual("5000948880", f.Id);
}

[TestMethod]
[TestMethod]
public async Task GetCollaborationsCollectionAsync_ValidResponse_NextMarker()
{
var responseJSON = "{\"next_marker\":\"ZmlsZS0xLTE%3D\",\"previous_marker\":\"\",\"entries\":[{\"type\":\"collaboration\",\"id\":\"11111\",\"created_by\":{\"type\":\"user\",\"id\":\"33333\",\"name\":\"Test User\",\"login\":\"testuser@example.com\"},\"created_at\":\"2019-01-21T07:58:18-08:00\",\"modified_at\":\"2019-01-21T14:49:18-08:00\",\"expires_at\":null,\"status\":\"accepted\",\"accessible_by\":{\"type\":\"user\",\"id\":\"44444\",\"name\":\"Test User 2\",\"login\":\"testuser2@example.com\"},\"role\":\"editor\",\"acknowledged_at\":\"2019-01-21T07:58:18-08:00\",\"item\":{\"type\":\"file\",\"id\":\"22222\",\"file_version\":{\"type\":\"file_version\",\"id\":\"12345\",\"sha1\":\"96619397759a43a01537da34ea3e0bab86b22e9d\"},\"sequence_id\":\"26\",\"etag\":\"26\",\"sha1\":\"96619397759a43a01537da34ea3e0bab86b22e9d\",\"name\":\"Meeting Notes.boxnote\"}}]}";
Expand Down Expand Up @@ -204,7 +205,7 @@ public async Task GetFileInformation_ValidResponse_ValidFile()
Assert.IsFalse(f.IsExternallyOwned.Value);
}

[TestMethod]
[TestMethod]
public async Task UploadFile_ValidResponse_ValidFile()
{
/*** Arrange ***/
Expand Down Expand Up @@ -362,7 +363,7 @@ public async Task UpdateFileInformation_ValidResponse_ValidFile()
Assert.AreEqual("needs review", f.Tags[1]);
}

[TestMethod]
[TestMethod]
public async Task CopyFile_ValidResponse_ValidFile()
{
/*** Arrange ***/
Expand Down Expand Up @@ -798,6 +799,41 @@ public async Task DeleteFile_ValidResponse_FileDeleted()

}

[TestMethod]
public async Task DeleteFile_ErrorResponse_Exception()
{

/*** Arrange ***/
var headers = new HttpResponseMessage().Headers;
Handler.Setup(h => h.ExecuteAsync<BoxFile>(It.IsAny<IBoxRequest>()))
.Returns(Task<IBoxResponse<BoxFile>>.Factory.StartNew(() => new BoxResponse<BoxFile>()
{
StatusCode = System.Net.HttpStatusCode.Forbidden,
Status = ResponseStatus.Forbidden,
Headers = headers,
ContentString = "{\"type\": \"error\", \"status\": 403, \"code\": \"forbidden_by_policy\", \"message\": \"Access denied by Shield policy\", \"request_id\": \"5hr712h2ip6deox0\"}"
}));

/*** Act ***/
try
{
var result = await _filesManager.DeleteAsync("34122832467");

Assert.Fail("Expected delete file throws when delete without permissions");
}
catch (BoxAPIException ex)
{
/*** Assert ***/
Assert.AreEqual(System.Net.HttpStatusCode.Forbidden, ex.StatusCode);
Assert.AreEqual("forbidden_by_policy", ex.ErrorCode);
Assert.AreEqual("Access denied by Shield policy", ex.ErrorDescription);
Assert.AreEqual("5hr712h2ip6deox0", ex.Error.RequestId);
Assert.AreEqual("403", ex.Error.Status);
Assert.AreEqual("forbidden_by_policy", ex.Error.Code);
Assert.AreEqual("Access denied by Shield policy", ex.Error.Message);
}
}

[TestMethod]
public async Task DownloadStream_ValidResponse_ValidStream()
{
Expand Down
2 changes: 1 addition & 1 deletion Box.V2/Exceptions/BoxException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected internal BoxAPIException(string message, BoxError error, HttpStatusCod
protected internal static BoxAPIException GetResponseException<T>(string message, IBoxResponse<T> response) where T : class
{
var error = GetResponseError(response);
return new BoxAPIException(GetErrorMessage(message, response, error), response.Error, response.StatusCode, response.Headers);
return new BoxAPIException(GetErrorMessage(message, response, error), response.Error ?? error, response.StatusCode, response.Headers);
}

protected internal static BoxError GetResponseError<T>(IBoxResponse<T> response) where T : class
Expand Down

0 comments on commit a298f01

Please sign in to comment.