forked from haoduotnt/aspnetwebstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPassiveAuthenticationMessageHandlerTest.cs
418 lines (365 loc) · 17.7 KB
/
PassiveAuthenticationMessageHandlerTest.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Hosting;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.Owin
{
public class PassiveAuthenticationMessageHandlerTest
{
[Fact]
public void ConstructorWithConfiguration_SetsPrincipalService()
{
// Arrange
IHostPrincipalService expectedPrincipalService = CreateDummyPrincipalService();
IHostPrincipalService principalService;
using (HttpConfiguration configuration = new HttpConfiguration())
{
configuration.Services.Replace(typeof(IHostPrincipalService), expectedPrincipalService);
PassiveAuthenticationMessageHandler handler = new PassiveAuthenticationMessageHandler(configuration);
// Act
principalService = handler.HostPrincipalService;
}
// Assert
Assert.Same(expectedPrincipalService, principalService);
}
[Fact]
public void ConstructorWithConfiguration_Throws_WhenConfigurationIsNull()
{
// Arrange
HttpConfiguration configuration = null;
// Act & Assert
Assert.ThrowsArgumentNull(() => { new PassiveAuthenticationMessageHandler(configuration); },
"configuration");
}
[Fact]
public void ConstructorWithConfiguration_Throws_WhenPrincipalServiceIsNull()
{
// Arrange
using (HttpConfiguration configuration = new HttpConfiguration())
{
configuration.Services.Replace(typeof(IHostPrincipalService), null);
// Act & Assert
InvalidOperationException exception = Assert.Throws<InvalidOperationException>(() =>
{
new PassiveAuthenticationMessageHandler(configuration);
});
Assert.Equal("ServicesContainer must have an IHostPrincipalService.", exception.Message);
}
}
[Fact]
public void ConstructorWithPrincipalService_SetsPrincipalService()
{
// Arrange
IHostPrincipalService expectedPrincipalService = CreateDummyPrincipalService();
PassiveAuthenticationMessageHandler handler = new PassiveAuthenticationMessageHandler(
expectedPrincipalService);
// Act
IHostPrincipalService principalService = handler.HostPrincipalService;
// Assert
Assert.Same(expectedPrincipalService, principalService);
}
[Fact]
public void ConstructorWithPrincipalService_Throws_WhenPrincipalServiceIsNull()
{
// Arrange
IHostPrincipalService principalService = null;
// Act & Assert
Assert.ThrowsArgumentNull(() => { new PassiveAuthenticationMessageHandler(principalService); },
"principalService");
}
[Fact]
public void SendAsync_DelegatesToInnerHandler()
{
// Arrange
IHostPrincipalService principalService = CreateStubPrincipalService();
HttpRequestMessage request = null;
CancellationToken cancellationToken = default(CancellationToken);
IOwinContext context = CreateOwinContext();
using (HttpResponseMessage expectedResponse = new HttpResponseMessage())
{
HttpMessageHandler innerHandler = new LambdaHttpMessageHandler((r, c) =>
{
request = r;
cancellationToken = c;
return Task.FromResult(expectedResponse);
});
HttpMessageHandler handler = CreateProductUnderTest(principalService, innerHandler);
CancellationToken expectedCancellationToken = new CancellationToken(true);
using (HttpRequestMessage expectedRequest = CreateRequestWithOwinContext(context))
{
// Act
HttpResponseMessage response = handler.SendAsync(expectedRequest,
expectedCancellationToken).Result;
// Assert
Assert.Same(expectedRequest, request);
Assert.Equal(expectedCancellationToken, cancellationToken);
Assert.Same(expectedResponse, response);
}
}
}
[Fact]
public void SendAsync_Throws_WhenOwinContextIsNull()
{
// Arrange
IHostPrincipalService principalService = CreateStubPrincipalService();
HttpMessageHandler innerHandler = CreateStubHandler();
HttpMessageHandler handler = CreateProductUnderTest(principalService, innerHandler);
using (HttpRequestMessage request = new HttpRequestMessage())
{
// Act & Assert
InvalidOperationException exception = Assert.Throws<InvalidOperationException>(() =>
{
HttpResponseMessage ignore = handler.SendAsync(request, CancellationToken.None).Result;
});
Assert.Equal("No OWIN authentication manager is associated with the request.", exception.Message);
}
}
[Fact]
public void SendAsync_Throws_WhenAuthenticationManagerIsNull()
{
// Arrange
IHostPrincipalService principalService = CreateStubPrincipalService();
HttpMessageHandler innerHandler = CreateStubHandler();
HttpMessageHandler handler = CreateProductUnderTest(principalService, innerHandler);
IOwinContext context = CreateOwinContext(null);
using (HttpRequestMessage request = CreateRequestWithOwinContext(context))
{
// Act & Assert
InvalidOperationException exception = Assert.Throws<InvalidOperationException>(() =>
{
HttpResponseMessage ignore = handler.SendAsync(request, CancellationToken.None).Result;
});
Assert.Equal("No OWIN authentication manager is associated with the request.", exception.Message);
}
}
[Fact]
public void SendAsync_SetsCurrentPrincipalToAnonymous_BeforeCallingInnerHandler()
{
// Arrange
IPrincipal principalServiceCurrentPrincipal = null;
IHostPrincipalService principalService = CreateSpyPrincipalService((p) =>
{
principalServiceCurrentPrincipal = p;
});
IPrincipal principalBeforeInnerHandler = null;
HttpMessageHandler inner = new LambdaHttpMessageHandler((ignore1, ignore2) =>
{
principalBeforeInnerHandler = principalServiceCurrentPrincipal;
return Task.FromResult<HttpResponseMessage>(null);
});
HttpMessageHandler handler = CreateProductUnderTest(principalService, inner);
IOwinContext context = CreateOwinContext();
using (HttpRequestMessage request = CreateRequestWithOwinContext(context))
{
// Act
HttpResponseMessage ignore = handler.SendAsync(request, CancellationToken.None).Result;
}
// Assert
Assert.NotNull(principalBeforeInnerHandler);
IIdentity identity = principalBeforeInnerHandler.Identity;
Assert.NotNull(identity);
Assert.False(identity.IsAuthenticated);
Assert.Null(identity.Name);
Assert.Null(identity.AuthenticationType);
}
[Fact]
public void SendAsync_SuppressesAuthenticationChallenges_WhenNoChallengeIsSet()
{
// Arrange
IHostPrincipalService principalService = CreateStubPrincipalService();
HttpMessageHandler inner = CreateStubHandler();
HttpMessageHandler handler = CreateProductUnderTest(principalService, inner);
IOwinContext context = CreateOwinContext();
using (HttpRequestMessage request = CreateRequestWithOwinContext(context))
{
// Act
HttpResponseMessage ignore = handler.SendAsync(request, CancellationToken.None).Result;
}
// Assert
IAuthenticationManager authenticationManager = context.Authentication;
AuthenticationResponseChallenge challenge = authenticationManager.AuthenticationResponseChallenge;
Assert.NotNull(challenge);
string[] authenticationTypes = challenge.AuthenticationTypes;
Assert.NotNull(authenticationTypes);
Assert.Equal(1, authenticationTypes.Length);
string authenticationType = authenticationTypes[0];
Assert.Null(authenticationType);
}
[Fact]
public void SendAsync_SuppressesAuthenticationChallenges_WhenExistingAuthenticationTypesIsNull()
{
// Arrange
IHostPrincipalService principalService = CreateStubPrincipalService();
HttpMessageHandler inner = CreateStubHandler();
HttpMessageHandler handler = CreateProductUnderTest(principalService, inner);
IOwinContext context = CreateOwinContext();
IAuthenticationManager authenticationManager = context.Authentication;
IDictionary<string, string> expectedExtra = new Dictionary<string, string>();
AuthenticationProperties extraWrapper = new AuthenticationProperties(expectedExtra);
context.Authentication.AuthenticationResponseChallenge = new AuthenticationResponseChallenge(null,
extraWrapper);
using (HttpRequestMessage request = CreateRequestWithOwinContext(context))
{
// Act
HttpResponseMessage ignore = handler.SendAsync(request, CancellationToken.None).Result;
}
// Assert
AuthenticationResponseChallenge challenge = authenticationManager.AuthenticationResponseChallenge;
Assert.NotNull(challenge);
string[] authenticationTypes = challenge.AuthenticationTypes;
Assert.NotNull(authenticationTypes);
Assert.Equal(1, authenticationTypes.Length);
string authenticationType = authenticationTypes[0];
Assert.Null(authenticationType);
AuthenticationProperties actualExtraWrapper = challenge.Properties;
Assert.NotNull(actualExtraWrapper);
Assert.Same(expectedExtra, actualExtraWrapper.Dictionary);
}
[Fact]
public void SendAsync_SuppressesAuthenticationChallenges_WhenExistingAuthenticationTypesIsEmpty()
{
// Arrange
IHostPrincipalService principalService = CreateStubPrincipalService();
HttpMessageHandler inner = CreateStubHandler();
HttpMessageHandler handler = CreateProductUnderTest(principalService, inner);
IOwinContext context = CreateOwinContext();
IAuthenticationManager authenticationManager = context.Authentication;
AuthenticationProperties extraWrapper = new AuthenticationProperties();
IDictionary<string, string> expectedExtra = extraWrapper.Dictionary;
authenticationManager.AuthenticationResponseChallenge = new AuthenticationResponseChallenge(new string[0],
extraWrapper);
using (HttpRequestMessage request = CreateRequestWithOwinContext(context))
{
// Act
HttpResponseMessage ignore = handler.SendAsync(request, CancellationToken.None).Result;
}
// Assert
AuthenticationResponseChallenge challenge = authenticationManager.AuthenticationResponseChallenge;
Assert.NotNull(challenge);
string[] authenticationTypes = challenge.AuthenticationTypes;
Assert.NotNull(authenticationTypes);
Assert.Equal(1, authenticationTypes.Length);
string authenticationType = authenticationTypes[0];
Assert.Null(authenticationType);
AuthenticationProperties actualExtraWrapper = challenge.Properties;
Assert.NotNull(actualExtraWrapper);
Assert.Same(expectedExtra, actualExtraWrapper.Dictionary);
}
[Fact]
public void SendAsync_LeavesAuthenticationChallenges_WhenExistingAuthenticationTypesIsNonEmpty()
{
// Arrange
IHostPrincipalService principalService = CreateStubPrincipalService();
HttpMessageHandler inner = CreateStubHandler();
HttpMessageHandler handler = CreateProductUnderTest(principalService, inner);
IOwinContext context = CreateOwinContext();
IAuthenticationManager authenticationManager = context.Authentication;
AuthenticationProperties extraWrapper = new AuthenticationProperties();
string[] expectedAuthenticationTypes = new string[] { "Existing" };
IDictionary<string, string> expectedExtra = extraWrapper.Dictionary;
authenticationManager.AuthenticationResponseChallenge = new AuthenticationResponseChallenge(
expectedAuthenticationTypes, extraWrapper);
using (HttpRequestMessage request = CreateRequestWithOwinContext(context))
{
// Act
HttpResponseMessage ignore = handler.SendAsync(request, CancellationToken.None).Result;
}
// Assert
AuthenticationResponseChallenge challenge = authenticationManager.AuthenticationResponseChallenge;
Assert.NotNull(challenge);
Assert.Same(expectedAuthenticationTypes, challenge.AuthenticationTypes);
AuthenticationProperties actualExtraWrapper = challenge.Properties;
Assert.NotNull(actualExtraWrapper);
Assert.Same(expectedExtra, actualExtraWrapper.Dictionary);
}
private static HttpMessageHandler CreateDummyHandler()
{
return new DummyHttpMessageHandler();
}
private static IOwinContext CreateOwinContext()
{
return new OwinContext();
}
private static IOwinContext CreateOwinContext(IAuthenticationManager authenticationManager)
{
Mock<IOwinContext> mock = new Mock<IOwinContext>(MockBehavior.Strict);
mock.SetupGet(m => m.Authentication).Returns(authenticationManager);
return mock.Object;
}
private static IHostPrincipalService CreateDummyPrincipalService()
{
return new Mock<IHostPrincipalService>(MockBehavior.Strict).Object;
}
private static AuthenticationProperties CreateExtra()
{
return new AuthenticationProperties();
}
private static PassiveAuthenticationMessageHandler CreateProductUnderTest(
IHostPrincipalService principalService, HttpMessageHandler innerHandler)
{
PassiveAuthenticationMessageHandler handler = new PassiveAuthenticationMessageHandler(principalService);
handler.InnerHandler = innerHandler;
return handler;
}
private static HttpRequestMessage CreateRequestWithOwinContext(IOwinContext context)
{
HttpRequestMessage request = new HttpRequestMessage();
request.SetOwinContext(context);
return request;
}
private static IHostPrincipalService CreateSpyPrincipalService(Action<IPrincipal> setPrincipal)
{
Mock<IHostPrincipalService> mock = new Mock<IHostPrincipalService>(MockBehavior.Strict);
mock.Setup(s => s.SetCurrentPrincipal(It.IsAny<IPrincipal>(),
It.IsAny<HttpRequestMessage>())).Callback<IPrincipal, HttpRequestMessage>(
(p, ignore) => { setPrincipal(p); });
return mock.Object;
}
private static HttpMessageHandler CreateStubHandler()
{
return new LambdaHttpMessageHandler((ignore1, ignore2) =>
{
return Task.FromResult<HttpResponseMessage>(null);
});
}
private static IHostPrincipalService CreateStubPrincipalService()
{
Mock<IHostPrincipalService> mock = new Mock<IHostPrincipalService>(MockBehavior.Strict);
mock.Setup(s => s.SetCurrentPrincipal(It.IsAny<IPrincipal>(), It.IsAny<HttpRequestMessage>()));
return mock.Object;
}
private class DummyHttpMessageHandler : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
private class LambdaHttpMessageHandler : HttpMessageHandler
{
private readonly Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> _sendAsync;
public LambdaHttpMessageHandler(Func<HttpRequestMessage, CancellationToken,
Task<HttpResponseMessage>> sendAsync)
{
if (sendAsync == null)
{
throw new ArgumentNullException("sendAsync");
}
_sendAsync = sendAsync;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
return _sendAsync.Invoke(request, cancellationToken);
}
}
}
}