forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassiveAuthenticationMessageHandlerTest.cs
More file actions
343 lines (302 loc) · 14.7 KB
/
Copy pathPassiveAuthenticationMessageHandlerTest.cs
File metadata and controls
343 lines (302 loc) · 14.7 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. 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.Controllers;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.Owin
{
public class PassiveAuthenticationMessageHandlerTest
{
[Fact]
public async Task SendAsync_DelegatesToInnerHandler()
{
// Arrange
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(innerHandler);
CancellationToken expectedCancellationToken = new CancellationToken(true);
using (HttpRequestMessage expectedRequest = CreateRequestWithOwinContextAndRequestContext(context))
{
// Act
HttpResponseMessage response = await handler.SendAsync(expectedRequest, expectedCancellationToken);
// Assert
Assert.Same(expectedRequest, request);
Assert.Equal(expectedCancellationToken, cancellationToken);
Assert.Same(expectedResponse, response);
}
}
}
[Fact]
public async Task SendAsync_Throws_WhenRequestContextIsNull()
{
// Arrange
HttpMessageHandler innerHandler = CreateDummyHandler();
HttpMessageHandler handler = CreateProductUnderTest(innerHandler);
using (HttpRequestMessage request = new HttpRequestMessage())
{
// Act & Assert
await Assert.ThrowsArgumentAsync(
() => handler.SendAsync(request, CancellationToken.None),
"request",
"The request must have a request context.");
}
}
[Fact]
public async Task SendAsync_Throws_WhenOwinContextIsNull()
{
// Arrange
HttpMessageHandler innerHandler = CreateStubHandler();
HttpMessageHandler handler = CreateProductUnderTest(innerHandler);
using (HttpRequestMessage request = new HttpRequestMessage())
{
request.SetRequestContext(new HttpRequestContext());
// Act & Assert
InvalidOperationException exception = await Assert.ThrowsAsync<InvalidOperationException>(
() => handler.SendAsync(request, CancellationToken.None));
Assert.Equal("No OWIN authentication manager is associated with the request.", exception.Message);
}
}
[Fact]
public async Task SendAsync_Throws_WhenAuthenticationManagerIsNull()
{
// Arrange
HttpMessageHandler innerHandler = CreateStubHandler();
HttpMessageHandler handler = CreateProductUnderTest(innerHandler);
IOwinContext context = CreateOwinContext(null);
using (HttpRequestMessage request = CreateRequestWithOwinContextAndRequestContext(context))
{
// Act & Assert
InvalidOperationException exception = await Assert.ThrowsAsync<InvalidOperationException>(
() => handler.SendAsync(request, CancellationToken.None));
Assert.Equal("No OWIN authentication manager is associated with the request.", exception.Message);
}
}
[Fact]
public async Task SendAsync_SetsRequestContextPrincipalToAnonymous_BeforeCallingInnerHandler()
{
// Arrange
var requestContextMock = new Mock<HttpRequestContext>(MockBehavior.Strict);
var sequence = new MockSequence();
var initialPrincipal = new GenericPrincipal(new GenericIdentity("generic user"), new[] { "generic role" });
IPrincipal requestContextPrincipal = null;
requestContextMock
.InSequence(sequence)
.SetupGet(c => c.Principal)
.Returns(initialPrincipal);
requestContextMock
.InSequence(sequence)
.SetupSet(c => c.Principal = It.IsAny<IPrincipal>())
.Callback<IPrincipal>(value => requestContextPrincipal = value);
// SendAsync also restores the old principal.
requestContextMock
.InSequence(sequence)
.SetupGet(c => c.Principal)
.Returns(requestContextPrincipal);
requestContextMock
.InSequence(sequence)
.SetupSet(c => c.Principal = initialPrincipal);
IPrincipal principalBeforeInnerHandler = null;
HttpMessageHandler inner = new LambdaHttpMessageHandler((ignore1, ignore2) =>
{
principalBeforeInnerHandler = requestContextPrincipal;
return Task.FromResult<HttpResponseMessage>(null);
});
HttpMessageHandler handler = CreateProductUnderTest(inner);
var context = CreateOwinContext();
using (var request = new HttpRequestMessage())
{
request.SetOwinContext(context);
request.SetRequestContext(requestContextMock.Object);
// Act
await handler.SendAsync(request, CancellationToken.None);
}
// Assert
Assert.Equal(requestContextPrincipal, principalBeforeInnerHandler);
Assert.NotNull(principalBeforeInnerHandler);
var identity = principalBeforeInnerHandler.Identity;
Assert.NotNull(identity);
Assert.False(identity.IsAuthenticated);
Assert.Null(identity.Name);
Assert.Null(identity.AuthenticationType);
}
[Fact]
public async Task SendAsync_SuppressesAuthenticationChallenges_WhenNoChallengeIsSet()
{
// Arrange
HttpMessageHandler inner = CreateStubHandler();
HttpMessageHandler handler = CreateProductUnderTest(inner);
IOwinContext context = CreateOwinContext();
using (HttpRequestMessage request = CreateRequestWithOwinContextAndRequestContext(context))
{
// Act
HttpResponseMessage ignore = await handler.SendAsync(request, CancellationToken.None);
}
// Assert
IAuthenticationManager authenticationManager = context.Authentication;
AuthenticationResponseChallenge challenge = authenticationManager.AuthenticationResponseChallenge;
Assert.NotNull(challenge);
string[] authenticationTypes = challenge.AuthenticationTypes;
Assert.NotNull(authenticationTypes);
string authenticationType = Assert.Single(authenticationTypes);
Assert.Null(authenticationType);
}
[Fact]
public async Task SendAsync_SuppressesAuthenticationChallenges_WhenExistingAuthenticationTypesIsNull()
{
// Arrange
HttpMessageHandler inner = CreateStubHandler();
HttpMessageHandler handler = CreateProductUnderTest(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 = CreateRequestWithOwinContextAndRequestContext(context))
{
// Act
HttpResponseMessage ignore = await handler.SendAsync(request, CancellationToken.None);
}
// Assert
AuthenticationResponseChallenge challenge = authenticationManager.AuthenticationResponseChallenge;
Assert.NotNull(challenge);
string[] authenticationTypes = challenge.AuthenticationTypes;
Assert.NotNull(authenticationTypes);
string authenticationType = Assert.Single(authenticationTypes);
Assert.Null(authenticationType);
AuthenticationProperties actualExtraWrapper = challenge.Properties;
Assert.NotNull(actualExtraWrapper);
Assert.Same(expectedExtra, actualExtraWrapper.Dictionary);
}
[Fact]
public async Task SendAsync_SuppressesAuthenticationChallenges_WhenExistingAuthenticationTypesIsEmpty()
{
// Arrange
HttpMessageHandler inner = CreateStubHandler();
HttpMessageHandler handler = CreateProductUnderTest(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 = CreateRequestWithOwinContextAndRequestContext(context))
{
// Act
HttpResponseMessage ignore = await handler.SendAsync(request, CancellationToken.None);
}
// Assert
AuthenticationResponseChallenge challenge = authenticationManager.AuthenticationResponseChallenge;
Assert.NotNull(challenge);
string[] authenticationTypes = challenge.AuthenticationTypes;
Assert.NotNull(authenticationTypes);
string authenticationType = Assert.Single(authenticationTypes);
Assert.Null(authenticationType);
AuthenticationProperties actualExtraWrapper = challenge.Properties;
Assert.NotNull(actualExtraWrapper);
Assert.Same(expectedExtra, actualExtraWrapper.Dictionary);
}
[Fact]
public async Task SendAsync_LeavesAuthenticationChallenges_WhenExistingAuthenticationTypesIsNonEmpty()
{
// Arrange
HttpMessageHandler inner = CreateStubHandler();
HttpMessageHandler handler = CreateProductUnderTest(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 = CreateRequestWithOwinContextAndRequestContext(context))
{
// Act
HttpResponseMessage ignore = await handler.SendAsync(request, CancellationToken.None);
}
// 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 PassiveAuthenticationMessageHandler CreateProductUnderTest(HttpMessageHandler innerHandler)
{
PassiveAuthenticationMessageHandler handler = new PassiveAuthenticationMessageHandler();
handler.InnerHandler = innerHandler;
return handler;
}
private static HttpRequestMessage CreateRequestWithOwinContextAndRequestContext(IOwinContext context)
{
HttpRequestMessage request = new HttpRequestMessage();
request.SetOwinContext(context);
request.SetRequestContext(new HttpRequestContext());
return request;
}
private static HttpMessageHandler CreateStubHandler()
{
return new LambdaHttpMessageHandler((ignore1, ignore2) =>
{
return Task.FromResult<HttpResponseMessage>(null);
});
}
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);
}
}
}
}