Skip to content

Commit

Permalink
fix: Add missing fields of EventSource (#956)
Browse files Browse the repository at this point in the history
Co-authored-by: wmagnuson <166155021+wmagnuson@users.noreply.github.com>
  • Loading branch information
congminh1254 and wmagnuson committed Jun 21, 2024
1 parent 1f89bb0 commit 138eda5
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 3 deletions.
56 changes: 55 additions & 1 deletion Box.V2.Test/BoxEventsManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task GetGroupEvents_ValidResponse()
[TestMethod]
public async Task GetUserEventsFile_ValidResponse()
{
var responseString = "{\"chunk_size\": 1, \"next_stream_position\": 123, \"entries\": [{\"source\":{\"file_id\":\"283257336425\",\"file_name\":\"ScreenShot2018-03-12at5.44.00PM.png\",\"user_id\":\"285663442\",\"user_name\":\"foo\",\"parent\":{\"type\":\"folder\",\"name\":\"AllFiles\",\"id\":\"0\"}},\"created_by\":{\"type\":\"user\",\"id\":\"11111\",\"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\":\"COLLABORATION_INVITE\",\"ip_address\":\"UnknownIP\",\"type\":\"event\",\"session_id\":null,\"additional_details\":null}]}";
var responseString = "{\"chunk_size\": 1, \"next_stream_position\": 123, \"entries\": [{\"source\":{\"file_id\":\"283257336425\",\"file_name\":\"ScreenShot2018-03-12at5.44.00PM.png\",\"user_id\":\"285663442\",\"user_name\":\"foo\",\"parent\":{\"type\":\"folder\",\"name\":\"AllFiles\",\"id\":\"0\"},\"owned_by\":{\"type\": \"user\",\"id\":\"33333\",\"name\": \"Test User\",\"login\":\"testuser@example.com\"}},\"created_by\":{\"type\":\"user\",\"id\":\"11111\",\"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\":\"COLLABORATION_INVITE\",\"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>>()
Expand All @@ -88,6 +88,36 @@ public async Task GetUserEventsFile_ValidResponse()
Assert.AreEqual(userFileEvents.Entries[0].Source.GetType(), typeof(BoxUserFileCollaborationEventSource));
Assert.AreEqual(userFileEventSource.Id, "283257336425");
Assert.AreEqual(userFileEventSource.Name, "ScreenShot2018-03-12at5.44.00PM.png");
Assert.AreEqual(userFileEventSource.UserId, "285663442");
Assert.AreEqual(userFileEventSource.UserName, "foo");
Assert.AreEqual(userFileEventSource.OwnedBy.Id, "33333");
Assert.AreEqual(userFileEventSource.OwnedBy.Name, "Test User");
}

[TestMethod]
public async Task GetUserEventsFile_ExternalUser_ValidResponse()
{
var responseString = "{\"chunk_size\": 1, \"next_stream_position\": 123, \"entries\": [{\"source\":{\"file_id\":\"283257336425\",\"file_name\":\"ScreenShot2018-03-12at5.44.00PM.png\",\"user_email\":\"externaluser@box.com\",\"parent\":{\"type\":\"folder\",\"name\":\"AllFiles\",\"id\":\"0\"},\"owned_by\":{\"type\": \"user\",\"id\":\"33333\",\"name\": \"Test User\",\"login\":\"testuser@example.com\"}},\"created_by\":{\"type\":\"user\",\"id\":\"11111\",\"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\":\"COLLABORATION_INVITE\",\"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 userFileEvents = await _eventsManager.EnterpriseEventsAsync();

var userFileEventSource = userFileEvents.Entries[0].Source as BoxUserFileCollaborationEventSource;

Assert.AreEqual(userFileEvents.Entries[0].EventType, "COLLABORATION_INVITE");
Assert.AreEqual(userFileEvents.Entries[0].Source.GetType(), typeof(BoxUserFileCollaborationEventSource));
Assert.AreEqual(userFileEventSource.Id, "283257336425");
Assert.AreEqual(userFileEventSource.Name, "ScreenShot2018-03-12at5.44.00PM.png");
Assert.AreEqual(userFileEventSource.UserEmail, "externaluser@box.com");
Assert.AreEqual(userFileEventSource.OwnedBy.Id, "33333");
Assert.AreEqual(userFileEventSource.OwnedBy.Name, "Test User");
}

[TestMethod]
Expand All @@ -113,6 +143,30 @@ public async Task GetUserEventsFolder_ValidResponse()
Assert.AreEqual(userFolderEventSource.Name, "SharedWithServiceAccount");
}

