Skip to content

Commit

Permalink
feat: add admin_logs_streaming support (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwwoda committed Jan 24, 2022
1 parent ae8cd8b commit a775e1e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
32 changes: 32 additions & 0 deletions Box.V2.Test.IntegrationNew/BoxEventsManagerIntegrationTest.cs
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Box.V2.Models;
using Box.V2.Test.IntegrationNew.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Box.V2.Test.IntegrationNew
{
[TestClass]
public class BoxEventsManagerIntegrationTest : TestInFolder
{
[TestMethod]
public async Task EnterpriseEventsStreamingAsync_ForNewFile_ShouldReturnUploadFileEvent()
{
var uploadedFile = await CreateSmallFile(FolderId);

var events = await AdminClient.EventsManager.EnterpriseEventsStreamingAsync();
BoxEnterpriseEvent uploadedFileEvent = null;
while (events.ChunkSize == 500 || uploadedFileEvent == null)
{
events = await AdminClient.EventsManager.EnterpriseEventsStreamingAsync(500, events.NextStreamPosition, new List<string>() { "UPLOAD" });
uploadedFileEvent = events.Entries.FirstOrDefault(x => x.Source?.Id == uploadedFile.Id);
}

Assert.IsNotNull(uploadedFileEvent);
Assert.AreEqual("UPLOAD", uploadedFileEvent.EventType);
Assert.AreEqual(uploadedFile.Id, uploadedFileEvent.Source.Id);
Assert.AreEqual("file", uploadedFileEvent.Source.Type);
}
}
}
27 changes: 27 additions & 0 deletions Box.V2.Test/BoxEventsManagerTest.cs
Expand Up @@ -213,5 +213,32 @@ public async Task GetWebLinkEvents_ValidResponse()
Assert.AreEqual(webLinkEventSource.Type, "web_link");
Assert.AreEqual(webLinkEventSource.Parent.Id, "22222");
}

[TestMethod]
[TestCategory("CI-UNIT-TEST")]
public async Task EnterpriseEventsStreamingAsync_ValidResponse()
{
var responseString = "{\"chunk_size\": 1, \"next_stream_position\": 123, \"entries\": [{\"source\":{\"group_id\":\"942617509\",\"group_name\":\"Groupies\"},\"created_by\":{\"type\":\"user\",\"id\":\"11111\",\"name\":\"Test User\",\"login\":\"test@user.com\"},\"action_by\":{\"type\":\"user\",\"id\":\"12345\",\"name\":\"Test User\",\"login\":\"test@user.com\"},\"created_at\":\"2018-03-16T15:12:52-07:00\",\"event_id\":\"85c57bf3-bc15-4d24-93bc-955c796217c8\",\"event_type\":\"GROUP_EDITED\",\"ip_address\":\"UnknownIP\",\"type\":\"event\",\"session_id\":null,\"additional_details\":null}]}";
IBoxRequest boxRequest = null;
Handler.Setup(h => h.ExecuteAsync<BoxEventCollection<BoxEnterpriseEvent>>(It.IsAny<IBoxRequest>()))
.Returns(Task.FromResult<IBoxResponse<BoxEventCollection<BoxEnterpriseEvent>>>(new BoxResponse<BoxEventCollection<BoxEnterpriseEvent>>()
{
Status = ResponseStatus.Success,
ContentString = responseString
})).Callback<IBoxRequest>(r => boxRequest = r);

/*** Act ***/
var events = await _eventsManager.EnterpriseEventsStreamingAsync();
var firstEvent = events.Entries.First<BoxEnterpriseEvent>();

/*** Assert ***/
Assert.AreEqual("stream_type=admin_logs_streaming&limit=500", boxRequest.GetQueryString());
Assert.AreEqual("12345", firstEvent.ActionBy.Id);
Assert.AreEqual("user", firstEvent.ActionBy.Type);
Assert.AreEqual("Test User", firstEvent.ActionBy.Name);
Assert.AreEqual("test@user.com", firstEvent.ActionBy.Login);
Assert.AreEqual("942617509", firstEvent.Source.Id);
Assert.AreEqual("85c57bf3-bc15-4d24-93bc-955c796217c8", firstEvent.EventId);
}
}
}
25 changes: 24 additions & 1 deletion Box.V2/Managers/BoxEventsManager.cs
Expand Up @@ -18,13 +18,14 @@ namespace Box.V2.Managers
public class BoxEventsManager : BoxResourceManager, IBoxEventsManager
{
public const string ENTERPRISE_EVENTS_STREAM_TYPE = "admin_logs";
public const string ENTERPRISE_EVENTS_STREAMING_STREAM_TYPE = "admin_logs_streaming";
public readonly LRUCache<string, bool> USER_EVENTS_DEDUPE_CACHE = new LRUCache<string, bool>(1000);

public BoxEventsManager(IBoxConfig config, IBoxService service, IBoxConverter converter, IAuthRepository auth, string asUser = null, bool? suppressNotifications = null)
: base(config, service, converter, auth, asUser, suppressNotifications) { }

/// <summary>
/// Retrieve a chunk of Enterprise Events. You must be using a token that is scoped to admin level in order to use this endpoint.
/// Retrieves up to a year's events for all users in the enterprise. High latency. You must be using a token that is scoped to admin level in order to use this endpoint.
/// </summary>
/// <param name="limit">Limits the number of events returned (defaults to 500).</param>
/// <param name="streamPosition">The starting position for fetching the events. This is used in combination with the limit to determine which events to return to the caller. Use the results from the next_stream_position of your last call to get the next set of events.</param>
Expand Down Expand Up @@ -160,5 +161,27 @@ public BoxEventsManager(IBoxConfig config, IBoxService service, IBoxConverter co
} while (pollAgain);
}
}

