public async Task ThrowException_WhenNameIsInvalid()
        {
            var userName     = "******";
            var userPassword = "******";
            var roleId       = 2;
            var invalidName  = "Invalid Name";

            var userFactoryMock = new Mock <IUserFactory>();
            var bannFactoryMock = new Mock <IBannFactory>();
            var hasherMock      = new Mock <IHasher>();

            var options = TestUtilities.GetOptions(nameof(ThrowException_WhenNameIsInvalid));

            var bann = new Bann();
            var role = new Role();
            var user = new User(userName, userPassword, roleId);

            using (var arrangeContext = new CocktailMagicianDb(options))
            {
                user.Bann = bann;
                user.Role = role;
                arrangeContext.Users.Add(user);
                await arrangeContext.SaveChangesAsync();
            }
            using (var assertContext = new CocktailMagicianDb(options))
            {
                var sut = new UserServices(assertContext, userFactoryMock.Object, bannFactoryMock.Object, hasherMock.Object);

                await Assert.ThrowsExceptionAsync <InvalidOperationException>(() => sut.FindUserDTOAsync(invalidName));
            }
        }
        public async Task IsBannedAsync_False()
        {
            var userName     = "******";
            var userPassword = "******";
            var roleId       = 1;
            var bannReason   = "Just banned.";

            var userFactoryMock = new Mock <IUserFactory>();
            var bannFactoryMock = new Mock <IBannFactory>();
            var hasherMock      = new Mock <IHasher>();

            var options = TestUtilities.GetOptions(nameof(IsBannedAsync_False));

            var user = new User(userName, userPassword, roleId);
            var bann = new Bann(bannReason, DateTime.MaxValue, user);

            using (var arrangeContext = new CocktailMagicianDb(options))
            {
                arrangeContext.Users.Add(user);
                arrangeContext.Banns.Add(bann);
                bann.User = user;
                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CocktailMagicianDb(options))
            {
                var sut      = new UserServices(assertContext, userFactoryMock.Object, bannFactoryMock.Object, hasherMock.Object);
                var isBanned = await sut.IsBannedAsync(user.UserName);

                Assert.AreEqual(true, isBanned);
            }
        }
        public async Task ThrowsExceptionWhen_UserNotFound()
        {
            var userName     = "******";
            var userPassword = "******";
            var roleId       = 2;

            var userFactoryMock = new Mock <IUserFactory>();
            var bannFactoryMock = new Mock <IBannFactory>();
            var hasherMock      = new Mock <IHasher>();

            var bann = new Bann();
            var role = new Role();

            var user = new User(userName, userPassword, roleId);

            user.Bann = bann;
            user.Role = role;

            var options = TestUtilities.GetOptions(nameof(ThrowsExceptionWhen_UserNotFound));

            using (var arrangeContext = new CocktailMagicianDb(options))
            {
                arrangeContext.Users.Add(user);
                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CocktailMagicianDb(options))
            {
                var sut = new UserServices(assertContext, userFactoryMock.Object, bannFactoryMock.Object, hasherMock.Object);

                await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.LoginAsync(userName, userPassword));
            };
        }
Пример #4
0
        public async Task DeleteUserAsync()
        {
            var userName     = "******";
            var userPassword = "******";
            var roleId       = 2;

            var userFactoryMock = new Mock <IUserFactory>();
            var bannFactoryMock = new Mock <IBannFactory>();
            var hasherMock      = new Mock <IHasher>();

            var options = TestUtilities.GetOptions(nameof(DeleteUserAsync));

            var bann = new Bann();
            var role = new Role();
            var user = new User(userName, userPassword, roleId);

            using (var actContext = new CocktailMagicianDb(options))
            {
                user.Bann = bann;
                user.Role = role;
                actContext.Users.Add(user);
                await actContext.SaveChangesAsync();

                var sut = new UserServices(actContext, userFactoryMock.Object, bannFactoryMock.Object, hasherMock.Object);
                await sut.DeleteUserAsync(user.Id);
            }
            using (var assertContext = new CocktailMagicianDb(options))
            {
                var userTest = await assertContext.Users.FirstAsync(u => u.UserName == userName);

                Assert.IsTrue(userTest.IsDeleted);
            }
        }