[TestMethod]
public async Task GetUserEventsFolder_ExternalUser_ValidResponse()
{
var responseString = "{\"chunk_size\": 1, \"next_stream_position\": 123, \"entries\": [{\"source\":{\"folder_id\":\"47846340014\",\"folder_name\":\"SharedWithServiceAccount\",\"user_email\":\"externaluser@box.com\",\"parent\":{\"type\":\"folder\",\"name\":\"AllFiles\",\"id\":\"0\"}},\"created_by\":{\"type\":\"user\",\"id\":\"11111\",\"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\":\"COLLABORATION_INVITE\",\"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 userFolderEvents = await _eventsManager.EnterpriseEventsAsync();

var userFolderEventSource = userFolderEvents.Entries[0].Source as BoxUserFolderCollaborationEventSource;

Assert.AreEqual(userFolderEvents.Entries[0].EventType, "COLLABORATION_INVITE");
Assert.AreEqual(userFolderEvents.Entries[0].Source.GetType(), typeof(BoxUserFolderCollaborationEventSource));
Assert.AreEqual(userFolderEventSource.Id, "47846340014");
Assert.AreEqual(userFolderEventSource.Name, "SharedWithServiceAccount");
Assert.AreEqual(userFolderEventSource.UserEmail, "externaluser@box.com");
}

