Beispiel #1
0
        public async Task TestEditIdeaGoodData()
        {
            ACMDbContext    context         = ACMDbContextInMemoryFactory.InitializeContext();
            HomeownerSevice homeownerSevice = new HomeownerSevice(context);
            ACMUser         user            = new ACMUser {
                Email = "*****@*****.**", FullName = "gosho"
            };
            Idea idea1 = new Idea {
                Id = "1", User = user, Text = "idea1"
            };
            Idea idea2 = new Idea {
                Id = "2", User = user, Text = "idea2"
            };
            await context.Users.AddAsync(user);

            await context.Ideas.AddAsync(idea1);

            await context.Ideas.AddAsync(idea2);

            await context.SaveChangesAsync();

            bool output = await homeownerSevice.EditIdea(idea1.Id, user.Email, "Edited text");

            Assert.True(output);
            Assert.Equal("Edited text", context.Ideas
                         .Where(x => x.Id == idea1.Id)
                         .FirstOrDefault()
                         .Text);
        }
Beispiel #2
0
        public async Task TestGetIdeaInvalidId()
        {
            ACMDbContext    context         = ACMDbContextInMemoryFactory.InitializeContext();
            HomeownerSevice homeownerSevice = new HomeownerSevice(context);
            ACMUser         user            = new ACMUser {
                Email = "*****@*****.**", FullName = "gosho"
            };
            Idea idea1 = new Idea {
                Id = "1", User = user, Text = "idea1"
            };
            Idea idea2 = new Idea {
                Id = "2", User = user, Text = "idea2"
            };
            await context.Users.AddAsync(user);

            await context.Ideas.AddAsync(idea1);

            await context.Ideas.AddAsync(idea2);

            await context.SaveChangesAsync();

            Action act = () => homeownerSevice
                         .GetIdea(idea1.Id + "Random string", user.Email);

            Assert.Throws <ACMException>(act);
        }
Beispiel #3
0
        public async Task TestAllGoodData()
        {
            ACMDbContext    context         = ACMDbContextInMemoryFactory.InitializeContext();
            HomeownerSevice homeownerSevice = new HomeownerSevice(context);
            ACMUser         user            = new ACMUser {
                Email = "*****@*****.**", FullName = "gosho"
            };
            Idea idea1 = new Idea {
                Id = "1", User = user, Text = "idea1"
            };
            Idea idea2 = new Idea {
                Id = "2", User = user, Text = "idea2"
            };
            await context.Users.AddAsync(user);

            await context.Ideas.AddAsync(idea1);

            await context.Ideas.AddAsync(idea2);

            await context.SaveChangesAsync();

            var list = homeownerSevice.All();

            Assert.Equal(2, list.Count);
            Assert.Equal("1", list[1].Id);
            Assert.Equal("idea1", list[1].Text);
            Assert.Equal("*****@*****.**", list[1].UserName);
            Assert.Equal("gosho", list[1].Name);
            Assert.Equal("2", list[0].Id);
            Assert.Equal("idea2", list[0].Text);
            Assert.Equal("*****@*****.**", list[0].UserName);
            Assert.Equal("gosho", list[0].Name);
        }
Beispiel #4
0
        public async Task TestGetAllApartmentsWithGoodData()
        {
            ACMDbContext     context          = ACMDbContextInMemoryFactory.InitializeContext();
            ApartmentService apartmentService = new ApartmentService(context);
            ACMUser          user             = new ACMUser {
                AppartentNumber = 1
            };
            Apartment apartment1 = new Apartment {
                Number = 1, User = user
            };
            Apartment apartment2 = new Apartment {
                Number = 2
            };
            await context.Apartments.AddAsync(apartment1);

            await context.Apartments.AddAsync(apartment2);

            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            List <Models.ApartmentListDTO> list = apartmentService.GetAllApartments();

            Assert.Equal(2, list.Count);
            Assert.Equal(1, list[0].Number);
            Assert.Equal(2, list[1].Number);
            Assert.Equal(1, list[0].RegisteredUsersCount);
            Assert.Equal(0, list[1].RegisteredUsersCount);
        }
