Skip to content

Commit c2a58bb

Browse files
authored
Merge branch 'master' into show-version-header-11d
2 parents c1334ce + 3486e1f commit c2a58bb

4 files changed

Lines changed: 144 additions & 3 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ temp/
184184
TestResults.xml
185185

186186
# Nuget outputs
187+
.nuget/nuget.exe
187188
nuget/*.nupkg
188189
nupkg/
189190
release.cmd

.nuget/nuget.exe

7.89 MB
Binary file not shown.

src/XrmMockup365/Requests/SendEmailRequestHandler.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ internal class SendEmailRequestHandler : RequestHandler
1010
{
1111
const int EMAIL_STATE_COMPLETED = 1;
1212
const int EMAIL_STATUS_DRAFT = 1;
13+
const int EMAIL_STATUS_FAILED = 8;
1314
const int EMAIL_STATUS_PENDING_SEND = 6;
1415
const int EMAIL_STATUS_SENT = 3;
1516

@@ -50,15 +51,32 @@ internal override void CheckSecurity(OrganizationRequest orgRequest, EntityRefer
5051
// Remaining security checks have been omitted to reduce complexity
5152
}
5253

54+
private static string GetEmailAddress(Entity entity)
55+
{
56+
if (entity.LogicalName == "systemuser")
57+
{
58+
return entity.GetAttributeValue<string>("internalemailaddress");
59+
}
60+
else if (entity.LogicalName == "queue")
61+
{
62+
return entity.GetAttributeValue<string>("emailaddress");
63+
}
64+
else
65+
{
66+
return entity.GetAttributeValue<string>("emailaddress1");
67+
}
68+
}
69+
5370
internal override OrganizationResponse Execute(OrganizationRequest orgRequest, EntityReference userRef)
5471
{
5572
var request = MakeRequest<SendEmailRequest>(orgRequest);
5673

5774
var email = db.GetEntity(new EntityReference("email", request.EmailId));
5875

59-
if (email.GetAttributeValue<OptionSetValue>("statuscode").Value != EMAIL_STATUS_DRAFT)
76+
var statusCode = email.GetAttributeValue<OptionSetValue>("statuscode").Value;
77+
if (statusCode != EMAIL_STATUS_DRAFT && statusCode != EMAIL_STATUS_FAILED)
6078
{
61-
throw new FaultException("Email must be in Draft status to send");
79+
throw new FaultException("Email must be in Draft or Failed status to send");
6280
}
6381

6482
if (email.GetAttributeValue<bool>("directioncode") is false)
@@ -111,7 +129,7 @@ internal override OrganizationResponse Execute(OrganizationRequest orgRequest, E
111129
{
112130
throw new FaultException($"{partyRef.LogicalName} with Id = {partyRef.Id} does not exist");
113131
}
114-
if (string.IsNullOrEmpty(partyEntity.GetAttributeValue<string>("emailaddress1")))
132+
if (string.IsNullOrEmpty(GetEmailAddress(partyEntity)))
115133
{
116134
throw new FaultException($"{partyRef.LogicalName} with Id = {partyRef.Id} does not have an email address");
117135
}

tests/XrmMockup365Test/TestSendEmail.cs

Lines changed: 122 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;
@@ -146,6 +147,55 @@ public void TestSendEmailRequestSuccessWithUnresolvedRecipient()
146147
}
147148
}
148149

150+
[Fact]
151+
public void TestSendEmailRequestSuccessWhenEmailStatusFailed()
152+
{
153+
var contact = new Contact
154+
{
155+
FirstName = "Test",
156+
EMailAddress1 = "test@test.com"
157+
};
158+
contact.Id = orgAdminUIService.Create(contact);
159+
160+
var email = new Email
161+
{
162+
From = new ActivityParty[]
163+
{
164+
new ActivityParty
165+
{
166+
PartyId = crm.AdminUser
167+
}
168+
},
169+
To = new ActivityParty[]
170+
{
171+
new ActivityParty
172+
{
173+
PartyId = contact.ToEntityReference()
174+
}
175+
},
176+
Subject = "Test Email",
177+
StatusCode = Email_StatusCode.Failed
178+
};
179+
email.Id = orgAdminUIService.Create(email);
180+
181+
var sendEmailRequest = new SendEmailRequest
182+
{
183+
EmailId = email.Id,
184+
IssueSend = true
185+
};
186+
187+
var response = orgAdminUIService.Execute(sendEmailRequest) as SendEmailResponse;
188+
Assert.NotNull(response);
189+
Assert.Equal(email.Subject, response.Subject);
190+
191+
using (var context = new Xrm(orgAdminUIService))
192+
{
193+
var retrievedEmail = context.EmailSet.FirstOrDefault();
194+
Assert.Equal(EmailState.Completed, retrievedEmail.StateCode);
195+
Assert.Equal(Email_StatusCode.PendingSend, retrievedEmail.StatusCode);
196+
}
197+
}
198+
149199
[Fact]
150200
public void TestSendEmailRequestFailsWhenTryingToSendAgain()
151201
{
@@ -393,5 +443,77 @@ public void TestSendEmailRequestFailsWhenRecipientHasNoEmailAddress1()
393443

394444
Assert.Throws<FaultException>(() => orgAdminUIService.Execute(sendEmailRequest));
395445
}
446+
447+
[Fact]
448+
public void TestSendEmailRequestSuccessWithSystemUserRecipient()
449+
{
450+
var email = new Email
451+
{
452+
From = new ActivityParty[]
453+
{
454+
new ActivityParty
455+
{
456+
PartyId = crm.AdminUser
457+
}
458+
},
459+
To = new ActivityParty[]
460+
{
461+
new ActivityParty
462+
{
463+
PartyId = testUser1.ToEntityReference()
464+
}
465+
},
466+
Subject = "Test Email",
467+
};
468+
email.Id = orgAdminUIService.Create(email);
469+
470+
var sendEmailRequest = new SendEmailRequest
471+
{
472+
EmailId = email.Id,
473+
IssueSend = true
474+
};
475+
476+
var response = orgAdminUIService.Execute(sendEmailRequest) as SendEmailResponse;
477+
Assert.NotNull(response);
478+
Assert.Equal(email.Subject, response.Subject);
479+
}
480+
481+
[Fact]
482+
public void TestSendEmailRequestFailsWhenSystemUserRecipientHasNoEmailAddress()
483+
{
484+
var userWithNoEmail = new SystemUser
485+
{
486+
BusinessUnitId = crm.RootBusinessUnit,
487+
IsLicensed = true
488+
};
489+
userWithNoEmail = crm.CreateUser(orgAdminService, userWithNoEmail, new System.Guid[0]).ToEntity<SystemUser>();
490+
491+
var email = new Email
492+
{
493+
From = new ActivityParty[]
494+
{
495+
new ActivityParty
496+
{
497+
PartyId = crm.AdminUser
498+
}
499+
},
500+
To = new ActivityParty[]
501+
{
502+
new ActivityParty
503+
{
504+
PartyId = userWithNoEmail.ToEntityReference()
505+
}
506+
},
507+
Subject = "Test Email",
508+
};
509+
email.Id = orgAdminUIService.Create(email);
510+
511+
var sendEmailRequest = new SendEmailRequest
512+
{
513+
EmailId = email.Id
514+
};
515+
516+
Assert.Throws<FaultException>(() => orgAdminUIService.Execute(sendEmailRequest));
517+
}
396518
}
397519
}

0 commit comments

Comments
 (0)