protected override VerseData OnPostVerseForPoem(Poem poem, AddVerseModel verseModel, VerseEngineeringUser user)
        {
            Verse verse = verseService.GetById(verseModel.Id);

            if (verse == null)
            {
                verse = InsertCandidateVerseWithNoPreviousScratchVerseDefined(poem.Id, verseModel, user.UserId);
                return Mapper.Map<VerseData>(verse);
            }

            if ((verse.VerseType is ScratchVerseType) == false)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("Only Scratch verses can be submited to Candidates") });
            }

            if (verse.AuthorProfileId != user.UserId)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden) { Content = new StringContent("You are not allowed to modify verse which wasn't written by you") });
            }

            if (poem.Id != verse.PoemId)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("Poem identifier is not equivalent with verse identifier") });
            }

            verseService.UpdateVerse(verse, verseService.GetVerseType<CandidateVerseType>());
            var response = Mapper.Map<VerseData>(verse);
            signalizerFactory.GetSignal<CandidateVerseAddedSignal<Raw>>()
                             .FromUser(user.UserId, user.Name)
                             .Send(poem.Id, response);
            return response;
        }
        public void CreateNewVerseNotAllowedBecauseNotInPlayersList()
        {
            const int PoemId = 123456;
            const long AuthorProfileId = 123;

            poemService.Setup(service => service.GetPoemById(PoemId)).Returns(
                new ClassicalPoem
                    {
                        Id = PoemId,
                        Players =
                            new Collection<Player>(
                                new[] { new Player { UserProfileId = 1 }, new Player { UserProfileId = 2 } })
                    });

            VerseEngineeringUser requestUser = new VerseEngineeringUser(
                AuthorProfileId, "TestUser", "TestFacebookAccessToken");

            try
            {
                scratchPadVersesController.PostVerseForPoem(
                    new AddVerseModel { PoemId = PoemId, Text = "This is a verse to be created" }, requestUser);
            }
            catch (HttpResponseException exception)
            {
                Assert.IsTrue(exception.Response.StatusCode == HttpStatusCode.Forbidden);
                return;
            }

            Assert.Fail();
        }
        public void CreateNewCandidateVerseFailedBecauseBaseVerseTypeIsNotScratchPad()
        {
            const int PoemId = 123456;
            const long AuthorProfileId = 123;
            const long VerseId = 1234567;

            poemService.Setup(service => service.GetPoemById(PoemId)).Returns(
                new ClassicalPoem
                    {
                        Id = PoemId,
                        Players =
                            new Collection<Player>(
                                new[]
                                    {
                                        new Player { UserProfileId = AuthorProfileId }, new Player { UserProfileId = 1 }
                                    })
                    });

            verseService.Setup(service => service.GetById(VerseId)).Returns(
                new Verse { Id = VerseId, PoemId = PoemId, VerseType = new PoemVerseType() });
            VerseEngineeringUser requestUser = new VerseEngineeringUser(AuthorProfileId, "TestUser", "TestFacebookAccessToken");
            try
            {
                candidateVersesController.PostVerseForPoem(
                    new AddVerseModel { Id = VerseId, PoemId = PoemId, Text = "This is a verse to be updated" },
                    requestUser);
            }
            catch (HttpResponseException exception)
            {
                Assert.IsTrue(exception.Response.StatusCode == HttpStatusCode.BadRequest);
                return;
            }

            Assert.Fail();
        }
        protected override VerseData OnUpdateVerseFromPoem(Verse verse, string verseText, VerseEngineeringUser user)
        {
            if (verse.AuthorProfileId != user.UserId)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden) { Content = new StringContent("You are not allowed to update verse which wasn't written by you") });
            }

            verseService.UpdateVerseText(verse, verseText);
            return Mapper.Map<VerseData>(verse);
        }
        protected override VerseData OnPostVerseForPoem(Poem poem, AddVerseModel verseModel, VerseEngineeringUser user)
        {
            Verse verse = new Verse
            {
                AuthorProfileId = user.UserId,
                FromIpAddress = string.Empty,
                LastEdit = DateTime.UtcNow,
                WrittenAt = DateTime.UtcNow,
                Location = string.Empty,
                OrderNr = 0,
                PoemId = poem.Id,
                Text = verseModel.Text,
            };

            verseService.Insert<ScratchVerseType>(verse);
            return Mapper.Map<VerseData>(verse);
        }
