Пример #1
0
        public void SetPasswordHashAsyncTests()
        {
            var target = Target();

            UtilsLol.AssertThrows(() => target.SetPasswordHashAsync(null, null).Wait());
            UtilsLol.AssertThrows(() => target.SetPasswordHashAsync(null, "").Wait());
            UtilsLol.AssertThrows(() => target.SetPasswordHashAsync(null, "  ").Wait());
            UtilsLol.AssertThrows(() => target.SetPasswordHashAsync(null, "derp").Wait());

            var user = new AzureTableUser
            {
                Id       = "derpHash",
                UserName = "******"
            };

            UtilsLol.AssertThrows(() => target.SetPasswordHashAsync(user, null).Wait());
            UtilsLol.AssertThrows(() => target.SetPasswordHashAsync(user, "").Wait());
            UtilsLol.AssertThrows(() => target.SetPasswordHashAsync(user, "  ").Wait());
            target.CreateAsync(user).Wait();
            UtilsLol.AssertThrows(() => target.SetPasswordHashAsync(user, null).Wait());
            UtilsLol.AssertThrows(() => target.SetPasswordHashAsync(user, "").Wait());
            UtilsLol.AssertThrows(() => target.SetPasswordHashAsync(user, "  ").Wait());

            Assert.IsNull(target.GetPasswordHashAsync(user).Result);
            // eh, oh well.
            var hash = "stupid or liar";

            target.SetPasswordHashAsync(user, hash).Wait();
            Assert.AreEqual(hash, target.GetPasswordHashAsync(user).Result);
        }
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new AzureTableUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Пример #3
0
        public void FindByIdAsyncTest()
        {
            // holy crap... this entire class is copypasta
            var target = Target();

            try
            {
                target.FindByIdAsync(null).Wait();
                Assert.Fail("FindByIdAsync didn't throw on null");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentException);
            }
            try
            {
                target.FindByIdAsync("").Wait();
                Assert.Fail("FindByIdAsync didn't throw on empty");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentException);
            }
            try
            {
                target.FindByIdAsync(" ").Wait();
                Assert.Fail("FindByIdAsync didn't throw on whitespace");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentException);
            }

            var user = new AzureTableUser
            {
                Id       = "foo",
                UserName = "******"
            };

            target.CreateAsync(user).Wait();
            Assert.IsNotNull(target.FindByIdAsync(user.Id).Result); // sanity

            target.DeleteAsync(user).Wait();

            Assert.IsNull(target.FindByIdAsync(user.Id).Result);

            target.Dispose();

            try
            {
                target.FindByIdAsync(user.Id).Wait();
                Assert.Fail("FindByIdAsync doesn't throw when disposed");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ObjectDisposedException);
            }
        }
Пример #4
0
        public void CreateAsyncTest()
        {
            // wow, such similar, very copypasta
            var target = Target();
            var user   = new AzureTableUser
            {
                //ASP.NET identity doesn't set the ID on a new user, adding this into the provider
                //Id = "foo",
                UserName = "******"
            };

            try
            {
                target.CreateAsync(null).Wait();
                Assert.Fail("CreateAsync didn't throw on null");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentNullException);
            }

            target.CreateAsync(user).Wait();
            Assert.IsTrue(!string.IsNullOrWhiteSpace(user.ETag));

            try
            {
                target.CreateAsync(user).Wait();
                Assert.Fail("CreateAsync created the same user twice");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is AzureTableUserException);
            }
            var backAgain = target.FindByIdAsync(user.Id).Result;

            Assert.AreEqual(user.UserName, backAgain.UserName);
            target.Dispose();

            try
            {
                target.CreateAsync(user).Wait();
                Assert.Fail("CreateAsync doesn't throw when disposed");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ObjectDisposedException);
            }
        }
        public void GetRolesAsyncTest()
        {
            var target = Target();
            var user = new AzureTableUser
            {
                Id = "foo",
                UserName = "******"
            };
            var role1 = "derp";
            var role2 = "herp";

            try
            {
                target.GetRolesAsync(null).Wait();
                Assert.Fail("GetRolesAsync didn't throw on null");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentNullException);
            }


            var result = target.GetRolesAsync(user).Result;
            Assert.AreEqual(0, result.Count);

            target.AddToRoleAsync(user, role1).Wait();
            target.AddToRoleAsync(user, role2).Wait();


            result = target.GetRolesAsync(user).Result;
            Assert.AreEqual(2, result.Count);
            Assert.IsTrue(result.All(x => x.Equals(role1) || x.Equals(role2)));

            target.Dispose();

            try
            {
                target.GetRolesAsync(user).Wait();
                Assert.Fail("GetRolesAsync doesn't throw when disposed");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ObjectDisposedException);
            }
        }
