Exemplo n.º 1
0
        public async void AddPreReleaseUser_Fails_ExistingReleaseInvite()
        {
            var release = new Release
            {
                Publication = new Publication()
            };
            var contextId = Guid.NewGuid().ToString();

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                context.Add(
                    new UserReleaseInvite
                {
                    Release = release,
                    Role    = ReleaseRole.PrereleaseViewer,
                    Email   = "*****@*****.**",
                }
                    );

                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.AddPreReleaseUser(release.Id, "*****@*****.**");

                    Assert.True(result.IsLeft);

                    var badRequestObjectResult = Assert.IsType <BadRequestObjectResult>(result.Left);
                    var details = Assert.IsType <ValidationProblemDetails>(badRequestObjectResult.Value);
                    Assert.Equal("USER_ALREADY_EXISTS", details.Errors[""].First());
                }
        }
Exemplo n.º 2
0
        public async void GetPreReleaseUsers_FiltersInvalidReleaseInvites()
        {
            var release   = new Release();
            var contextId = Guid.NewGuid().ToString();

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                await context.AddRangeAsync(
                    // Not a prerelease viewer
                    new UserReleaseInvite
                {
                    Release  = release,
                    Email    = "*****@*****.**",
                    Role     = ReleaseRole.Contributor,
                    Accepted = false
                },
                    // Different release
                    new UserReleaseInvite
                {
                    Release  = new Release(),
                    Email    = "*****@*****.**",
                    Role     = ReleaseRole.PrereleaseViewer,
                    Accepted = false
                },
                    // Already accepted
                    new UserReleaseInvite
                {
                    Release  = release,
                    Email    = "*****@*****.**",
                    Role     = ReleaseRole.PrereleaseViewer,
                    Accepted = true
                }
                    );


                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.Empty(users);
                }
        }
Exemplo n.º 3
0
        public async void AddPreReleaseUser_Fails_NoRelease()
        {
            var contextId = Guid.NewGuid().ToString();

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
                await using (var userAndRolesDbContext = DbUtils.InMemoryUserAndRolesDbContext())
                {
                    var service = SetupPreReleaseUserService(context, usersAndRolesDbContext: userAndRolesDbContext);
                    var result  = await service.AddPreReleaseUser(Guid.NewGuid(), "*****@*****.**");

                    Assert.True(result.IsLeft);
                    Assert.IsType <NotFoundResult>(result.Left);
                }
        }
Exemplo n.º 4
0
        public async void GetPreReleaseUsers_FiltersInvalidReleaseUsers()
        {
            var release   = new Release();
            var contextId = Guid.NewGuid().ToString();

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                await context.AddRangeAsync(
                    // Not a prerelease viewer
                    new UserReleaseRole
                {
                    Release = release,
                    Role    = ReleaseRole.Lead,
                    User    = new User
                    {
                        Email = "*****@*****.**",
                    }
                },
                    // Different release user
                    new UserReleaseRole
                {
                    Release = new Release(),
                    Role    = ReleaseRole.PrereleaseViewer,
                    User    = new User
                    {
                        Email = "*****@*****.**",
                    }
                }
                    );

                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.Empty(users);
                }
        }
Exemplo n.º 5
0
        public async void RemovePreReleaseUser_Fails_InvalidEmail()
        {
            var release   = new Release();
            var contextId = Guid.NewGuid().ToString();

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                context.Add(release);
                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.RemovePreReleaseUser(release.Id, "not an email");

                    Assert.True(result.IsLeft);

                    var badRequestObjectResult = Assert.IsType <BadRequestObjectResult>(result.Left);
                    var details = Assert.IsType <ValidationProblemDetails>(badRequestObjectResult.Value);
                    Assert.Equal("INVALID_EMAIL_ADDRESS", details.Errors[""].First());
                }
        }
Exemplo n.º 6
0
        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);
                }
        }
Exemplo n.º 7
0
        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);
            }
        }
Exemplo n.º 8
0
        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);
                }
        }
Exemplo n.º 9
0
        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);
                }
        }