public async Task AcceptInvitation_WithValidData_ShouldReturnCorrectInvitation() { var company = new Company() { Address = "asd", Name = "asd", }; var user = new MISUser() { FirstName = "asd", LastName = "asd", UserName = "******", }; await this.dbContext.AddAsync(company); await this.dbContext.AddAsync(user); await this.dbContext.SaveChangesAsync(); var expected = await this.invitationService.InviteAsync(company.Id, user.Id); var actual = await this.invitationService.AcceptInvitationAsync(expected.Id, false); Assert.AreEqual(expected.Id, actual.Id); Assert.AreEqual(expected.User.Id, actual.User.Id); }
public async Task GetAllUsers_WithValidData_ShouldReturnCorrectUsers() { var receipt = new Receipt(); var userFirst = new MISUser() { Email = Name, FirstName = Name, LastName = Name, UserName = Name + 1, }; var userSecond = new MISUser() { Email = Name, FirstName = Name, LastName = Name, UserName = Name + 2, }; var userThird = new MISUser() { Email = Name, FirstName = Name, LastName = Name, UserName = Name + 3, }; await this.dbContext.AddAsync(userFirst); await this.dbContext.AddAsync(userSecond); await this.dbContext.AddAsync(userThird); await this.dbContext.SaveChangesAsync(); var actual = await this.userService.GetAllUsersAsync(); var actualArray = actual.OrderBy(x => x.UserName).ToArray(); Assert.AreEqual(userFirst.Id, actualArray[0].Id); Assert.AreEqual(userSecond.Id, actualArray[1].Id); Assert.AreEqual(userThird.Id, actualArray[2].Id); }
public async Task GetAllInvitation_WithValidData_ShouldReturnCorrectInvitations() { var company = new Company() { Address = "asd", Name = "asd", }; var user = new MISUser() { FirstName = "asd", LastName = "asd", UserName = "******", }; await this.dbContext.AddAsync(company); await this.dbContext.AddAsync(user); await this.dbContext.SaveChangesAsync(); var expected = await this.invitationService.InviteAsync(company.Id, user.Id); var actual = await this.invitationService.GetAllAsync(user.Id); var actualFirst = actual?.FirstOrDefault(); Assert.AreEqual(expected.Id, actualFirst?.Id); Assert.AreEqual(expected.User.Id, actualFirst?.User.Id); }
public async Task Init() { var options = new DbContextOptionsBuilder <MISDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; this.dbContext = new MISDbContext(options); var userService = new UserService(this.dbContext); var companyService = new CompanyService(this.dbContext, userService); var warehouseService = new WareHouseService(this.dbContext, companyService); var categoryService = new CategoryService(warehouseService, this.dbContext); var productService = new ProductService(this.dbContext, categoryService); this.receiptService = new ReceiptService(this.dbContext, userService, companyService, productService); var company = new Company() { Name = "asd", Address = "asd", }; await this.dbContext.AddAsync(company); await this.dbContext.SaveChangesAsync(); var warehouse = new WareHouse() { Name = "asd", Company = company, }; var user = new MISUser() { UserName = "******", Email = "*****@*****.**", FirstName = "asdddd", Company = company, LastName = "asdddd", }; await this.dbContext.AddAsync(warehouse); await this.dbContext.AddAsync(user); await this.dbContext.SaveChangesAsync(); var category = new Category() { Name = "asd", WareHouse = warehouse }; await this.dbContext.AddAsync(category); await this.dbContext.SaveChangesAsync(); }
public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information during confirmation."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } if (this.ModelState.IsValid) { var user = new MISUser { UserName = Input.Username, Email = Input.Email, FirstName = info.Principal.FindFirstValue(ClaimTypes.GivenName), LastName = info.Principal.FindFirstValue(ClaimTypes.Surname), }; var result = await _userManager.CreateAsync(user, password : Input.Password); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { var code = await this._userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = this.Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: this.Request.Scheme); await this._emailSender.SendEmailAsync(this.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); _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } LoginProvider = info.LoginProvider; ReturnUrl = returnUrl; return(Page()); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { if (this.User.Identity.IsAuthenticated) { return(this.RedirectToAction("Index", "Home")); } returnUrl = returnUrl ?? this.Url.Content("~/"); if (this.ModelState.IsValid) { var isAvailable = await this._userManager.FindByNameAsync(this.Input.Username) == null && await this._userManager.FindByEmailAsync(this.Input.Email) == null; if (!isAvailable) { return(this.Page()); } var user = new MISUser { UserName = this.Input.Username, Email = this.Input.Email, LastName = this.Input.LastName, FirstName = this.Input.FirstName, }; var result = await this._userManager.CreateAsync(user, this.Input.Password); if (result.Succeeded) { this.TempData[GlobalConstants.TempDataConfirmEmail] = "Please confirm your email, before you try to login."; this._logger.LogInformation("User created as new account with password."); var code = await this._userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = this.Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: this.Request.Scheme); await this._emailSender.SendEmailAsync(this.Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); //await this._signInManager.SignInAsync(user, isPersistent: false); return(this.LocalRedirect("/Identity/Account/Login")); } foreach (var error in result.Errors) { this.ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(this.Page()); }
public async Task CreateCompany_WithInvalidData_ShouldReturnNull() { var user = new MISUser() { Email = "pesho", FirstName = "pesho", LastName = "pesho", UserName = "******", }; await this.dbContext.AddAsync(user); await this.dbContext.SaveChangesAsync(); var company = await this.companyService.CreateAsync(CompanyName, CompanyAddress, "asd"); Assert.IsNull(company); }
public async Task SetReceipt_WithInvalidData_ShouldReturnNull() { var receipt = new Receipt(); var user = new MISUser() { Email = Name, FirstName = Name, LastName = Name, UserName = Name, }; await this.dbContext.AddAsync(user); await this.dbContext.SaveChangesAsync(); var actual = await this.userService.SetReceiptAsync(receipt, "asd"); Assert.IsNull(actual); }
public async Task CreateCompany_WithValidData_ShouldWorkCorrectly() { var user = new MISUser() { Email = "pesho", FirstName = "pesho", LastName = "pesho", UserName = "******", }; await this.dbContext.AddAsync(user); await this.dbContext.SaveChangesAsync(); var company = await this.companyService.CreateAsync(CompanyName, CompanyAddress, user.Id); var employee = company.Employees.FirstOrDefault(); Assert.AreEqual(user.Id, employee?.Id); }
public async Task SetReceipt_WithValidData_ShouldReturnCorrectUser() { var receipt = new Receipt(); var user = new MISUser() { Email = Name, FirstName = Name, LastName = Name, UserName = Name, }; await this.dbContext.AddAsync(user); await this.dbContext.SaveChangesAsync(); user.CompanyId = "123"; await this.userService.SetReceiptAsync(receipt, Name); Assert.AreEqual(user.Id, receipt.User.Id); }
public async Task SetInvitation_WithInvalidData_ShouldReturnNull() { var invitation = new Invitation(); var user = new MISUser() { Email = Name, FirstName = Name, LastName = Name, UserName = Name, }; await this.dbContext.AddAsync(user); await this.dbContext.SaveChangesAsync(); await this.userService.SetInvitationAsync(invitation, "asd"); var actual = invitation.User; Assert.IsNull(actual); }
public async Task SetInvitation_WithValidData_ShouldReturnCorrectUser() { var invitation = new Invitation(); var user = new MISUser() { Email = Name, FirstName = Name, LastName = Name, UserName = Name, }; await this.dbContext.AddAsync(user); await this.dbContext.SaveChangesAsync(); await this.userService.SetInvitationAsync(invitation, user.Id); var actual = invitation.User; Assert.AreEqual(user.Id, actual.Id); }
private async Task LoadSharedKeyAndQrCodeUriAsync(MISUser user) { // Load the authenticator key & QR code URI to display on the form var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); if (string.IsNullOrEmpty(unformattedKey)) { await _userManager.ResetAuthenticatorKeyAsync(user); unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); } SharedKey = FormatKey(unformattedKey); var email = await _userManager.GetEmailAsync(user); AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey); }
public async Task RemoveEmployee_WithValidData_ShouldReturnCorrectEmployee() { var company = await this.companyService.CreateAsync(CompanyName, CompanyAddress); var employee = new MISUser() { CompanyId = company.Id, Email = "pesho", FirstName = "pesho", LastName = "pesho", UserName = "******", }; await this.dbContext.AddAsync(employee); await this.dbContext.SaveChangesAsync(); var actual = await this.companyService.RemoveEmployeeAsync(employee.Id); Assert.AreEqual(0, actual.Employees.Count); }
public async Task RemoveEmployee_WithInvalidData_ShouldReturnNull() { var company = await this.companyService.CreateAsync(CompanyName, CompanyAddress); var employee = new MISUser() { CompanyId = company.Id, Email = "pesho", FirstName = "pesho", LastName = "pesho", UserName = "******", }; await this.dbContext.AddAsync(employee); await this.dbContext.SaveChangesAsync(); var actual = await this.companyService.RemoveEmployeeAsync("asd"); Assert.IsNull(actual); }
public async Task AddToCompany_WithValidData_ShouldReturnCorrectUser() { var company = new Company(); var user = new MISUser() { CompanyId = company.Id, Email = Name, FirstName = Name, LastName = Name, UserName = Name, }; await this.dbContext.AddAsync(user); await this.dbContext.SaveChangesAsync(); await this.userService.AddToCompanyAsync(company, user.Id); var actual = company.Employees.FirstOrDefault(x => x.Id == user.Id); Assert.AreEqual(user.Id, actual.Id); }
public async Task Seed() { if (!this.userManager.Users.Any()) { var user = new MISUser { UserName = GlobalConstants.RootAdminName, Email = RootAdminEmail, FirstName = RootAdminFirstName, LastName = RootAdminLastName, EmailConfirmed = true, PhoneNumber = RootAdminPhoneNumber, }; var company = new Company() { Name = RootAdminCompanyName, Address = RootAdminCompanyAddress }; await this.context.AddAsync(company); await this.context.SaveChangesAsync(); var password = RootAdminPassword; user.Company = company; var result = await this.userManager.CreateAsync(user, password); if (result.Succeeded) { await this.userManager.AddToRoleAsync(user, GlobalConstants.AdministratorAreaRole); await this.userManager.AddToRoleAsync(user, GlobalConstants.CompanyOwnerRole); } } }
public async Task <ReportServiceModel> CreateAsync(string companyId, string name, DateTime from, DateTime to, MISUser user) { var report = new Report { Name = name, From = from, To = to, User = user, }; await this.companyService.SetCompanyAsync(report, companyId); if (report.Company == null) { return(null); } await this.receiptService.SetReceiptsAsync(report, from, to, companyId); await this.dbContext.AddAsync(report); await this.dbContext.SaveChangesAsync(); return(report.MapTo <ReportServiceModel>()); }