@@ -15,9 +15,66 @@ namespace Microsoft.AspNet.Facebook.Test
15
15
public class FacebookAuthorizeFilterHookTest
16
16
{
17
17
[ Theory ]
18
- [ InlineData ( "http://example.com" , "email" , true ) ]
19
- [ InlineData ( "http://example.com?error=access_denied" , "email" , false ) ]
20
- [ InlineData ( "http://example.com?error=access_denied" , null , false ) ]
18
+ [ InlineData ( "~/home/cannotcreatecookies" , "https://apps.facebook.com/DefaultAppId/home/cannotcreatecookies" ) ]
19
+ [ InlineData ( null , "https://www.facebook.com/" ) ]
20
+ public void OnAuthorization_CannotCreateCookiesHookRedirectsToConfigValueOrDefault (
21
+ string cannotCreateCookiesRedirectPath ,
22
+ string expectedRedirectPath )
23
+ {
24
+ // Arrange
25
+ var config = BuildConfiguration ( "~/home/permissions" , cannotCreateCookiesRedirectPath ) ;
26
+ var authorizeFilter = new FacebookAuthorizeFilter ( config ) ;
27
+ var context = BuildSignedAuthorizationContext ( "http://contoso.com?__fb_mps=" , "email" ) ;
28
+
29
+ // Act
30
+ authorizeFilter . OnAuthorization ( context ) ;
31
+ var result = context . Result as JavaScriptRedirectResult ;
32
+
33
+ // Assert
34
+ Assert . Equal ( result . RedirectUrl . AbsoluteUri , new Uri ( expectedRedirectPath ) . AbsoluteUri ) ;
35
+ }
36
+
37
+ [ Fact ]
38
+ public void OnAuthorization_OnlyTriggersCannotCreateCookiesHook ( )
39
+ {
40
+ // Arrange
41
+ var config = BuildConfiguration ( "~/home/permissions" ) ;
42
+ var authorizeFilter = new CustomDefaultAuthorizeFilter ( config ) ;
43
+ var context = BuildSignedAuthorizationContext ( "http://contoso.com?__fb_mps=" , "email" ) ;
44
+
45
+ // Act
46
+ authorizeFilter . OnAuthorization ( context ) ;
47
+
48
+ // Assert
49
+ Assert . True ( authorizeFilter . CannotCreateCookiesHookTriggered ) ;
50
+ Assert . False ( authorizeFilter . PermissionPromptHookTriggered ) ;
51
+ Assert . False ( authorizeFilter . DeniedPermissionPromptHookTriggered ) ;
52
+ }
53
+
54
+ [ Theory ]
55
+ [ InlineData ( "http://contoso.com?__fb_mps=" , "email" , true ) ]
56
+ [ InlineData ( "http://contoso.com" , "email" , false ) ]
57
+ [ InlineData ( "http://contoso.com?__fb_mps=" , null , false ) ]
58
+ public void OnAuthorization_TriggersCannotCreateCookiesHook ( string requestUrl ,
59
+ string permission ,
60
+ bool expectedTrigger )
61
+ {
62
+ // Arrange
63
+ var config = BuildConfiguration ( "~/home/permissions" ) ;
64
+ var authorizeFilter = new CustomDefaultAuthorizeFilter ( config ) ;
65
+ var context = BuildSignedAuthorizationContext ( requestUrl , permission ) ;
66
+
67
+ // Act
68
+ authorizeFilter . OnAuthorization ( context ) ;
69
+
70
+ // Assert
71
+ Assert . Equal ( expectedTrigger , authorizeFilter . CannotCreateCookiesHookTriggered ) ;
72
+ }
73
+
74
+ [ Theory ]
75
+ [ InlineData ( "http://contoso.com" , "email" , true ) ]
76
+ [ InlineData ( "http://contoso.com?error=access_denied" , "email" , false ) ]
77
+ [ InlineData ( "http://contoso.com?error=access_denied" , null , false ) ]
21
78
public void OnAuthorization_TriggersPreHookPriorToPermissionsDialog ( string requestUrl ,
22
79
string permission ,
23
80
bool expectedTrigger )
@@ -35,9 +92,9 @@ public void OnAuthorization_TriggersPreHookPriorToPermissionsDialog(string reque
35
92
}
36
93
37
94
[ Theory ]
38
- [ InlineData ( "http://example .com" , "email" , true ) ]
39
- [ InlineData ( "http://example .com?error=access_denied" , "email" , false ) ]
40
- [ InlineData ( "http://example .com?error=access_denied" , null , false ) ]
95
+ [ InlineData ( "http://contoso .com" , "email" , true ) ]
96
+ [ InlineData ( "http://contoso .com?error=access_denied" , "email" , false ) ]
97
+ [ InlineData ( "http://contoso .com?error=access_denied" , null , false ) ]
41
98
public void OnAuthorization_TriggersDeniedHook ( string requestUrl , string permission , bool expectedTrigger )
42
99
{
43
100
// Arrange
@@ -57,11 +114,11 @@ public void OnAuthorization_TriggersDeniedHook(string requestUrl, string permiss
57
114
}
58
115
59
116
[ Theory ]
60
- [ InlineData ( "http://example .com" , "email" , "email" , true ) ]
61
- [ InlineData ( "http://example .com" , "email" , "foo" , false ) ]
62
- [ InlineData ( "http://example .com?error=access_denied" , "email" , "email" , false ) ]
63
- [ InlineData ( "http://example .com?error=access_denied" , "email" , "foo" , false ) ]
64
- [ InlineData ( "http://example .com?error=access_denied" , null , "foo" , false ) ]
117
+ [ InlineData ( "http://contoso .com" , "email" , "email" , true ) ]
118
+ [ InlineData ( "http://contoso .com" , "email" , "foo" , false ) ]
119
+ [ InlineData ( "http://contoso .com?error=access_denied" , "email" , "email" , false ) ]
120
+ [ InlineData ( "http://contoso .com?error=access_denied" , "email" , "foo" , false ) ]
121
+ [ InlineData ( "http://contoso .com?error=access_denied" , null , "foo" , false ) ]
65
122
public void OnAuthorization_TriggersDeniedHookWithRevokedPermissions ( string requestUrl ,
66
123
string permission ,
67
124
string permissionInStatus ,
@@ -89,9 +146,9 @@ public void OnAuthorization_TriggersDeniedHookWithRevokedPermissions(string requ
89
146
}
90
147
91
148
[ Theory ]
92
- [ InlineData ( "http://example .com" , "email" , true ) ]
93
- [ InlineData ( "http://example .com?error=access_denied" , "email" , false ) ]
94
- [ InlineData ( "http://example .com?error=access_denied" , null , false ) ]
149
+ [ InlineData ( "http://contoso .com" , "email" , true ) ]
150
+ [ InlineData ( "http://contoso .com?error=access_denied" , "email" , false ) ]
151
+ [ InlineData ( "http://contoso .com?error=access_denied" , null , false ) ]
95
152
public void OnAuthorization_TriggersDeniedHookAfterPersistingRequestedPermissions ( string requestUrl ,
96
153
string permission ,
97
154
bool expectedTrigger )
@@ -122,13 +179,28 @@ public void OnAuthorization_TriggersDeniedHookAfterPersistingRequestedPermission
122
179
Assert . Equal ( expectedTrigger , authorizeFilter . DeniedPermissionPromptHookTriggered ) ;
123
180
}
124
181
182
+ [ Fact ]
183
+ public void OnAuthorization_CannotCreateCookiesHookNullFlows ( )
184
+ {
185
+ // Arrange
186
+ var config = BuildConfiguration ( "~/home/permissions" ) ;
187
+ var authorizeFilter = new CustomInvalidAuthorizeFilter ( config ) ;
188
+ var context = BuildSignedAuthorizationContext ( "http://contoso.com?__fb_mps=" , "email" ) ;
189
+
190
+ // Act
191
+ authorizeFilter . OnAuthorization ( context ) ;
192
+
193
+ // Assert
194
+ Assert . Null ( context . Result ) ;
195
+ }
196
+
125
197
[ Fact ]
126
198
public void OnAuthorization_PreHookNullTreatedLikeIgnoreResult ( )
127
199
{
128
200
// Arrange
129
201
var config = BuildConfiguration ( "~/home/permissions" ) ;
130
202
var authorizeFilter = new CustomInvalidAuthorizeFilter ( config ) ;
131
- var context = BuildSignedAuthorizationContext ( "http://www.example .com" , "email" ) ;
203
+ var context = BuildSignedAuthorizationContext ( "http://contoso .com" , "email" ) ;
132
204
133
205
// Act
134
206
authorizeFilter . OnAuthorization ( context ) ;
@@ -147,7 +219,7 @@ public void OnAuthorization_DeniedHookNullTreatedLikeIgnoreResult()
147
219
persistedCookies . Add (
148
220
new HttpCookie (
149
221
PermissionHelper . RequestedPermissionCookieName , "email" ) ) ;
150
- var context = BuildSignedAuthorizationContext ( "http://www.example .com" , "email" , persistedCookies ) ;
222
+ var context = BuildSignedAuthorizationContext ( "http://contoso .com" , "email" , persistedCookies ) ;
151
223
152
224
// Act
153
225
authorizeFilter . OnAuthorization ( context ) ;
@@ -156,14 +228,35 @@ public void OnAuthorization_DeniedHookNullTreatedLikeIgnoreResult()
156
228
Assert . Null ( context . Result ) ;
157
229
}
158
230
231
+ [ Fact ]
232
+ public void OnAuthorization_CannotCreateCookiesHookCustomActionResultIsContextsResult ( )
233
+ {
234
+ // Arrange
235
+ var tempUrl = "http://contoso.com?__fb_mps=" ;
236
+ var config = BuildConfiguration ( "~/home/permissions" ) ;
237
+ var cannotCreateCookiesHookResult = new RedirectResult ( tempUrl ) ;
238
+ var authorizeFilter = new CustomReturningAuthorizeFilter ( config ,
239
+ cannotCreateCookiesHookResult ,
240
+ new RedirectResult ( tempUrl ) ,
241
+ new RedirectResult ( tempUrl ) ) ;
242
+ var context = BuildSignedAuthorizationContext ( tempUrl , "email" ) ;
243
+
244
+ // Act
245
+ authorizeFilter . OnAuthorization ( context ) ;
246
+
247
+ // Assert
248
+ Assert . Equal ( cannotCreateCookiesHookResult , context . Result ) ;
249
+ }
250
+
159
251
[ Fact ]
160
252
public void OnAuthorization_PreHookCustomActionResultIsContextsResult ( )
161
253
{
162
254
// Arrange
163
- var tempUrl = "http://www.example .com" ;
255
+ var tempUrl = "http://contoso .com" ;
164
256
var config = BuildConfiguration ( "~/home/permissions" ) ;
165
257
var preHookResult = new RedirectResult ( tempUrl ) ;
166
258
var authorizeFilter = new CustomReturningAuthorizeFilter ( config ,
259
+ new RedirectResult ( tempUrl ) ,
167
260
preHookResult ,
168
261
new RedirectResult ( tempUrl ) ) ;
169
262
var context = BuildSignedAuthorizationContext ( tempUrl , "email" ) ;
@@ -179,10 +272,11 @@ public void OnAuthorization_PreHookCustomActionResultIsContextsResult()
179
272
public void OnAuthorization_DeniedHookCustomActionResultIsContextsResult ( )
180
273
{
181
274
// Arrange
182
- var tempUrl = "http://www.example .com" ;
275
+ var tempUrl = "http://contoso .com" ;
183
276
var config = BuildConfiguration ( "~/home/permissions" ) ;
184
277
var deniedHookResult = new RedirectResult ( tempUrl ) ;
185
278
var authorizeFilter = new CustomReturningAuthorizeFilter ( config ,
279
+ new RedirectResult ( tempUrl ) ,
186
280
new RedirectResult ( tempUrl ) ,
187
281
deniedHookResult ) ;
188
282
var persistedCookies = new HttpCookieCollection ( ) ;
@@ -200,13 +294,19 @@ public void OnAuthorization_DeniedHookCustomActionResultIsContextsResult()
200
294
201
295
// Helper methods and classes
202
296
private FacebookConfiguration BuildConfiguration ( string authorizationRedirectPath ,
297
+ string cannotCreateCookiesRedirectPath = null ,
203
298
PermissionsStatus userPermissionsStatus = null )
204
299
{
205
300
var client = MockHelpers . CreateFacebookClient ( ) ;
206
301
var permissionService = MockHelpers . CreatePermissionService ( new [ ] { "" } , userPermissionsStatus ) ;
207
302
var config = MockHelpers . CreateConfiguration ( client , permissionService ) ;
208
303
config . AuthorizationRedirectPath = authorizationRedirectPath ;
209
304
305
+ if ( cannotCreateCookiesRedirectPath != null )
306
+ {
307
+ config . CannotCreateCookieRedirectPath = cannotCreateCookiesRedirectPath ;
308
+ }
309
+
210
310
return config ;
211
311
}
212
312
@@ -216,13 +316,15 @@ private AuthorizationContext BuildSignedAuthorizationContext(string requestUrl,
216
316
{
217
317
var permissions = permission == null ? new string [ 0 ] : new string [ ] { permission } ;
218
318
319
+ var requestUri = new Uri ( requestUrl ) ;
320
+
219
321
var context = new AuthorizationContext (
220
322
MockHelpers . CreateControllerContext ( new NameValueCollection
221
323
{
222
324
{ "signed_request" , "exampleSignedRequest" }
223
325
} ,
224
- null ,
225
- new Uri ( requestUrl ) ,
326
+ HttpUtility . ParseQueryString ( requestUri . Query ) ,
327
+ requestUri ,
226
328
requestCookies ) ,
227
329
MockHelpers . CreateActionDescriptor ( new [ ] { new FacebookAuthorizeAttribute ( permissions ) } ) ) ;
228
330
@@ -235,6 +337,11 @@ public CustomInvalidAuthorizeFilter(FacebookConfiguration config)
235
337
: base ( config )
236
338
{ }
237
339
340
+ protected override void OnCannotCreateCookies ( PermissionContext context )
341
+ {
342
+ context . Result = null ;
343
+ }
344
+
238
345
protected override void OnPermissionPrompt ( PermissionContext context )
239
346
{
240
347
context . Result = null ;
@@ -252,9 +359,17 @@ public CustomDefaultAuthorizeFilter(FacebookConfiguration config)
252
359
: base ( config )
253
360
{ }
254
361
362
+ public bool CannotCreateCookiesHookTriggered { get ; private set ; }
255
363
public bool PermissionPromptHookTriggered { get ; private set ; }
256
364
public bool DeniedPermissionPromptHookTriggered { get ; private set ; }
257
365
366
+ protected override void OnCannotCreateCookies ( PermissionContext context )
367
+ {
368
+ CannotCreateCookiesHookTriggered = true ;
369
+
370
+ base . OnCannotCreateCookies ( context ) ;
371
+ }
372
+
258
373
protected override void OnPermissionPrompt ( PermissionContext context )
259
374
{
260
375
PermissionPromptHookTriggered = true ;
@@ -272,18 +387,26 @@ protected override void OnDeniedPermissionPrompt(PermissionContext context)
272
387
273
388
private class CustomReturningAuthorizeFilter : FacebookAuthorizeFilter
274
389
{
390
+ private ActionResult _cannotCreateCookieResult ;
275
391
private ActionResult _promptPermissionHookResult ;
276
392
private ActionResult _deniedPermissionPromptHookResult ;
277
393
278
394
public CustomReturningAuthorizeFilter ( FacebookConfiguration config ,
395
+ ActionResult cannotCreateCookieResult ,
279
396
ActionResult promptPermissionHookResult ,
280
397
ActionResult deniedPermissionPromptHookResult )
281
398
: base ( config )
282
399
{
400
+ _cannotCreateCookieResult = cannotCreateCookieResult ;
283
401
_promptPermissionHookResult = promptPermissionHookResult ;
284
402
_deniedPermissionPromptHookResult = deniedPermissionPromptHookResult ;
285
403
}
286
404
405
+ protected override void OnCannotCreateCookies ( PermissionContext context )
406
+ {
407
+ context . Result = _cannotCreateCookieResult ;
408
+ }
409
+
287
410
protected override void OnPermissionPrompt ( PermissionContext context )
288
411
{
289
412
context . Result = _promptPermissionHookResult ;
0 commit comments