1
1
using Microsoft . Extensions . Configuration ;
2
2
using Microsoft . Extensions . Logging ;
3
+ using Microsoft . Extensions . Options ;
3
4
using Moq ;
4
5
using Moq . Protected ;
5
6
using NUnit . Framework ;
14
15
using Umbraco . Cms . Core . Services ;
15
16
using Umbraco . Forms . Core ;
16
17
using Umbraco . Forms . Core . Persistence . Dtos ;
18
+ using Umbraco . Forms . Integrations . Crm . Hubspot . Configuration ;
17
19
using Umbraco . Forms . Integrations . Crm . Hubspot . Models ;
18
20
using Umbraco . Forms . Integrations . Crm . Hubspot . Services ;
19
21
@@ -102,10 +104,11 @@ public class HubspotContactServiceTests
102
104
[ Test ]
103
105
public async Task GetContactProperties_WithoutApiKeyConfigured_ReturnsEmptyCollectionWithLoggedWarning ( )
104
106
{
105
- Mock < IConfiguration > mockedConfig = CreateMockedConfiguration ( withApiKey : false ) ;
107
+ Mock < IOptions < HubspotSettings > > mockedConfig = CreateMockedConfiguration ( withApiKey : false ) ;
108
+ var mockedHttpClientFactory = new Mock < IHttpClientFactory > ( ) ;
106
109
var mockedLogger = new Mock < ILogger < HubspotContactService > > ( ) ;
107
110
var mockedKeyValueService = new Mock < IKeyValueService > ( ) ;
108
- var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
111
+ var sut = new HubspotContactService ( mockedHttpClientFactory . Object , mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
109
112
110
113
var result = await sut . GetContactPropertiesAsync ( ) ;
111
114
@@ -125,13 +128,16 @@ public async Task GetContactProperties_WithoutApiKeyConfigured_ReturnsEmptyColle
125
128
[ Test ]
126
129
public async Task GetContactProperties_WithFailedRequest_ReturnsEmptyCollectionWithLoggedError ( )
127
130
{
128
- Mock < IConfiguration > mockedConfig = CreateMockedConfiguration ( ) ;
131
+ Mock < IOptions < HubspotSettings > > mockedConfig = CreateMockedConfiguration ( ) ;
132
+
133
+ var mockedHttpClientFactory = new Mock < IHttpClientFactory > ( ) ;
134
+ mockedHttpClientFactory
135
+ . Setup < HttpClient > ( x => x . CreateClient ( It . IsAny < string > ( ) ) )
136
+ . Returns ( CreateMockedHttpClient ( HttpStatusCode . InternalServerError ) ) ;
137
+
129
138
var mockedLogger = new Mock < ILogger < HubspotContactService > > ( ) ;
130
139
var mockedKeyValueService = new Mock < IKeyValueService > ( ) ;
131
- var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
132
-
133
- var httpClient = CreateMockedHttpClient ( HttpStatusCode . InternalServerError ) ;
134
- HubspotContactService . ClientFactory = ( ) => httpClient ;
140
+ var sut = new HubspotContactService ( mockedHttpClientFactory . Object , mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
135
141
136
142
var result = await sut . GetContactPropertiesAsync ( ) ;
137
143
@@ -151,13 +157,16 @@ public async Task GetContactProperties_WithFailedRequest_ReturnsEmptyCollectionW
151
157
[ Test ]
152
158
public async Task GetContactProperties_WithSuccessfulRequest_ReturnsMappedAndOrderedPropertyCollection ( )
153
159
{
154
- Mock < IConfiguration > mockedConfig = CreateMockedConfiguration ( ) ;
160
+ Mock < IOptions < HubspotSettings > > mockedConfig = CreateMockedConfiguration ( ) ;
161
+
162
+ var mockedHttpClientFactory = new Mock < IHttpClientFactory > ( ) ;
163
+ mockedHttpClientFactory
164
+ . Setup < HttpClient > ( x => x . CreateClient ( It . IsAny < string > ( ) ) )
165
+ . Returns ( CreateMockedHttpClient ( HttpStatusCode . OK , s_contactPropertiesResponse ) ) ;
166
+
155
167
var mockedLogger = new Mock < ILogger < HubspotContactService > > ( ) ;
156
168
var mockedKeyValueService = new Mock < IKeyValueService > ( ) ;
157
- var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
158
-
159
- var httpClient = CreateMockedHttpClient ( HttpStatusCode . OK , s_contactPropertiesResponse ) ;
160
- HubspotContactService . ClientFactory = ( ) => httpClient ;
169
+ var sut = new HubspotContactService ( mockedHttpClientFactory . Object , mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
161
170
162
171
var result = await sut . GetContactPropertiesAsync ( ) ;
163
172
@@ -169,10 +178,11 @@ public async Task GetContactProperties_WithSuccessfulRequest_ReturnsMappedAndOrd
169
178
[ Test ]
170
179
public async Task PostContact_WithoutApiKeyConfigured_ReturnsNotConfiguredWithLoggedWarning ( )
171
180
{
172
- Mock < IConfiguration > mockedConfig = CreateMockedConfiguration ( withApiKey : false ) ;
181
+ Mock < IOptions < HubspotSettings > > mockedConfig = CreateMockedConfiguration ( withApiKey : false ) ;
182
+ var mockedHttpClientFactory = new Mock < IHttpClientFactory > ( ) ;
173
183
var mockedLogger = new Mock < ILogger < HubspotContactService > > ( ) ;
174
184
var mockedKeyValueService = new Mock < IKeyValueService > ( ) ;
175
- var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
185
+ var sut = new HubspotContactService ( mockedHttpClientFactory . Object , mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
176
186
177
187
var record = new Record ( ) ;
178
188
var fieldMappings = new List < MappedProperty > ( ) ;
@@ -194,13 +204,16 @@ public async Task PostContact_WithoutApiKeyConfigured_ReturnsNotConfiguredWithLo
194
204
[ Test ]
195
205
public async Task PostContact_WithFailedRequest_ReturnsFailedWithLoggedError ( )
196
206
{
197
- Mock < IConfiguration > mockedConfig = CreateMockedConfiguration ( ) ;
207
+ Mock < IOptions < HubspotSettings > > mockedConfig = CreateMockedConfiguration ( ) ;
208
+
209
+ var mockedHttpClientFactory = new Mock < IHttpClientFactory > ( ) ;
210
+ mockedHttpClientFactory
211
+ . Setup < HttpClient > ( x => x . CreateClient ( It . IsAny < string > ( ) ) )
212
+ . Returns ( CreateMockedHttpClient ( HttpStatusCode . InternalServerError ) ) ;
213
+
198
214
var mockedLogger = new Mock < ILogger < HubspotContactService > > ( ) ;
199
215
var mockedKeyValueService = new Mock < IKeyValueService > ( ) ;
200
- var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
201
-
202
- var httpClient = CreateMockedHttpClient ( HttpStatusCode . InternalServerError ) ;
203
- HubspotContactService . ClientFactory = ( ) => httpClient ;
216
+ var sut = new HubspotContactService ( mockedHttpClientFactory . Object , mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
204
217
205
218
var record = new Record ( ) ;
206
219
var fieldMappings = new List < MappedProperty > ( ) ;
@@ -222,13 +235,16 @@ public async Task PostContact_WithFailedRequest_ReturnsFailedWithLoggedError()
222
235
[ Test ]
223
236
public async Task PostContact_WithSuccessfulRequest_ReturnSuccess ( )
224
237
{
225
- Mock < IConfiguration > mockedConfig = CreateMockedConfiguration ( ) ;
238
+ Mock < IOptions < HubspotSettings > > mockedConfig = CreateMockedConfiguration ( ) ;
239
+
240
+ var mockedHttpClientFactory = new Mock < IHttpClientFactory > ( ) ;
241
+ mockedHttpClientFactory
242
+ . Setup < HttpClient > ( x => x . CreateClient ( It . IsAny < string > ( ) ) )
243
+ . Returns ( CreateMockedHttpClient ( HttpStatusCode . OK ) ) ;
244
+
226
245
var mockedLogger = new Mock < ILogger < HubspotContactService > > ( ) ;
227
246
var mockedKeyValueService = new Mock < IKeyValueService > ( ) ;
228
- var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
229
-
230
- var httpClient = CreateMockedHttpClient ( HttpStatusCode . OK ) ;
231
- HubspotContactService . ClientFactory = ( ) => httpClient ;
247
+ var sut = new HubspotContactService ( mockedHttpClientFactory . Object , mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
232
248
233
249
var formFieldId = Guid . NewGuid ( ) ;
234
250
var record = new Record ( ) ;
@@ -263,13 +279,16 @@ public async Task PostContact_WithSuccessfulRequest_ReturnSuccess()
263
279
[ Test ]
264
280
public async Task PostContact_WithSuccessfulRequestAndUnmappedField_ReturnSuccessWithLoggedWarning ( )
265
281
{
266
- Mock < IConfiguration > mockedConfig = CreateMockedConfiguration ( ) ;
282
+ Mock < IOptions < HubspotSettings > > mockedConfig = CreateMockedConfiguration ( ) ;
283
+
284
+ var mockedHttpClientFactory = new Mock < IHttpClientFactory > ( ) ;
285
+ mockedHttpClientFactory
286
+ . Setup < HttpClient > ( x => x . CreateClient ( It . IsAny < string > ( ) ) )
287
+ . Returns ( CreateMockedHttpClient ( HttpStatusCode . OK ) ) ;
288
+
267
289
var mockedLogger = new Mock < ILogger < HubspotContactService > > ( ) ;
268
290
var mockedKeyValueService = new Mock < IKeyValueService > ( ) ;
269
- var sut = new HubspotContactService ( mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
270
-
271
- var httpClient = CreateMockedHttpClient ( HttpStatusCode . OK ) ;
272
- HubspotContactService . ClientFactory = ( ) => httpClient ;
291
+ var sut = new HubspotContactService ( mockedHttpClientFactory . Object , mockedConfig . Object , mockedLogger . Object , AppCaches . NoCache , mockedKeyValueService . Object ) ;
273
292
274
293
var formFieldId = Guid . NewGuid ( ) ;
275
294
var record = new Record ( ) ;
@@ -296,16 +315,11 @@ public async Task PostContact_WithSuccessfulRequestAndUnmappedField_ReturnSucces
296
315
Assert . AreEqual ( CommandResult . Success , result ) ;
297
316
}
298
317
299
- private static Mock < IConfiguration > CreateMockedConfiguration ( bool withApiKey = true )
318
+ private static Mock < IOptions < HubspotSettings > > CreateMockedConfiguration ( bool withApiKey = true )
300
319
{
301
- var mockedConfiguration = new Mock < IConfiguration > ( ) ;
302
- if ( withApiKey )
303
- {
304
- mockedConfiguration
305
- . Setup ( x => x [ It . Is < string > ( y => y == HubspotWorkflow . HubspotApiKey ) ] )
306
- . Returns ( ApiKey ) ;
307
- }
308
-
320
+ var mockedConfiguration = new Mock < IOptions < HubspotSettings > > ( ) ;
321
+ mockedConfiguration . Setup ( x => x . Value ) . Returns ( new HubspotSettings { ApiKey = withApiKey ? ApiKey : String . Empty } ) ;
322
+
309
323
return mockedConfiguration ;
310
324
}
311
325
0 commit comments