Beispiel #5
0
        public async Task TestDeleteIdeaGoodData()
        {
            ACMDbContext    context         = ACMDbContextInMemoryFactory.InitializeContext();
            HomeownerSevice homeownerSevice = new HomeownerSevice(context);
            ACMUser         user            = new ACMUser {
                Email = "*****@*****.**", FullName = "gosho"
            };
            Idea idea1 = new Idea {
                Id = "1", User = user, Text = "idea1"
            };
            Idea idea2 = new Idea {
                Id = "2", User = user, Text = "idea2"
            };
            await context.Users.AddAsync(user);

            await context.Ideas.AddAsync(idea1);

            await context.Ideas.AddAsync(idea2);

            await context.SaveChangesAsync();

            bool output = await homeownerSevice.DeleteIdea(idea1.Id, user.Email);

            Assert.True(output);
            Assert.Single(context.Ideas.ToList());
            Assert.True(context.Ideas.Any(x => x.Id == idea2.Id));
        }
Beispiel #6
0
        public async Task TestGetIdeaGoodData()
        {
            ACMDbContext    context         = ACMDbContextInMemoryFactory.InitializeContext();
            HomeownerSevice homeownerSevice = new HomeownerSevice(context);
            ACMUser         user            = new ACMUser {
                Email = "*****@*****.**", FullName = "gosho"
            };
            Idea idea1 = new Idea {
                Id = "1", User = user, Text = "idea1"
            };
            Idea idea2 = new Idea {
                Id = "2", User = user, Text = "idea2"
            };
            await context.Users.AddAsync(user);

            await context.Ideas.AddAsync(idea1);

            await context.Ideas.AddAsync(idea2);

            await context.SaveChangesAsync();

            EditIdeaDTO output = homeownerSevice.GetIdea(idea1.Id, user.Email);

            Assert.Equal(idea1.Text, output.Text);
            Assert.Equal(idea1.Id, output.Id);
        }
Beispiel #7
0
        public ACMUser GetOneUser(string email)
        {
            ACMUser user = context.Users.Where(x => x.Email == email).FirstOrDefault();

            if (user == null)
            {
                throw new ACMException();
            }
            return(user);
        }
Beispiel #8
0
        public int GetApartmentNumber(string name)
        {
            ACMUser user = context.Users
                           .Where(x => x.Email == name)
                           .FirstOrDefault();

            if (user == null)
            {
                throw new ACMException();
            }
            return(user.AppartentNumber);
        }
Beispiel #9
0
        public async Task TestCreateInvalidUser()
        {
            ACMDbContext    context         = ACMDbContextInMemoryFactory.InitializeContext();
            HomeownerSevice homeownerSevice = new HomeownerSevice(context);
            ACMUser         user            = new ACMUser {
                Email = "*****@*****.**"
            };
            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            await Assert.ThrowsAsync <ACMException>(()
                                                    => homeownerSevice.Create("beer", "NOT [email protected]"));
        }
Beispiel #10
0
        public async Task TestCreateInvalidUser()
        {
            ACMDbContext context   = ACMDbContextInMemoryFactory.InitializeContext();
            IPService    iPService = new IPService(context);
            ACMUser      user      = new ACMUser {
                UserName = "******", FullName = "gosho"
            };
            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            IpDTO model = new IpDTO(null, "123.123.123...");
            await Assert.ThrowsAsync <ACMException>(() => iPService.Create(model));
        }
Beispiel #11
0
        public async Task TestGenerateCodeInvalidUser()
        {
            ACMDbContext context     = ACMDbContextInMemoryFactory.InitializeContext();
            UserService  userService = new UserService(context);
            ACMUser      user        = new ACMUser {
                UserName = "******"
            };
            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            await Assert.ThrowsAsync <ACMException>(()
                                                    => userService.GenerateCode("Not gosho"));
        }
Beispiel #12
0
        public async Task TestIsUserValidinvalidUser()
        {
            ACMDbContext context     = ACMDbContextInMemoryFactory.InitializeContext();
            UserService  userService = new UserService(context);
            ACMUser      user        = new ACMUser {
                Email = "gosho"
            };
            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            bool output = userService.IsUserValid("Not gosho");

            Assert.False(output);
        }
Beispiel #13
0
        public async Task TestGenerateCodeGoodData()
        {
            ACMDbContext context     = ACMDbContextInMemoryFactory.InitializeContext();
            UserService  userService = new UserService(context);
            ACMUser      user        = new ACMUser {
                UserName = "******"
            };
            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            string code = await userService.GenerateCode("gosho");

            Assert.True(context.Users.Any(x => x.ExpectedCode == code));
        }
