Example #1
0
        public async Task DupeUserClaimTest()
        {
            var db = UnitTestHelper.CreateDefaultDb();
            var manager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(db));
            var user = new IdentityUser("u1");
            var result = await manager.CreateAsync(user);
            Assert.NotNull(user);
            var claims = new[]
            {
                new Claim("c1", "v1"),
                new Claim("c2", "v2"),
                new Claim("c3", "v3")
            };
            foreach (Claim c in claims)
            {
                // Add dupes
                UnitTestHelper.IsSuccess(await manager.AddClaimAsync(user.Id, c));
                UnitTestHelper.IsSuccess(await manager.AddClaimAsync(user.Id, c));
            }

            var userClaims = new List<Claim>(await manager.GetClaimsAsync(user.Id));
            Assert.Equal(6, userClaims.Count);
            var currentExpected = 6;
            foreach (Claim c in claims)
            {
                Assert.True(userClaims.Exists(u => u.Type == c.Type && u.Value == c.Value));
                UnitTestHelper.IsSuccess(await manager.RemoveClaimAsync(user.Id, c));
                var cs = await manager.GetClaimsAsync(user.Id);
                currentExpected -= 2;
                Assert.Equal(currentExpected, cs.Count());
                Assert.Equal(currentExpected, db.Set<IdentityUserClaim>().Count());
            }
        }
      public async Task Delete(int? gameID)
      {
         if (gameID == null)
         {
            throw new ArgumentNullException("gameID");
         }

         var id = gameID.Value;

         var game = await db.Games.FindAsync(id);

         if (game == null)
         {
            throw new ArgumentException("Could not find game with id " + id);
         }
         var userID = this.User.Identity.GetUserId();

         var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this.db));

         var appUser = await manager.FindByIdAsync(this.User.Identity.GetUserId());

         if (!DoesUserOwnGame(appUser, id))
         {
            throw new ArgumentException("Could not delete game");
         }

         var memberships = await db.GameMemberships.Where(m => m.GameID == id).Include(m => m.ApplicationUser).ToListAsync();

         var claims = await Task.WhenAll(memberships.Select(m => m.ApplicationUser).Select(user =>
            manager.GetClaimsAsync(user.Id).ContinueWith(t => new { Claims = t.Result, User = user })));

         var strId = id.ToString();
         var removeTasks = from tuple in claims
                           from claim in tuple.Claims
                           where (claim.Type == "GameOwnership" || claim.Type == "GameOwnership" || claim.Type == "GameMaster") && claim.Value == strId
                           select manager.RemoveClaimAsync(tuple.User.Id, claim);
         await Task.WhenAll(removeTasks);


         db.GameMemberships.RemoveRange(memberships);
         db.Games.Remove(game);

         await db.SaveChangesAsync();
      }