Skip to content

Commit

Permalink
Add support for filtering when getting Groups (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
sujaygarlanka authored Oct 16, 2020
1 parent b1f88f3 commit 2874ba7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
18 changes: 14 additions & 4 deletions Box.V2.Test/BoxGroupsManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,35 @@ public BoxGroupsManagerTest()
[TestCategory("CI-UNIT-TEST")]
public async Task GetGroupItems_ValidResponse_ValidGroups()
{
IBoxRequest boxRequest = null;
Handler.Setup(h => h.ExecuteAsync<BoxCollection<BoxGroup>>(It.IsAny<IBoxRequest>()))
.Returns(() => Task.FromResult<IBoxResponse<BoxCollection<BoxGroup>>>(new BoxResponse<BoxCollection<BoxGroup>>()
{
Status = ResponseStatus.Success,
ContentString = @"{""total_count"": 14,
""entries"": [ {""type"": ""group"", ""id"": ""26477"", ""name"": ""adfasdf"", ""created_at"": ""2011-02-15T14:07:22-08:00"", ""modified_at"": ""2011-10-05T19:04:40-07:00""},
{""type"": ""group"", ""id"": ""1263"", ""name"": ""Enterprise Migration"", ""created_at"": ""2009-04-20T19:36:17-07:00"", ""modified_at"": ""2011-10-05T19:05:10-07:00""}],
""entries"": [ {""type"": ""group"", ""id"": ""26477"", ""name"": ""adfasdf"", ""created_at"": ""2011-02-15T14:07:22-08:00"", ""modified_at"": ""2011-10-05T19:04:40-07:00"", ""invitability_level"": ""none""},
{""type"": ""group"", ""id"": ""1263"", ""name"": ""Enterprise Migration"", ""created_at"": ""2009-04-20T19:36:17-07:00"", ""modified_at"": ""2011-10-05T19:05:10-07:00"", ""invitability_level"": ""admins_only""}],
""limit"": 2, ""offset"": 0}"
}));
}))
.Callback<IBoxRequest>(r => boxRequest = r);

BoxCollection<BoxGroup> items = await _groupsManager.GetAllGroupsAsync();
/*** Act ***/
BoxCollection<BoxGroup> items = await _groupsManager.GetAllGroupsAsync(filterTerm: "test");

/*** Assert ***/

// Request check
Assert.AreEqual("filter_term=test", boxRequest.GetQueryString());

// Response check
Assert.AreEqual(items.TotalCount, 14, "Wrong total count");
Assert.AreEqual(items.Entries.Count, 2, "Wrong count of entries");
Assert.AreEqual(items.Entries[0].Type, "group", "Wrong type");
Assert.AreEqual(items.Entries[0].Id, "26477", "Wrong id");
Assert.AreEqual(items.Entries[0].Name, "adfasdf", "Wrong name");
Assert.AreEqual(items.Entries[0].CreatedAt, DateTime.Parse("2011-02-15T14:07:22-08:00"), "Wrong created at");
Assert.AreEqual(items.Entries[0].ModifiedAt, DateTime.Parse("2011-10-05T19:04:40-07:00"), "Wrong modified at");
Assert.AreEqual(items.Entries[0].InvitabilityLevel, "none");
Assert.AreEqual(items.Offset, 0, "Wrong offset");
Assert.AreEqual(items.Limit, 2, "Wrong limit");
}
Expand Down
8 changes: 7 additions & 1 deletion Box.V2/Managers/BoxGroupsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@ public BoxGroupsManager(IBoxConfig config, IBoxService service, IBoxConverter co
/// <param name="offset">The offset of the results. Refer to the Box API for more details.</param>
/// <param name="fields">Attribute(s) to include in the response.</param>
/// <param name="autoPaginate">Whether or not to auto-paginate to fetch all groups; defaults to false.</param>
/// <param name="filterTerm">Limits the results to only groups whose name starts with the search term.</param>
/// <returns>A collection of groups.</returns>
public async Task<BoxCollection<BoxGroup>> GetAllGroupsAsync(int? limit = null, int? offset = null, IEnumerable<string> fields = null, bool autoPaginate = false)
public async Task<BoxCollection<BoxGroup>> GetAllGroupsAsync(int? limit = null, int? offset = null, IEnumerable<string> fields = null, bool autoPaginate = false, string filterTerm = null)
{
BoxRequest request = new BoxRequest(_config.GroupsEndpointUri)
.Param(ParamFields, fields)
.Param("limit", limit.ToString())
.Param("offset", offset.ToString());

if (filterTerm != null)
{
request.Param("filter_term", filterTerm);
}

if (autoPaginate)
{
if (!limit.HasValue)
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

**New Features and Enhancements:**

- Add support for filtering when getting Groups (#703)
- Add zip functionality (#700)
- Deprecate one of the overloaded `ExecuteMetadataQueryAsync()` methods (#699)
- Add support for `copyInstanceOnItemCopy` field for metadata templates (#698)
Expand Down
4 changes: 2 additions & 2 deletions docs/groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Get All Groups
--------------

To get a list of all groups in the calling user's enterprise, call
`GroupsManager.GetAllGroupsAsync(int? limit = null, int? offset = null, IEnumerable<string> fields = null, bool autoPaginate = false)`.
`GroupsManager.GetAllGroupsAsync(int? limit = null, int? offset = null, IEnumerable<string> fields = null, bool autoPaginate = false, string filterTerm = null)`.
Note that this requires permission to view an enterprise's groups, which is reserved for enterprise administrators.

<!-- sample get_groups -->
Expand Down Expand Up @@ -186,4 +186,4 @@ view groups, which is restricted to enterprise administrators.
```c#
BoxCollection<BoxGroupMembership> memberships = await client.GroupsManager
.GetAllGroupMembershipsForUserAsync(userId: "11111");
```
```

0 comments on commit 2874ba7

Please sign in to comment.