public async Task TestGenerateLogin() { User u = new User(); int pin = _context.GenerateLogin(u); Assert.IsFalse(await _context.User.AnyAsync(a => a.Login == u.Login)); Assert.AreEqual(u.HashPin(pin), u.Pin); }
public async Task <IActionResult> AddUser([Bind("Id,Name,BirthNumber,Adress,Email,Phone,AccountNumber,CardNumber,Money,Login,Pin,Role")] User user, string Turing, [Bind("AccGen")] bool AccGen = false, [Bind("CardGen")] bool CardGen = false) { if (!Turing.ToLower().Equals("human")) { ViewBag.ErrTuring = "We don't talk to machines, call a human being."; return(View(user)); } if (user.Role == Role.User) { if (AccGen) { user.AccountNumber = _context.GenerateAccountNumber(); } if (CardGen) { user.CardNumber = _context.GenerateCardNumber(); } if (user.AccountNumber == null) { ViewBag.ErrAcc = "Account number is required"; return(View(user)); } if (!_context.IsAccountUnique((long)user.AccountNumber)) { ViewBag.ErrAcc = "Account number is not unique."; return(View(user)); } if (user.CardNumber == null) { ViewBag.ErrAcc = "Card number is required"; return(View(user)); } if (!_context.IsCardUnique((long)user.CardNumber)) { ViewBag.ErrCard = "Card number is not unique."; return(View(user)); } if (user.Money == null) { user.Money = 0; } } if (ModelState.IsValid) { int pin = _context.GenerateLogin(user); _context.Add(user); await _context.SaveChangesAsync(); MailClient.Singleton.SendAddUser(user, pin); return(RedirectToAction(nameof(Index))); } else { ViewBag.ErrMsg = "Values are not valid."; return(View(user)); } }