Beispiel #14
0
        public async Task TestGetApartmentNumberGoodData()
        {
            ACMDbContext context     = ACMDbContextInMemoryFactory.InitializeContext();
            UserService  userService = new UserService(context);
            ACMUser      user        = new ACMUser {
                Email = "gosho", AppartentNumber = 1
            };
            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            int output = userService.GetApartmentNumber("gosho");

            Assert.True(context.Users.Any(x => x.AppartentNumber == output));
        }
Beispiel #15
0
        public async Task TestIsCodeValidInvalidCode()
        {
            ACMDbContext context     = ACMDbContextInMemoryFactory.InitializeContext();
            UserService  userService = new UserService(context);
            ACMUser      user        = new ACMUser {
                UserName = "******", ExpectedCode = "1"
            };
            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            bool output = userService.IsCodeValid("2", "gosho");

            Assert.False(output);
        }
Beispiel #16
0
        public async Task TestGetApartmentNumberInvalidUser()
        {
            ACMDbContext context     = ACMDbContextInMemoryFactory.InitializeContext();
            UserService  userService = new UserService(context);
            ACMUser      user        = new ACMUser {
                Email = "gosho", AppartentNumber = 1
            };
            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            Action act = () => userService.GetApartmentNumber("Not gosho");

            Assert.Throws <ACMException>(act);
        }
Beispiel #17
0
        public async Task TestGetOneUserGoodData()
        {
            ACMDbContext context     = ACMDbContextInMemoryFactory.InitializeContext();
            UserService  userService = new UserService(context);
            ACMUser      user        = new ACMUser {
                Email = "gosho"
            };
            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            ACMUser output = userService.GetOneUser("gosho");

            Assert.NotNull(output);
            Assert.Equal("gosho", output.Email);
        }
Beispiel #18
0
        public async Task <string> GenerateCode(string userName)
        {
            string  code = string.Join("", Guid.NewGuid().ToString().Take(4)).ToUpper();
            ACMUser user = context.Users
                           .Where(x => x.UserName == userName)
                           .FirstOrDefault();

            if (user == null)
            {
                throw new ACMException();
            }
            user.ExpectedCode = code;
            await context.SaveChangesAsync();

            return(code);
        }
Beispiel #19
0
        public async Task <string> AddNewIp(string name, string ip)
        {
            ACMUser user = context.Users.Where(x => x.UserName == name).FirstOrDefault();

            if (user == null)
            {
                throw new ACMException();
            }
            IP newIp = new IP {
                User = user, IpString = ip
            };
            await context.IPs.AddAsync(newIp);

            await context.SaveChangesAsync();

            return(newIp.Id);
        }
Beispiel #20
0
        public async Task <IActionResult> ChangePassword(ChangePasswordDTO model)
        {
            if (ModelState.IsValid)
            {
                if (model.ConfirmedPassword == model.NewPassword)
                {
                    ACMUser        user   = userService.GetOneUser(User.Identity.Name);
                    IdentityResult result = await userManager.ChangePasswordAsync(user, model.OldPassword, model.NewPassword);

                    if (result.Succeeded)
                    {
                        return(Redirect("/"));
                    }
                    model.IsPasswordInvalid = true;
                }
            }
            return(View(model));
        }
Beispiel #21
0
        public async Task TestAddIpGoodData()
        {
            ACMDbContext context   = ACMDbContextInMemoryFactory.InitializeContext();
            IPService    iPService = new IPService(context);
            ACMUser      user      = new ACMUser {
                UserName = "******", FullName = "gosho"
            };
            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            string output = await iPService.AddNewIp("*****@*****.**", "123.123.123...");

            Assert.Single(context.IPs.ToList());
            Assert.True(context.IPs.Any(x => x.Id == output));
            Assert.Equal("*****@*****.**", context.IPs.FirstOrDefault().User.UserName);
            Assert.Equal("123.123.123...", context.IPs.FirstOrDefault().IpString);
        }
        public async Task <string> Create(string text, string name)
        {
            ACMUser user = context.Users.Where(x => x.Email == name).FirstOrDefault();

            if (user == null)
            {
                throw new ACMException();
            }
            Idea idea = new Idea
            {
                Text = text,
                User = user
            };
            await context.Ideas.AddAsync(idea);

            await context.SaveChangesAsync();

            return(idea.Id);
        }