[TestMethod]
public async Task GetGroupEventsFolder_ValidResponse()
{
Expand Down
107 changes: 107 additions & 0 deletions Box.V2.Test/Converters/BoxJsonConverterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using Box.V2.Converter;
using Box.V2.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Box.V2.Test
{
[TestClass]
public class BoxJsonConverterTest : BoxResourceManagerTest
{
private readonly IBoxConverter _converter;

public BoxJsonConverterTest()
{
_converter = new BoxJsonConverter();
}

[TestMethod]
public void BoxUserFileCollaborationEventSource_ValidateConversion()
{
var sourceString = "{\"file_id\":\"283257336425\",\"file_name\":\"ScreenShot2018-03-12at5.44.00PM.png\",\"user_id\":\"285663442\",\"user_name\":\"foo\",\"parent\":{\"type\":\"folder\",\"name\":\"AllFiles\",\"id\":\"0\"},\"owned_by\":{\"type\":\"user\",\"id\":\"11111\",\"name\":\"Test User\",\"login\":\"test@user.com\"}}";
var sourceObject = _converter.Parse<BoxUserFileCollaborationEventSource>(sourceString);

var serializedObjectString = _converter.Serialize(sourceObject);

var boxUserFileCollaborationEventSource = _converter.Parse<BoxUserFileCollaborationEventSource>(serializedObjectString);

Assert.AreEqual(boxUserFileCollaborationEventSource.GetType(), typeof(BoxUserFileCollaborationEventSource));
Assert.AreEqual(boxUserFileCollaborationEventSource.Id, "283257336425");
Assert.AreEqual(boxUserFileCollaborationEventSource.Name, "ScreenShot2018-03-12at5.44.00PM.png");
Assert.AreEqual(boxUserFileCollaborationEventSource.UserName, "foo");
Assert.AreEqual(boxUserFileCollaborationEventSource.OwnedBy.Id, "11111");
Assert.AreEqual(boxUserFileCollaborationEventSource.Parent.Id, "0");
}

[TestMethod]
public void BoxUserFolderCollaborationEventSource_ValidateConversion()
{
var sourceString = "{\"folder_id\":\"47846340014\",\"folder_name\":\"SharedWithServiceAccount\",\"user_id\":\"182069272\",\"user_name\":\"MattWiller\",\"parent\":{\"type\":\"folder\",\"name\":\"AllFiles\",\"id\":\"0\"},\"owned_by\":{\"type\":\"user\",\"id\":\"11111\",\"name\":\"Test User\",\"login\":\"test@user.com\"}}";
var sourceObject = _converter.Parse<BoxUserFolderCollaborationEventSource>(sourceString);

var serializedObjectString = _converter.Serialize(sourceObject);

var boxUserFolderCollaborationEventSource = _converter.Parse<BoxUserFolderCollaborationEventSource>(serializedObjectString);

Assert.AreEqual(boxUserFolderCollaborationEventSource.GetType(), typeof(BoxUserFolderCollaborationEventSource));
Assert.AreEqual(boxUserFolderCollaborationEventSource.Id, "47846340014");
Assert.AreEqual(boxUserFolderCollaborationEventSource.UserId, "182069272");
Assert.AreEqual(boxUserFolderCollaborationEventSource.UserName, "MattWiller");
Assert.AreEqual(boxUserFolderCollaborationEventSource.Name, "SharedWithServiceAccount");
Assert.AreEqual(boxUserFolderCollaborationEventSource.OwnedBy.Id, "11111");
Assert.AreEqual(boxUserFolderCollaborationEventSource.Parent.Id, "0");
}

[TestMethod]
public void BoxGroupFolderCollaborationEventSource_ValidateConversion()
{
var sourceString = "{\"folder_id\":\"47846340014\",\"folder_name\":\"SharedWithServiceAccount\",\"group_id\":\"182069272\",\"group_name\":\"TestGroup\",\"parent\":{\"type\":\"folder\",\"name\":\"AllFiles\",\"id\":\"0\"},\"owned_by\":{\"type\":\"user\",\"id\":\"275035869\",\"name\":\"MattWiller\",\"login\":\"mwiller + appusers@box.com\"}}";
var sourceObject = _converter.Parse<BoxGroupFolderCollaborationEventSource>(sourceString);

var serializedObjectString = _converter.Serialize(sourceObject);

var boxGroupFolderCollaborationEventSource = _converter.Parse<BoxGroupFolderCollaborationEventSource>(serializedObjectString);

Assert.AreEqual(boxGroupFolderCollaborationEventSource.GetType(), typeof(BoxGroupFolderCollaborationEventSource));
Assert.AreEqual(boxGroupFolderCollaborationEventSource.Id, "47846340014");
Assert.AreEqual(boxGroupFolderCollaborationEventSource.Name, "SharedWithServiceAccount");
Assert.AreEqual(boxGroupFolderCollaborationEventSource.GroupId, "182069272");
Assert.AreEqual(boxGroupFolderCollaborationEventSource.GroupName, "TestGroup");
Assert.AreEqual(boxGroupFolderCollaborationEventSource.Parent.Id, "0");
Assert.AreEqual(boxGroupFolderCollaborationEventSource.OwnedBy.Id, "275035869");
}

[TestMethod]
public void BoxGroupFileCollaborationEventSource_ValidateConversion()
{
var sourceString = "{\"file_id\":\"47846340014\",\"file_name\":\"test-picture.jpg\",\"group_id\":\"182069272\",\"group_name\":\"TestGroup\",\"parent\":{\"type\":\"folder\",\"name\":\"AllFiles\",\"id\":\"0\"},\"owned_by\":{\"type\":\"user\",\"id\":\"11111\",\"name\":\"Test User\",\"login\":\"test@user.com\"}}";
var sourceObject = _converter.Parse<BoxGroupFileCollaborationEventSource>(sourceString);

var serializedObjectString = _converter.Serialize(sourceObject);

var boxGroupFileCollaborationEventSource = _converter.Parse<BoxGroupFileCollaborationEventSource>(serializedObjectString);

Assert.AreEqual(boxGroupFileCollaborationEventSource.GetType(), typeof(BoxGroupFileCollaborationEventSource));
Assert.AreEqual(boxGroupFileCollaborationEventSource.Id, "47846340014");
Assert.AreEqual(boxGroupFileCollaborationEventSource.Name, "test-picture.jpg");
Assert.AreEqual(boxGroupFileCollaborationEventSource.GroupId, "182069272");
Assert.AreEqual(boxGroupFileCollaborationEventSource.GroupName, "TestGroup");
Assert.AreEqual(boxGroupFileCollaborationEventSource.Parent.Id, "0");
Assert.AreEqual(boxGroupFileCollaborationEventSource.OwnedBy.Id, "11111");
}

[TestMethod]
public void BoxGroupEventSource_ValidateConversion()
{
var sourceString = "{\"group_id\": \"182069272\",\"group_name\": \"TestGroup\"}";
var sourceObject = _converter.Parse<BoxGroupEventSource>(sourceString);

var serializedObjectString = _converter.Serialize(sourceObject);

var boxGroupEventSource = _converter.Parse<BoxGroupEventSource>(serializedObjectString);

Assert.AreEqual(boxGroupEventSource.GetType(), typeof(BoxGroupEventSource));
Assert.AreEqual(boxGroupEventSource.Id, "182069272");
Assert.AreEqual(boxGroupEventSource.Name, "TestGroup");
}
}
}
5 changes: 3 additions & 2 deletions Box.V2/Converter/BoxItemConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal class BoxItemConverter : JsonCreationConverter<BoxEntity>
private const string WatermarkType = "watermark";
private const string GroupId = "group_id";
private const string UserId = "user_id";
private const string UserEmail = "user_email";
private const string FolderId = "folder_id";
private const string FileId = "file_id";

Expand Down Expand Up @@ -133,11 +134,11 @@ protected override BoxEntity Create(Type objectType, JObject jObject)
return new BoxGroupEventSource();
}
}
else if (FieldExists(UserId, jObject) && FieldExists(FileId, jObject))
else if ((FieldExists(UserId, jObject) || FieldExists(UserEmail, jObject)) && FieldExists(FileId, jObject))
{
return new BoxUserFileCollaborationEventSource();
}
else if (FieldExists(UserId, jObject) && FieldExists(FolderId, jObject))
else if ((FieldExists(UserId, jObject) || FieldExists(UserEmail, jObject)) && FieldExists(FolderId, jObject))
{
return new BoxUserFolderCollaborationEventSource();
}
Expand Down
7 changes: 7 additions & 0 deletions Box.V2/Models/BoxFileEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class BoxFileEventSource : BoxEntity
public const string FieldItemId = "item_id";
public const string FieldItemName = "item_name";
public const string FieldItemParent = "parent";
public const string FieldOwnedBy = "owned_by";