예제 #6
0
        public ActionResult Play(long poemId, VerseEngineeringUser user, HideCandidateVersesFlag hideCandidateVerses)
        {
            if (!IsPlayRequestValid(poemId, user))
            {
                return RedirectToAction("Start");
            }

            Poem poem = poemService.GetPoemById(poemId);

            if (poem == null)
            {
                return RedirectToAction("Start");
            }

            if(!IsUserInPlayersList(user, poem))
            {
                return RedirectToAction("Start");
            }

            PoemVersesBag versesBag = verseService.GetPoemVersesForUser(poemId, user.UserId);

            if (hideCandidateVerses.HidePreviousCandidateVerses)
            {
                versesBag.FilterToLatestCandidateVerses();
            }

            signalizerFactory.GetSignal<PlayerEnteredTheGameSignal<Raw>>()
                .FromUser(user.UserId, user.Name)
                .Send(poem.Id, new UserProfileData { Id = user.UserId, Name = user.Name, PictureUrl = user.UserId.ToSquarePictureLink() });

            PlayPoemViewModel viewModel = new PlayPoemViewModel
                {
                    Poem = poem,
                    Verses = versesBag,
                    AcceptedPlayers = poemService.GetPlayersThatAcceptedGameInvitation(poem),
                    PendingPlayers = poemService.GetPendingGamePlayers(poem),
                    IsAbleToHidePreviousCandidateVerses = IsAbleToHidePreviousCandidateVerses(versesBag, hideCandidateVerses),
                    Me = user,
                    AuthToken = cryptoService.Encrypt(new PoemToken { PoemId = poem.Id, ProfileId = user.UserId })
                };

            return View(viewModel);
        }
        protected override VerseData OnPostVerseForPoem(Poem poem, AddVerseModel verseModel, VerseEngineeringUser user)
        {
            Verse verse = verseService.GetById(verseModel.Id);

            if (verse == null)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent("No verse with such idetifier was found") });
            }

            if (verse.VerseType is CandidateVerseType == false)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("Only Candidate verses can be submited to Poem") });
            }

            if (poem.CurrentGm != user.UserId)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden) { Content = new StringContent("You are not allowed to post poem verses unless you are game master") });
            }

            if (poem.Id != verse.PoemId)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("Poem identifier is not equivalent with verse identifier") });
            }

            verseService.UpdateVerse(verse, verseService.GetVerseType<PoemVerseType>());
            VerseData response = Mapper.Map<VerseData>(verse);
            signalizerFactory.GetSignal<PoemVerseAddedSignal<Raw>>()
                             .FromUser(user.UserId, user.Name)
                             .Send(poem.Id, response);

            if (IsGameMasterChanged(poem.CurrentGm, verse.AuthorProfileId))
            {
                UpdateGameMaster(poem, verse);
                signalizerFactory.GetSignal<PoemGameMasterChangedSignal<Raw>>()
                    .FromUser(user.UserId, user.Name)
                    .Send(poem.Id, Mapper.Map<UserProfileData>(poem.CurrentGameMasterUserProfile));
            }

            return response;
        }
        public void CreateNewVerseFailedBecauseNoPoemFound()
        {
            const int PoemId = 123456;
            const long AuthorProfileId = 123;

            poemService.Setup(service => service.GetPoemById(PoemId)).Returns((ClassicalPoem)null);

            var requestUser = new VerseEngineeringUser(AuthorProfileId, "TestUser", "TestFacebookAccessToken");

            try
            {
                scratchPadVersesController.PostVerseForPoem(
                    new AddVerseModel { PoemId = PoemId, Text = "This is a verse to be created" }, requestUser);
            }
            catch (HttpResponseException exception)
            {
                Assert.IsTrue(exception.Response.StatusCode == HttpStatusCode.NotFound);
                return;
            }

            Assert.Fail();
        }
        protected bool IsRequestValid(long poemId, VerseEngineeringUser user)
        {
            if (user == null)
            {
                return false;
            }

            return poemId > 0;
        }
        public void CreateNewVerseSuccessful()
        {
            const int PoemId = 123456;
            const long AuthorProfileId = 123;
            const long VerseId = 1234567;

            poemService.Setup(service => service.GetPoemById(PoemId)).Returns(
                new ClassicalPoem
                    {
                        Id = PoemId,
                        Players =
                            new Collection<Player>(
                                new[]
                                    {
                                        new Player { UserProfileId = AuthorProfileId }, new Player { UserProfileId = 1 }
                                    })
                    });

            verseService.Setup(service => service.Insert<ScratchVerseType>(It.IsAny<Verse>())).Callback(
                (Verse verse) => { verse.Id = VerseId; });

            VerseEngineeringUser requestUser = new VerseEngineeringUser(
                AuthorProfileId, "TestUser", "TestFacebookAccessToken");

            var response =
                scratchPadVersesController.PostVerseForPoem(
                    new AddVerseModel { PoemId = PoemId, Text = "This is a verse to be created" }, requestUser);

            Assert.IsTrue(response.Id == VerseId);
            Assert.IsTrue(response.PoemId == PoemId);
            Assert.IsTrue(response.Text == "This is a verse to be created");
        }