Beispiel #23
0
        public async Task <IActionResult> RessetPassword(ResetpasswordDTO model)
        {
            if (ModelState.IsValid)
            {
                if (userService.IsUserValid(model.Email))
                {
                    ACMUser user        = userService.GetOneUser(model.Email);
                    string  newPassword = string.Join("", Guid.NewGuid().ToString().Take(8)).ToUpper();
                    emailSender.Send(model.Email, "Password Resset", $"Your new Password is {newPassword}");
                    string token = await userManager.GeneratePasswordResetTokenAsync(user);

                    await userManager.ResetPasswordAsync(user, token, newPassword);

                    return(Redirect("/Home/RessetedPassword"));
                }
            }
            model.IsValid = false;
            return(View(model));
        }
Beispiel #24
0
        public async Task TestIsNewIpFalse()
        {
            ACMDbContext context   = ACMDbContextInMemoryFactory.InitializeContext();
            IPService    iPService = new IPService(context);
            ACMUser      user      = new ACMUser {
                UserName = "******", FullName = "gosho"
            };
            IP iP = new IP
            {
                IpString = "123.123.123...",
                User     = user
            };
            await context.Users.AddAsync(user);

            await context.IPs.AddAsync(iP);

            await context.SaveChangesAsync();

            bool output = iPService.IsNewIp(user.UserName, "123.123.123...");

            Assert.False(output);
        }
Beispiel #25
0
        public async Task TestCreateGoodData()
        {
            ACMDbContext    context         = ACMDbContextInMemoryFactory.InitializeContext();
            HomeownerSevice homeownerSevice = new HomeownerSevice(context);
            ACMUser         user            = new ACMUser {
                Email = "*****@*****.**"
            };
            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            string id = await homeownerSevice.Create("beer", "*****@*****.**");

            Assert.Single(context.Ideas.ToList());
            Assert.Equal("beer", context.Ideas
                         .Where(x => x.Id == id)
                         .FirstOrDefault()
                         .Text);
            Assert.Equal("*****@*****.**", context.Ideas
                         .Where(x => x.Id == id)
                         .FirstOrDefault()
                         .User.Email);
        }
Beispiel #26
0
 public IpDTO(ACMUser user, string ip)
 {
     User = user;
     Ip   = ip;
 }
Beispiel #27
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            if (ModelState.IsValid)
            {
                if (MagicStrings.AdminEmails.Any(x => x == Input.Email.ToUpper()))
                {
                    var user = new ACMUser {
                        UserName = Input.Email, Email = Input.Email, FullName = Input.FullName, PhoneNumber = Input.PhoneNumber, AppartentNumber = int.Parse(Input.Code.Split("_")[0])
                    };
                    var result = await _userManager.CreateAsync(user, Input.Password);

                    if (result.Succeeded)
                    {
                        _logger.LogInformation("User created a new account with password.");

                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        var callbackUrl = Url.Page(
                            "/Account/ConfirmEmail",
                            pageHandler: null,
                            values: new { userId = user.Id, code = code },
                            protocol: Request.Scheme);

                        //  await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                        //     $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
                        await _userManager.AddToRoleAsync(user, MagicStrings.AdminString);

                        await _signInManager.SignInAsync(user, isPersistent : false);

                        string ip = Request.Host.Value;
                        await iPService.Create(new Models.IpDTO(user, ip));

                        return(LocalRedirect(returnUrl));
                    }
                }
                else if (codeService.IsCodeValid(Input.Code))
                {
                    var user = new ACMUser {
                        UserName = Input.Email, Email = Input.Email, FullName = Input.FullName, PhoneNumber = Input.PhoneNumber, AppartentNumber = int.Parse(Input.Code.Split("_")[0])
                    };
                    var result = await _userManager.CreateAsync(user, Input.Password);

                    if (result.Succeeded)
                    {
                        _logger.LogInformation("User created a new account with password.");

                        var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        var callbackUrl = Url.Page(
                            "/Account/ConfirmEmail",
                            pageHandler: null,
                            values: new { userId = user.Id, code = code },
                            protocol: Request.Scheme);

                        //  await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                        //     $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                        await _signInManager.SignInAsync(user, isPersistent : false);

                        string ip = Request.Host.Value;
                        await iPService.Create(new Models.IpDTO(user, ip));

                        await codeService.DeleteCode(Input.Code);

                        return(LocalRedirect(returnUrl));
                    }
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }