Skip to content

Commit 930d33b

Browse files
committed
HubSpot Forms updates for Umbraco 10; Fix unit tests.
1 parent 6f4c82f commit 930d33b

File tree

11 files changed

+219
-198
lines changed

11 files changed

+219
-198
lines changed

src/Umbraco.Forms.Integrations.Crm.Hubspot.Tests/Services/HubspotContactServiceTests.cs

Lines changed: 85 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.Logging;
13
using Moq;
24
using Moq.Protected;
35
using NUnit.Framework;
@@ -8,12 +10,10 @@
810
using System.Net.Http;
911
using System.Threading;
1012
using System.Threading.Tasks;
11-
using Umbraco.Core.Cache;
12-
using Umbraco.Core.Logging;
13-
using Umbraco.Core.Services;
13+
using Umbraco.Cms.Core.Cache;
14+
using Umbraco.Cms.Core.Services;
1415
using Umbraco.Forms.Core;
1516
using Umbraco.Forms.Core.Persistence.Dtos;
16-
using Umbraco.Forms.Core.Providers.Models;
1717
using Umbraco.Forms.Integrations.Crm.Hubspot.Models;
1818
using Umbraco.Forms.Integrations.Crm.Hubspot.Services;
1919

@@ -102,46 +102,59 @@ public class HubspotContactServiceTests
102102
[Test]
103103
public async Task GetContactProperties_WithoutApiKeyConfigured_ReturnsEmptyCollectionWithLoggedWarning()
104104
{
105-
Mock<IFacadeConfiguration> mockedConfig = CreateMockedConfiguration(withApiKey: false);
106-
var mockedLogger = new Mock<ILogger>();
107-
var mockedAppCaches = new Mock<AppCaches>();
105+
Mock<IConfiguration> mockedConfig = CreateMockedConfiguration(withApiKey: false);
106+
var mockedLogger = new Mock<ILogger<HubspotContactService>>();
108107
var mockedKeyValueService = new Mock<IKeyValueService>();
109-
var sut = new HubspotContactService(mockedConfig.Object, mockedLogger.Object, mockedAppCaches.Object, mockedKeyValueService.Object);
108+
var sut = new HubspotContactService(mockedConfig.Object, mockedLogger.Object, AppCaches.NoCache, mockedKeyValueService.Object);
110109

111110
var result = await sut.GetContactPropertiesAsync();
112111

113-
mockedLogger
114-
.Verify(x => x.Info(It.Is<Type>(y => y == typeof(HubspotContactService)), It.IsAny<string>()), Times.Once);
112+
mockedLogger.Verify(
113+
m => m.Log(
114+
LogLevel.Information,
115+
It.IsAny<EventId>(),
116+
It.IsAny<It.IsAnyType>(),
117+
It.IsAny<Exception>(),
118+
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
119+
Times.Once,
120+
It.IsAny<string>());
121+
115122
Assert.IsEmpty(result);
116123
}
117124

118125
[Test]
119126
public async Task GetContactProperties_WithFailedRequest_ReturnsEmptyCollectionWithLoggedError()
120127
{
121-
Mock<IFacadeConfiguration> mockedConfig = CreateMockedConfiguration();
122-
var mockedLogger = new Mock<ILogger>();
123-
var mockedAppCaches = new Mock<AppCaches>();
128+
Mock<IConfiguration> mockedConfig = CreateMockedConfiguration();
129+
var mockedLogger = new Mock<ILogger<HubspotContactService>>();
124130
var mockedKeyValueService = new Mock<IKeyValueService>();
125-
var sut = new HubspotContactService(mockedConfig.Object, mockedLogger.Object, mockedAppCaches.Object, mockedKeyValueService.Object);
131+
var sut = new HubspotContactService(mockedConfig.Object, mockedLogger.Object, AppCaches.NoCache, mockedKeyValueService.Object);
126132

127133
var httpClient = CreateMockedHttpClient(HttpStatusCode.InternalServerError);
128134
HubspotContactService.ClientFactory = () => httpClient;
129135

130136
var result = await sut.GetContactPropertiesAsync();
131137

132-
mockedLogger
133-
.Verify(x => x.Error(It.Is<Type>(y => y == typeof(HubspotContactService)), It.IsAny<string>(), It.IsAny<object[]>()), Times.Once);
138+
mockedLogger.Verify(
139+
m => m.Log(
140+
LogLevel.Error,
141+
It.IsAny<EventId>(),
142+
It.IsAny<It.IsAnyType>(),
143+
It.IsAny<Exception>(),
144+
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
145+
Times.Once,
146+
It.IsAny<string>());
147+
134148
Assert.IsEmpty(result);
135149
}
136150

137151
[Test]
138152
public async Task GetContactProperties_WithSuccessfulRequest_ReturnsMappedAndOrderedPropertyCollection()
139153
{
140-
Mock<IFacadeConfiguration> mockedConfig = CreateMockedConfiguration();
141-
var mockedLogger = new Mock<ILogger>();
142-
var mockedAppCaches = new Mock<AppCaches>();
154+
Mock<IConfiguration> mockedConfig = CreateMockedConfiguration();
155+
var mockedLogger = new Mock<ILogger<HubspotContactService>>();
143156
var mockedKeyValueService = new Mock<IKeyValueService>();
144-
var sut = new HubspotContactService(mockedConfig.Object, mockedLogger.Object, mockedAppCaches.Object, mockedKeyValueService.Object);
157+
var sut = new HubspotContactService(mockedConfig.Object, mockedLogger.Object, AppCaches.NoCache, mockedKeyValueService.Object);
145158

146159
var httpClient = CreateMockedHttpClient(HttpStatusCode.OK, s_contactPropertiesResponse);
147160
HubspotContactService.ClientFactory = () => httpClient;
@@ -156,29 +169,35 @@ public async Task GetContactProperties_WithSuccessfulRequest_ReturnsMappedAndOrd
156169
[Test]
157170
public async Task PostContact_WithoutApiKeyConfigured_ReturnsNotConfiguredWithLoggedWarning()
158171
{
159-
Mock<IFacadeConfiguration> mockedConfig = CreateMockedConfiguration(withApiKey: false);
160-
var mockedLogger = new Mock<ILogger>();
161-
var mockedAppCaches = new Mock<AppCaches>();
172+
Mock<IConfiguration> mockedConfig = CreateMockedConfiguration(withApiKey: false);
173+
var mockedLogger = new Mock<ILogger<HubspotContactService>>();
162174
var mockedKeyValueService = new Mock<IKeyValueService>();
163-
var sut = new HubspotContactService(mockedConfig.Object, mockedLogger.Object, mockedAppCaches.Object, mockedKeyValueService.Object);
175+
var sut = new HubspotContactService(mockedConfig.Object, mockedLogger.Object, AppCaches.NoCache, mockedKeyValueService.Object);
164176

165177
var record = new Record();
166178
var fieldMappings = new List<MappedProperty>();
167179
var result = await sut.PostContactAsync(record, fieldMappings);
168180

169-
mockedLogger
170-
.Verify(x => x.Warn(It.Is<Type>(y => y == typeof(HubspotContactService)), It.IsAny<string>()), Times.Once);
181+
mockedLogger.Verify(
182+
m => m.Log(
183+
LogLevel.Warning,
184+
It.IsAny<EventId>(),
185+
It.IsAny<It.IsAnyType>(),
186+
It.IsAny<Exception>(),
187+
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
188+
Times.Once,
189+
It.IsAny<string>());
190+
171191
Assert.AreEqual(CommandResult.NotConfigured, result);
172192
}
173193

174194
[Test]
175195
public async Task PostContact_WithFailedRequest_ReturnsFailedWithLoggedError()
176196
{
177-
Mock<IFacadeConfiguration> mockedConfig = CreateMockedConfiguration();
178-
var mockedLogger = new Mock<ILogger>();
179-
var mockedAppCaches = new Mock<AppCaches>();
197+
Mock<IConfiguration> mockedConfig = CreateMockedConfiguration();
198+
var mockedLogger = new Mock<ILogger<HubspotContactService>>();
180199
var mockedKeyValueService = new Mock<IKeyValueService>();
181-
var sut = new HubspotContactService(mockedConfig.Object, mockedLogger.Object, mockedAppCaches.Object, mockedKeyValueService.Object);
200+
var sut = new HubspotContactService(mockedConfig.Object, mockedLogger.Object, AppCaches.NoCache, mockedKeyValueService.Object);
182201

183202
var httpClient = CreateMockedHttpClient(HttpStatusCode.InternalServerError);
184203
HubspotContactService.ClientFactory = () => httpClient;
@@ -187,20 +206,26 @@ public async Task PostContact_WithFailedRequest_ReturnsFailedWithLoggedError()
187206
var fieldMappings = new List<MappedProperty>();
188207
var result = await sut.PostContactAsync(record, fieldMappings);
189208

190-
mockedLogger
191-
.Verify(x => x.Error(It.Is<Type>(y => y == typeof(HubspotContactService)), It.IsAny<string>()), Times.Once);
209+
mockedLogger.Verify(
210+
m => m.Log(
211+
LogLevel.Error,
212+
It.IsAny<EventId>(),
213+
It.IsAny<It.IsAnyType>(),
214+
It.IsAny<Exception>(),
215+
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
216+
Times.Once,
217+
It.IsAny<string>());
192218

193219
Assert.AreEqual(CommandResult.Failed, result);
194220
}
195221

196222
[Test]
197223
public async Task PostContact_WithSuccessfulRequest_ReturnSuccess()
198224
{
199-
Mock<IFacadeConfiguration> mockedConfig = CreateMockedConfiguration();
200-
var mockedLogger = new Mock<ILogger>();
201-
var mockedAppCaches = new Mock<AppCaches>();
225+
Mock<IConfiguration> mockedConfig = CreateMockedConfiguration();
226+
var mockedLogger = new Mock<ILogger<HubspotContactService>>();
202227
var mockedKeyValueService = new Mock<IKeyValueService>();
203-
var sut = new HubspotContactService(mockedConfig.Object, mockedLogger.Object, mockedAppCaches.Object, mockedKeyValueService.Object);
228+
var sut = new HubspotContactService(mockedConfig.Object, mockedLogger.Object, AppCaches.NoCache, mockedKeyValueService.Object);
204229

205230
var httpClient = CreateMockedHttpClient(HttpStatusCode.OK);
206231
HubspotContactService.ClientFactory = () => httpClient;
@@ -222,20 +247,26 @@ public async Task PostContact_WithSuccessfulRequest_ReturnSuccess()
222247
};
223248
var result = await sut.PostContactAsync(record, fieldMappings);
224249

225-
mockedLogger
226-
.Verify(x => x.Warn(It.Is<Type>(y => y == typeof(HubspotContactService)), It.IsAny<string>(), It.IsAny<object[]>()), Times.Never);
250+
mockedLogger.Verify(
251+
m => m.Log(
252+
LogLevel.Warning,
253+
It.IsAny<EventId>(),
254+
It.IsAny<It.IsAnyType>(),
255+
It.IsAny<Exception>(),
256+
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
257+
Times.Never,
258+
It.IsAny<string>());
227259

228260
Assert.AreEqual(CommandResult.Success, result);
229261
}
230262

231263
[Test]
232264
public async Task PostContact_WithSuccessfulRequestAndUnmappedField_ReturnSuccessWithLoggedWarning()
233265
{
234-
Mock<IFacadeConfiguration> mockedConfig = CreateMockedConfiguration();
235-
var mockedLogger = new Mock<ILogger>();
236-
var mockedAppCaches = new Mock<AppCaches>();
266+
Mock<IConfiguration> mockedConfig = CreateMockedConfiguration();
267+
var mockedLogger = new Mock<ILogger<HubspotContactService>>();
237268
var mockedKeyValueService = new Mock<IKeyValueService>();
238-
var sut = new HubspotContactService(mockedConfig.Object, mockedLogger.Object, mockedAppCaches.Object, mockedKeyValueService.Object);
269+
var sut = new HubspotContactService(mockedConfig.Object, mockedLogger.Object, AppCaches.NoCache, mockedKeyValueService.Object);
239270

240271
var httpClient = CreateMockedHttpClient(HttpStatusCode.OK);
241272
HubspotContactService.ClientFactory = () => httpClient;
@@ -252,19 +283,26 @@ public async Task PostContact_WithSuccessfulRequestAndUnmappedField_ReturnSucces
252283
};
253284
var result = await sut.PostContactAsync(record, fieldMappings);
254285

255-
mockedLogger
256-
.Verify(x => x.Warn(It.Is<Type>(y => y == typeof(HubspotContactService)), It.IsAny<string>(), It.IsAny<object[]>()), Times.Once);
286+
mockedLogger.Verify(
287+
m => m.Log(
288+
LogLevel.Warning,
289+
It.IsAny<EventId>(),
290+
It.IsAny<It.IsAnyType>(),
291+
It.IsAny<Exception>(),
292+
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
293+
Times.Once,
294+
It.IsAny<string>());
257295

258296
Assert.AreEqual(CommandResult.Success, result);
259297
}
260298

261-
private static Mock<IFacadeConfiguration> CreateMockedConfiguration(bool withApiKey = true)
299+
private static Mock<IConfiguration> CreateMockedConfiguration(bool withApiKey = true)
262300
{
263-
var mockedConfiguration = new Mock<IFacadeConfiguration>();
301+
var mockedConfiguration = new Mock<IConfiguration>();
264302
if (withApiKey)
265303
{
266304
mockedConfiguration
267-
.Setup(x => x.GetSetting(It.Is<string>(y => y == "HubSpotApiKey")))
305+
.Setup(x => x[It.Is<string>(y => y == HubspotWorkflow.HubspotApiKey)])
268306
.Returns(ApiKey);
269307
}
270308

src/Umbraco.Forms.Integrations.Crm.Hubspot.Tests/Umbraco.Forms.Integrations.Crm.Hubspot.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net472</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>

src/Umbraco.Forms.Integrations.Crm.Hubspot/Controllers/HubspotController.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
using System.Collections.Generic;
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
using System.Collections.Generic;
24
using System.Threading.Tasks;
3-
using System.Web.Http;
5+
6+
using Umbraco.Cms.Web.BackOffice.Controllers;
7+
using Umbraco.Cms.Web.Common.Attributes;
48
using Umbraco.Forms.Integrations.Crm.Hubspot.Models.Dtos;
59
using Umbraco.Forms.Integrations.Crm.Hubspot.Models.Responses;
610
using Umbraco.Forms.Integrations.Crm.Hubspot.Services;
7-
using Umbraco.Web.Editors;
8-
using Umbraco.Web.Mvc;
911

1012
namespace Umbraco.Forms.Integrations.Crm.Hubspot.Controllers
1113
{

src/Umbraco.Forms.Integrations.Crm.Hubspot/HubspotComponent.cs

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
using Umbraco.Core;
2-
using Umbraco.Core.Composing;
1+
using Microsoft.Extensions.DependencyInjection;
2+
3+
using Umbraco.Cms.Core.Composing;
4+
using Umbraco.Cms.Core.DependencyInjection;
5+
using Umbraco.Cms.Core.Notifications;
6+
using Umbraco.Forms.Core.Providers;
37
using Umbraco.Forms.Integrations.Crm.Hubspot.Services;
48

59
namespace Umbraco.Forms.Integrations.Crm.Hubspot
610
{
7-
public class HubspotComposer : IUserComposer
11+
public class HubspotComposer : IComposer
812
{
9-
public void Compose(Composition composition)
13+
public void Compose(IUmbracoBuilder builder)
1014
{
11-
composition.Register<IContactService, HubspotContactService>(Lifetime.Singleton);
15+
builder.Services.AddSingleton<IContactService, HubspotContactService>();
16+
17+
builder.AddNotificationHandler<ServerVariablesParsingNotification, HubspotServerVariablesParsingHandler>();
1218

13-
composition.Components().Append<HubspotComponent>();
19+
builder.WithCollectionBuilder<WorkflowCollectionBuilder>()
20+
.Add<HubspotWorkflow>();
1421
}
1522
}
1623
}

0 commit comments

Comments
 (0)