Exemplo n.º 1
0
        public void GetContacts()
        {
            using (var context = DbUtils.InMemoryApplicationDbContext("Find"))
            {
                var contactsToSave = new List <Contact>
                {
                    new Contact()
                    {
                        Id           = Guid.NewGuid(),
                        ContactName  = "Contact A",
                        TeamEmail    = "*****@*****.**",
                        ContactTelNo = "123456789"
                    },
                    new Contact()
                    {
                        Id           = Guid.NewGuid(),
                        ContactName  = "Contact B",
                        TeamEmail    = "*****@*****.**",
                        ContactTelNo = "123456789"
                    }
                };


                context.AddRange(contactsToSave);
                context.SaveChanges();

                var service = new ContactService(context);
                // Method under test
                var retrievedContacts = service.GetContacts();
                Assert.True(retrievedContacts.Exists(c => c.ContactName == "Contact A"));
                Assert.True(retrievedContacts.Exists(c => c.ContactName == "Contact B"));
            }
        }
Exemplo n.º 2
0
        public void GetReleaseTypes()
        {
            using (var context = DbUtils.InMemoryApplicationDbContext("Find"))
            {
                var releaseTypesToSave = new List <ReleaseType>
                {
                    new ReleaseType()
                    {
                        Id    = Guid.NewGuid(),
                        Title = "Ad Hoc",
                    },
                    new ReleaseType()
                    {
                        Id    = Guid.NewGuid(),
                        Title = "Official Statistics",
                    }
                };


                context.AddRange(releaseTypesToSave);
                context.SaveChanges();

                var service = new MetaService(context);
                // Method under test
                var retrievedReleaseTypes = service.GetReleaseTypes();
                Assert.True(retrievedReleaseTypes.Exists(rt => rt.Title == "Ad Hoc"));
                Assert.True(retrievedReleaseTypes.Exists(rt => rt.Title == "Official Statistics"));
            }
        }
Exemplo n.º 3
0
        public async Task GetThemes_CanViewAllTopics()
        {
            var contextId = Guid.NewGuid().ToString();

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                context.Add(
                    new Theme
                {
                    Title = "Test theme"
                }
                    );

                await context.SaveChangesAsync();
            }

            await PolicyCheckBuilder()
            .SetupCheck(SecurityPolicies.CanAccessSystem)
            .SetupCheck(SecurityPolicies.CanManageAllTaxonomy)
            .AssertSuccess(
                async userService =>
            {
                await using var context = DbUtils.InMemoryApplicationDbContext(contextId);

                var service = SetupThemeService(userService: userService.Object, context: context);
                var result  = await service.GetThemes();

                Assert.Single(result.Right);
                Assert.Equal("Test theme", result.Right[0].Title);

                return(result);
            }
                );
        }
        public async Task UpdatePublication_CanCreatePublicationForSpecificTopic()
        {
            await using var context = DbUtils.InMemoryApplicationDbContext();

            var mocks = Mocks();

            context.Add(_topic);
            context.Add(_publication);

            await context.SaveChangesAsync();

            PermissionTestUtil.AssertSecurityPoliciesChecked(
                async service =>
                await service.UpdatePublication(_publication.Id, new SavePublicationViewModel
            {
                TopicId = _topic.Id,
                Title   = "Updated publication",
                Contact = new SaveContactViewModel
                {
                    TeamName     = "Test team",
                    TeamEmail    = "*****@*****.**",
                    ContactName  = "John Smith",
                    ContactTelNo = "0123456789"
                }
            }),
                _topic,
                mocks.UserService,
                BuildPublicationService(mocks),
                SecurityPolicies.CanCreatePublicationForSpecificTopic);
        }
Exemplo n.º 5
0
        public async Task GetTopic()
        {
            var topic = new Topic
            {
                Title = "Test topic",
                Slug  = "test-topic",
                Theme = new Theme
                {
                    Title = "Test theme"
                }
            };

            var contextId = Guid.NewGuid().ToString();

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                context.Add(topic);
                await context.SaveChangesAsync();
            }

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                var service = SetupTopicService(context);

                var result = await service.GetTopic(topic.Id);

                Assert.True(result.IsRight);

                Assert.Equal(topic.Id, result.Right.Id);
                Assert.Equal("Test topic", result.Right.Title);
                Assert.Equal(topic.ThemeId, result.Right.ThemeId);
            }
        }
