public async void RemovePreReleaseUser_AcceptedSystemInviteNotRemoved() { var release = new Release(); var contextId = Guid.NewGuid().ToString(); await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) await using (var userAndRolesDbContext = DbUtils.InMemoryUserAndRolesDbContext(contextId)) { await context.AddRangeAsync( new UserReleaseInvite { Release = release, Role = ReleaseRole.PrereleaseViewer, Email = "*****@*****.**" } ); await context.SaveChangesAsync(); userAndRolesDbContext.Add( new UserInvite { Email = "*****@*****.**", Role = new IdentityRole { Name = "Prerelease User" }, Accepted = true } ); await userAndRolesDbContext.SaveChangesAsync(); } await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) await using (var userAndRolesDbContext = DbUtils.InMemoryUserAndRolesDbContext(contextId)) { var service = SetupPreReleaseUserService( context, usersAndRolesDbContext: userAndRolesDbContext ); var result = await service.RemovePreReleaseUser( release.Id, "*****@*****.**" ); Assert.True(result.IsRight); Assert.IsType <Unit>(result.Right); } await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) await using (var userAndRolesDbContext = DbUtils.InMemoryUserAndRolesDbContext(contextId)) { var savedUserReleaseInvites = await context.UserReleaseInvites .ToListAsync(); Assert.Empty(savedUserReleaseInvites); var savedUserInvites = await userAndRolesDbContext.UserInvites .ToListAsync(); Assert.Single(savedUserInvites); Assert.Equal("*****@*****.**", savedUserInvites[0].Email); } }
public async void AddPreReleaseUser_InvitesNewUser() { var release = new Release { ReleaseName = "2020", TimePeriodCoverage = TimeIdentifier.CalendarYear, PublishScheduled = DateTime.Parse("2020-09-09T00:00:00.00Z", styles: DateTimeStyles.AdjustToUniversal), Publication = new Publication { Title = "Test publication", } }; var identityRole = new IdentityRole { Name = "Prerelease User" }; var contextId = Guid.NewGuid().ToString(); await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) await using (var userAndRolesDbContext = DbUtils.InMemoryUserAndRolesDbContext(contextId)) { context.Add(release); await context.SaveChangesAsync(); userAndRolesDbContext.Add(identityRole); await userAndRolesDbContext.SaveChangesAsync(); } await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) await using (var userAndRolesDbContext = DbUtils.InMemoryUserAndRolesDbContext(contextId)) { var preReleaseService = new Mock <IPreReleaseService>(); preReleaseService .Setup(s => s.GetPreReleaseWindow(It.IsAny <Release>())) .Returns( new PreReleaseWindow { Start = DateTime.Parse("2020-09-08T08:30:00.00Z", styles: DateTimeStyles.AdjustToUniversal), End = DateTime.Parse("2020-09-08T22:59:59.00Z", styles: DateTimeStyles.AdjustToUniversal), } ); var emailService = new Mock <IEmailService>(); var service = SetupPreReleaseUserService( context, usersAndRolesDbContext: userAndRolesDbContext, preReleaseService: preReleaseService.Object, emailService: emailService.Object ); var result = await service.AddPreReleaseUser( release.Id, "*****@*****.**" ); emailService.Verify( s => s.SendEmail( "*****@*****.**", "the-template-id", new Dictionary <string, dynamic> { { "newUser", "yes" }, { "release name", "Calendar Year 2020" }, { "publication name", "Test publication" }, { "prerelease link", $"http://localhost/publication/{release.PublicationId}/release/{release.Id}/prerelease" }, { "prerelease day", "Tuesday 08 September 2020" }, { "prerelease time", "09:30" }, { "publish day", "Wednesday 09 September 2020" }, { "publish time", "09:30" }, } ) ); Assert.True(result.IsRight); Assert.Equal("*****@*****.**", result.Right.Email); } await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) await using (var userAndRolesDbContext = DbUtils.InMemoryUserAndRolesDbContext(contextId)) { var releaseInvite = await context.UserReleaseInvites .Where(userReleaseInvite => userReleaseInvite.ReleaseId == release.Id) .SingleAsync(); Assert.Equal("*****@*****.**", releaseInvite.Email); Assert.Equal(ReleaseRole.PrereleaseViewer, releaseInvite.Role); var systemInvite = await userAndRolesDbContext.UserInvites .Where(userInvite => userInvite.Email == "*****@*****.**") .SingleAsync(); Assert.Equal("*****@*****.**", systemInvite.Email); Assert.Equal(identityRole.Id, systemInvite.RoleId); } }
public async void RemovePreReleaseUser_OtherReleaseRolesNotRemoved() { var release = new Release(); var otherRelease = new Release(); var contextId = Guid.NewGuid().ToString(); await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) await using (var userAndRolesDbContext = DbUtils.InMemoryUserAndRolesDbContext(contextId)) { context.Add(release); await context.AddRangeAsync( new UserReleaseRole { Release = release, Role = ReleaseRole.PrereleaseViewer, User = new User { Email = "*****@*****.**" } }, // Belongs to another release new UserReleaseRole { Release = otherRelease, Role = ReleaseRole.PrereleaseViewer, User = new User { Email = "*****@*****.**" } }, // Has a different role on the release new UserReleaseRole { Release = release, Role = ReleaseRole.Lead, User = new User { Email = "*****@*****.**" } } ); await context.SaveChangesAsync(); userAndRolesDbContext.Add( new UserInvite { Email = "*****@*****.**", Role = new IdentityRole { Name = "Prerelease User" }, Accepted = false } ); await userAndRolesDbContext.SaveChangesAsync(); } await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) await using (var userAndRolesDbContext = DbUtils.InMemoryUserAndRolesDbContext(contextId)) { var service = SetupPreReleaseUserService( context, usersAndRolesDbContext: userAndRolesDbContext ); var result = await service.RemovePreReleaseUser( release.Id, "*****@*****.**" ); Assert.True(result.IsRight); Assert.IsType <Unit>(result.Right); } await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) { var savedUserReleaseRoles = await context.UserReleaseRoles .ToListAsync(); Assert.Equal(2, savedUserReleaseRoles.Count); Assert.Equal(otherRelease.Id, savedUserReleaseRoles[0].ReleaseId); Assert.Equal(ReleaseRole.PrereleaseViewer, savedUserReleaseRoles[0].Role); Assert.Equal(release.Id, savedUserReleaseRoles[1].ReleaseId); Assert.Equal(ReleaseRole.Lead, savedUserReleaseRoles[1].Role); } }
public async void GetPreReleaseUsers_OrderedCorrectly() { var release = new Release(); var contextId = Guid.NewGuid().ToString(); await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) { await context.AddRangeAsync( new UserReleaseRole { Release = release, Role = ReleaseRole.PrereleaseViewer, User = new User { Email = "*****@*****.**", } }, new UserReleaseRole { Release = release, Role = ReleaseRole.PrereleaseViewer, User = new User { Email = "*****@*****.**", } } ); await context.AddRangeAsync( new UserReleaseInvite { Release = release, Email = "*****@*****.**", Role = ReleaseRole.PrereleaseViewer, Accepted = false }, new UserReleaseInvite { Release = release, Email = "*****@*****.**", Role = ReleaseRole.PrereleaseViewer, Accepted = false } ); await context.SaveChangesAsync(); } await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) await using (var userAndRolesDbContext = DbUtils.InMemoryUserAndRolesDbContext()) { var service = SetupPreReleaseUserService(context, usersAndRolesDbContext: userAndRolesDbContext); var result = await service.GetPreReleaseUsers(release.Id); Assert.True(result.IsRight); var users = result.Right; Assert.Equal(4, users.Count); Assert.Equal("*****@*****.**", users[0].Email); Assert.Equal("*****@*****.**", users[1].Email); Assert.Equal("*****@*****.**", users[2].Email); Assert.Equal("*****@*****.**", users[3].Email); } }