public async Task CreateAsync(CreateCategory command) { await _createValidator.ValidateCommandAsync(command); var categoriesCount = await _dbContext.Categories .Where(x => x.SiteId == command.SiteId && x.Status != CategoryStatusType.Deleted) .CountAsync(); var sortOrder = categoriesCount + 1; var category = new Category(command.Id, command.SiteId, command.Name, sortOrder, command.PermissionSetId); _dbContext.Categories.Add(category); _dbContext.Events.Add(new Event(command.SiteId, command.UserId, EventType.Created, typeof(Category), category.Id, new { category.Name, category.PermissionSetId, category.SortOrder })); await _dbContext.SaveChangesAsync(); _cacheManager.Remove(CacheKeys.Categories(command.SiteId)); _cacheManager.Remove(CacheKeys.CurrentForums(command.SiteId)); }
public async Task CreateAsync(CreatePermissionSet command) { await _createValidator.ValidateCommandAsync(command); var permissionSet = new PermissionSet(command.Id, command.SiteId, command.Name, command.Permissions); _dbContext.PermissionSets.Add(permissionSet); _dbContext.Events.Add(new Event(command.SiteId, command.UserId, EventType.Created, typeof(PermissionSet), command.Id, new { command.SiteId, command.Name, command.Permissions })); await _dbContext.SaveChangesAsync(); }
public async Task <string> CreateAsync(CreateTopic command) { await _createValidator.ValidateCommandAsync(command); var title = Regex.Replace(command.Title, @"\s+", " "); // Remove multiple spaces from title var slug = string.IsNullOrWhiteSpace(command.Slug) ? await GenerateSlugAsync(command.ForumId, title) : command.Slug; var topic = Post.CreateTopic(command.Id, command.ForumId, command.UserId, title, slug, command.Content, command.Status); _dbContext.Posts.Add(topic); _dbContext.Events.Add(new Event(command.SiteId, command.UserId, EventType.Created, typeof(Post), command.Id, new { command.ForumId, title, Slug = slug, command.Content, command.Status })); var forum = await _dbContext.Forums.Include(x => x.Category).FirstOrDefaultAsync(x => x.Id == topic.ForumId); forum.UpdateLastPost(topic.Id); forum.IncreaseTopicsCount(); forum.Category.IncreaseTopicsCount(); var user = await _dbContext.Users.FirstOrDefaultAsync(x => x.Id == topic.CreatedBy); user.IncreaseTopicsCount(); await _dbContext.SaveChangesAsync(); _cacheManager.Remove(CacheKeys.Forum(forum.Id)); return(slug); }
public async Task Should_return_false_when_name_is_not_unique_for_existing_forum() { var options = Shared.CreateContextOptions(); var siteId = Guid.NewGuid(); var categoryId = Guid.NewGuid(); var forumId = Guid.NewGuid(); using (var dbContext = new AtlesDbContext(options)) { var category = new Category(categoryId, siteId, "Category", 1, Guid.NewGuid()); var forum1 = new Forum(categoryId, "Forum 1", "My Forum", "my-forum", 1); var forum2 = new Forum(forumId, categoryId, "Forum 2", "my-forum-2", "My Forum", 2); dbContext.Categories.Add(category); dbContext.Forums.Add(forum1); dbContext.Forums.Add(forum2); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var sut = new ForumRules(dbContext); var actual = await sut.IsNameUniqueAsync(siteId, categoryId, "Forum 1", forumId); Assert.IsFalse(actual); } }
public async Task Should_return_false_when_name_is_not_unique_for_existing_permission_set() { var options = Shared.CreateContextOptions(); var siteId = Guid.NewGuid(); var permissionSetId = Guid.NewGuid(); using (var dbContext = new AtlesDbContext(options)) { var site = new Site(siteId, "Name", "Title"); var permissionSet1 = new PermissionSet(siteId, "Permission Set 1", new List <PermissionCommand>()); var permissionSet2 = new PermissionSet(permissionSetId, siteId, "Permission Set 2", new List <PermissionCommand>()); dbContext.Sites.Add(site); dbContext.PermissionSets.Add(permissionSet1); dbContext.PermissionSets.Add(permissionSet2); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var sut = new PermissionSetRules(dbContext); var actual = await sut.IsNameUniqueAsync(siteId, "Permission Set 1", permissionSetId); Assert.IsFalse(actual); } }
public async Task CreateAsync(CreateReply command) { await _createValidator.ValidateCommandAsync(command); var reply = Post.CreateReply(command.Id, command.TopicId, command.ForumId, command.UserId, command.Content, command.Status); _dbContext.Posts.Add(reply); _dbContext.Events.Add(new Event(command.SiteId, command.UserId, EventType.Created, typeof(Post), command.Id, new { command.TopicId, command.ForumId, command.Content, command.Status })); var topic = await _dbContext.Posts .Include(x => x.Forum) .ThenInclude(x => x.Category) .FirstOrDefaultAsync(x => x.Id == reply.TopicId); topic.UpdateLastReply(reply.Id); topic.IncreaseRepliesCount(); topic.Forum.UpdateLastPost(reply.Id); topic.Forum.IncreaseRepliesCount(); topic.Forum.Category.IncreaseRepliesCount(); var user = await _dbContext.Users.FirstOrDefaultAsync(x => x.Id == reply.CreatedBy); user.IncreaseRepliesCount(); await _dbContext.SaveChangesAsync(); _cacheManager.Remove(CacheKeys.Forum(topic.ForumId)); }
public async Task Should_update_reply_and_add_event() { var options = Shared.CreateContextOptions(); var siteId = Guid.NewGuid(); var categoryId = Guid.NewGuid(); var forumId = Guid.NewGuid(); var topicId = Guid.NewGuid(); var category = new Category(categoryId, siteId, "Category", 1, Guid.NewGuid()); var forum = new Forum(forumId, categoryId, "Forum", "my-forum", "My Forum", 1); var topic = Post.CreateTopic(topicId, forumId, Guid.NewGuid(), "Title", "slug", "Content", PostStatusType.Published); var reply = Post.CreateReply(Guid.NewGuid(), topicId, forumId, Guid.NewGuid(), "Content", PostStatusType.Published); using (var dbContext = new AtlesDbContext(options)) { dbContext.Categories.Add(category); dbContext.Forums.Add(forum); dbContext.Posts.Add(topic); dbContext.Posts.Add(reply); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var command = Fixture.Build <UpdateReply>() .With(x => x.Id, reply.Id) .With(x => x.SiteId, siteId) .With(x => x.ForumId, forumId) .With(x => x.TopicId, topicId) .Create(); var cacheManager = new Mock <ICacheManager>(); var createValidator = new Mock <IValidator <CreateReply> >(); var updateValidator = new Mock <IValidator <UpdateReply> >(); updateValidator .Setup(x => x.ValidateAsync(command, new CancellationToken())) .ReturnsAsync(new ValidationResult()); var sut = new ReplyService(dbContext, cacheManager.Object, createValidator.Object, updateValidator.Object); await sut.UpdateAsync(command); var updatedReply = await dbContext.Posts.FirstOrDefaultAsync(x => x.Id == command.Id); var @event = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == command.Id); updateValidator.Verify(x => x.ValidateAsync(command, new CancellationToken())); Assert.AreEqual(command.Content, updatedReply.Content); Assert.NotNull(@event); } }
public async Task Should_move_forum_down_and_add_events() { var options = Shared.CreateContextOptions(); var categoryId = Guid.NewGuid(); var siteId = Guid.NewGuid(); var category = new Category(categoryId, siteId, "Category", 1, Guid.NewGuid()); var forum1 = new Forum(categoryId, "Forum 1", "my-forum-1", "Forum 1", 1); var forum2 = new Forum(categoryId, "Forum 2", "my-forum-2", "Forum 2", 2); using (var dbContext = new AtlesDbContext(options)) { dbContext.Categories.Add(category); dbContext.Forums.Add(forum1); dbContext.Forums.Add(forum2); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var command = new MoveForum { Id = forum1.Id, Direction = Direction.Down, SiteId = siteId }; var cacheManager = new Mock <ICacheManager>(); var createValidator = new Mock <IValidator <CreateForum> >(); var updateValidator = new Mock <IValidator <UpdateForum> >(); var sut = new ForumService(dbContext, cacheManager.Object, createValidator.Object, updateValidator.Object); await sut.MoveAsync(command); var updatedForum1 = await dbContext.Forums.FirstOrDefaultAsync(x => x.Id == forum1.Id); var updatedForum2 = await dbContext.Forums.FirstOrDefaultAsync(x => x.Id == forum2.Id); var event1 = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == forum1.Id); var event2 = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == forum2.Id); Assert.AreEqual(forum2.SortOrder, updatedForum1.SortOrder); Assert.AreEqual(forum1.SortOrder, updatedForum2.SortOrder); Assert.NotNull(event1); Assert.NotNull(event2); } }
public async Task Should_update_permission_set_and_add_event() { var options = Shared.CreateContextOptions(); var permissionSet = new PermissionSet(Guid.NewGuid(), Guid.NewGuid(), "Default", new List <PermissionCommand>()); using (var dbContext = new AtlesDbContext(options)) { dbContext.PermissionSets.Add(permissionSet); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var command = new UpdatePermissionSet { SiteId = permissionSet.SiteId, Id = permissionSet.Id, Name = "Permission Set", Permissions = new List <PermissionCommand> { new PermissionCommand { Type = PermissionType.Start, RoleId = Guid.NewGuid().ToString() } } }; var cacheManager = new Mock <ICacheManager>(); var createValidator = new Mock <IValidator <CreatePermissionSet> >(); var updateValidator = new Mock <IValidator <UpdatePermissionSet> >(); updateValidator .Setup(x => x.ValidateAsync(command, new CancellationToken())) .ReturnsAsync(new ValidationResult()); var sut = new PermissionSetService(dbContext, cacheManager.Object, createValidator.Object, updateValidator.Object); await sut.UpdateAsync(command); var updatedPermissionSet = await dbContext.PermissionSets.FirstOrDefaultAsync(x => x.Id == command.Id); var @event = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == command.Id); updateValidator.Verify(x => x.ValidateAsync(command, new CancellationToken())); Assert.AreEqual(command.Name, updatedPermissionSet.Name); Assert.NotNull(@event); } }
public async Task CreateAsync(CreateForum command) { await _createValidator.ValidateCommandAsync(command); var forumsCount = await _dbContext.Forums .Where(x => x.CategoryId == command.CategoryId && x.Status != ForumStatusType.Deleted) .CountAsync(); var sortOrder = forumsCount + 1; var forum = new Forum(command.Id, command.CategoryId, command.Name, command.Slug, command.Description, sortOrder, command.PermissionSetId); _dbContext.Forums.Add(forum); _dbContext.Events.Add(new Event(command.SiteId, command.UserId, EventType.Created, typeof(Forum), forum.Id, new { forum.Name, forum.Slug, forum.Description, forum.CategoryId, forum.PermissionSetId, forum.SortOrder })); await _dbContext.SaveChangesAsync(); _cacheManager.Remove(CacheKeys.Categories(command.SiteId)); _cacheManager.Remove(CacheKeys.CurrentForums(command.SiteId)); }
public async Task Should_update_forum_and_add_event() { var options = Shared.CreateContextOptions(); var categoryId = Guid.NewGuid(); var siteId = Guid.NewGuid(); var category = new Category(categoryId, siteId, "Category", 1, Guid.NewGuid()); var forum = new Forum(categoryId, "Forum 1", "my-forum", "Forum 1", 1); using (var dbContext = new AtlesDbContext(options)) { dbContext.Categories.Add(category); dbContext.Forums.Add(forum); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var command = Fixture.Build <UpdateForum>() .With(x => x.Id, forum.Id) .With(x => x.CategoryId, forum.CategoryId) .With(x => x.SiteId, siteId) .Create(); var cacheManager = new Mock <ICacheManager>(); var createValidator = new Mock <IValidator <CreateForum> >(); var updateValidator = new Mock <IValidator <UpdateForum> >(); updateValidator .Setup(x => x.ValidateAsync(command, new CancellationToken())) .ReturnsAsync(new ValidationResult()); var sut = new ForumService(dbContext, cacheManager.Object, createValidator.Object, updateValidator.Object); await sut.UpdateAsync(command); var updatedForum = await dbContext.Forums.FirstOrDefaultAsync(x => x.Id == command.Id); var @event = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == command.Id); updateValidator.Verify(x => x.ValidateAsync(command, new CancellationToken())); Assert.AreEqual(command.Name, updatedForum.Name); Assert.NotNull(@event); } }
public async Task Should_move_category_up_and_add_events() { var options = Shared.CreateContextOptions(); var siteId = Guid.NewGuid(); var category1 = new Category(siteId, "Category 1", 1, Guid.NewGuid()); var category2 = new Category(siteId, "Category 2", 2, Guid.NewGuid()); using (var dbContext = new AtlesDbContext(options)) { dbContext.Categories.Add(category1); dbContext.Categories.Add(category2); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var command = new MoveCategory { Id = category2.Id, Direction = Direction.Up, SiteId = siteId }; var cacheManager = new Mock <ICacheManager>(); var createValidator = new Mock <IValidator <CreateCategory> >(); var updateValidator = new Mock <IValidator <UpdateCategory> >(); var sut = new CategoryService(dbContext, cacheManager.Object, createValidator.Object, updateValidator.Object); await sut.MoveAsync(command); var updatedCategory1 = await dbContext.Categories.FirstOrDefaultAsync(x => x.Id == category1.Id); var updatedCategory2 = await dbContext.Categories.FirstOrDefaultAsync(x => x.Id == category2.Id); var event1 = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == category1.Id); var event2 = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == category2.Id); Assert.AreEqual(category2.SortOrder, updatedCategory1.SortOrder); Assert.AreEqual(category1.SortOrder, updatedCategory2.SortOrder); Assert.NotNull(event1); Assert.NotNull(event2); } }
public async Task Should_lock_topic_and_add_event() { var options = Shared.CreateContextOptions(); var siteId = Guid.NewGuid(); var categoryId = Guid.NewGuid(); var forumId = Guid.NewGuid(); var category = new Category(categoryId, siteId, "Category", 1, Guid.NewGuid()); var forum = new Forum(forumId, category.Id, "Forum", "my-forum", "My Forum", 1); var topic = Post.CreateTopic(forumId, Guid.NewGuid(), "Title", "slug", "Content", PostStatusType.Published); using (var dbContext = new AtlesDbContext(options)) { dbContext.Categories.Add(category); dbContext.Forums.Add(forum); dbContext.Posts.Add(topic); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var command = Fixture.Build <LockTopic>() .With(x => x.Id, topic.Id) .With(x => x.ForumId, forumId) .With(x => x.SiteId, siteId) .Create(); var cacheManager = new Mock <ICacheManager>(); var createValidator = new Mock <IValidator <CreateTopic> >(); var updateValidator = new Mock <IValidator <UpdateTopic> >(); var sut = new TopicService(dbContext, cacheManager.Object, createValidator.Object, updateValidator.Object); await sut.LockAsync(command); var lockedTopic = await dbContext.Posts.FirstOrDefaultAsync(x => x.Id == command.Id); var @event = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == command.Id); Assert.AreEqual(command.Locked, lockedTopic.Locked); Assert.NotNull(@event); } }
public async Task Should_update_category_and_add_event() { var options = Shared.CreateContextOptions(); var permissionSet = new PermissionSet(Guid.NewGuid(), Guid.NewGuid(), "Default", new List <PermissionCommand>()); var category = new Category(Guid.NewGuid(), permissionSet.SiteId, "Category", 1, permissionSet.Id); using (var dbContext = new AtlesDbContext(options)) { dbContext.PermissionSets.Add(permissionSet); dbContext.Categories.Add(category); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var command = Fixture.Build <UpdateCategory>() .With(x => x.Id, category.Id) .With(x => x.SiteId, category.SiteId) .Create(); var cacheManager = new Mock <ICacheManager>(); var createValidator = new Mock <IValidator <CreateCategory> >(); var updateValidator = new Mock <IValidator <UpdateCategory> >(); updateValidator .Setup(x => x.ValidateAsync(command, new CancellationToken())) .ReturnsAsync(new ValidationResult()); var sut = new CategoryService(dbContext, cacheManager.Object, createValidator.Object, updateValidator.Object); await sut.UpdateAsync(command); var updatedCategory = await dbContext.Categories.FirstOrDefaultAsync(x => x.Id == command.Id); var @event = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == command.Id); updateValidator.Verify(x => x.ValidateAsync(command, new CancellationToken())); Assert.AreEqual(command.Name, updatedCategory.Name); Assert.NotNull(@event); } }
public async Task Should_return_false_when_display_name_is_not_unique() { var options = Shared.CreateContextOptions(); var displayName = "Display Name"; using (var dbContext = new AtlesDbContext(options)) { var user = new User(Guid.NewGuid().ToString(), "*****@*****.**", displayName); dbContext.Users.Add(user); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var sut = new UserRules(dbContext); var actual = await sut.IsDisplayNameUniqueAsync(displayName); Assert.IsFalse(actual); } }
public async Task UpdateAsync(UpdateSite command) { await _updateValidator.ValidateCommandAsync(command); var site = await _dbContext.Sites .FirstOrDefaultAsync(x => x.Id == command.SiteId); if (site == null) { throw new DataException($"Site with Id {command.SiteId} not found."); } site.UpdateDetails(command.Title, command.Theme, command.Css, command.Language, command.Privacy, command.Terms, command.HeadScript); _dbContext.Events.Add(new Event(site.Id, command.UserId, EventType.Updated, typeof(Site), site.Id, new { site.Title, site.PublicTheme, site.PublicCss, site.Language, site.Privacy, site.Terms, site.HeadScript })); await _dbContext.SaveChangesAsync(); _cacheManager.Remove(CacheKeys.CurrentSite(site.Name)); }
public async Task Should_return_false_when_name_is_not_unique() { var options = Shared.CreateContextOptions(); var siteId = Guid.NewGuid(); var categoryName = "My Category"; using (var dbContext = new AtlesDbContext(options)) { var category = new Category(siteId, categoryName, 1, Guid.NewGuid()); dbContext.Categories.Add(category); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var sut = new CategoryRules(dbContext); var actual = await sut.IsNameUniqueAsync(siteId, categoryName); Assert.IsFalse(actual); } }
public async Task Should_return_true_when_permission_set_is_valid() { var options = Shared.CreateContextOptions(); var site = new Site(Guid.NewGuid(), "Name", "Title"); var permissionSet = new PermissionSet(Guid.NewGuid(), site.Id, "Permission Set", new List <PermissionCommand>()); using (var dbContext = new AtlesDbContext(options)) { dbContext.Sites.Add(site); dbContext.PermissionSets.Add(permissionSet); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(Shared.CreateContextOptions())) { var sut = new PermissionSetRules(dbContext); var actual = await sut.IsValidAsync(site.Id, permissionSet.Id); Assert.IsTrue(actual); } }
public async Task Should_return_true_when_forum_is_valid() { var options = Shared.CreateContextOptions(); var category = new Category(Guid.NewGuid(), Guid.NewGuid(), "Category", 1, Guid.NewGuid()); var forum = new Forum(Guid.NewGuid(), category.Id, "Forum", "my-forum", "My Forum", 1); using (var dbContext = new AtlesDbContext(options)) { dbContext.Categories.Add(category); dbContext.Forums.Add(forum); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var sut = new ForumRules(dbContext); var actual = await sut.IsValidAsync(forum.Category.SiteId, forum.Id); Assert.IsTrue(actual); } }
public async Task Should_delete_permission_set_and_reorder_other_categories_and_add_event() { var options = Shared.CreateContextOptions(); var siteId = Guid.NewGuid(); var permissionSet = new PermissionSet(siteId, "Permission Set", new List <PermissionCommand>()); using (var dbContext = new AtlesDbContext(options)) { dbContext.PermissionSets.Add(permissionSet); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var command = Fixture.Build <DeletePermissionSet>() .With(x => x.Id, permissionSet.Id) .With(x => x.SiteId, siteId) .Create(); var cacheManager = new Mock <ICacheManager>(); var createValidator = new Mock <IValidator <CreatePermissionSet> >(); var updateValidator = new Mock <IValidator <UpdatePermissionSet> >(); var sut = new PermissionSetService(dbContext, cacheManager.Object, createValidator.Object, updateValidator.Object); await sut.DeleteAsync(command); var permissionSetDeleted = await dbContext.PermissionSets.FirstOrDefaultAsync(x => x.Id == command.Id); var permissionSetEvent = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == command.Id); Assert.AreEqual(PermissionSetStatusType.Deleted, permissionSetDeleted.Status); Assert.NotNull(permissionSetEvent); } }
public async Task Should_update_user_and_add_event() { var options = Shared.CreateContextOptions(); var user = new User(Guid.NewGuid(), Guid.NewGuid().ToString(), "*****@*****.**", "Display Name"); using (var dbContext = new AtlesDbContext(options)) { dbContext.Users.Add(user); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var command = Fixture.Build <UpdateUser>() .With(x => x.Id, user.Id) .Create(); var createValidator = new Mock <IValidator <CreateUser> >(); var updateValidator = new Mock <IValidator <UpdateUser> >(); updateValidator .Setup(x => x.ValidateAsync(command, new CancellationToken())) .ReturnsAsync(new ValidationResult()); var sut = new UserService(dbContext, createValidator.Object, updateValidator.Object); await sut.UpdateAsync(command); var updatedUser = await dbContext.Users.FirstOrDefaultAsync(x => x.Id == command.Id); var @event = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == command.Id); updateValidator.Verify(x => x.ValidateAsync(command, new CancellationToken())); Assert.AreEqual(command.DisplayName, updatedUser.DisplayName); Assert.NotNull(@event); } }
public async Task Should_update_site_and_add_event() { var options = Shared.CreateContextOptions(); var site = new Site(Guid.NewGuid(), "Name", "Title"); using (var dbContext = new AtlesDbContext(options)) { dbContext.Sites.Add(site); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var command = Fixture.Build <UpdateSite>() .With(x => x.SiteId, site.Id) .Create(); var cacheManager = new Mock <ICacheManager>(); var updateValidator = new Mock <IValidator <UpdateSite> >(); updateValidator .Setup(x => x.ValidateAsync(command, new CancellationToken())) .ReturnsAsync(new ValidationResult()); var sut = new SiteService(dbContext, cacheManager.Object, updateValidator.Object); await sut.UpdateAsync(command); var updatedSite = await dbContext.Sites.FirstOrDefaultAsync(x => x.Id == command.SiteId); var @event = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == command.SiteId); updateValidator.Verify(x => x.ValidateAsync(command, new CancellationToken())); Assert.AreEqual(command.Title, updatedSite.Title); Assert.NotNull(@event); } }
public async Task Should_return_false_when_display_name_is_not_unique_for_existing_member() { var options = Shared.CreateContextOptions(); var userId = Guid.NewGuid(); using (var dbContext = new AtlesDbContext(options)) { var user1 = new User(Guid.NewGuid().ToString(), "*****@*****.**", "User 1"); var user2 = new User(userId, Guid.NewGuid().ToString(), "*****@*****.**", "User 2"); dbContext.Users.Add(user1); dbContext.Users.Add(user2); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var sut = new UserRules(dbContext); var actual = await sut.IsDisplayNameUniqueAsync("User 1", userId); Assert.IsFalse(actual); } }
public async Task Should_return_false_when_name_is_not_unique_for_existing_category() { var options = Shared.CreateContextOptions(); var siteId = Guid.NewGuid(); var categoryId = Guid.NewGuid(); using (var dbContext = new AtlesDbContext(options)) { var category1 = new Category(siteId, "Category 1", 1, Guid.NewGuid()); var category2 = new Category(categoryId, siteId, "Category 2", 2, Guid.NewGuid()); dbContext.Categories.Add(category1); dbContext.Categories.Add(category2); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var sut = new CategoryRules(dbContext); var actual = await sut.IsNameUniqueAsync(siteId, "Category 1", categoryId); Assert.IsFalse(actual); } }
public async Task Should_return_true_when_topic_is_valid() { var options = Shared.CreateContextOptions(); var category = new Category(Guid.NewGuid(), Guid.NewGuid(), "Category", 1, Guid.NewGuid()); var forum = new Forum(Guid.NewGuid(), category.Id, "Forum", "my-forum", "My Forum", 1, Guid.NewGuid()); var topic = Post.CreateTopic(Guid.NewGuid(), forum.Id, Guid.NewGuid(), "Title", "slug", "Content", PostStatusType.Published); using (var dbContext = new AtlesDbContext(options)) { dbContext.Categories.Add(category); dbContext.Forums.Add(forum); dbContext.Posts.Add(topic); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var sut = new TopicRules(dbContext); var actual = await sut.IsValidAsync(category.SiteId, forum.Id, topic.Id); Assert.IsTrue(actual); } }
public async Task Should_suspend_user_and_add_event() { var options = Shared.CreateContextOptions(); var userId = Guid.NewGuid(); var user = new User(userId, Guid.NewGuid().ToString(), "*****@*****.**", "Display Name"); using (var dbContext = new AtlesDbContext(options)) { dbContext.Users.Add(user); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var command = Fixture.Build <SuspendUser>() .With(x => x.Id, user.Id) .Create(); var createValidator = new Mock <IValidator <CreateUser> >(); var updateValidator = new Mock <IValidator <UpdateUser> >(); var sut = new UserService(dbContext, createValidator.Object, updateValidator.Object); await sut.SuspendAsync(command); var userSuspended = await dbContext.Users.FirstOrDefaultAsync(x => x.Id == user.Id); var userEvent = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == user.Id); Assert.AreEqual(UserStatusType.Suspended, userSuspended.Status); Assert.NotNull(userEvent); } }
public async Task CreateAsync(CreateUser command) { await _createValidator.ValidateCommandAsync(command); var displayName = await GenerateDisplayNameAsync(); var user = new User(command.Id, command.IdentityUserId, command.Email, displayName); if (command.Confirm) { user.Confirm(); } _dbContext.Users.Add(user); var userIdForEvent = command.UserId == Guid.Empty ? user.Id : command.UserId; _dbContext.Events.Add(new Event(command.SiteId, userIdForEvent, EventType.Created, typeof(User), command.Id, new { UserId = command.IdentityUserId, command.Email, DisplayName = displayName, user.Status })); await _dbContext.SaveChangesAsync(); }
public async Task Should_delete_category_and_reorder_other_categories_and_add_event() { var options = Shared.CreateContextOptions(); var siteId = Guid.NewGuid(); var category1 = new Category(siteId, "Category 1", 1, Guid.NewGuid()); var category2 = new Category(siteId, "Category 2", 2, Guid.NewGuid()); var category3 = new Category(siteId, "Category 3", 3, Guid.NewGuid()); var category4 = new Category(siteId, "Category 4", 4, Guid.NewGuid()); var forum1 = new Forum(category2.Id, "Forum 1", "my-forum-1", "My Forum", 1); var forum2 = new Forum(category2.Id, "Forum 2", "my-forum-2", "My Forum", 2); using (var dbContext = new AtlesDbContext(options)) { dbContext.Categories.Add(category1); dbContext.Categories.Add(category2); dbContext.Categories.Add(category3); dbContext.Categories.Add(category4); dbContext.Forums.Add(forum1); dbContext.Forums.Add(forum2); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var command = Fixture.Build <DeleteCategory>() .With(x => x.Id, category2.Id) .With(x => x.SiteId, siteId) .Create(); var cacheManager = new Mock <ICacheManager>(); var createValidator = new Mock <IValidator <CreateCategory> >(); var updateValidator = new Mock <IValidator <UpdateCategory> >(); var sut = new CategoryService(dbContext, cacheManager.Object, createValidator.Object, updateValidator.Object); await sut.DeleteAsync(command); var category1Reordered = await dbContext.Categories.FirstOrDefaultAsync(x => x.Id == category1.Id); var category2Deleted = await dbContext.Categories.FirstOrDefaultAsync(x => x.Id == command.Id); var category3Reordered = await dbContext.Categories.FirstOrDefaultAsync(x => x.Id == category3.Id); var category4Reordered = await dbContext.Categories.FirstOrDefaultAsync(x => x.Id == category4.Id); var forum1Deleted = await dbContext.Forums.FirstOrDefaultAsync(x => x.Id == forum1.Id); var forum2Deleted = await dbContext.Forums.FirstOrDefaultAsync(x => x.Id == forum2.Id); var category1Event = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == category1.Id); var category2Event = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == command.Id); var category3Event = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == category3.Id); var category4Event = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == category4.Id); var forum1Event = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == forum1.Id); var forum2Event = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == forum2.Id); Assert.AreEqual(category1.SortOrder, category1Reordered.SortOrder); Assert.AreEqual(CategoryStatusType.Deleted, category2Deleted.Status); Assert.AreEqual(category2.SortOrder, category3Reordered.SortOrder); Assert.AreEqual(category3.SortOrder, category4Reordered.SortOrder); Assert.AreEqual(ForumStatusType.Deleted, forum1Deleted.Status); Assert.AreEqual(ForumStatusType.Deleted, forum2Deleted.Status); Assert.NotNull(category1Event); Assert.NotNull(category2Event); Assert.NotNull(category3Event); Assert.NotNull(category4Event); Assert.NotNull(forum1Event); Assert.NotNull(forum2Event); } }
public async Task Should_delete_topic_and_add_event() { var options = Shared.CreateContextOptions(); var siteId = Guid.NewGuid(); var categoryId = Guid.NewGuid(); var forumId = Guid.NewGuid(); var userId = Guid.NewGuid(); var category = new Category(categoryId, siteId, "Category", 1, Guid.NewGuid()); var forum = new Forum(forumId, category.Id, "Forum", "my-forum", "My Forum", 1); var topic = Post.CreateTopic(forumId, userId, "Title", "slug", "Content", PostStatusType.Published); var user = new User(userId, Guid.NewGuid().ToString(), "Email", "Display Name"); category.IncreaseTopicsCount(); forum.IncreaseTopicsCount(); user.IncreaseTopicsCount(); using (var dbContext = new AtlesDbContext(options)) { dbContext.Categories.Add(category); dbContext.Forums.Add(forum); dbContext.Posts.Add(topic); dbContext.Users.Add(user); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var command = Fixture.Build <DeleteTopic>() .With(x => x.Id, topic.Id) .With(x => x.ForumId, forumId) .With(x => x.SiteId, siteId) .Create(); var cacheManager = new Mock <ICacheManager>(); var createValidator = new Mock <IValidator <CreateTopic> >(); var updateValidator = new Mock <IValidator <UpdateTopic> >(); var sut = new TopicService(dbContext, cacheManager.Object, createValidator.Object, updateValidator.Object); await sut.DeleteAsync(command); var topicDeleted = await dbContext.Posts.FirstOrDefaultAsync(x => x.Id == topic.Id); var topicEvent = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == topic.Id); var updatedCategory = await dbContext.Categories.FirstOrDefaultAsync(x => x.Id == category.Id); var updatedForum = await dbContext.Forums.FirstOrDefaultAsync(x => x.Id == forum.Id); var updatedUser = await dbContext.Users.FirstOrDefaultAsync(x => x.Id == user.Id); Assert.AreEqual(PostStatusType.Deleted, topicDeleted.Status); Assert.NotNull(topicEvent); Assert.AreEqual(0, updatedCategory.TopicsCount); Assert.AreEqual(0, updatedForum.TopicsCount); Assert.AreEqual(0, updatedUser.TopicsCount); } }
public async Task Should_create_new_topic_and_add_event() { var options = Shared.CreateContextOptions(); var categoryId = Guid.NewGuid(); var forumId = Guid.NewGuid(); var userId = Guid.NewGuid(); var category = new Category(categoryId, Guid.NewGuid(), "Category", 1, Guid.NewGuid()); var forum = new Forum(forumId, category.Id, "Forum", "my-forum", "My Forum", 1); var user = new User(userId, Guid.NewGuid().ToString(), "Email", "Display Name"); using (var dbContext = new AtlesDbContext(options)) { dbContext.Categories.Add(category); dbContext.Forums.Add(forum); dbContext.Users.Add(user); await dbContext.SaveChangesAsync(); } using (var dbContext = new AtlesDbContext(options)) { var command = Fixture.Build <CreateTopic>() .With(x => x.ForumId, forum.Id) .With(x => x.UserId, userId) .Create(); var cacheManager = new Mock <ICacheManager>(); var createValidator = new Mock <IValidator <CreateTopic> >(); createValidator .Setup(x => x.ValidateAsync(command, new CancellationToken())) .ReturnsAsync(new ValidationResult()); var updateValidator = new Mock <IValidator <UpdateTopic> >(); var sut = new TopicService(dbContext, cacheManager.Object, createValidator.Object, updateValidator.Object); await sut.CreateAsync(command); var topic = await dbContext.Posts.FirstOrDefaultAsync(x => x.Id == command.Id); var @event = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == command.Id); var updatedCategory = await dbContext.Categories.FirstOrDefaultAsync(x => x.Id == category.Id); var updatedForum = await dbContext.Forums.FirstOrDefaultAsync(x => x.Id == forum.Id); var updatedUser = await dbContext.Users.FirstOrDefaultAsync(x => x.Id == userId); createValidator.Verify(x => x.ValidateAsync(command, new CancellationToken())); Assert.NotNull(topic); Assert.NotNull(@event); Assert.AreEqual(category.TopicsCount + 1, updatedCategory.TopicsCount); Assert.AreEqual(topic.Id, updatedForum.LastPostId); Assert.AreEqual(forum.TopicsCount + 1, updatedForum.TopicsCount); Assert.AreEqual(forum.TopicsCount + 1, updatedForum.TopicsCount); Assert.AreEqual(user.TopicsCount + 1, updatedUser.TopicsCount); } }