Exemplo n.º 6
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.º 7
0
        public void DeleteAll()
        {
            var releaseFile = new ReleaseFile
            {
                Release = _release,
                File    = new File
                {
                    Filename = "ancillary.pdf",
                    Type     = Ancillary,
                    Release  = _release
                }
            };

            var contentDbContextId = Guid.NewGuid().ToString();

            using (var contentDbContext = DbUtils.InMemoryApplicationDbContext(contentDbContextId))
            {
                contentDbContext.AddAsync(releaseFile);
                contentDbContext.SaveChangesAsync();
            }

            PolicyCheckBuilder <SecurityPolicies>()
            .ExpectResourceCheckToFail(_release, CanUpdateSpecificRelease)
            .AssertForbidden(
                userService =>
            {
                var service = SetupReleaseFileService(
                    contentDbContext: DbUtils.InMemoryApplicationDbContext(contentDbContextId),
                    userService: userService.Object);
                return(service.DeleteAll(_release.Id));
            }
                );
        }
Exemplo n.º 8
0
        public async void UpdateTopic()
        {
            var theme = new Theme
            {
                Title = "New theme",
            };

            var topic = new Topic
            {
                Title = "Old title",
                Slug  = "old-title",
                Theme = new Theme
                {
                    Title = "Old theme"
                }
            };

            var contextId = Guid.NewGuid().ToString();

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                context.Add(theme);
                context.Add(topic);

                await context.SaveChangesAsync();
            }

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                var service = SetupTopicService(context);

                var result = await service.UpdateTopic(
                    topic.Id,
                    new SaveTopicViewModel
                {
                    Title   = "New title",
                    ThemeId = theme.Id
                }
                    );

                Assert.True(result.IsRight);
                Assert.Equal(topic.Id, result.Right.Id);
                Assert.Equal("New title", result.Right.Title);
                Assert.Equal("new-title", result.Right.Slug);
                Assert.Equal(theme.Id, result.Right.ThemeId);

                var savedTopic = await context.Topics.FindAsync(result.Right.Id);

                Assert.Equal("New title", savedTopic.Title);
                Assert.Equal("new-title", savedTopic.Slug);
                Assert.Equal(theme.Id, savedTopic.ThemeId);
            }
        }
Exemplo n.º 9
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.º 10
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.º 11
0
        public async void UpdateTopic_FailsNonUniqueSlug()
        {
            var theme = new Theme
            {
                Title = "Test theme",
            };
            var topic = new Topic
            {
                Title = "Old title",
                Slug  = "old-title",
                Theme = theme
            };

            var contextId = Guid.NewGuid().ToString();

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                context.Add(topic);
                context.Add(
                    new Topic
                {
                    Title = "Other topic",
                    Slug  = "other-topic",
                    Theme = theme
                }
                    );

                await context.SaveChangesAsync();
            }

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                var service = SetupTopicService(context);

                var result = await service.UpdateTopic(
                    topic.Id,
                    new SaveTopicViewModel
                {
                    Title   = "Other topic",
                    ThemeId = topic.ThemeId
                }
                    );

                Assert.True(result.IsLeft);
                var badRequestResult = Assert.IsType <BadRequestObjectResult>(result.Left);
                var details          = Assert.IsType <ValidationProblemDetails>(badRequestResult.Value);

                Assert.Equal("SLUG_NOT_UNIQUE", details.Errors[""].First());
            }
        }
Exemplo n.º 12
0
        public async Task CreateTopic()
        {
            var theme = new Theme
            {
                Title = "Test theme",
            };

            var contextId = Guid.NewGuid().ToString();

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                context.Add(theme);

                await context.SaveChangesAsync();
            }

            var publishingService = new Mock <IPublishingService>(Strict);

            publishingService.Setup(s => s.TaxonomyChanged())
            .ReturnsAsync(Unit.Instance);

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                var service = SetupTopicService(contentContext: context,
                                                publishingService: publishingService.Object);

                var result = await service.CreateTopic(
                    new TopicSaveViewModel
                {
                    Title   = "Test topic",
                    ThemeId = theme.Id
                }
                    );

                VerifyAllMocks(publishingService);

                Assert.True(result.IsRight);
                Assert.Equal("Test topic", result.Right.Title);
                Assert.Equal("test-topic", result.Right.Slug);
                Assert.Equal(theme.Id, result.Right.ThemeId);

                var savedTopic = await context.Topics.FindAsync(result.Right.Id);

                Assert.NotNull(savedTopic);
                Assert.Equal("Test topic", savedTopic !.Title);
                Assert.Equal("test-topic", savedTopic.Slug);
                Assert.Equal(theme.Id, savedTopic.ThemeId);
            }
        }
