public void GetStoryById_Throw_Exception_with_NonExistant_Id()
        {
            //arrange

            var story = new FanFictionStory
            {
                Title     = "One",
                Id        = 1,
                CreatedOn = DateTime.Now,
                Summary   = null,
                ImageUrl  = GlobalConstants.DefaultNoImage,
                Type      = new StoryType
                {
                    Id   = 1,
                    Name = "Fantasy"
                },
                AuthorId = "1111"
            };

            this.Context.FictionStories.Add(story);
            this.Context.SaveChanges();

            //act
            var    storyService = GetService();
            Action act          = () => storyService.GetStoryById(2);

            act.Should().Throw <ArgumentException>().WithMessage(GlobalConstants.MissingStory);
        }
        public void GetStoryById_Should_Return_StoryDetailsModel_With_Correct_Id()
        {
            //arrange

            var story = new FanFictionStory
            {
                Title     = "One",
                Id        = 1,
                CreatedOn = DateTime.Now,
                Summary   = null,
                ImageUrl  = GlobalConstants.DefaultNoImage,
                Type      = new StoryType
                {
                    Id   = 1,
                    Name = "Fantasy"
                },
                AuthorId = "1111"
            };

            this.Context.FictionStories.Add(story);
            this.Context.SaveChanges();

            //act
            var storyService = GetService();

            var result = storyService.GetStoryById(1);

            result.Should().BeOfType <StoryDetailsOutputModel>().And.Should().NotBeNull();
        }