Пример #6
0
        public void DeleteAsyncTest()
        {
            var target = Target();
            var user   = new AzureTableUser
            {
                Id       = "foo",
                UserName = "******"
            };

            UtilsLol.AssertThrows(() => target.DeleteAsync(new AzureTableUser
            {
                UserName = "******"
            }).Wait());
            try
            {
                target.DeleteAsync(null).Wait();
                Assert.Fail("DeleteAsync didn't throw on null");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentNullException);
            }

            target.CreateAsync(user).Wait();
            Assert.IsNotNull(target.FindByIdAsync(user.Id).Result); // sanity

            target.DeleteAsync(user).Wait();

            Assert.IsNull(target.FindByIdAsync(user.Id).Result);

            target.DeleteAsync(user).Wait();

            target.Dispose();

            try
            {
                target.DeleteAsync(user).Wait();
                Assert.Fail("deleteAsync doesn't throw when disposed");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ObjectDisposedException);
            }
        }
Пример #7
0
        public void UpdateAsyncTest()
        {
            var target = Target();
            var user   = new AzureTableUser
            {
                Id       = "foo",
                UserName = "******"
            };

            UtilsLol.AssertThrows(() => target.UpdateAsync(new AzureTableUser
            {
                UserName = "******"
            }).Wait());

            UtilsLol.AssertThrows(() => target.UpdateAsync(null).Wait());

            target.CreateAsync(user).Wait();
            Assert.IsNotNull(target.FindByIdAsync(user.Id).Result); // sanity

            var newUser = new AzureTableUser
            {
                Id       = "foo",
                UserName = "******"
            };

            target.UpdateAsync(newUser).Wait();

            Assert.AreEqual(newUser.UserName, target.FindByIdAsync(user.Id).Result.UserName);

            target.Dispose();

            try
            {
                target.UpdateAsync(newUser).Wait();
                Assert.Fail("FindByNameAsync doesn't throw when disposed");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ObjectDisposedException);
            }
        }