Exemplo n.º 13
0
        public async void DeleteTopic()
        {
            var topicId = Guid.NewGuid();

            var topic = new Topic
            {
                Id    = topicId,
                Title = "UI test topic"
            };

            var release = new Release
            {
                Publication = new Publication
                {
                    Topic = new Data.Model.Topic
                    {
                        Id    = topicId,
                        Title = "UI test topic"
                    }
                }
            };

            var contextId = Guid.NewGuid().ToString();

            await using (var contentContext = DbUtils.InMemoryApplicationDbContext(contextId))
                await using (var statisticsContext = InMemoryStatisticsDbContext(contextId))
                {
                    contentContext.Add(topic);
                    statisticsContext.Add(release);

                    await contentContext.SaveChangesAsync();

                    await statisticsContext.SaveChangesAsync();
                }

            await using (var contentContext = DbUtils.InMemoryApplicationDbContext(contextId))
                await using (var statisticsContext = InMemoryStatisticsDbContext(contextId))
                {
                    var service = SetupTopicService(contentContext, statisticsContext: statisticsContext);

                    var result = await service.DeleteTopic(topic.Id);

                    Assert.True(result.IsRight);

                    Assert.Equal(0, contentContext.Topics.Count());
                    Assert.Equal(0, statisticsContext.Release.Count());
                    Assert.Equal(0, statisticsContext.Topic.Count());
                }
        }
Exemplo n.º 14
0
        public async Task CreateTopic_FailsNonExistingTheme()
        {
            await using var context = DbUtils.InMemoryApplicationDbContext();

            var service = SetupTopicService(context);

            var result = await service.CreateTopic(
                new TopicSaveViewModel
            {
                Title   = "Test topic",
                ThemeId = Guid.NewGuid()
            }
                );

            result.AssertBadRequest(ThemeDoesNotExist);
        }
Exemplo n.º 15
0
        public async Task UpdateTopic_FailsNonUniqueSlug()
        {
            var theme = new Theme
            {
                Title = "Test theme",
            };
            var topic = new Topic
            {
                Title = "Old title",
                Slug  = "old-title",
                Theme = theme
            };

            var contextId = Guid.NewGuid().ToString();

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                context.Add(topic);
                context.Add(
                    new Topic
                {
                    Title = "Other topic",
                    Slug  = "other-topic",
                    Theme = theme
                }
                    );

                await context.SaveChangesAsync();
            }

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                var service = SetupTopicService(context);

                var result = await service.UpdateTopic(
                    topic.Id,
                    new TopicSaveViewModel
                {
                    Title   = "Other topic",
                    ThemeId = topic.ThemeId
                }
                    );

                result.AssertBadRequest(SlugNotUnique);
            }
        }
Exemplo n.º 16
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.º 17
0
        public async void CreateTopic_FailsNonExistingTheme()
        {
            await using var context = DbUtils.InMemoryApplicationDbContext();

            var service = SetupTopicService(context);

            var result = await service.CreateTopic(
                new SaveTopicViewModel
            {
                Title   = "Test topic",
                ThemeId = Guid.NewGuid()
            }
                );

            Assert.True(result.IsLeft);
            var badRequestResult = Assert.IsType <BadRequestObjectResult>(result.Left);
            var details          = Assert.IsType <ValidationProblemDetails>(badRequestResult.Value);

            Assert.Equal("THEME_DOES_NOT_EXIST", details.Errors[""].First());
        }
        private void AssertSecurityPoliciesChecked <T>(
            Func <DataBlockService, Task <Either <ActionResult, T> > > protectedAction, params SecurityPolicies[] policies)
        {
            var(userService, persistenceHelper, fileStorageService) = Mocks();

            using (var context = DbUtils.InMemoryApplicationDbContext())
            {
                context.Add(new ReleaseContentBlock
                {
                    Release        = _release,
                    ContentBlockId = _dataBlock.Id
                });
                context.SaveChanges();

                var service = new DataBlockService(context, AdminMapper(),
                                                   persistenceHelper.Object, userService.Object,
                                                   fileStorageService.Object);

                PermissionTestUtil.AssertSecurityPoliciesChecked(protectedAction, _release, userService, service, policies);
            }
        }
        public async Task CreatePublication()
        {
            await using var context = DbUtils.InMemoryApplicationDbContext();

            var mocks = Mocks();

            context.Add(_topic);

            await context.SaveChangesAsync();

            PermissionTestUtil.AssertSecurityPoliciesChecked(
                async service =>
                await service.CreatePublication(new SavePublicationViewModel
            {
                TopicId = _topic.Id,
            }),
                _topic,
                mocks.UserService,
                BuildPublicationService(mocks),
                SecurityPolicies.CanCreatePublicationForSpecificTopic);
        }