Example #3
0
        public void StoryExists_Should_Return_True()
        {
            //arrange
            var story = new FanFictionStory
            {
                Title     = "One",
                Id        = 1,
                CreatedOn = DateTime.Now,
                Summary   = null,
                ImageUrl  = GlobalConstants.DefaultNoImage,
                Type      = new StoryType
                {
                    Id   = 1,
                    Name = "Fantasy"
                },
                AuthorId = "1111",
            };

            this.Context.FictionStories.Add(story);
            this.Context.SaveChanges();

            //act
            int storyId = story.Id;
            var exists  = this.Context.FictionStories.Any(x => x.Id == storyId);

            //assert

            exists.Should().BeTrue();
        }
        public void EditChapter_Should_Edit_Chapter_Content_Or_Title()
        {
            //arrange

            var someUser = new FanFictionUser
            {
                Id       = "AnotherUserId",
                Nickname = "ThirdUser",
                UserName = "******",
            };

            var story = new FanFictionStory
            {
                Id       = 1,
                Author   = someUser,
                Summary  = "some summary",
                Title    = "Story To test",
                AuthorId = "AnotherUserId",
            };

            var chapter = new Chapter
            {
                Id                = 1,
                Content           = "SomeContent",
                AuthorId          = someUser.Id,
                FanFictionUser    = someUser,
                FanFictionStory   = story,
                FanFictionStoryId = story.Id,
                Title             = "Test Chapter",
                CreatedOn         = DateTime.UtcNow
            };

            var record = new ChapterEditModel
            {
                Author    = someUser.UserName,
                Content   = "Some new content",
                CreatedOn = chapter.CreatedOn,
                Id        = chapter.Id,
                StoryId   = story.Id,
                Title     = "New Title"
            };

            userManager.CreateAsync(someUser).GetAwaiter();
            this.Context.Chapters.Add(chapter);
            this.Context.FictionStories.Add(story);
            this.Context.SaveChanges();

            //act
            this.chapterService.EditChapter(record);

            //assert
            var result = this.Context.Chapters.ProjectTo <ChapterEditModel>().FirstOrDefault(x => x.Id == chapter.Id);

            result.Should().NotBeNull().And.Subject.Should().BeEquivalentTo(record);
        }
        public void DeleteComment_Should_Delete_Comment()
        {
            //arrange

            var user = new FanFictionUser
            {
                Id       = "user",
                UserName = "******"
            };

            var genre = new StoryType
            {
                Id   = 1,
                Name = "Fantasy"
            };

            var story = new FanFictionStory
            {
                Title     = "One",
                Id        = 1,
                CreatedOn = DateTime.Now,
                Summary   = null,
                ImageUrl  = GlobalConstants.DefaultNoImage,
                Type      = genre,
                AuthorId  = "user"
            };

            var comment = new Comment
            {
                StoryId         = 1,
                FanFictionUser  = user,
                FanFictionStory = story,
                CommentedOn     = DateTime.Now.Date,
                Message         = "SomeComment",
                UserId          = user.Id,
                Id = 1
            };

            var userManager = (UserManager <FanFictionUser>) this.Provider.GetService(typeof(UserManager <FanFictionUser>));

            userManager.CreateAsync(user).GetAwaiter();
            this.Context.Comments.Add(comment);
            this.Context.StoryTypes.Add(genre);
            this.Context.FictionStories.Add(story);
            this.Context.SaveChanges();

            //act

            this.CommentService.DeleteComment(1);
            var result = this.Context.Comments.ToList();

            //assert

            result.Should().BeEmpty();
        }
        public void GetChapterToEditById_Should_Return_The_ChapterEditModel_By_Id()
        {
            //arrange

            var someUser = new FanFictionUser
            {
                Id       = "AnotherUserId",
                Nickname = "ThirdUser",
                UserName = "******",
            };

            var story = new FanFictionStory
            {
                Id       = 1,
                Author   = someUser,
                Summary  = "some summary",
                Title    = "Story To test",
                AuthorId = "someUserId",
            };

            var chapter = new Chapter
            {
                Id                = 1,
                Content           = "SomeContent",
                AuthorId          = someUser.Id,
                FanFictionUser    = someUser,
                FanFictionStory   = story,
                FanFictionStoryId = story.Id,
                Title             = "Test Chapter",
                CreatedOn         = DateTime.UtcNow
            };

            var record = new ChapterEditModel
            {
                Author    = someUser.UserName,
                Content   = chapter.Content,
                CreatedOn = chapter.CreatedOn,
                Id        = chapter.Id,
                StoryId   = story.Id,
                Title     = chapter.Title
            };

            userManager.CreateAsync(someUser).GetAwaiter();
            this.Context.Chapters.Add(chapter);
            this.Context.FictionStories.Add(story);
            this.Context.SaveChanges();

            //act
            int chapterId = chapter.Id;
            var result    = this.chapterService.GetChapterToEditById(chapterId);

            //assert
            result.Should().NotBeNull().And.Subject.Should().BeEquivalentTo(record);
        }
        public void DeleteStory_Should_Be_Successful_For_Admin()
        {
            //arrange
            var user = new FanFictionUser
            {
                Id       = "UserId",
                Nickname = "TestStory",
                UserName = "******",
            };
            var author = new FanFictionUser
            {
                Id       = "AuthorId",
                Nickname = "StoryAuthor",
                UserName = "******",
            };

            var story = new FanFictionStory
            {
                Id       = 1,
                Author   = null,
                Summary  = "some summary",
                Title    = "Story To test",
                AuthorId = author.Id,
            };

            var role = new IdentityRole {
                Name = "admin"
            };

            var usermanager = this.Provider.GetRequiredService <UserManager <FanFictionUser> >();
            var roleManager = this.Provider.GetRequiredService <RoleManager <IdentityRole> >();

            usermanager.CreateAsync(author).GetAwaiter();
            usermanager.CreateAsync(user).GetAwaiter();
            roleManager.CreateAsync(role).GetAwaiter();

            usermanager.AddToRoleAsync(user, "admin").GetAwaiter();

            this.Context.FictionStories.Add(story);
            this.Context.SaveChanges();

            //act
            var storyService = GetService();

            storyService.DeleteStory(story.Id, user.UserName).GetAwaiter();

            //assert

            var result = this.Context.FictionStories.FirstOrDefault();

            result.Should().BeNull();
        }
        public void Add_Rating_Should_Throw_Exception_If_AlreadyRated_Is_True()
        {
            //arrange
            var story = new FanFictionStory
            {
                Id      = 1,
                Author  = null,
                Summary = "some summary",
                Title   = "Story To test"
            };

            var user = new FanFictionUser
            {
                Id       = "UserId",
                Nickname = "TestStory",
                UserName = "******",
            };

            var rating = new StoryRating
            {
                Rating         = 8,
                UserId         = user.Id,
                Id             = 1,
                FanFictionUser = user
            };

            var storyRating = new FanFictionRating
            {
                FanFictionId    = story.Id,
                RatingId        = rating.Id,
                FanFictionStory = story,
                StoryRating     = rating
            };

            var usermanager = this.Provider.GetRequiredService <UserManager <FanFictionUser> >();

            usermanager.CreateAsync(user).GetAwaiter();

            this.Context.FictionStories.Add(story);
            this.Context.StoryRatings.Add(rating);
            this.Context.FictionRatings.Add(storyRating);
            this.Context.SaveChanges();

            //act

            var storyService = GetService();

            Action act = () => storyService.AddRating(story.Id, 1, user.UserName);

            //assert
            act.Should().Throw <InvalidOperationException>().WithMessage(GlobalConstants.AlreadyRated);
        }
        public async Task Follow_Should_Create_UserStory_Entity_In_Db()
        {
            //arrange

            var story = new FanFictionStory
            {
                Id      = 1,
                Author  = null,
                Summary = "some summary",
                Title   = "Story To test"
            };

            var user = new FanFictionUser
            {
                Id       = "UserId",
                Nickname = "TestStory",
                UserName = "******",
            };

            this.Context.FictionStories.Add(story);

            var usermanager = this.Provider.GetRequiredService <UserManager <FanFictionUser> >();
            await usermanager.CreateAsync(user);

            this.Context.SaveChanges();

            //act
            var storyService = GetService();

            var storyId  = story.Id;
            var username = user.UserName;
            var userId   = user.Id;
            await storyService.Follow(username, userId, storyId);

            //assert

            var result = this.Context.UsersStories.FirstOrDefault();

            var userStory = new UserStory
            {
                FanfictionUserId  = user.Id,
                FanFictionStoryId = story.Id
            };

            result.Should().BeOfType <UserStory>().And.Subject.Should().Equals(userStory);
        }
        public async Task UnFollow_Should_Remove_UserStoryLink_From_Db()
        {
            var story = new FanFictionStory
            {
                Id      = 1,
                Author  = null,
                Summary = "some summary",
                Title   = "Story To test"
            };

            var user = new FanFictionUser
            {
                Id       = "UserId",
                Nickname = "TestStory",
                UserName = "******",
            };

            var userStory = new UserStory
            {
                FanfictionUserId  = user.Id,
                FanFictionStoryId = story.Id
            };

            var usermanager = this.Provider.GetRequiredService <UserManager <FanFictionUser> >();
            await usermanager.CreateAsync(user);

            this.Context.FictionStories.Add(story);
            this.Context.UsersStories.Add(userStory);
            this.Context.SaveChanges();
            this.Context.Entry(userStory).State = EntityState.Detached;

            //act
            var storyService = GetService();

            var storyId = story.Id;
            var userId  = user.Id;

            storyService.UnFollow(userId, storyId).GetAwaiter().GetResult();

            //assert

            var result = this.Context.UsersStories.FirstOrDefault();

            result.Should().BeNull();
        }
        public async Task Add_Rating_Should_Add_Rating_To_A_Story()
        {
            //arrange

            var story = new FanFictionStory
            {
                Id      = 1,
                Author  = null,
                Summary = "some summary",
                Title   = "Story To test"
            };

            var user = new FanFictionUser
            {
                Id       = "UserId",
                Nickname = "TestStory",
                UserName = "******",
            };

            var usermanager = this.Provider.GetRequiredService <UserManager <FanFictionUser> >();
            await usermanager.CreateAsync(user);

            this.Context.FictionStories.Add(story);
            this.Context.SaveChanges();
            //act

            var storyService = GetService();

            storyService.AddRating(story.Id, 10, user.UserName);

            //assert

            var storyRating = new FanFictionRating
            {
                FanFictionId = 1,
                RatingId     = 1
            };

            var result = this.Context.FictionRatings.FirstOrDefault();

            result.Should().NotBeNull().And.
            Subject.Should()
            .BeOfType <FanFictionRating>().And.Should()
            .BeEquivalentTo(storyRating, opt => opt.ExcludingMissingMembers());
        }
        public void DeleteStory_Should_Throw_Exception_With_Admin_And_Author_Bools_False()
        {
            //arrange
            var user = new FanFictionUser
            {
                Id       = "UserId",
                Nickname = "TestStory",
                UserName = "******",
            };

            var someUser = new FanFictionUser
            {
                Id       = "AnotherUserId",
                Nickname = "ThirdUser",
                UserName = "******",
            };

            var story = new FanFictionStory
            {
                Id       = 1,
                Author   = someUser,
                Summary  = "some summary",
                Title    = "Story To test",
                AuthorId = "someUserId",
            };

            var usermanager = this.Provider.GetRequiredService <UserManager <FanFictionUser> >();

            usermanager.CreateAsync(user).GetAwaiter();
            usermanager.CreateAsync(someUser).GetAwaiter();

            this.Context.FictionStories.Add(story);
            this.Context.SaveChanges();

            //act
            var         storyService = GetService();
            Func <Task> act          = async() => await storyService.DeleteStory(story.Id, user.UserName);

            //assert

            act.Should().Throw <OperationCanceledException>();
        }
        public async Task Follow_Should_Throw_OperationException_When_Try_To_Add_Duplicate_Entity()
        {
            //arrange

            var story = new FanFictionStory
            {
                Id      = 1,
                Author  = null,
                Summary = "some summary",
                Title   = "Story To test"
            };

            var user = new FanFictionUser
            {
                Id       = "UserId",
                Nickname = "TestStory",
                UserName = "******",
            };

            this.Context.FictionStories.Add(story);

            var usermanager = this.Provider.GetRequiredService <UserManager <FanFictionUser> >();
            await usermanager.CreateAsync(user);

            this.Context.SaveChanges();

            //act
            var storyService = GetService();

            var storyId  = story.Id;
            var username = user.UserName;
            var userId   = user.Id;
            await storyService.Follow(username, userId, storyId);

            //assert

            Action act = () => storyService.Follow(username, userId, storyId).GetAwaiter().GetResult();

            string message = string.Join(GlobalConstants.UserFollowAlready, user.UserName);

            act.Should().Throw <InvalidOperationException>().WithMessage(message);
        }
        public async Task UnFollow_Should_Throw_Exception_For_Missing_UserStory_Entity()
        {
            var story = new FanFictionStory
            {
                Id      = 1,
                Author  = null,
                Summary = "some summary",
                Title   = "Story To test"
            };

            var user = new FanFictionUser
            {
                Id       = "UserId",
                Nickname = "TestStory",
                UserName = "******",
            };

            var usermanager = this.Provider.GetRequiredService <UserManager <FanFictionUser> >();
            await usermanager.CreateAsync(user);

            this.Context.FictionStories.Add(story);

            this.Context.SaveChanges();

            //act
            var storyService = GetService();

            var storyId = story.Id;
            var userId  = user.Id;

            Func <Task> act = async() => await storyService.UnFollow(userId, storyId);

            //assert

            await act.Should().ThrowAsync <ArgumentNullException>();
        }
        public void Stories_Should_Return_All_Stories_From_Db()
        {
            //arrange
            var user = new FanFictionUser
            {
                UserName = "******",
                Nickname = "userOne"
            };

            var author = new FanFictionUser
            {
                UserName = "******",
                Nickname = "AuthorNick"
            };

            this.UserManager.CreateAsync(user).GetAwaiter();
            this.UserManager.CreateAsync(author).GetAwaiter();
            var stories = new FanFictionStory[]
            {
                new FanFictionStory
                {
                    Title     = "One",
                    Id        = 1,
                    CreatedOn = DateTime.Now,
                    Summary   = null,
                    ImageUrl  = GlobalConstants.DefaultNoImage,
                    Type      = new StoryType
                    {
                        Id   = 1,
                        Name = "Fantasy"
                    },
                    AuthorId = author.Id
                },
                new FanFictionStory
                {
                    Title     = "Two",
                    Id        = 2,
                    CreatedOn = DateTime.Now.AddMonths(1),
                    Summary   = null,
                    ImageUrl  = GlobalConstants.DefaultNoImage,
                    Type      = new StoryType
                    {
                        Id   = 2,
                        Name = "Horror"
                    },
                    AuthorId = author.Id
                },
                new FanFictionStory
                {
                    Title     = "three",
                    Id        = 3,
                    CreatedOn = DateTime.Now.AddDays(2),
                    Summary   = null,
                    ImageUrl  = GlobalConstants.DefaultNoImage,
                    Type      = new StoryType
                    {
                        Id   = 3,
                        Name = "Science Fiction"
                    },
                    AuthorId = user.Id
                },
            };

            this.Context.FictionStories.AddRange(stories);
            this.Context.SaveChanges();
            //act

            var result = this.ApiService.Stories();

            //assert

            int count = stories.Length;

            result.Should().NotBeEmpty()
            .And.HaveCount(count)
            .And.AllBeOfType <ApiFanFictionStoryOutputModel>();
        }