Пример #8
0
        public void HasPasswordAsyncTests()
        {
            var target = Target();

            UtilsLol.AssertThrows(() => target.HasPasswordAsync(null).Result, typeof(ArgumentNullException));

            var user = new AzureTableUser
            {
                Id       = "derp",
                UserName = "******"
            };

            Assert.IsFalse(target.HasPasswordAsync(user).Result);
            target.CreateAsync(user);
            Assert.IsFalse(target.HasPasswordAsync(user).Result);

            var hash = "stupid or liar";

            target.SetPasswordHashAsync(user, hash).Wait();
            Assert.IsTrue(target.HasPasswordAsync(user).Result);
        }
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Manage"));
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new AzureTableUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
Пример #10
0
        public void FindByNameAsyncTest()
        {
            var userName = "******";
            var target   = Target();
            var user     = new AzureTableUser
            {
                Id       = "foo",
                UserName = userName
            };

            try
            {
                target.FindByNameAsync(null).Wait();
                Assert.Fail("FindByNameAsync didn't throw on null");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentException);
            }
            try
            {
                target.FindByNameAsync("").Wait();
                Assert.Fail("FindByNameAsync didn't throw on empty");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentException);
            }
            try
            {
                target.FindByNameAsync(" ").Wait();
                Assert.Fail("FindByNameAsync didn't throw on whitespace");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentException);
            }

            target.CreateAsync(user).Wait();
            var backagain = target.FindByNameAsync(user.UserName).Result;

            Assert.IsNotNull(backagain); // sanity

            //FindByName should be CASE INSENSITIVE!
            // The current storage emulator has a BUG that prevents this part of the test from working :/
            //var insensitive = target.FindByNameAsync(userName.ToLowerInvariant()).Result;
            //Assert.IsNotNull(insensitive);
            //insensitive = target.FindByNameAsync(userName.ToUpperInvariant()).Result;
            //Assert.IsNotNull(insensitive);
            //insensitive = target.FindByNameAsync(userName).Result;
            //Assert.IsNotNull(insensitive);

            target.DeleteAsync(user).Wait();

            Assert.IsNull(target.FindByNameAsync(user.Id).Result);

            target.Dispose();

            try
            {
                target.FindByNameAsync(user.Id).Wait();
                Assert.Fail("FindByNameAsync doesn't throw when disposed");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ObjectDisposedException);
            }
        }
        public void IsInRoleAsyncTest()
        {

            var target = Target();
            var user = new AzureTableUser
            {
                Id = "foo",
                UserName = "******"
            };
            var role1 = "derp";
            var role2 = "herp";

            try
            {
                target.IsInRoleAsync(null, null).Wait();
                Assert.Fail("IsInRoleAsync didn't throw on null");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentNullException);
            }
            try
            {
                target.IsInRoleAsync(null, role1).Wait();
                Assert.Fail("IsInRoleAsync didn't throw on null");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentNullException);
            }
            try
            {
                target.IsInRoleAsync(user, null).Wait();
                Assert.Fail("IsInRoleAsync didn't throw on null");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentNullException);
            }
            try
            {
                target.IsInRoleAsync(user, "").Wait();
                Assert.Fail("IsInRoleAsync didn't throw on empty");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentException);
            }
            try
            {
                target.IsInRoleAsync(user, " ").Wait();
                Assert.Fail("IsInRoleAsync didn't throw on whitespace");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentException);
            }

            Assert.IsFalse(target.IsInRoleAsync(user, role1).Result);

            target.AddToRoleAsync(user, role1).Wait();
            target.AddToRoleAsync(user, role2).Wait();

            Assert.IsTrue(target.IsInRoleAsync(user, role1).Result);
            Assert.IsTrue(target.IsInRoleAsync(user, role2).Result);

            target.Dispose();

            try
            {
                target.IsInRoleAsync(user, role1).Wait();
                Assert.Fail("GetRolesAsync doesn't throw when disposed");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ObjectDisposedException);
            }
        }
        public void AddToRoleAsyncTest()
        {
            var target = Target();
            var user = new AzureTableUser
            {
                Id = "fooRole",
                UserName = "******"
            };
            var role = "derp";

            try
            {
                target.AddToRoleAsync(null, null).Wait();
                Assert.Fail("CreateAsync didn't throw on null");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentNullException);
            }
            try
            {
                target.AddToRoleAsync(null, role).Wait();
                Assert.Fail("CreateAsync didn't throw on null");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentNullException);
            }
            try
            {
                target.AddToRoleAsync(user, null).Wait();
                Assert.Fail("CreateAsync didn't throw on null");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentNullException);
            }
            try
            {
                target.AddToRoleAsync(user, "").Wait();
                Assert.Fail("CreateAsync didn't throw on empty");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentException);
            }
            try
            {
                target.AddToRoleAsync(user, " ").Wait();
                Assert.Fail("CreateAsync didn't throw on whitespace");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ArgumentException);
            }

            target.AddToRoleAsync(user, role).Wait();
            target.AddToRoleAsync(user, role).Wait();
            var roles = target.GetRolesAsync(user).Result;
            Assert.AreEqual(1, roles.Count);
            Assert.AreEqual(role, roles[0]);

            target.Dispose();

            try
            {
                target.AddToRoleAsync(user, role).Wait();
                Assert.Fail("CreateAsync doesn't throw when disposed");
            }
            catch (AggregateException ex)
            {
                Assert.IsTrue(ex.InnerException is ObjectDisposedException);
            }
        }