Exemplo n.º 1
0
        public async Task TestMovingQuestion__WhenThNewTopicDoesNotExist__ReturnsTheFullTopicModel()
        {
            using (var fixture = new TestFixture())
            {
                await fixture.UserHelper.CreateUser("admin");

                var question = await fixture.QuestionHelper.CreateQuestion("admin", "Test", "test");

                var questionService = fixture.Container.Resolve <IQuestionService>();

                using (var db = new TestDbContext())
                {
                    using (var user = await fixture.UserHelper.Become(db, "admin"))
                    {
                        await questionService.UpdateQuestion(db, user, new UpdateQuestion()
                        {
                            QuestionId = question.QuestionId.ToString(),
                            RowVersion = PangulRowVersion.GetString(question.RowVersion),
                            Body       = question.Body,
                            Title      = question.Title,
                            Topic      = "newTopic"
                        });

                        await db.SaveChangesAsync();

                        var questionFetched = await questionService.GetQuestion(db, user, question.QuestionId.ToString());

                        Assert.Equal("newtopic", questionFetched.Topic.Name);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public async Task TestUpdateAnswer__WithTheWrongRowVersion__DoesntUpdateAnything()
        {
            using (var fixture = new TestFixture())
            {
                await fixture.UserHelper.CreateUser("admin");

                var question = await fixture.QuestionHelper.CreateQuestion("admin", "Test", "test");

                var answer = await fixture.AnswerHelper.AnswerQuestion("admin", question, "Some answer");

                var answerService = fixture.Container.Resolve <IAnswerService>();

                using (var db = new TestDbContext())
                {
                    using (var user = await fixture.UserHelper.Become(db, "admin"))
                    {
                        await Assert.ThrowsAsync <PangulCommandFailedException>(async() =>
                        {
                            await answerService.UpdateExistingAnswer(db, user, new UpdateAnswer()
                            {
                                AnswerId   = answer.AnswerId.ToString(),
                                NewBody    = "Some answer body",
                                RowVersion = PangulRowVersion.GetString(Guid.NewGuid().ToByteArray())
                            });
                            await db.SaveChangesAsync();
                        });

                        var answerCopy = await answerService.GetAnswer(db, user, answer.AnswerId.ToString());

                        Assert.Equal("Some answer", answerCopy.Body);
                    }
                }
            }
        }
Exemplo n.º 3
0
 public TopicViewModel(Topic model)
 {
     TopicId     = model.TopicId.ToString();
     Name        = model.Name;
     Description = model.Description ?? "";
     Icon        = PangulStringEncoding.GetDataUrlFromBytes(model.Icon, model.IconType);
     RowVersion  = PangulRowVersion.GetString(model.RowVersion);
 }
Exemplo n.º 4
0
 public static AnswerViewModel From(Answer answer)
 {
     return(new AnswerViewModel()
     {
         UserId = answer.UserId.ToString(),
         QuestionId = answer.QuestionId.ToString(),
         AnswerId = answer.AnswerId.ToString(),
         Body = answer.Body,
         CanEdit = answer.CanEdit,
         RowVersion = PangulRowVersion.GetString(answer.RowVersion)
     });
 }
Exemplo n.º 5
0
 public static AnswerMetaViewModel From(AnswerMetaInternalModel model)
 {
     return(new AnswerMetaViewModel()
     {
         AnswerId = model.Meta.AnswerId.ToString(),
         AnswerMetaId = model.Meta.AnswerMetaId.ToString(),
         Votes = model.Meta.Votes,
         Global = new AnswerGlobalMetaViewModel()
         {
             Votes = model.GlobalMeta.Votes
         },
         RowVersion = PangulRowVersion.GetString(model.Meta.RowVersion)
     });
 }
Exemplo n.º 6
0
 public static QuestionViewModel From(Question question)
 {
     return(new QuestionViewModel()
     {
         CanEdit = question.CanEdit,
         UserId = question.UserId.ToString(),
         QuestionId = question.QuestionId.ToString(),
         Title = question.Title,
         Tags = question.Tags.Select(i => i.Tag).ToList(),
         Body = question.Body,
         RowVersion = PangulRowVersion.GetString(question.RowVersion),
         Topic = question.Topic.Name
     });
 }
Exemplo n.º 7
0
 private object MapToAnswerMetaResult(AnswerMetaInternalModel model)
 {
     return(new
     {
         AnswerId = model.Meta.AnswerId.ToString(),
         AnswerMetaId = model.Meta.AnswerMetaId.ToString(),
         Votes = model.Meta.Votes,
         Global = new
         {
             Votes = model.GlobalMeta.Votes
         },
         RowVersion = PangulRowVersion.GetString(model.Meta.RowVersion),
     });
 }
Exemplo n.º 8
0
 public static QuestionMetaViewModel From(Services.Model.QuestionMetaInternalModel model)
 {
     return(new QuestionMetaViewModel()
     {
         QuestionId = model.Meta.QuestionId.ToString(),
         QuestionMetaId = model.Meta.QuestionMetaId.ToString(),
         Votes = model.Meta.Votes,
         Star = model.Meta.Star,
         Global = new QuestionGlobalMetaViewModel()
         {
             Votes = model.GlobalMeta.Votes
         },
         RowVersion = PangulRowVersion.GetString(model.Meta.RowVersion),
     });
 }
Exemplo n.º 9
0
        public async Task TestVoteOnQuestion__FromSingleUser__SetsVotesCountCorrectly()
        {
            using (var fixture = new TestFixture())
            {
                await fixture.UserHelper.CreateUser("admin");

                var question = await fixture.QuestionHelper.CreateQuestion("admin", "New Question", "Some question body");

                var questionService = fixture.Container.Resolve <IQuestionService>();
                using (var db = new TestDbContext())
                {
                    using (var user = await fixture.UserHelper.Become(db, "admin"))
                    {
                        var metadata = await questionService.GetQuestionMetadata(db, user, question.QuestionId.ToString());

                        await questionService.UpdateQuestionMetadata(db, user, new UpdateQuestionMeta()
                        {
                            QuestionId = question.QuestionId.ToString(),
                            RowVersion = PangulRowVersion.GetString(metadata.Meta.RowVersion),
                            Votes      = 1
                        });

                        await db.SaveChangesAsync();

                        var meta = await questionService.GetQuestionMetadata(db, user, question.QuestionId.ToString());

                        Assert.Equal(1, meta.Meta.Votes);
                        Assert.Equal(1, meta.GlobalMeta.Votes);

                        await questionService.UpdateQuestionMetadata(db, user, new UpdateQuestionMeta()
                        {
                            QuestionId = question.QuestionId.ToString(),
                            RowVersion = PangulRowVersion.GetString(metadata.Meta.RowVersion),
                            Votes      = 1
                        });

                        await db.SaveChangesAsync();

                        meta = await questionService.GetQuestionMetadata(db, user, question.QuestionId.ToString());

                        Assert.Equal(1, meta.Meta.Votes);
                        Assert.Equal(1, meta.GlobalMeta.Votes);
                    }
                }
            }
        }
Exemplo n.º 10
0
        public async Task VoteForQuestion(string username, Question question, int votes = 1)
        {
            var service = _fixture.Container.Resolve <IQuestionService>();

            using (var db = new TestDbContext())
            {
                using (var user = await _fixture.UserHelper.Become(db, username))
                {
                    await service.UpdateQuestionMetadata(db, user, new UpdateQuestionMeta()
                    {
                        RowVersion = PangulRowVersion.GetString(question.RowVersion),
                        Votes      = votes,
                        QuestionId = question.QuestionId.ToString()
                    });

                    await db.SaveChangesAsync();
                }
            }
        }
Exemplo n.º 11
0
        public async Task TestMoveQuestionToDifferentTopic__FromExistingTopicToNewTopic__CreatesNewTopicAndMovesQuestionToIt()
        {
            using (var fixture = new TestFixture())
            {
                await fixture.UserHelper.CreateUser("admin");

                await fixture.TopicHelper.RequireTopic("admin", "one");

                await fixture.TopicHelper.RequireTopic("admin", "two");

                var question = await fixture.QuestionHelper.CreateQuestion("admin", "Some question", "Body", "one");

                var service  = fixture.Container.Resolve <IQuestionService>();
                var searcher = fixture.Container.Resolve <IInternalSearchService>();
                using (var db = new TestDbContext())
                {
                    using (var user = await fixture.UserHelper.Become(db, "admin"))
                    {
                        await service.UpdateQuestionTopic(db, user, new UpdateQuestionTopic()
                        {
                            QuestionId = question.QuestionId.ToString(),
                            RowVersion = PangulRowVersion.GetString(question.RowVersion),
                            TopicName  = "two"
                        });

                        await db.SaveChangesAsync();
                    }
                }

                using (var db = new TestDbContext())
                {
                    using (var user = await fixture.UserHelper.Become(db, "admin"))
                    {
                        var noMatches = await searcher.SearchQuestions(db, user, "topic:one question", 0, 10);

                        var matches = await searcher.SearchQuestions(db, user, "topic:two question", 0, 10);

                        Assert.Single(matches.IdentityList);
                        Assert.Empty(noMatches.IdentityList);
                    }
                }
            }
        }
Exemplo n.º 12
0
        public async Task VoteForAnswer(string username, Answer answer, int votes = 1)
        {
            var service = _fixture.Container.Resolve <IAnswerService>();

            using (var db = new TestDbContext())
            {
                using (var user = await _fixture.UserHelper.Become(db, username))
                {
                    await service.UpdateAnswerMetadata(db, user, new UpdateAnswerMeta()
                    {
                        RowVersion = PangulRowVersion.GetString(answer.RowVersion),
                        Votes      = votes,
                        AnswerId   = answer.AnswerId.ToString()
                    });

                    await db.SaveChangesAsync();
                }
            }
        }
Exemplo n.º 13
0
        public async Task TestUpdateQuestion__WithInvalidRowData__Fails()
        {
            using (var fixture = new TestFixture())
            {
                await fixture.UserHelper.CreateUser("admin");

                var question = await fixture.QuestionHelper.CreateQuestion("admin", "New question", "Some question body");

                var questionService = fixture.Container.Resolve <IQuestionService>();

                using (var db = new TestDbContext())
                {
                    using (var user = await fixture.UserHelper.Become(db, "admin"))
                    {
                        var instance = await questionService.GetQuestion(db, user, question.QuestionId.ToString());

                        Assert.NotNull(instance.RowVersion);
                    }
                }

                using (var db = new TestDbContext())
                {
                    using (var user = await fixture.UserHelper.Become(db, "admin"))
                    {
                        await Assert.ThrowsAsync <Pangul.Services.Infrastructure.Errors.PangulCommandFailedException>(async() =>
                        {
                            await questionService.UpdateQuestion(db, user, new UpdateQuestion()
                            {
                                QuestionId = question.QuestionId.ToString(),
                                Title      = "New title",
                                Body       = "Body",
                                Tags       = new string[] { },
                                RowVersion = PangulRowVersion.GetString(Guid.NewGuid().ToByteArray())
                            });
                            await db.SaveChangesAsync();
                        });
                    }
                }
            }
        }
Exemplo n.º 14
0
        public async Task TestUpdateAnswer__WithTrivialData__UpdatesTheAnswerAndRowVersion()
        {
            using (var fixture = new TestFixture())
            {
                await fixture.UserHelper.CreateUser("admin");

                var question = await fixture.QuestionHelper.CreateQuestion("admin", "Test", "test");

                var answer = await fixture.AnswerHelper.AnswerQuestion("admin", question, "Some answer");

                var answerService = fixture.Container.Resolve <IAnswerService>();

                using (var db = new TestDbContext())
                {
                    using (var user = await fixture.UserHelper.Become(db, "admin"))
                    {
                        var updatedAnswer = await answerService.UpdateExistingAnswer(db, user, new UpdateAnswer()
                        {
                            AnswerId   = answer.AnswerId.ToString(),
                            NewBody    = "Some answer body",
                            RowVersion = PangulRowVersion.GetString(answer.RowVersion)
                        });

                        await db.SaveChangesAsync();

                        Assert.Equal("Some answer body", updatedAnswer.Body);
                        Assert.NotEqual(answer.RowVersion, updatedAnswer.RowVersion);

                        var answerCopy = await answerService.GetAnswer(db, user, answer.AnswerId.ToString());

                        Assert.Equal("Some answer body", answerCopy.Body);
                        Assert.NotEqual(answer.RowVersion, answerCopy.RowVersion);
                    }
                }
            }
        }
Exemplo n.º 15
0
        public async Task TestVoteOnAnswer__WithTrivialData__CreatesAValidAnswerWithVotes()
        {
            using (var fixture = new TestFixture())
            {
                await fixture.UserHelper.CreateUser("admin1");

                await fixture.UserHelper.CreateUser("admin2");

                await fixture.UserHelper.CreateUser("admin3");

                var question = await fixture.QuestionHelper.CreateQuestion("admin1", "Test", "test");

                var answer = await fixture.AnswerHelper.AnswerQuestion("admin1", question, "Some answer goes here");

                var answerService = fixture.Container.Resolve <IAnswerService>();

                using (var db = new TestDbContext())
                {
                    using (var user = await fixture.UserHelper.Become(db, "admin1"))
                    {
                        var copy = await answerService.GetAnswer(db, user, answer.AnswerId.ToString());

                        await answerService.UpdateAnswerMetadata(db, user, new UpdateAnswerMeta()
                        {
                            AnswerId   = answer.AnswerId.ToString(),
                            RowVersion = PangulRowVersion.GetString(copy.RowVersion),
                            Votes      = 1
                        });

                        await db.SaveChangesAsync();
                    }
                }

                using (var db = new TestDbContext())
                {
                    using (var user = await fixture.UserHelper.Become(db, "admin2"))
                    {
                        var copy = await answerService.GetAnswer(db, user, answer.AnswerId.ToString());

                        await answerService.UpdateAnswerMetadata(db, user, new UpdateAnswerMeta()
                        {
                            AnswerId   = answer.AnswerId.ToString(),
                            RowVersion = PangulRowVersion.GetString(copy.RowVersion),
                            Votes      = 1
                        });

                        await db.SaveChangesAsync();
                    }
                }

                using (var db = new TestDbContext())
                {
                    using (var user = await fixture.UserHelper.Become(db, "admin3"))
                    {
                        var copy = await answerService.GetAnswer(db, user, answer.AnswerId.ToString());

                        await answerService.UpdateAnswerMetadata(db, user, new UpdateAnswerMeta()
                        {
                            AnswerId   = answer.AnswerId.ToString(),
                            RowVersion = PangulRowVersion.GetString(copy.RowVersion),
                            Votes      = 1
                        });

                        await db.SaveChangesAsync();
                    }
                }

                using (var db = new TestDbContext())
                {
                    using (var user = await fixture.UserHelper.Become(db, "admin1"))
                    {
                        var copy = await answerService.GetAnswer(db, user, answer.AnswerId.ToString());

                        await answerService.UpdateAnswerMetadata(db, user, new UpdateAnswerMeta()
                        {
                            AnswerId   = answer.AnswerId.ToString(),
                            RowVersion = PangulRowVersion.GetString(copy.RowVersion),
                            Votes      = 1
                        });

                        await db.SaveChangesAsync(); // Does nothing, admin1 already voted
                    }
                }

                using (var db = new TestDbContext())
                {
                    using (var user = await fixture.UserHelper.Become(db, "admin1"))
                    {
                        var meta = await answerService.GetAnswerMetadata(db, user, answer.AnswerId.ToString());

                        Assert.Equal(1, meta.Meta.Votes);
                        Assert.Equal(3, meta.GlobalMeta.Votes);
                    }
                }
            }
        }
Exemplo n.º 16
0
        public async Task TestVoteOnQuestion__FromMultipleUsers__SetsVotesCountCorrectly()
        {
            using (var fixture = new TestFixture())
            {
                await fixture.UserHelper.CreateUser("admin1");

                await fixture.UserHelper.CreateUser("admin2");

                await fixture.UserHelper.CreateUser("admin3");

                await fixture.UserHelper.CreateUser("admin4");

                await fixture.UserHelper.CreateUser("admin5");

                await fixture.UserHelper.CreateUser("admin6");

                var questionService = fixture.Container.Resolve <IQuestionService>();
                using (var db = new TestDbContext())
                {
                    long questionId = 0;
                    using (var user = await fixture.UserHelper.Become(db, "admin1"))
                    {
                        var question = await questionService.CreateQuestion(db, user, new CreateNewQuestion()
                        {
                            Title = "question",
                            Body  = "body",
                            Tags  = new[] { QuestionHelper.NewQuestionTag },
                            Topic = Topic.DefaultTopic
                        });

                        await db.SaveChangesAsync();

                        var metadata = await questionService.GetQuestionMetadata(db, user, question.QuestionId.ToString());

                        await questionService.UpdateQuestionMetadata(db, user, new UpdateQuestionMeta()
                        {
                            QuestionId = question.QuestionId.ToString(),
                            RowVersion = PangulRowVersion.GetString(metadata.Meta.RowVersion),
                            Votes      = 1
                        });

                        await db.SaveChangesAsync();

                        questionId = question.QuestionId;
                    }

                    using (var user = await fixture.UserHelper.Become(db, "admin2"))
                    {
                        var metadata = await questionService.GetQuestionMetadata(db, user, questionId.ToString());

                        await questionService.UpdateQuestionMetadata(db, user, new UpdateQuestionMeta()
                        {
                            QuestionId = questionId.ToString(),
                            RowVersion = PangulRowVersion.GetString(metadata.Meta.RowVersion),
                            Votes      = 1
                        });

                        await db.SaveChangesAsync();
                    }

                    using (var user = await fixture.UserHelper.Become(db, "admin3"))
                    {
                        var metadata = await questionService.GetQuestionMetadata(db, user, questionId.ToString());

                        await questionService.UpdateQuestionMetadata(db, user, new UpdateQuestionMeta()
                        {
                            QuestionId = questionId.ToString(),
                            RowVersion = PangulRowVersion.GetString(metadata.Meta.RowVersion),
                            Votes      = 1
                        });

                        await db.SaveChangesAsync();
                    }

                    using (var user = await fixture.UserHelper.Become(db, "admin4"))
                    {
                        var metadata = await questionService.GetQuestionMetadata(db, user, questionId.ToString());

                        await questionService.UpdateQuestionMetadata(db, user, new UpdateQuestionMeta()
                        {
                            QuestionId = questionId.ToString(),
                            RowVersion = PangulRowVersion.GetString(metadata.Meta.RowVersion),
                            Votes      = 0
                        });

                        await db.SaveChangesAsync();
                    }

                    using (var user = await fixture.UserHelper.Become(db, "admin5"))
                    {
                        var metadata = await questionService.GetQuestionMetadata(db, user, questionId.ToString());

                        await questionService.UpdateQuestionMetadata(db, user, new UpdateQuestionMeta()
                        {
                            QuestionId = questionId.ToString(),
                            RowVersion = PangulRowVersion.GetString(metadata.Meta.RowVersion),
                            Votes      = -1
                        });

                        await db.SaveChangesAsync();
                    }

                    using (var user = await fixture.UserHelper.Become(db, "admin1"))
                    {
                        var meta = await questionService.GetQuestionMetadata(db, user, questionId.ToString());

                        Assert.Equal(2, meta.GlobalMeta.Votes);
                    }
                }
            }
        }