Skip to content

Commit 080bc10

Browse files
CopilotJonasGLund99
andcommitted
Fix SendEmailRequestHandler to check correct email field for SystemUser and Queue entities
Co-authored-by: JonasGLund99 <89989380+JonasGLund99@users.noreply.github.com>
1 parent bc24d55 commit 080bc10

3 files changed

Lines changed: 90 additions & 1 deletion

File tree

.nuget/nuget.exe

7.89 MB
Binary file not shown.

src/XrmMockup365/Requests/SendEmailRequestHandler.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ internal override void CheckSecurity(OrganizationRequest orgRequest, EntityRefer
5050
// Remaining security checks have been omitted to reduce complexity
5151
}
5252

53+
private static string GetEmailAddress(Entity entity)
54+
{
55+
if (entity.LogicalName == "systemuser")
56+
{
57+
return entity.GetAttributeValue<string>("internalemailaddress");
58+
}
59+
else if (entity.LogicalName == "queue")
60+
{
61+
return entity.GetAttributeValue<string>("emailaddress");
62+
}
63+
else
64+
{
65+
return entity.GetAttributeValue<string>("emailaddress1");
66+
}
67+
}
68+
5369
internal override OrganizationResponse Execute(OrganizationRequest orgRequest, EntityReference userRef)
5470
{
5571
var request = MakeRequest<SendEmailRequest>(orgRequest);
@@ -111,7 +127,7 @@ internal override OrganizationResponse Execute(OrganizationRequest orgRequest, E
111127
{
112128
throw new FaultException($"{partyRef.LogicalName} with Id = {partyRef.Id} does not exist");
113129
}
114-
if (string.IsNullOrEmpty(partyEntity.GetAttributeValue<string>("emailaddress1")))
130+
if (string.IsNullOrEmpty(GetEmailAddress(partyEntity)))
115131
{
116132
throw new FaultException($"{partyRef.LogicalName} with Id = {partyRef.Id} does not have an email address");
117133
}

tests/XrmMockup365Test/TestSendEmail.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DG.XrmFramework.BusinessDomain.ServiceContext;
22
using Microsoft.Crm.Sdk.Messages;
3+
using Microsoft.Xrm.Sdk;
34
using System.Linq;
45
using System.ServiceModel;
56
using Xunit;
@@ -393,5 +394,77 @@ public void TestSendEmailRequestFailsWhenRecipientHasNoEmailAddress1()
393394

394395
Assert.Throws<FaultException>(() => orgAdminUIService.Execute(sendEmailRequest));
395396
}
397+
398+
[Fact]
399+
public void TestSendEmailRequestSuccessWithSystemUserRecipient()
400+
{
401+
var email = new Email
402+
{
403+
From = new ActivityParty[]
404+
{
405+
new ActivityParty
406+
{
407+
PartyId = crm.AdminUser
408+
}
409+
},
410+
To = new ActivityParty[]
411+
{
412+
new ActivityParty
413+
{
414+
PartyId = testUser1.ToEntityReference()
415+
}
416+
},
417+
Subject = "Test Email",
418+
};
419+
email.Id = orgAdminUIService.Create(email);
420+
421+
var sendEmailRequest = new SendEmailRequest
422+
{
423+
EmailId = email.Id,
424+
IssueSend = true
425+
};
426+
427+
var response = orgAdminUIService.Execute(sendEmailRequest) as SendEmailResponse;
428+
Assert.NotNull(response);
429+
Assert.Equal(email.Subject, response.Subject);
430+
}
431+
432+
[Fact]
433+
public void TestSendEmailRequestFailsWhenSystemUserRecipientHasNoEmailAddress()
434+
{
435+
var userWithNoEmail = new SystemUser
436+
{
437+
BusinessUnitId = crm.RootBusinessUnit,
438+
IsLicensed = true
439+
};
440+
userWithNoEmail = crm.CreateUser(orgAdminService, userWithNoEmail, new System.Guid[0]).ToEntity<SystemUser>();
441+
442+
var email = new Email
443+
{
444+
From = new ActivityParty[]
445+
{
446+
new ActivityParty
447+
{
448+
PartyId = crm.AdminUser
449+
}
450+
},
451+
To = new ActivityParty[]
452+
{
453+
new ActivityParty
454+
{
455+
PartyId = userWithNoEmail.ToEntityReference()
456+
}
457+
},
458+
Subject = "Test Email",
459+
};
460+
email.Id = orgAdminUIService.Create(email);
461+
462+
var sendEmailRequest = new SendEmailRequest
463+
{
464+
EmailId = email.Id
465+
};
466+
467+
Assert.Throws<FaultException>(() => orgAdminUIService.Execute(sendEmailRequest));
468+
}
396469
}
397470
}

0 commit comments

Comments
 (0)