Skip to content

Commit 59e65a4

Browse files
committed
Render DeviceType display name in event integration templates
The #DeviceType# integration template token resolved to the DeviceType enum member identifier (e.g. "ChromeExtension") because ReplaceTokens calls .ToString() on the value. Expose the [Display(Name)] value via GetDisplayName() so rendered event payloads show the friendly name (e.g. "Chrome Extension") instead.
1 parent 3e79593 commit 59e65a4

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

src/Core/Dirt/Models/Data/EventIntegrations/IntegrationTemplateContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class IntegrationTemplateContext(EventMessage eventMessage)
1212

1313
public string DomainName => Event.DomainName;
1414
public string IpAddress => Event.IpAddress;
15-
public DeviceType? DeviceType => Event.DeviceType;
15+
public string? DeviceType => Event.DeviceType?.GetDisplayName();
1616
public int? DeviceTypeId => Event.DeviceType is not null ? (int)Event.DeviceType : null;
1717
public Guid? ActingUserId => Event.ActingUserId;
1818
public Guid? OrganizationUserId => Event.OrganizationUserId;

test/Core.Test/Dirt/Models/Data/EventIntegrations/IntegrationTemplateContextTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Text.Json;
33
using Bit.Core.AdminConsole.Entities;
44
using Bit.Core.Dirt.Models.Data.EventIntegrations;
5+
using Bit.Core.Enums;
56
using Bit.Core.Models.Data;
67
using Bit.Core.Models.Data.Organizations.OrganizationUsers;
78
using Bit.Test.Common.AutoFixture.Attributes;
@@ -34,6 +35,43 @@ public void DateIso8601_ReturnsIso8601FormattedDate(EventMessage eventMessage)
3435
Assert.True(DateTime.TryParse(result, out _));
3536
}
3637

38+
[Theory, BitAutoData]
39+
public void DeviceType_WhenSet_ReturnsDisplayName(EventMessage eventMessage)
40+
{
41+
eventMessage.DeviceType = DeviceType.ChromeExtension;
42+
var sut = new IntegrationTemplateContext(eventMessage);
43+
44+
// Display name from [Display(Name = "...")], not the enum member identifier "ChromeExtension"
45+
Assert.Equal("Chrome Extension", sut.DeviceType);
46+
}
47+
48+
[Theory, BitAutoData]
49+
public void DeviceType_WhenNull_ReturnsNull(EventMessage eventMessage)
50+
{
51+
eventMessage.DeviceType = null;
52+
var sut = new IntegrationTemplateContext(eventMessage);
53+
54+
Assert.Null(sut.DeviceType);
55+
}
56+
57+
[Theory, BitAutoData]
58+
public void DeviceTypeId_WhenSet_ReturnsNumericValue(EventMessage eventMessage)
59+
{
60+
eventMessage.DeviceType = DeviceType.ChromeExtension;
61+
var sut = new IntegrationTemplateContext(eventMessage);
62+
63+
Assert.Equal((int)DeviceType.ChromeExtension, sut.DeviceTypeId);
64+
}
65+
66+
[Theory, BitAutoData]
67+
public void DeviceTypeId_WhenNull_ReturnsNull(EventMessage eventMessage)
68+
{
69+
eventMessage.DeviceType = null;
70+
var sut = new IntegrationTemplateContext(eventMessage);
71+
72+
Assert.Null(sut.DeviceTypeId);
73+
}
74+
3775
[Theory, BitAutoData]
3876
public void UserName_WhenUserIsSet_ReturnsName(EventMessage eventMessage, OrganizationUserUserDetails user)
3977
{

test/Core.Test/Dirt/Services/EventIntegrationHandlerTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Bit.Core.AdminConsole.Repositories;
77
using Bit.Core.Dirt.Enums;
88
using Bit.Core.Dirt.Models.Data.EventIntegrations;
9+
using Bit.Core.Enums;
910
using Bit.Core.Dirt.Repositories;
1011
using Bit.Core.Dirt.Services;
1112
using Bit.Core.Dirt.Services.Implementations;
@@ -773,7 +774,7 @@ public async Task HandleEventAsync_SubstituteTemplateTags(EventMessage eventMess
773774
""actingUserId"": ""{eventMessage.ActingUserId}"",
774775
""installationId"": ""{eventMessage.InstallationId}"",
775776
""date"": ""{eventMessage.Date.ToString("o")}"",
776-
""deviceType"": ""{eventMessage.DeviceType}"",
777+
""deviceType"": ""{eventMessage.DeviceType?.GetDisplayName()}"",
777778
""deviceTypeId"": ""{deviceTypeId}"",
778779
""ipAddress"": ""{eventMessage.IpAddress}"",
779780
""systemUser"": ""{systemUser}"",

0 commit comments

Comments
 (0)