/// <summary>
/// The type of the event source
Expand All @@ -35,5 +36,11 @@ public class BoxFileEventSource : BoxEntity
/// </summary>
[JsonProperty(PropertyName = FieldItemParent)]
public BoxFolder Parent { get; private set; }

/// <summary>
/// The user who owns this item
/// </summary>
[JsonProperty(PropertyName = FieldOwnedBy)]
public BoxUser OwnedBy { get; private set; }
}
}
7 changes: 7 additions & 0 deletions Box.V2/Models/BoxFolderEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class BoxFolderEventSource : BoxEntity
public const string FieldItemId = "item_id";
public const string FieldItemName = "item_name";
public const string FieldItemParent = "parent";
public const string FieldOwnedBy = "owned_by";

/// <summary>
/// The type of the event source
Expand All @@ -35,5 +36,11 @@ public class BoxFolderEventSource : BoxEntity
/// </summary>
[JsonProperty(PropertyName = FieldItemParent)]
public BoxFolder Parent { get; private set; }

/// <summary>
/// The user who owns this item
/// </summary>
[JsonProperty(PropertyName = FieldOwnedBy)]
public BoxUser OwnedBy { get; private set; }
}
}
1 change: 1 addition & 0 deletions Box.V2/Models/BoxGroupEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class BoxGroupEventSource : BoxEntity
/// <summary>
/// The type of the object.
/// </summary>
[JsonIgnore]
public override string Type { get { return "group"; } protected set { return; } }

/// <summary>
Expand Down
8 changes: 8 additions & 0 deletions Box.V2/Models/BoxGroupFileCollaborationEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class BoxGroupFileCollaborationEventSource : BoxEntity
public const string FieldGroupId = "group_id";
public const string FieldGroupName = "group_name";
public const string FieldParent = "parent";
public const string FieldOwnedBy = "owned_by";

/// <summary>
/// The unique ID of the file being collaborated on.
Expand All @@ -19,6 +20,7 @@ public class BoxGroupFileCollaborationEventSource : BoxEntity
/// <summary>
/// The type of the object.
/// </summary>
[JsonIgnore]
public override string Type { get { return "file"; } protected set { return; } }

/// <summary>
Expand All @@ -44,5 +46,11 @@ public class BoxGroupFileCollaborationEventSource : BoxEntity
/// </summary>
[JsonProperty(PropertyName = FieldParent)]
public BoxFolder Parent { get; private set; }

/// <summary>
/// The user who owns this item
/// </summary>
[JsonProperty(PropertyName = FieldOwnedBy)]
public BoxUser OwnedBy { get; private set; }
}
}
8 changes: 8 additions & 0 deletions Box.V2/Models/BoxGroupFolderCollaborationEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class BoxGroupFolderCollaborationEventSource : BoxEntity
public const string FieldGroupId = "group_id";
public const string FieldGroupName = "group_name";
public const string FieldParent = "parent";
public const string FieldOwnedBy = "owned_by";

/// <summary>
/// The unique ID of the folder being collaborated on.
Expand All @@ -19,6 +20,7 @@ public class BoxGroupFolderCollaborationEventSource : BoxEntity
/// <summary>
/// The type of the object.
/// </summary>
[JsonIgnore]
public override string Type { get { return "folder"; } protected set { return; } }

/// <summary>
Expand All @@ -44,5 +46,11 @@ public class BoxGroupFolderCollaborationEventSource : BoxEntity
/// </summary>
[JsonProperty(PropertyName = FieldParent)]
public BoxFolder Parent { get; private set; }

/// <summary>
/// The user who owns this item
/// </summary>
[JsonProperty(PropertyName = FieldOwnedBy)]
public BoxUser OwnedBy { get; private set; }
}
}
Loading

0 comments on commit 138eda5

Please sign in to comment.