Skip to content

Commit

Permalink
Add support for getting User Avatar (#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Willer authored Feb 1, 2019
1 parent 1c05d83 commit fc44cb8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
2 changes: 0 additions & 2 deletions Box.V2.Test/BoxFilesManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -605,14 +605,12 @@ public async Task GetThumbnail_ValidResponse_ValidStream()
{
Status = ResponseStatus.Success,
ResponseObject = thumb

}));

/*** Act ***/
Stream result = await _filesManager.GetThumbnailAsync("34122832467");

/*** Assert ***/

Assert.IsNotNull(result, "Stream is Null");

}
Expand Down
35 changes: 33 additions & 2 deletions Box.V2.Test/BoxUsersManagerTest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Box.V2.Managers;
using Box.V2.Managers;
using Box.V2.Models;
using Box.V2.Models.Request;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

Expand Down Expand Up @@ -547,5 +548,35 @@ public async Task GetMembershipsForUser_ValidResponse_ValidFolder()
Assert.AreEqual("119720", result.Entries[0].Group.Id);
Assert.AreEqual("member", result.Entries[0].Role);
}

[TestMethod]
[TestCategory("CI-UNIT-TEST")]
public async Task GetUserAvatar_ValidResponse_ValidStream()
{
byte[] avatarBytes = { 1, 2, 3 };
IBoxRequest boxRequest = null;
using (var avatar = new MemoryStream(avatarBytes))
{
/*** Arrange ***/

Handler.Setup(h => h.ExecuteAsync<Stream>(It.IsAny<IBoxRequest>()))

.Returns(Task.FromResult<IBoxResponse<Stream>>(new BoxResponse<Stream>()
{
Status = ResponseStatus.Success,
ResponseObject = avatar
}))
.Callback<IBoxRequest>(r => boxRequest = r);

/*** Act ***/
Stream result = await _usersManager.GetUserAvatar("88888");

/*** Assert ***/
Assert.AreEqual(new Uri("https://api.box.com/2.0/users/88888/avatar"), boxRequest.AbsoluteUri);
Assert.IsNotNull(result, "Stream is Null");
Assert.AreEqual(3, result.Length);

}
}
}
}
}
17 changes: 16 additions & 1 deletion Box.V2/Managers/BoxUsersManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Box.V2.Auth;
using Box.V2.Auth;
using Box.V2.Config;
using Box.V2.Converter;
using Box.V2.Extensions;
Expand All @@ -7,6 +7,7 @@
using Box.V2.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Box.V2.Managers
Expand Down Expand Up @@ -327,5 +328,19 @@ public async Task<BoxCollection<BoxGroupMembership>> GetMembershipsForUserAsync(
return response.ResponseObject;
}
}

/// <summary>
/// Retrieves a user's avatar image.
/// </summary>
/// <param name="userId">The Id of the user.</param>
/// <returns>A stream of the bytes for the user's avatar image.</returns>
public async Task<Stream> GetUserAvatar(string userId)
{
var request = new BoxRequest(_config.UserEndpointUri, userId + "/avatar")
.Method(RequestMethod.Get);

IBoxResponse<Stream> response = await ToResponseAsync<Stream>(request).ConfigureAwait(false);
return response.ResponseObject;
}
}
}

0 comments on commit fc44cb8

Please sign in to comment.