-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCTA_Form.cs
162 lines (130 loc) · 5.01 KB
/
CTA_Form.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System.ComponentModel.DataAnnotations;
using System.Net.Mail;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Our.Umbraco.Test.Examples.Support;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
using Umbraco.Cms.Web.Website.ActionResults;
using Umbraco.Cms.Web.Website.Controllers;
using Umbraco.Extensions;
namespace Our.Umbraco.Test.Examples.SurfaceControllers
{
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public class CTA_Form : UmbracoIntegrationTestWithContent
{
private readonly CtaFormModel validModel = new()
{
Name = "E.X. Ample",
Email = "[email protected]"
};
private readonly CtaFormModel invalidModel = new();
private CtaFormController controller = null!;
[Test]
public async Task Redirects_To_Current_Page_With_Success_Querystring_For_Valid_Model()
{
controller.PrepareModelState(validModel);
var result = await controller.PostForm(validModel);
Assert.That(
result,
Is.InstanceOf<RedirectToUmbracoPageResult>() &
Has.Property(nameof(RedirectToUmbracoPageResult.Key)).EqualTo(Textpage.Key)
);
}
[Test]
public async Task Sends_Mail_For_Valid_Model()
{
controller.PrepareModelState(validModel);
await controller.PostForm(validModel);
var file = GetRequiredService<SmtpClient>().Last()!;
await VerifyFile(file).ScrubLinesContaining("Date:");
}
[Test]
public async Task Returns_Current_Page_For_Invalid_Model()
{
controller.PrepareModelState(invalidModel);
var result = await controller.PostForm(invalidModel);
Assert.That(result, Is.InstanceOf<UmbracoPageResult>());
}
[Test]
public async Task Does_Not_Send_Mail_For_Invalid_Model()
{
controller.PrepareModelState(invalidModel);
await controller.PostForm(invalidModel);
var file = GetRequiredService<SmtpClient>().Last()!;
Assert.That(file, Is.Null);
}
[SetUp]
public void SetupRequest()
{
ContentService.SaveAndPublish(Textpage);
var address = "https://localhost";
var httpContext = TestHelper.GetHttpContextAccessor().HttpContext!;
httpContext.PrepareUrl(address);
var ctx = GetRequiredService<IUmbracoContextFactory>().EnsureUmbracoContext();
var publishedContent = ctx.UmbracoContext.Content!.GetById(Textpage.Id);
httpContext.PreparePublishedRequest(ctx, address, publishedContent);
controller = GetRequiredService<CtaFormController>();
controller.ControllerContext.HttpContext = httpContext;
}
protected override void CustomTestSetup(IUmbracoBuilder builder)
{
builder.AddNuCache();
}
protected override void ConfigureTestServices(IServiceCollection services)
{
services.AddTestSmtpClient();
services.AddTransient<CtaFormController>();
}
[TearDown]
public void Cleanup()
{
GetRequiredService<SmtpClient>().Dispose();
}
}
public class CtaFormController : SurfaceController
{
private readonly SmtpClient smtpClient;
public CtaFormController(
SmtpClient smtpClient,
IUmbracoContextAccessor umbracoContextAccessor,
IUmbracoDatabaseFactory databaseFactory,
ServiceContext services,
AppCaches appCaches,
IProfilingLogger profilingLogger,
IPublishedUrlProvider publishedUrlProvider
) : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
{
this.smtpClient = smtpClient;
}
public async Task<IActionResult> PostForm(CtaFormModel model)
{
if (!ModelState.IsValid)
{
return CurrentUmbracoPage();
}
var msg = new MailMessage("[email protected]", "[email protected]", "Hello", $"{model.Name} sent a request.")
{
ReplyToList = { model.Email }
};
await smtpClient.SendMailAsync(msg);
return RedirectToCurrentUmbracoPage(new QueryString("?success=true"));
}
}
public class CtaFormModel
{
[Required]
public string Name { get; set; } = "";
[Required]
[EmailAddress]
public string Email { get; set; } = "";
}
}