예제 #11
0
        private bool IsStartRequestValid(StartPoemModel startPoem, VerseEngineeringUser user)
        {
            if (!ModelState.IsValid)
            {
                return false;
            }

            if (!IsFacebookNotificationResponseValid(startPoem))
            {
                logger.Error(
                    string.Format(
                        "Facebook notification response is invalid\n {0}", startPoem.FacebookNotificationResponse));
                return false;
            }

            if (user == null)
            {
                logger.Error(string.Format("VerseEngineeringUser is null after authorization"));
                return false;
            }

            return true;
        }
        public void GetVerseByIdSuccessful()
        {
            const int PoemId = 123456;
            const long AuthorProfileId = 123;
            const long VerseId = 1001;

            VerseEngineeringUser requestUser = new VerseEngineeringUser(AuthorProfileId, "TestUser", "TestFacebookAccessToken");

            verseService.Setup(service => service.GetById(VerseId)).Returns(
                new Verse
                    {
                        AuthorProfileId = AuthorProfileId,
                        Id = VerseId,
                        PoemId = PoemId,
                        Text = "Sample verse text",
                        VerseType = new ScratchVerseType()
                    });

            var response = scratchPadVersesController.GetVerse(VerseId, requestUser);

            Assert.IsNotNull(response);
            Assert.IsTrue(response.Id == VerseId);
            Assert.IsTrue(response.PoemId == PoemId);
            Assert.IsTrue(response.Text == "Sample verse text");
        }
        public void GetVerseByIdWhichIsNotFound()
        {
            const long AuthorProfileId = 123;
            const long VerseId = 1001;

            VerseEngineeringUser requestUser = new VerseEngineeringUser(AuthorProfileId, "TestUser", "TestFacebookAccessToken");

            verseService.Setup(service => service.GetById(VerseId)).Returns((Verse)null);

            try
            {
                scratchPadVersesController.GetVerse(VerseId, requestUser);
            }
            catch (HttpResponseException exception)
            {
                Assert.IsTrue(exception.Response.StatusCode == HttpStatusCode.NotFound);
                return;
            }

            Assert.Fail();
        }
        public void CreateNewCandidateVerseSuccessfullWithNoPreviousScratchVerse()
        {
            const int PoemId = 123456;
            const long AuthorProfileId = 123;
            const long VerseId = 1234567;

            poemService.Setup(service => service.GetPoemById(PoemId)).Returns(
                new ClassicalPoem
                    {
                        Id = PoemId,
                        Players =
                            new Collection<Player>(
                                new[]
                                    {
                                        new Player { UserProfileId = AuthorProfileId }, new Player { UserProfileId = 1 }
                                    })
                    });

            verseService.Setup(service => service.GetById(VerseId)).Returns((Verse)null);
            verseService.Setup(service => service.Insert<CandidateVerseType>(It.IsAny<Verse>()));
            signalizerFactory.Setup(factory => factory.GetSignal<CandidateVerseAddedSignal<Raw>>())
                             .Returns(new CandidateVerseAddedSignal<Raw>(logger.Object));
            VerseEngineeringUser requestUser = new VerseEngineeringUser(
                AuthorProfileId, "TestUser", "TestFacebookAccessToken");

            var response =
                candidateVersesController.PostVerseForPoem(
                    new AddVerseModel { Id = VerseId, PoemId = PoemId, Text = "This is a verse to be added" },
                    requestUser);

            Assert.IsNotNull(response);
            Assert.IsTrue(response.PoemId == PoemId);
            Assert.AreEqual("This is a verse to be added", response.Text);
        }