/// <summary>
/// Retrieves up to a two weeks's events for all users in the enterprise. Low latency. You must be using a token that is scoped to admin level in order to use this endpoint.
/// </summary>
/// <param name="limit">Limits the number of events returned (defaults to 500).</param>
/// <param name="streamPosition">The starting position for fetching the events. This is used in combination with the limit to determine which events to return to the caller. Use the results from the next_stream_position of your last call to get the next set of events.</param>
/// <param name="eventTypes">Events to filter by.</param>
/// <returns></returns>
public async Task<BoxEventCollection<BoxEnterpriseEvent>> EnterpriseEventsStreamingAsync(int limit = 500,
string streamPosition = null,
IEnumerable<string> eventTypes = null)
{
BoxRequest request = new BoxRequest(_config.EventsUri)
.Param("stream_type", ENTERPRISE_EVENTS_STREAMING_STREAM_TYPE)
.Param("limit", limit.ToString())
.Param("stream_position", streamPosition)
.Param("event_type", eventTypes);

IBoxResponse<BoxEventCollection<BoxEnterpriseEvent>> response = await ToResponseAsync<BoxEventCollection<BoxEnterpriseEvent>>(request).ConfigureAwait(false);

return response.ResponseObject;
}
}
}
13 changes: 12 additions & 1 deletion Box.V2/Managers/IBoxEventsManager.cs
Expand Up @@ -12,7 +12,7 @@ namespace Box.V2.Managers
public interface IBoxEventsManager
{
/// <summary>
/// Retrieve a chunk of Enterprise Events. You must be using a token that is scoped to admin level in order to use this endpoint.
/// Retrieves up to a year's events for all users in the enterprise. High latency. You must be using a token that is scoped to admin level in order to use this endpoint.
/// </summary>
/// <param name="limit">Limits the number of events returned (defaults to 500).</param>
/// <param name="streamPosition">The starting position for fetching the events. This is used in combination with the limit to determine which events to return to the caller. Use the results from the next_stream_position of your last call to get the next set of events.</param>
Expand Down Expand Up @@ -55,5 +55,16 @@ public interface IBoxEventsManager
UserEventsStreamType streamType = UserEventsStreamType.all,
bool dedupeEvents = true,
int? retryTimeoutOverride = null);

/// <summary>
/// Retrieves up to a two weeks's events for all users in the enterprise. Low latency. You must be using a token that is scoped to admin level in order to use this endpoint.
/// </summary>
/// <param name="limit">Limits the number of events returned (defaults to 500).</param>
/// <param name="streamPosition">The starting position for fetching the events. This is used in combination with the limit to determine which events to return to the caller. Use the results from the next_stream_position of your last call to get the next set of events.</param>
/// <param name="eventTypes">Events to filter by.</param>
/// <returns></returns>
Task<BoxEventCollection<BoxEnterpriseEvent>> EnterpriseEventsStreamingAsync(int limit = 500,
string streamPosition = null,
IEnumerable<string> eventTypes = null);
}
}

0 comments on commit a775e1e

Please sign in to comment.