Example #16
0
        public void AllUsers_Should_Return_Correct_Info_Per_User()
        {
            //arrange
            var author = new FanFictionUser
            {
                Id       = "AuthorId",
                Nickname = "StoryAuthor",
                UserName = "******",
            };

            var user = new FanFictionUser
            {
                Id       = "userId",
                Nickname = "AnotherUser",
                UserName = "******",
            };

            var story = new FanFictionStory
            {
                Id       = 1,
                Author   = null,
                Summary  = "some summary",
                Title    = "Story To test",
                AuthorId = author.Id,
            };

            var comments = new[]
            {
                new Comment
                {
                    FanFictionUser  = author,
                    Id              = 1,
                    UserId          = author.Id,
                    StoryId         = story.Id,
                    FanFictionStory = story,
                    Message         = "someMessage"
                },
                new Comment
                {
                    FanFictionUser  = author,
                    Id              = 2,
                    UserId          = author.Id,
                    StoryId         = story.Id,
                    FanFictionStory = story,
                    Message         = "someMessageTwo"
                },
            };

            var messages = new[]
            {
                new Message
                {
                    Id         = 1,
                    IsReaden   = false,
                    Receiver   = author,
                    ReceiverId = author.Id,
                    Sender     = user,
                    SenderId   = user.Id
                },
                new Message
                {
                    Id         = 2,
                    IsReaden   = true,
                    Receiver   = author,
                    ReceiverId = author.Id,
                    Sender     = user,
                    SenderId   = user.Id
                },
                new Message
                {
                    Id         = 3,
                    IsReaden   = false,
                    Receiver   = user,
                    ReceiverId = user.Id,
                    Sender     = author,
                    SenderId   = author.Id
                },
                new Message
                {
                    Id         = 4,
                    IsReaden   = true,
                    Receiver   = user,
                    ReceiverId = user.Id,
                    Sender     = author,
                    SenderId   = author.Id
                }
            };

            this.userManager.CreateAsync(user).GetAwaiter();
            this.userManager.CreateAsync(author).GetAwaiter();
            this.Context.FictionStories.Add(story);
            this.Context.Comments.AddRange(comments);
            this.Context.Messages.AddRange(messages);
            this.Context.SaveChanges();

            //act
            var result = this.adminService.AllUsers().GetAwaiter().GetResult();

            int indexToTakeFrom = 0;
            var userToCompare   = result?.ElementAt(indexToTakeFrom);
            //assert
            int totalAuthorStories = 1;

            var expectedAuthorOutput = new UserAdminViewOutputModel
            {
                Id           = author.Id,
                Comments     = comments.Length,
                Nickname     = author.Nickname,
                MessageCount = messages.Length,
                Username     = author.UserName,
                Stories      = totalAuthorStories,
                Role         = GlobalConstants.DefaultRole
            };

            userToCompare.Should().NotBeNull()
            .And.Subject.Should().BeEquivalentTo(expectedAuthorOutput);
        }
        public void DeleteChapter_ShouldThrowArgumentException()
        {
            var someUser = new FanFictionUser
            {
                Id       = "AnotherUserId",
                Nickname = "ThirdUser",
                UserName = "******",
            };

            var story = new FanFictionStory
            {
                Id       = 1,
                Author   = someUser,
                Summary  = "some summary",
                Title    = "Story To test",
                AuthorId = "AnotherUserId",
            };

            var storyTwo = new FanFictionStory
            {
                Id       = 2,
                Author   = someUser,
                Summary  = "some summary",
                Title    = "Story To test",
                AuthorId = "AnotherUserId",
            };

            var chapters = new[]
            {
                new Chapter
                {
                    Id                = 1,
                    FanFictionUser    = someUser,
                    FanFictionStory   = story,
                    AuthorId          = someUser.Id,
                    FanFictionStoryId = story.Id,
                    Content           = "SomeContent",
                    Title             = "Test Chapter",
                    CreatedOn         = DateTime.UtcNow
                },
                new Chapter
                {
                    Id                = 2,
                    FanFictionUser    = someUser,
                    FanFictionStory   = storyTwo,
                    AuthorId          = someUser.Id,
                    FanFictionStoryId = storyTwo.Id,
                    Content           = "Some new content",
                    Title             = "New chapter",
                    CreatedOn         = DateTime.Now,
                }
            };

            userManager.CreateAsync(someUser).GetAwaiter();

            this.Context.Chapters.AddRange(chapters);
            this.Context.FictionStories.Add(story);
            this.Context.FictionStories.Add(storyTwo);
            this.Context.SaveChanges();

            //act
            int    storyId   = story.Id;
            int    chapterId = chapters[1].Id;
            string username  = someUser.UserName;
            Action act       = () => this.chapterService.DeleteChapter(storyId, chapterId, username);

            //assert
            string message = string.Join(GlobalConstants.NotValidChapterStoryConnection, storyId, chapterId);

            act.Should().Throw <ArgumentException>().WithMessage(message);
        }
        public void DeleteChapter_ShouldThrowInvalidOperationException()
        {
            //arrange
            var randomUser = new FanFictionUser
            {
                Id       = "user",
                Nickname = "ThirdUser",
                UserName = "******",
            };
            var someUser = new FanFictionUser
            {
                Id       = "AnotherUserId",
                Nickname = "ThirdUser",
                UserName = "******",
            };

            var story = new FanFictionStory
            {
                Id       = 1,
                Author   = someUser,
                Summary  = "some summary",
                Title    = "Story To test",
                AuthorId = "AnotherUserId",
            };

            var chapters = new[]
            {
                new Chapter
                {
                    Id                = 1,
                    FanFictionUser    = someUser,
                    FanFictionStory   = story,
                    AuthorId          = someUser.Id,
                    FanFictionStoryId = story.Id,
                    Content           = "SomeContent",
                    Title             = "Test Chapter",
                    CreatedOn         = DateTime.UtcNow
                },
                new Chapter
                {
                    Id                = 2,
                    FanFictionUser    = someUser,
                    FanFictionStory   = story,
                    AuthorId          = someUser.Id,
                    FanFictionStoryId = story.Id,
                    Content           = "Some new content",
                    Title             = "New chapter",
                    CreatedOn         = DateTime.Now,
                }
            };

            var role = new IdentityRole
            {
                Name = GlobalConstants.Moderator
            };

            roleManager.CreateAsync(role).GetAwaiter();
            userManager.CreateAsync(someUser).GetAwaiter();
            userManager.CreateAsync(randomUser).GetAwaiter();

            this.Context.Chapters.AddRange(chapters);
            this.Context.FictionStories.Add(story);
            this.Context.SaveChanges();

            //act
            int    storyId   = story.Id;
            int    chapterId = chapters[0].Id;
            string username  = randomUser.UserName;

            Action act = () => this.chapterService.DeleteChapter(storyId, chapterId, username);

            //assert
            string message = GlobalConstants.UserHasNoRights + " " + GlobalConstants.NotAuthor;

            act.Should().Throw <InvalidOperationException>().WithMessage(message);
        }
        public void DeleteChaper_Author_Should_Delete_Chapter()
        {
            //arrange

            var someUser = new FanFictionUser
            {
                Id       = "AnotherUserId",
                Nickname = "ThirdUser",
                UserName = "******",
            };

            var story = new FanFictionStory
            {
                Id       = 1,
                Author   = someUser,
                Summary  = "some summary",
                Title    = "Story To test",
                AuthorId = "AnotherUserId",
            };

            var chapters = new[]
            {
                new Chapter
                {
                    Id                = 1,
                    FanFictionUser    = someUser,
                    FanFictionStory   = story,
                    AuthorId          = someUser.Id,
                    FanFictionStoryId = story.Id,
                    Content           = "SomeContent",
                    Title             = "Test Chapter",
                    CreatedOn         = DateTime.UtcNow
                },
                new Chapter
                {
                    Id                = 2,
                    FanFictionUser    = someUser,
                    FanFictionStory   = story,
                    AuthorId          = someUser.Id,
                    FanFictionStoryId = story.Id,
                    Content           = "Some new content",
                    Title             = "New chapter",
                    CreatedOn         = DateTime.Now,
                }
            };

            var role = new IdentityRole
            {
                Name = GlobalConstants.Moderator
            };

            roleManager.CreateAsync(role).GetAwaiter();
            userManager.CreateAsync(someUser).GetAwaiter();

            this.Context.Chapters.AddRange(chapters);
            this.Context.FictionStories.Add(story);
            this.Context.SaveChanges();

            //act
            int    storyId   = story.Id;
            int    chapterId = chapters[0].Id;
            string username  = someUser.UserName;

            this.chapterService.DeleteChapter(storyId, chapterId, username);

            //assert
            var result      = this.Context.Chapters.First();
            var chapterLeft = chapters[1];

            result.Should().NotBeNull()
            .And.Subject.As <Chapter>().Title.Should().BeEquivalentTo(chapterLeft.Title);
        }
