1 // 2 // ClaimsIdentityTest.cs - NUnit Test Cases for System.Security.Claims.ClaimsIdentity 3 // 4 5 6 using NUnit.Framework; 7 using System; 8 using System.Collections.Generic; 9 using System.Linq; 10 using System.Security.Claims; 11 using System.Security.Principal; 12 13 namespace MonoTests.System.Security.Claims 14 { 15 [TestFixture] 16 public class ClaimsIdentityTest 17 { 18 #region Ctor Empty 19 20 [Test] EmptyCtorWorks()21 public void EmptyCtorWorks () 22 { 23 var id = new ClaimsIdentity (); 24 Assert.IsNull (id.AuthenticationType, "#1"); 25 Assert.IsNull (id.Actor, "#2"); 26 Assert.IsNull (id.BootstrapContext, "#3"); 27 Assert.IsNotNull (id.Claims, "#4"); 28 Assert.AreEqual (0, id.Claims.Count (), "#5"); 29 Assert.IsFalse (id.IsAuthenticated, "#6"); 30 Assert.IsNull (id.Label, "#7"); 31 Assert.IsNull (id.Name, "#8"); 32 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 33 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 34 } 35 36 #endregion 37 38 #region Ctor AuthTypeOnly 39 40 [Test] AuthTypeOnlyCtorEmptyWorks()41 public void AuthTypeOnlyCtorEmptyWorks () 42 { 43 var id = new ClaimsIdentity (""); 44 //NOTE: According to MSDN the id.AuthenticationType should be null, but it isn't on .Net 4.5 45 //Assert.IsNull (id.AuthenticationType, "#1"); 46 Assert.AreEqual ("", id.AuthenticationType, "#1"); 47 Assert.IsNull (id.Actor, "#2"); 48 Assert.IsNull (id.BootstrapContext, "#3"); 49 Assert.IsNotNull (id.Claims, "#4"); 50 Assert.AreEqual (0, id.Claims.Count (), "#5"); 51 Assert.IsFalse (id.IsAuthenticated, "#6"); 52 Assert.IsNull (id.Label, "#7"); 53 Assert.IsNull (id.Name, "#8"); 54 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 55 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 56 } 57 58 [Test] AuthTypeOnlyCtorNullWorks()59 public void AuthTypeOnlyCtorNullWorks () 60 { 61 var id = new ClaimsIdentity ((string)null); 62 Assert.IsNull (id.AuthenticationType, "#1"); 63 Assert.IsNull (id.Actor, "#2"); 64 Assert.IsNull (id.BootstrapContext, "#3"); 65 Assert.IsNotNull (id.Claims, "#4"); 66 Assert.AreEqual (0, id.Claims.Count (), "#5"); 67 Assert.IsFalse (id.IsAuthenticated, "#6"); 68 Assert.IsNull (id.Label, "#7"); 69 Assert.IsNull (id.Name, "#8"); 70 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 71 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 72 } 73 74 [Test] AuthTypeOnlyCtorNotEmptyWorks()75 public void AuthTypeOnlyCtorNotEmptyWorks () 76 { 77 var id = new ClaimsIdentity ("auth_type"); 78 Assert.AreEqual ("auth_type", id.AuthenticationType, "#1"); 79 Assert.IsNull (id.Actor, "#2"); 80 Assert.IsNull (id.BootstrapContext, "#3"); 81 Assert.IsNotNull (id.Claims, "#4"); 82 Assert.AreEqual (0, id.Claims.Count (), "#5"); 83 Assert.IsTrue (id.IsAuthenticated, "#6"); 84 Assert.IsNull (id.Label, "#7"); 85 Assert.IsNull (id.Name, "#8"); 86 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 87 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 88 } 89 90 #endregion 91 92 #region Ctor IEnumerable<Claim> 93 94 [Test] EnumClaimsCtorNullWorks()95 public void EnumClaimsCtorNullWorks () 96 { 97 var id = new ClaimsIdentity ((IEnumerable<Claim>)null); 98 Assert.IsNull (id.AuthenticationType, "#1"); 99 Assert.IsNull (id.Actor, "#2"); 100 Assert.IsNull (id.BootstrapContext, "#3"); 101 Assert.IsNotNull (id.Claims, "#4"); 102 Assert.AreEqual (0, id.Claims.Count (), "#5"); 103 Assert.IsFalse (id.IsAuthenticated, "#6"); 104 Assert.IsNull (id.Label, "#7"); 105 Assert.IsNull (id.Name, "#8"); 106 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 107 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 108 } 109 110 [Test] EnumClaimsCtorEmptyWorks()111 public void EnumClaimsCtorEmptyWorks () 112 { 113 var id = new ClaimsIdentity ((IEnumerable<Claim>)null); 114 Assert.IsNull (id.AuthenticationType, "#1"); 115 Assert.IsNull (id.Actor, "#2"); 116 Assert.IsNull (id.BootstrapContext, "#3"); 117 Assert.IsNotNull (id.Claims, "#4"); 118 Assert.AreEqual (0, id.Claims.Count (), "#5"); 119 Assert.IsFalse (id.IsAuthenticated, "#6"); 120 Assert.IsNull (id.Label, "#7"); 121 Assert.IsNull (id.Name, "#8"); 122 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 123 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 124 } 125 126 [Test] EnumClaimsCtorWithClaimsWithNameWorks()127 public void EnumClaimsCtorWithClaimsWithNameWorks () 128 { 129 var id = new ClaimsIdentity ( 130 new [] { 131 new Claim ("claim_type", "claim_value"), 132 new Claim (ClaimsIdentity.DefaultNameClaimType, "claim_name_value"), 133 }); 134 Assert.IsNull (id.AuthenticationType, "#1"); 135 Assert.IsNull (id.Actor, "#2"); 136 Assert.IsNull (id.BootstrapContext, "#3"); 137 Assert.IsNotNull (id.Claims, "#4"); 138 Assert.AreEqual (2, id.Claims.Count (), "#5"); 139 Assert.IsFalse (id.IsAuthenticated, "#6"); 140 Assert.IsNull (id.Label, "#7"); 141 Assert.AreEqual ("claim_name_value", id.Name, "#8"); 142 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 143 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 144 } 145 146 [Test] EnumClaimsCtorWithClaimsWithoutNameWorks()147 public void EnumClaimsCtorWithClaimsWithoutNameWorks () 148 { 149 var id = new ClaimsIdentity ( 150 new [] { 151 new Claim ("claim_type", "claim_value"), 152 new Claim (ClaimsIdentity.DefaultNameClaimType + "_x", "claim_name_value"), 153 }); 154 Assert.IsNull (id.AuthenticationType, "#1"); 155 Assert.IsNull (id.Actor, "#2"); 156 Assert.IsNull (id.BootstrapContext, "#3"); 157 Assert.IsNotNull (id.Claims, "#4"); 158 Assert.AreEqual (2, id.Claims.Count (), "#5"); 159 Assert.IsFalse (id.IsAuthenticated, "#6"); 160 Assert.IsNull (id.Label, "#7"); 161 Assert.IsNull (id.Name, "#8"); 162 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 163 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 164 } 165 166 #endregion 167 168 #region Ctor IEnumerable<Claim>, authType, nameType, roleType 169 170 [Test] EnumClaimsAuthNameRoleTypeCtorWorks()171 public void EnumClaimsAuthNameRoleTypeCtorWorks () 172 { 173 var id = new ClaimsIdentity (new [] { 174 new Claim ("claim_type", "claim_value"), 175 new Claim (ClaimsIdentity.DefaultNameClaimType, "claim_name_value"), 176 new Claim ("claim_role_type", "claim_role_value"), 177 }, 178 "test_auth_type", "test_name_type", "claim_role_type"); 179 Assert.AreEqual ("test_auth_type", id.AuthenticationType, "#1"); 180 Assert.IsNull (id.Actor, "#2"); 181 Assert.IsNull (id.BootstrapContext, "#3"); 182 Assert.IsNotNull (id.Claims, "#4"); 183 Assert.AreEqual (3, id.Claims.Count (), "#5"); 184 Assert.IsTrue (id.IsAuthenticated, "#6"); 185 Assert.IsNull (id.Label, "#7"); 186 Assert.IsNull (id.Name, "#8"); 187 Assert.AreEqual ("test_name_type", id.NameClaimType, "#9"); 188 Assert.AreEqual ("claim_role_type", id.RoleClaimType, "#10"); 189 } 190 191 [Test] EnumClaimsAuthNameRoleTypeCtorAllArgsNullWorks()192 public void EnumClaimsAuthNameRoleTypeCtorAllArgsNullWorks () 193 { 194 var id = new ClaimsIdentity ((IEnumerable<Claim>)null, (string)null, (string)null, (string)null); 195 Assert.IsNull (id.AuthenticationType, "#1"); 196 Assert.IsNull (id.Actor, "#2"); 197 Assert.IsNull (id.BootstrapContext, "#3"); 198 Assert.IsNotNull (id.Claims, "#4"); 199 Assert.AreEqual (0, id.Claims.Count (), "#5"); 200 Assert.IsFalse (id.IsAuthenticated, "#6"); 201 Assert.IsNull (id.Label, "#7"); 202 Assert.IsNull (id.Name, "#8"); 203 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 204 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 205 } 206 207 [Test] EnumClaimsAuthNameRoleTypeCtorAllArgsEmptyWorks()208 public void EnumClaimsAuthNameRoleTypeCtorAllArgsEmptyWorks () 209 { 210 var id = new ClaimsIdentity (new Claim [0], "", "", ""); 211 //NOTE: According to MSDN the id.AuthenticationType should be null, but it isn't on .Net 4.5 212 //Assert.IsNull (id.AuthenticationType, "#1"); 213 Assert.AreEqual ("", id.AuthenticationType, "#1"); 214 Assert.IsNull (id.Actor, "#2"); 215 Assert.IsNull (id.BootstrapContext, "#3"); 216 Assert.IsNotNull (id.Claims, "#4"); 217 Assert.AreEqual (0, id.Claims.Count (), "#5"); 218 Assert.IsFalse (id.IsAuthenticated, "#6"); 219 Assert.IsNull (id.Label, "#7"); 220 Assert.IsNull (id.Name, "#8"); 221 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 222 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 223 } 224 225 [Test] EnumClaimsAuthNameRoleTypeCtorWithTwoClaimsAndTypesEmptyWorks()226 public void EnumClaimsAuthNameRoleTypeCtorWithTwoClaimsAndTypesEmptyWorks () 227 { 228 var id = new ClaimsIdentity ( 229 new [] { 230 new Claim ("claim_type", "claim_value"), 231 new Claim (ClaimsIdentity.DefaultNameClaimType, "claim_name_value"), 232 }, 233 "", "", ""); 234 //NOTE: According to MSDN the id.AuthenticationType should be null, but it isn't on .Net 4.5 235 //Assert.IsNull (id.AuthenticationType, "#1"); 236 Assert.AreEqual ("", id.AuthenticationType, "#1"); 237 Assert.IsNull (id.Actor, "#2"); 238 Assert.IsNull (id.BootstrapContext, "#3"); 239 Assert.IsNotNull (id.Claims, "#4"); 240 Assert.AreEqual (2, id.Claims.Count (), "#5"); 241 Assert.IsFalse (id.IsAuthenticated, "#6"); 242 Assert.IsNull (id.Label, "#7"); 243 Assert.AreEqual ("claim_name_value", id.Name, "#8"); 244 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 245 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 246 } 247 248 [Test] EnumClaimsAuthNameRoleTypeCtorWithTwoClaimsAndTypesNullWorks()249 public void EnumClaimsAuthNameRoleTypeCtorWithTwoClaimsAndTypesNullWorks () 250 { 251 var id = new ClaimsIdentity ( 252 new [] { 253 new Claim ("claim_type", "claim_value"), 254 new Claim (ClaimsIdentity.DefaultNameClaimType, "claim_name_value"), 255 }, 256 (string)null, (string)null, (string)null); 257 Assert.IsNull (id.AuthenticationType, "#1"); 258 Assert.IsNull (id.Actor, "#2"); 259 Assert.IsNull (id.BootstrapContext, "#3"); 260 Assert.IsNotNull (id.Claims, "#4"); 261 Assert.AreEqual (2, id.Claims.Count (), "#5"); 262 Assert.IsFalse (id.IsAuthenticated, "#6"); 263 Assert.IsNull (id.Label, "#7"); 264 Assert.AreEqual ("claim_name_value", id.Name, "#8"); 265 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 266 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 267 } 268 269 #endregion 270 271 #region Ctor IIdentity, IEnumerable<Claim>, authType, nameType, roleType 272 273 [Test] IdentityEnumClaimsAuthNameRoleTypeCtorNullsWorks()274 public void IdentityEnumClaimsAuthNameRoleTypeCtorNullsWorks () 275 { 276 var id = new ClaimsIdentity ((IIdentity)null, (IEnumerable<Claim>)null, (string)null, (string)null, (string)null); 277 Assert.IsNull (id.AuthenticationType, "#1"); 278 Assert.IsNull (id.Actor, "#2"); 279 Assert.IsNull (id.BootstrapContext, "#3"); 280 Assert.IsNotNull (id.Claims, "#4"); 281 Assert.AreEqual (0, id.Claims.Count (), "#5"); 282 Assert.IsFalse (id.IsAuthenticated, "#6"); 283 Assert.IsNull (id.Label, "#7"); 284 Assert.IsNull (id.Name, "#8"); 285 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 286 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 287 } 288 289 [Test] IdentityEnumClaimsAuthNameRoleTypeCtorIdentityNullRestEmptyWorks()290 public void IdentityEnumClaimsAuthNameRoleTypeCtorIdentityNullRestEmptyWorks () 291 { 292 var id = new ClaimsIdentity (null, new Claim [0], "", "", ""); 293 //NOTE: According to MSDN the id.AuthenticationType should be null, but it isn't on .Net 4.5 294 //Assert.IsNull (id.AuthenticationType, "#1"); 295 Assert.AreEqual ("", id.AuthenticationType, "#1"); 296 Assert.IsNull (id.Actor, "#2"); 297 Assert.IsNull (id.BootstrapContext, "#3"); 298 Assert.IsNotNull (id.Claims, "#4"); 299 Assert.AreEqual (0, id.Claims.Count (), "#5"); 300 Assert.IsFalse (id.IsAuthenticated, "#6"); 301 Assert.IsNull (id.Label, "#7"); 302 Assert.IsNull (id.Name, "#8"); 303 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 304 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 305 } 306 307 [Test] IdentityEnumClaimsAuthNameRoleTypeCtorNullClaimsArrayEmptyTypesWorks()308 public void IdentityEnumClaimsAuthNameRoleTypeCtorNullClaimsArrayEmptyTypesWorks () 309 { 310 var id = new ClaimsIdentity ( 311 null, 312 new [] { 313 new Claim ("claim_type", "claim_value"), 314 new Claim (ClaimsIdentity.DefaultNameClaimType, "claim_name_value"), 315 }, 316 "", "", ""); 317 //NOTE: According to MSDN the id.AuthenticationType should be null, but it isn't on .Net 4.5 318 //Assert.IsNull (id.AuthenticationType, "#1"); 319 Assert.AreEqual ("", id.AuthenticationType, "#1"); 320 Assert.IsNull (id.Actor, "#2"); 321 Assert.IsNull (id.BootstrapContext, "#3"); 322 Assert.IsNotNull (id.Claims, "#4"); 323 Assert.AreEqual (2, id.Claims.Count (), "#5"); 324 Assert.IsFalse (id.IsAuthenticated, "#6"); 325 Assert.IsNull (id.Label, "#7"); 326 Assert.AreEqual ("claim_name_value", id.Name, "#8"); 327 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 328 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 329 } 330 331 [Test] IdentityEnumClaimsAuthNameRoleTypeCtorNullClaimsArrayNullsWorks()332 public void IdentityEnumClaimsAuthNameRoleTypeCtorNullClaimsArrayNullsWorks () 333 { 334 var id = new ClaimsIdentity ( 335 null, 336 new [] { 337 new Claim ("claim_type", "claim_value"), 338 new Claim (ClaimsIdentity.DefaultNameClaimType, "claim_name_value"), 339 }, 340 (string)null, (string)null, (string)null); 341 Assert.IsNull (id.AuthenticationType, "#1"); 342 Assert.IsNull (id.Actor, "#2"); 343 Assert.IsNull (id.BootstrapContext, "#3"); 344 Assert.IsNotNull (id.Claims, "#4"); 345 Assert.AreEqual (2, id.Claims.Count (), "#5"); 346 Assert.IsFalse (id.IsAuthenticated, "#6"); 347 Assert.IsNull (id.Label, "#7"); 348 Assert.AreEqual ("claim_name_value", id.Name, "#8"); 349 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 350 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 351 } 352 353 [Test] IdentityEnumClaimsAuthNameRoleTypeCtorNullIdentityRestFilledWorks()354 public void IdentityEnumClaimsAuthNameRoleTypeCtorNullIdentityRestFilledWorks () 355 { 356 var id = new ClaimsIdentity ( 357 null, 358 new [] { 359 new Claim ("claim_type", "claim_value"), 360 new Claim (ClaimsIdentity.DefaultNameClaimType, "claim_name_value"), 361 new Claim ("claim_role_type", "claim_role_value"), 362 }, 363 "test_auth_type", "test_name_type", "claim_role_type"); 364 Assert.AreEqual ("test_auth_type", id.AuthenticationType, "#1"); 365 Assert.IsNull (id.Actor, "#2"); 366 Assert.IsNull (id.BootstrapContext, "#3"); 367 Assert.IsNotNull (id.Claims, "#4"); 368 Assert.AreEqual (3, id.Claims.Count (), "#5"); 369 Assert.IsTrue (id.IsAuthenticated, "#6"); 370 Assert.IsNull (id.Label, "#7"); 371 Assert.IsNull (id.Name, "#8"); 372 Assert.AreEqual ("test_name_type", id.NameClaimType, "#9"); 373 Assert.AreEqual ("claim_role_type", id.RoleClaimType, "#10"); 374 } 375 376 [Test] IdentityEnumClaimsAuthNameRoleTypeCtorClaimsIdentityRestFilledWorks()377 public void IdentityEnumClaimsAuthNameRoleTypeCtorClaimsIdentityRestFilledWorks () 378 { 379 var baseId = new ClaimsIdentity ( 380 new[] { new Claim ("base_claim_type", "base_claim_value") }, 381 "base_auth_type"); 382 383 baseId.Actor = new ClaimsIdentity ("base_actor"); 384 baseId.BootstrapContext = "bootstrap_context"; 385 baseId.Label = "base_label"; 386 387 Assert.IsTrue (baseId.IsAuthenticated, "#0"); 388 389 var id = new ClaimsIdentity ( 390 baseId, 391 new [] { 392 new Claim ("claim_type", "claim_value"), 393 new Claim (ClaimsIdentity.DefaultNameClaimType, "claim_name_value"), 394 new Claim ("claim_role_type", "claim_role_value"), 395 }, 396 "test_auth_type", "test_name_type", "claim_role_type"); 397 398 Assert.AreEqual ("test_auth_type", id.AuthenticationType, "#1"); 399 400 Assert.IsNotNull (id.Actor, "#2"); 401 Assert.AreEqual ("base_actor", id.Actor.AuthenticationType, "#2b"); 402 Assert.AreEqual ("bootstrap_context", id.BootstrapContext, "#3"); 403 Assert.IsNotNull (id.Claims, "#4"); 404 Assert.AreEqual (4, id.Claims.Count (), "#5"); 405 Assert.AreEqual ("base_claim_type", id.Claims.First ().Type); 406 Assert.IsTrue (id.IsAuthenticated, "#6"); 407 Assert.AreEqual ("base_label", id.Label, "#7"); 408 Assert.IsNull (id.Name, "#8"); 409 Assert.AreEqual ("test_name_type", id.NameClaimType, "#9"); 410 Assert.AreEqual ("claim_role_type", id.RoleClaimType, "#10"); 411 } 412 413 [Test] IdentityEnumClaimsAuthNameRoleTypeCtorNonClaimsIdentityRestEmptyWorks()414 public void IdentityEnumClaimsAuthNameRoleTypeCtorNonClaimsIdentityRestEmptyWorks () 415 { 416 var baseId = new TestIdentity { Name = "base_name", AuthenticationType = "TestId_AuthType" }; 417 418 var id = new ClaimsIdentity ( 419 baseId, 420 new [] { 421 new Claim ("claim_type", "claim_value"), 422 new Claim (ClaimsIdentity.DefaultNameClaimType, "claim_name_value"), 423 new Claim ("claim_role_type", "claim_role_value"), 424 }, 425 "", "", ""); 426 427 Assert.AreEqual ("TestId_AuthType", id.AuthenticationType, "#1"); 428 429 Assert.IsNull (id.Actor, "#2"); 430 Assert.IsNull (id.BootstrapContext, "#3"); 431 Assert.IsNotNull (id.Claims, "#4"); 432 Assert.AreEqual (4, id.Claims.Count (), "#5"); 433 Assert.AreEqual (2, id.Claims.Count (_ => _.Type == ClaimsIdentity.DefaultNameClaimType), "#5b"); 434 Assert.IsTrue (id.IsAuthenticated, "#6"); 435 Assert.IsNull (id.Label, "#7"); 436 Assert.AreEqual ("base_name", id.Name, "#8"); 437 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 438 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 439 } 440 441 #endregion 442 443 #region Ctor IIdentity, IEnumerable<Claim> 444 445 [Test] IdentityEnumClaimsCtorClaimsIdentityClaimsWorks()446 public void IdentityEnumClaimsCtorClaimsIdentityClaimsWorks () 447 { 448 var baseId = new ClaimsIdentity ( 449 new [] { new Claim ("base_claim_type", "base_claim_value") }, 450 "base_auth_type", "base_name_claim_type", null); 451 452 baseId.Actor = new ClaimsIdentity ("base_actor"); 453 baseId.BootstrapContext = "bootstrap_context"; 454 baseId.Label = "base_label"; 455 456 Assert.IsTrue (baseId.IsAuthenticated, "#0"); 457 458 var id = new ClaimsIdentity ( 459 baseId, 460 new [] { 461 new Claim ("claim_type", "claim_value"), 462 new Claim (ClaimsIdentity.DefaultNameClaimType, "claim_name_value"), 463 new Claim ("claim_role_type", "claim_role_value"), 464 }); 465 466 Assert.AreEqual ("base_auth_type", id.AuthenticationType, "#1"); 467 468 Assert.IsNotNull (id.Actor, "#2"); 469 Assert.AreEqual ("base_actor", id.Actor.AuthenticationType, "#2b"); 470 Assert.AreEqual ("bootstrap_context", id.BootstrapContext, "#3"); 471 Assert.IsNotNull (id.Claims, "#4"); 472 Assert.AreEqual (4, id.Claims.Count (), "#5"); 473 Assert.AreEqual ("base_claim_type", id.Claims.First ().Type, "#5b"); 474 Assert.IsTrue (id.IsAuthenticated, "#6"); 475 Assert.AreEqual ("base_label", id.Label, "#7"); 476 Assert.IsNull (id.Name, "#8"); 477 Assert.AreEqual ("base_name_claim_type", id.NameClaimType, "#9"); 478 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 479 } 480 481 [Test] IdentityEnumClaimsCtorNonClaimsIdentityClaimsWorks()482 public void IdentityEnumClaimsCtorNonClaimsIdentityClaimsWorks () 483 { 484 var baseId = new TestIdentity { 485 Name = "base_name", AuthenticationType = "TestId_AuthType" 486 }; 487 488 var id = new ClaimsIdentity ( 489 baseId, 490 new [] { 491 new Claim ("claim_type", "claim_value"), 492 new Claim (ClaimsIdentity.DefaultNameClaimType, "claim_name_value"), 493 new Claim ("claim_role_type", "claim_role_value"), 494 }); 495 496 Assert.AreEqual ("TestId_AuthType", id.AuthenticationType, "#1"); 497 498 Assert.IsNull (id.Actor, "#2"); 499 Assert.IsNull (id.BootstrapContext, "#3"); 500 Assert.IsNotNull (id.Claims, "#4"); 501 Assert.AreEqual (4, id.Claims.Count (), "#5"); 502 Assert.AreEqual (2, id.Claims.Count (_ => _.Type == ClaimsIdentity.DefaultNameClaimType), "#5b"); 503 Assert.IsTrue (id.IsAuthenticated, "#6"); 504 Assert.IsNull (id.Label, "#7"); 505 Assert.AreEqual ("base_name", id.Name, "#8"); 506 Assert.AreEqual (ClaimsIdentity.DefaultNameClaimType, id.NameClaimType, "#9"); 507 Assert.AreEqual (ClaimsIdentity.DefaultRoleClaimType, id.RoleClaimType, "#10"); 508 } 509 510 #endregion 511 512 [Test] FindCaseInsensivity()513 public void FindCaseInsensivity () 514 { 515 var claim_type = new Claim("TYpe", "value"); 516 var id = new ClaimsIdentity ( 517 new[] { claim_type }, 518 "base_auth_type", "base_name_claim_type", null); 519 520 var f1 = id.FindFirst ("tyPe"); 521 Assert.AreEqual ("value", f1.Value, "#1"); 522 523 var f2 = id.FindAll ("tyPE").First (); 524 Assert.AreEqual ("value", f2.Value, "#2"); 525 } 526 527 [Test] HasClaim_typeValue_Works()528 public void HasClaim_typeValue_Works() 529 { 530 var id = new ClaimsIdentity( 531 new[] { 532 new Claim ("claim_type", "claim_value"), 533 new Claim (ClaimsIdentity.DefaultNameClaimType, "claim_name_value"), 534 new Claim ("claim_role_type", "claim_role_value"), 535 }, "test_authority"); 536 537 Assert.IsTrue (id.HasClaim("claim_type", "claim_value"), "#1"); 538 Assert.IsTrue (id.HasClaim("cLaIm_TyPe", "claim_value"), "#2"); 539 Assert.IsFalse (id.HasClaim("claim_type", "cLaIm_VaLuE"), "#3"); 540 Assert.IsFalse (id.HasClaim("Xclaim_type", "claim_value"), "#4"); 541 Assert.IsFalse (id.HasClaim("claim_type", "Xclaim_value"), "#5"); 542 } 543 } 544 545 class TestIdentity : IIdentity 546 { 547 public string Name { 548 get; 549 set; 550 } 551 552 public string AuthenticationType { 553 get; 554 set; 555 } 556 557 public bool IsAuthenticated { 558 get { throw new NotImplementedException (); } 559 } 560 } 561 } 562 563