Exemplo n.º 20
0
        public async void CreateTopic()
        {
            var theme = new Theme
            {
                Title = "Test theme",
            };

            var contextId = Guid.NewGuid().ToString();

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                context.Add(theme);

                await context.SaveChangesAsync();
            }

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                var service = SetupTopicService(context);

                var result = await service.CreateTopic(
                    new SaveTopicViewModel
                {
                    Title   = "Test topic",
                    ThemeId = theme.Id
                }
                    );

                Assert.True(result.IsRight);
                Assert.Equal("Test topic", result.Right.Title);
                Assert.Equal("test-topic", result.Right.Slug);
                Assert.Equal(theme.Id, result.Right.ThemeId);

                var savedTopic = await context.Topics.FindAsync(result.Right.Id);

                Assert.Equal("Test topic", savedTopic.Title);
                Assert.Equal("test-topic", savedTopic.Slug);
                Assert.Equal(theme.Id, savedTopic.ThemeId);
            }
        }
Exemplo n.º 21
0
        private DataBlockService BuildDataBlockService(
            ContentDbContext?contentDbContext = null,
            IPersistenceHelper <ContentDbContext>?persistenceHelper = null,
            IReleaseFileService?releaseFileService = null,
            IReleaseContentBlockRepository?releaseContentBlockRepository = null,
            IUserService?userService         = null,
            IBlobCacheService?cacheService   = null,
            ICacheKeyService?cacheKeyService = null)
        {
            using var context = DbUtils.InMemoryApplicationDbContext();

            var service = new DataBlockService(
                contentDbContext ?? Mock.Of <ContentDbContext>(Strict),
                persistenceHelper ?? PersistenceHelperMock().Object,
                releaseFileService ?? Mock.Of <IReleaseFileService>(Strict),
                releaseContentBlockRepository ?? Mock.Of <IReleaseContentBlockRepository>(Strict),
                userService ?? Mock.Of <IUserService>(Strict),
                AdminMapper(),
                cacheService ?? Mock.Of <IBlobCacheService>(Strict),
                cacheKeyService ?? Mock.Of <ICacheKeyService>(Strict)
                );

            return(service);
        }
Exemplo n.º 22
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.º 23
0
        public async Task GetThemes_CanViewLinkedTopics()
        {
            var userId = Guid.NewGuid();

            var contextId = Guid.NewGuid().ToString();

            await using (var context = DbUtils.InMemoryApplicationDbContext(contextId))
            {
                await context.AddRangeAsync(
                    new UserReleaseRole
                {
                    Release = new Release
                    {
                        Publication = new Publication
                        {
                            Topic = new Topic
                            {
                                Title = "Another topic",
                                Theme = new Theme
                                {
                                    Title = "Another theme"
                                }
                            }
                        }
                    }
                },
                    new UserReleaseRole
                {
                    UserId  = userId,
                    Release = new Release
                    {
                        Publication = new Publication
                        {
                            Topic = new Topic
                            {
                                Title = "Expected topic",
                                Theme = new Theme
                                {
                                    Title = "Expected theme"
                                }
                            }
                        }
                    }
                }
                    );

                await context.SaveChangesAsync();
            }

            await PolicyCheckBuilder()
            .SetupCheck(SecurityPolicies.CanAccessSystem)
            .SetupCheck(SecurityPolicies.CanManageAllTaxonomy, false)
            .AssertSuccess(
                async userService =>
            {
                await using var context = DbUtils.InMemoryApplicationDbContext(contextId);

                userService
                .Setup(s => s.GetUserId())
                .Returns(userId);

                var service = SetupThemeService(userService: userService.Object, context: context);
                var result  = await service.GetThemes();

                Assert.Single(result.Right);
                Assert.Equal("Expected theme", result.Right[0].Title);

                Assert.Single(result.Right[0].Topics);
                Assert.Equal("Expected topic", result.Right[0].Topics[0].Title);

                return(result);
            }
                );
        }
Exemplo n.º 24
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.º 25
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.º 26
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.º 27
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);
                }
        }