private EmailService Target(User currentUser, Mock <IEmailSender> emailSender = null) { var authMock = new FakeAuth(currentUser); return(new EmailService( emailSender: emailSender?.Object ?? new TestFakeEmailSender(), authManager: authMock, viewRenderer: new ViewRendererFake(), url: new BaseUrlsStub())); }
public async Task SendInviteEmailsAsync_NotAllowedUsers_ExceptionAsync(Role currentUserRole) { await using var context = InMemoryDatabaseHelper.GetDbContext(); var userRepo = new UserRepository(context); var user1 = await new ApplicationUserFactory(Role.Employee).BuildAsync(userRepo); var user2 = await new ApplicationUserFactory(Role.Employee).CreateConfirmedAsync(context); IAuthorizationManager authMock = new FakeAuth(currentUserRole); var service = Target(context, authMock); await Assert.ThrowsAsync <NoPermissionsException>(() => service.SendInviteEmailsAsync()); }
public async Task InsertAsync_NoFunctionalManager_OkAsync(Role currentUserRole) { await using var context = InMemoryDatabaseHelper.GetDbContext(); IAuthorizationManager authMock = new FakeAuth(currentUserRole); var service = Target(context, authMock); User user = await service.GetByIdAsync( await service.InsertAsync( new UserDto("John", "Test", "*****@*****.**"))); Assert.Equal(Role.Employee, user.Role); Assert.Equal("John", user.FirstName); Assert.Equal("Test", user.LastName); Assert.Equal("*****@*****.**", user.UserName); Assert.Equal("*****@*****.**", user.Email); }
public async Task UpdateAsync_NotAdminSetHigherRole_ExceptionAsync(Role currentUserRole) { await using var context = InMemoryDatabaseHelper.GetDbContext(); var userRepo = new UserRepository(context); var user1 = await new ApplicationUserFactory(Role.Employee).BuildAsync(userRepo); var user2 = await new ApplicationUserFactory(Role.Employee).BuildAsync(userRepo); IAuthorizationManager authMock = new FakeAuth(currentUserRole); var service = Target(context, authMock); var data = new UserDto("John", "Test", "*****@*****.**"); long userId = await service.InsertAsync(data); data = AutomapperSingleton.Map <UserDto>(await service.GetByIdAsync(userId)); data.Role = Role.SystemAdministrator; await Assert.ThrowsAsync <BadAssException>(() => service.UpdateAsync(data)); }
public async Task SendInviteEmailsAsync_EmailWasNotSentToConfirmedUser_OkAsync(Role currentUserRole) { await using var context = InMemoryDatabaseHelper.GetDbContext(); var userRepo = new UserRepository(context); var user1 = await new ApplicationUserFactory(Role.Employee).CreateConfirmedAsync(context); var user2 = await new ApplicationUserFactory(Role.Employee).CreateConfirmedAsync(context); IAuthorizationManager authMock = new FakeAuth(currentUserRole); var emailStub = new UserEmailStub(); var target = new UserService( userRepository: userRepo, authorizationManager: new FakeAuth(currentUserRole), emailDomainValidatorService: new EmailDomainValidatorService("example.com|hipo.kz"), emailSender: emailStub, userEvent: new UserEventStub()); var emailsCount = await target.SendInviteEmailsAsync(); Assert.Equal(0, emailsCount); Assert.Equal(0, emailStub.SendInviteEmailAsyncInvocations); }