Пример #5
0
        public async Task FinGetUserInfoAsync()
        {
            var userName     = "******";
            var userPassword = "******";
            var roleId       = 2;

            var userFactoryMock = new Mock <IUserFactory>();
            var bannFactoryMock = new Mock <IBannFactory>();
            var hasherMock      = new Mock <IHasher>();

            var options = TestUtilities.GetOptions(nameof(FinGetUserInfoAsync));

            var bann = new Bann();
            var role = new Role();
            var user = new User(userName, userPassword, roleId);

            using (var arrangeContext = new CocktailMagicianDb(options))
            {
                user.Bann = bann;
                user.Role = role;
                arrangeContext.Users.Add(user);
                await arrangeContext.SaveChangesAsync();
            }
            using (var assertContext = new CocktailMagicianDb(options))
            {
                var sut      = new UserServices(assertContext, userFactoryMock.Object, bannFactoryMock.Object, hasherMock.Object);
                var userTest = await sut.GetUserInfoAsync(user.Id);

                Assert.AreEqual(userTest.UserName, user.UserName);
                Assert.AreEqual(userTest.Password, user.Password);
                Assert.AreEqual(userTest.RoleName, user.Role.Name);
                Assert.AreEqual(userTest.IsDeleted, user.IsDeleted);
            }
        }
        public async Task BannAsync()
        {
            var userName     = "******";
            var userPassword = "******";
            var roleId       = 2;
            var reasonForBan = "Just banned";

            var userFactoryMock = new Mock <IUserFactory>();
            var bannFactoryMock = new Mock <IBannFactory>();
            var hasherMock      = new Mock <IHasher>();

            var options = TestUtilities.GetOptions(nameof(BannAsync));


            var role     = new Role();
            var user     = new User(userName, userPassword, roleId);
            var bann     = new Bann();
            var dateTime = DateTime.Today;

            bannFactoryMock
            .Setup(b => b.CreateBan(reasonForBan, dateTime.AddDays(30), user))
            .Returns(bann);

            using (var actContext = new CocktailMagicianDb(options))
            {
                user.Role   = role;
                bann.User   = user;
                bann.Reason = reasonForBan;
                actContext.Users.Add(user);
                await actContext.SaveChangesAsync();


                var userDTO = new UserDTO
                {
                    Id       = user.Id,
                    UserName = user.UserName
                };
                var sut = new UserServices(actContext, userFactoryMock.Object, bannFactoryMock.Object, hasherMock.Object);
                await sut.BanAsync(reasonForBan, userDTO);
            }
            using (var assertContext = new CocktailMagicianDb(options))
            {
                var banTest = await assertContext.Banns.Include(b => b.User).FirstAsync(u => u.User == user);

                Assert.AreEqual(banTest.User.Id, user.Id);
                Assert.AreEqual(banTest.Reason, bann.Reason);
            }
        }
        public async Task GetListOfUsersDTO()
        {
            var userName      = "******";
            var userPassword  = "******";
            var userName2     = "user2";
            var userPassword2 = "user2";
            var roleId        = 1;
            var roleName      = "user";

            var userFactoryMock = new Mock <IUserFactory>();
            var bannFactoryMock = new Mock <IBannFactory>();
            var hasherMock      = new Mock <IHasher>();

            var options = TestUtilities.GetOptions(nameof(GetListOfUsersDTO));

            var user  = new User(userName, userPassword, roleId);
            var user2 = new User(userName2, userPassword2, roleId);

            var bann = new Bann();
            var role = new Role();

            using (var arrangeContext = new CocktailMagicianDb(options))
            {
                user.Role = role;
                user.Bann = bann;

                user2.Role = role;
                user2.Bann = bann;

                role.Name = roleName;
                role.Id   = roleId;
                arrangeContext.Roles.Add(role);
                arrangeContext.Users.Add(user);
                arrangeContext.Users.Add(user2);
                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CocktailMagicianDb(options))
            {
                var sut      = new UserServices(assertContext, userFactoryMock.Object, bannFactoryMock.Object, hasherMock.Object);
                var listTest = await sut.GetListOfUsersDTO();

                Assert.IsTrue(listTest.Count() == 2);
                Assert.IsTrue(assertContext.Users.Count() == 2);
            }
        }
        public async Task LoginAsync()
        {
            var userName       = "******";
            var userPassword   = "******";
            var roleId         = 2;
            var hashedPassword = "******";

            var userFactoryMock = new Mock <IUserFactory>();
            var bannFactoryMock = new Mock <IBannFactory>();
            var hasherMock      = new Mock <IHasher>();

            var options = TestUtilities.GetOptions(nameof(LoginAsync));

            var bann = new Bann();
            var role = new Role();

            var user = new User(userName, hashedPassword, roleId);

            hasherMock
            .Setup(h => h.Hasher(userPassword))
            .Returns(hashedPassword);

            user.Bann = bann;
            user.Role = role;


            using (var arrangeContext = new CocktailMagicianDb(options))
            {
                arrangeContext.Users.Add(user);
                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new CocktailMagicianDb(options))
            {
                var sut = new UserServices(assertContext, userFactoryMock.Object, bannFactoryMock.Object, hasherMock.Object);

                var userTest = await sut.LoginAsync(userName, userPassword);

                Assert.AreEqual(userTest.UserName, user.UserName);
                Assert.AreEqual(userTest.Password, user.Password);
                Assert.AreEqual(userTest.RoleName, user.Role.Name);
            }
        }
Пример #9
0
        public async Task ThrowException_WhenIdIsInvalid()
        {
            var userName     = "******";
            var userPassword = "******";
            var bannReason   = "Just Banned";
            var roleId       = 2;
            var invalidId    = "-18237129883";

            var userFactoryMock = new Mock <IUserFactory>();
            var bannFactoryMock = new Mock <IBannFactory>();
            var hasherMock      = new Mock <IHasher>();

            var options = TestUtilities.GetOptions(nameof(ThrowException_WhenIdIsInvalid));


            var user = new User(userName, userPassword, roleId);
            var bann = new Bann(bannReason, DateTime.MaxValue, user);
            var role = new Role();

            using (var actContext = new CocktailMagicianDb(options))
            {
                user.Bann = bann;
                user.Role = role;
                bann.User = user;
                actContext.Users.Add(user);
                actContext.Banns.Add(bann);
                await actContext.SaveChangesAsync();

                user.BannId = bann.Id;

                var sut = new UserServices(actContext, userFactoryMock.Object, bannFactoryMock.Object, hasherMock.Object);
                await sut.DeleteUserAsync(user.Id);
            }
            using (var assertContext = new CocktailMagicianDb(options))
            {
                var sut = new UserServices(assertContext, userFactoryMock.Object, bannFactoryMock.Object, hasherMock.Object);

                await Assert.ThrowsExceptionAsync <InvalidOperationException>(() => sut.GetUserInfoAsync(invalidId));
            }
        }