예제 #15
0
        private bool IsPlayRequestValid(long poemId, VerseEngineeringUser user)
        {
            if (user == null)
            {
                logger.Error(string.Format("VerseEngineeringUser is null after authorization"));
                return false;
            }

            return poemId > 0;
        }
 protected abstract VerseData OnPostVerseForPoem(Poem poem, AddVerseModel verseModel, VerseEngineeringUser user);
        private Poem GetPoemFromRequestForUser(long poemId, VerseEngineeringUser user)
        {
            if (!IsRequestValid(poemId, user))
            {
                throw new VerseEngineeringException("Request is not valid");
            }

            var poem = poemService.GetPoemById(poemId);

            if (poem == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            if (!IsUserInPlayersList(user, poem))
            {
                throw new HttpResponseException(
                    new HttpResponseMessage(HttpStatusCode.Forbidden)
                        {
                            Content = new StringContent("Not allowed to add verse to poem")
                        });
            }

            return poem;
        }
 protected abstract VerseData OnDeleteVerseFromPoem(Verse verse, VerseEngineeringUser user);
 protected override IEnumerable<VerseData> OnGetVersesForPoem(Poem poem, VerseEngineeringUser user)
 {
     throw new System.NotImplementedException();
 }
        public void CreateNewPoemVerseSuccessfulWithGameMasterChanged()
        {
            const int PoemId = 123456;
            const long AuthorProfileId = 123;
            const long VerseId = 1234567;

            Poem poem = new ClassicalPoem
                {
                    Id = PoemId,
                    Players =
                        new Collection<Player>(
                            new[] { new Player { UserProfileId = AuthorProfileId }, new Player { UserProfileId = 1 } }),
                    CurrentGm = AuthorProfileId,
                    CurrentGameMasterUserProfile = new UserProfile { Id = AuthorProfileId }
                };

            poemService.Setup(service => service.GetPoemById(PoemId)).Returns(poem);
            Verse verse = new Verse
                {
                    Id = VerseId,
                    PoemId = PoemId,
                    VerseType = new CandidateVerseType(),
                    AuthorProfileId = AuthorProfileId + 1,
                    Text = "This is a verse to be updated"
                };
            verseService.Setup(service => service.GetById(VerseId)).Returns(verse);
            verseService.Setup(service => service.UpdateVerse(verse, It.IsAny<PoemVerseType>()));
            verseService.Setup(service => service.GetVerseType<PoemVerseType>()).Returns(new PoemVerseType());
            poemService.Setup(service => service.Update(poem)).Callback(
                (Poem updated) => Assert.AreEqual(AuthorProfileId + 1, updated.CurrentGm));
            signalizerFactory.Setup(factory => factory.GetSignal<PoemVerseAddedSignal<Raw>>()).Returns(
                new PoemVerseAddedSignal<Raw>(logger.Object));
            signalizerFactory.Setup(factory => factory.GetSignal<PoemGameMasterChangedSignal<Raw>>()).Returns(
                new PoemGameMasterChangedSignal<Raw>(logger.Object));

            VerseEngineeringUser requestUser = new VerseEngineeringUser(
                AuthorProfileId, "TestUser", "TestFacebookAccessToken");
            VerseData verseData =
                poemVersesController.PostVerseForPoem(new AddVerseModel { Id = VerseId, PoemId = PoemId }, requestUser);

            Assert.AreEqual(VerseId, verseData.Id);
            Assert.AreEqual("This is a verse to be updated", verseData.Text);
            Assert.AreEqual(PoemId, verseData.PoemId);
        }
 protected override IEnumerable<VerseData> OnGetVersesForPoem(Poem poem, VerseEngineeringUser user)
 {
     return
         Mapper.Map<IEnumerable<VerseData>>(
             poem.Verses.Where(verse => verse.VerseTypeId == verseService.GetVerseType<ScratchVerseType>().Id));
 }
 protected override VerseData OnDeleteVerseFromPoem(Verse verse, VerseEngineeringUser user)
 {
     throw new NotImplementedException();
 }
 protected abstract IEnumerable<VerseData> OnGetVersesForPoem(Poem poem, VerseEngineeringUser user);
 protected override VerseData OnUpdateVerseFromPoem(Verse verse, string verseText, VerseEngineeringUser user)
 {
     throw new System.NotImplementedException();
 }
 protected abstract VerseData OnUpdateVerseFromPoem(Verse verse, string verseText, VerseEngineeringUser user);
        public void EditVerseFailedBecauseNoSuchIdInDatabase()
        {
            const long AuthorProfileId = 123;
            const long VerseId = 1001;

            VerseEngineeringUser requestUser = new VerseEngineeringUser(AuthorProfileId, "TestUser", "TestFacebookAccessToken");
            verseService.Setup(service => service.GetById(VerseId)).Returns((Verse)null);

            try
            {
                scratchPadVersesController.PutVerse(
                    VerseId, new UpdateVerseModel { Text = "This is an edited verse" }, requestUser);
            }
            catch (HttpResponseException exception)
            {
                Assert.IsTrue(exception.Response.StatusCode == HttpStatusCode.NotFound);
                return;
            }

            Assert.Fail();
        }
 private bool IsUserInPlayersList(VerseEngineeringUser user, Poem poem)
 {
     return poem.Players.Any(player => player.UserProfileId == user.UserId);
 }
        public void EditVerseFailedBecauseTheUserIsNotTheAuthor()
        {
            const int PoemId = 123456;
            const long AuthorProfileId = 123;
            const long VerseId = 1001;

            var requestUser = new VerseEngineeringUser(AuthorProfileId, "TestUser", "TestFacebookAccessToken");
            var verseToDelete = new Verse
            {
                AuthorProfileId = 321,
                Id = VerseId,
                PoemId = PoemId,
                Text = "Sample verse text",
                VerseType = new ScratchVerseType()
            };

            verseService.Setup(service => service.GetById(VerseId)).Returns(verseToDelete);
            try
            {
                scratchPadVersesController.PutVerse(VerseId, new UpdateVerseModel { Text = "This is an edited verse" }, requestUser);
            }
            catch (HttpResponseException exception)
            {
                Assert.IsTrue(exception.Response.StatusCode == HttpStatusCode.Forbidden);
                return;
            }

            Assert.Fail();
        }
        public void EditVerseSuccessful()
        {
            const int PoemId = 123456;
            const long AuthorProfileId = 123;
            const long VerseId = 1001;

            var requestUser = new VerseEngineeringUser(AuthorProfileId, "TestUser", "TestFacebookAccessToken");
            var verseToEdit = new Verse
            {
                AuthorProfileId = AuthorProfileId,
                Id = VerseId,
                PoemId = PoemId,
                Text = "Sample verse text",
                VerseType = new ScratchVerseType()
            };

            verseService.Setup(service => service.GetById(VerseId)).Returns(verseToEdit);
            verseService.Setup(service => service.UpdateVerseText(verseToEdit, "This is text to edit")).Callback(
                (Verse verse, string text) => { verse.Text = text; });

            var response = scratchPadVersesController.PutVerse(
                VerseId, new UpdateVerseModel { Text = "This is text to edit" }, requestUser);

            Assert.IsNotNull(response);
            Assert.IsTrue(response.Id == VerseId);
            Assert.IsTrue(response.PoemId == PoemId);
            Assert.IsTrue(response.Text == "This is text to edit");
        }
예제 #30
0
        public ActionResult Start(StartPoemModel startPoem, VerseEngineeringUser user)
        {
            if (!IsStartRequestValid(startPoem, user))
            {
                return View(startPoem);
            }

            if (user == null)
            {
                logger.Error(string.Format("VerseEngineeringUser is null after authorization"));
                return View(startPoem);
            }

            StartPoemConfiguration poemConfig = new ClassicalPoemConfiguration(
                user.UserId,
                startPoem.PoemTitle,
                startPoem.PoemDescription,
                startPoem.FacebookNotificationResponse.To,
                startPoem.FacebookNotificationResponse.Request);

            long poemId = poemService.StartNewPoem(poemConfig);

            return RedirectToAction("Play", new { poemId });
        }