Example #20
0
        public void AddNotification_Should_Send_To_All_Followers_Notification_For_New_Chapter()
        {
            //arrange
            var notificationUserOne = new FanFictionUser
            {
                Id       = "one",
                UserName = "******"
            };
            var notificationUserTwo = new FanFictionUser
            {
                Id       = "two",
                UserName = "******"
            };

            var story = new FanFictionStory
            {
                Title     = "One",
                Id        = 1,
                CreatedOn = DateTime.Now,
                Summary   = null,
                ImageUrl  = GlobalConstants.DefaultNoImage,
                Type      = new StoryType
                {
                    Id   = 1,
                    Name = "Fantasy"
                },
                AuthorId = "1111",
            };

            var userStoryOne = new UserStory
            {
                FanfictionUserId = notificationUserOne.Id,

                FanFictionStoryId = story.Id
            };
            var userStoryTwo = new UserStory
            {
                FanfictionUserId = notificationUserTwo.Id,

                FanFictionStoryId = story.Id
            };

            story.Followers.Add(userStoryOne);
            story.Followers.Add(userStoryTwo);

            userManager.CreateAsync(notificationUserOne).GetAwaiter().GetResult();
            userManager.CreateAsync(notificationUserTwo).GetAwaiter().GetResult();
            this.Context.FictionStories.Add(story);
            this.Context.UsersStories.Add(userStoryOne);
            this.Context.UsersStories.Add(userStoryTwo);
            this.Context.SaveChanges();

            //act
            int    storyId    = story.Id;
            string storyTitle = story.Title;

            this.notificationService.AddNotification(storyId, null, storyTitle);

            //assert
            var result = this.Context.UsersStories.ToList();

            result.Should().NotBeEmpty().And.HaveCount(2);
            result[0].FanFictionUser.Should().BeEquivalentTo(notificationUserOne);
            result[1].FanFictionUser.Should().BeEquivalentTo(notificationUserTwo);
        }
        public void AddChapter_Should_Add_New_Chapter_To_Story()
        {
            //arrange

            var someUser = new FanFictionUser
            {
                Id       = "AnotherUserId",
                Nickname = "ThirdUser",
                UserName = "******",
            };

            var story = new FanFictionStory
            {
                Id       = 1,
                Author   = someUser,
                Summary  = "some summary",
                Title    = "Story To test",
                AuthorId = "AnotherUserId",
            };

            // for testing purposes first chapter is with id =1 or Ef throws exception when adding the new chapter
            // it get's set to 1 and gets a conflict,also datetime.UtcNow and datetime.Now again for testing purposes
            // to ensure we always get the last chapter and that they are  ordered in the correct way.So the test to have a point

            var chapter = new Chapter
            {
                Id                = 2,
                Content           = "SomeContent",
                AuthorId          = someUser.Id,
                FanFictionUser    = someUser,
                FanFictionStory   = story,
                FanFictionStoryId = story.Id,
                Title             = "Test Chapter",
                CreatedOn         = DateTime.UtcNow
            };

            var newchapter = new ChapterInputModel
            {
                Author    = someUser.UserName,
                Content   = "Some new content",
                CreatedOn = DateTime.Now,
                StoryId   = story.Id,
                Title     = "New chapter"
            };

            userManager.CreateAsync(someUser).GetAwaiter();
            this.Context.Chapters.Add(chapter);
            this.Context.FictionStories.Add(story);
            this.Context.SaveChanges();

            //act
            this.chapterService.AddChapter(newchapter);

            //assert

            var result = this.Context.Chapters.OrderBy(x => x.CreatedOn).Last(x => x.FanFictionStoryId == story.Id);

            result.Should().NotBeNull()
            .And.Subject.As <Chapter>()
            .Title.Should().BeSameAs(newchapter.Title);
        }
        public void CurrentStories_with_Type_Null_Should_Return_OnlyOneType_Stories()
        {
            //arrange
            var stories = new FanFictionStory[]
            {
                new FanFictionStory
                {
                    Title     = "One",
                    Id        = 1,
                    CreatedOn = DateTime.Now,
                    Summary   = null,
                    ImageUrl  = GlobalConstants.DefaultNoImage,
                    Type      = new StoryType
                    {
                        Id   = 1,
                        Name = "Fantasy"
                    },
                    AuthorId = "1111"
                },
                new FanFictionStory
                {
                    Title     = "Two",
                    Id        = 2,
                    CreatedOn = DateTime.Now.AddMonths(1),
                    Summary   = null,
                    ImageUrl  = GlobalConstants.DefaultNoImage,
                    Type      = new StoryType
                    {
                        Id   = 2,
                        Name = "Horror"
                    },
                    AuthorId = "2222"
                },
                new FanFictionStory
                {
                    Title     = "three",
                    Id        = 3,
                    CreatedOn = DateTime.Now.AddDays(2),
                    Summary   = null,
                    ImageUrl  = GlobalConstants.DefaultNoImage,
                    Type      = new StoryType
                    {
                        Id   = 3,
                        Name = "Science Fiction"
                    },
                    AuthorId = "3333"
                },
            };

            this.Context.FictionStories.AddRange(stories);
            this.Context.SaveChanges();

            //act
            string storyType    = "Fantasy";
            var    storyService = GetService();
            var    result       = storyService.CurrentStories(storyType);

            //assert
            result.Should().NotBeEmpty()
            .And.ContainSingle(x => x.Type.Type == "Fantasy");
        }
        public void Authors_Should_Return_Only_Users_With_Stories()
        {
            //arrange

            var user = new FanFictionUser
            {
                UserName = "******",
                Nickname = "userOne"
            };

            var author = new FanFictionUser
            {
                UserName = "******",
                Nickname = "AuthorNick"
            };

            this.UserManager.CreateAsync(user).GetAwaiter();
            this.UserManager.CreateAsync(author).GetAwaiter();
            var stories = new FanFictionStory[]
            {
                new FanFictionStory
                {
                    Title     = "One",
                    Id        = 1,
                    CreatedOn = DateTime.Now,
                    Summary   = null,
                    ImageUrl  = GlobalConstants.DefaultNoImage,
                    Type      = new StoryType
                    {
                        Id   = 1,
                        Name = "Fantasy"
                    },
                    AuthorId = author.Id
                },
                new FanFictionStory
                {
                    Title     = "Two",
                    Id        = 2,
                    CreatedOn = DateTime.Now.AddMonths(1),
                    Summary   = null,
                    ImageUrl  = GlobalConstants.DefaultNoImage,
                    Type      = new StoryType
                    {
                        Id   = 2,
                        Name = "Horror"
                    },
                    AuthorId = author.Id
                },
                new FanFictionStory
                {
                    Title     = "three",
                    Id        = 3,
                    CreatedOn = DateTime.Now.AddDays(2),
                    Summary   = null,
                    ImageUrl  = GlobalConstants.DefaultNoImage,
                    Type      = new StoryType
                    {
                        Id   = 3,
                        Name = "Science Fiction"
                    },
                    AuthorId = "3333"
                },
            };

            this.Context.FictionStories.AddRange(stories);
            this.Context.SaveChanges();

            //act

            var result = this.ApiService.Authors();

            //assert

            result.Should().NotBeEmpty().And.HaveCount(1)
            .And.Subject.As <IEnumerable <ApiUserOutputModel> >().First()
            .Username.Should().Be(author.UserName);

            result.First()
            .Stories.Should().NotBeEmpty()
            .And.HaveCount(2).And.BeOfType <List <ApiFanFictionStoryOutputModel> >();
        }