Ejemplo n.º 1
0
 public ContestRegistrationViewModel(Contest contest, ContestRegistrationModel userAnswers, bool isOfficial)
     : this(contest, isOfficial)
 {
     this.Questions = this.Questions.Select(x =>
     {
         var userAnswer = userAnswers.Questions.FirstOrDefault(y => y.QuestionId == x.QuestionId);
         return(new QuestionViewModel
         {
             Answer = userAnswer == null ? null : userAnswer.Answer,
             QuestionId = x.QuestionId,
             Question = x.Question
         });
     });
 }
Ejemplo n.º 2
0
        public void RegisterActionWhenPostedDataPracticeHasPasswordAndProvidedCorrectPasswordShouldRedirectToIndex()
        {
            var contest = this.CreateAndSaveContest("contestName", this.InactiveContestOptions, this.ActiveContestWithPasswordOptions);

            var contestRegistrationModel = new ContestRegistrationModel(this.EmptyOjsData);

            contestRegistrationModel.Password  = this.DefaultPracticePassword;
            contestRegistrationModel.ContestId = contest.Id;

            var result = this.CompeteController.Register(this.IsPractice, contestRegistrationModel) as RedirectToRouteResult;

            Assert.IsNull(result.RouteValues["controller"]);
            Assert.AreEqual(GlobalConstants.Index, result.RouteValues["action"]);
            Assert.AreEqual(this.IsPractice, result.RouteValues["official"]);
            Assert.AreEqual(contest.Id, result.RouteValues["id"]);
        }
Ejemplo n.º 3
0
        public void RegisterActionWhenPostedDataAndContestCannotBeCompetedShouldThrowException()
        {
            var contest = this.CreateAndSaveContest("testContest", this.InactiveContestOptions, this.InactiveContestOptions);

            try
            {
                var contestRegistrationModel = new ContestRegistrationModel(this.EmptyOjsData);
                contestRegistrationModel.ContestId = contest.Id;
                this.CompeteController.Register(this.IsCompete, contestRegistrationModel);
                Assert.Fail("Expected exception trying to register to compete in a contest, when compete is not available");
            }
            catch (HttpException ex)
            {
                Assert.AreEqual((int)HttpStatusCode.Forbidden, ex.GetHttpCode());
            }
        }
        public void RegisterActionWhenPostedDataContestHasPasswordAndProvidedCorrectPasswordShouldRedirectToIndex()
        {
            var contest = this.CreateAndSaveContest("contestName", this.ActiveContestWithPasswordOptions, this.InactiveContestOptions);

            var registerModel = new ContestRegistrationModel
            {
                Password = this.DefaultCompetePassword
            };

            var result = this.CompeteController.Register(contest.Id, this.IsCompete, registerModel) as RedirectToRouteResult;

            Assert.IsNull(result.RouteValues["controller"]);
            Assert.AreEqual("Index", result.RouteValues["action"]);
            Assert.AreEqual(this.IsCompete, result.RouteValues["official"]);
            Assert.AreEqual(contest.Id, result.RouteValues["id"]);
        }
Ejemplo n.º 5
0
        public void RegisterActionWhenPostedDataPracticeHasPasswordAndQuestionsUnansweredShouldReturnView()
        {
            var contest = this.CreateAndSaveContest("testContest", this.InactiveContestOptions, this.ActiveContestWithPasswordAndQuestionsOptions);

            var contestRegistrationModel = new ContestRegistrationModel(this.EmptyOjsData);

            contestRegistrationModel.Password  = this.DefaultPracticePassword;
            contestRegistrationModel.ContestId = contest.Id;

            this.TryValidateModel(contestRegistrationModel, this.CompeteController);
            var result      = this.CompeteController.Register(this.IsPractice, contestRegistrationModel) as ViewResult;
            var resultModel = result.Model as ContestRegistrationViewModel;

            Assert.IsNotNull(resultModel);
            Assert.AreEqual(contest.Questions.Count, resultModel.Questions.Count());
            Assert.IsTrue(contest.HasPracticePassword);
        }
Ejemplo n.º 6
0
        public void RegisterActionWhenPostedDataAndUserAlreadyRegisteredShouldRedirectToIndex()
        {
            var contest = this.CreateAndSaveContest("testContest", this.ActiveContestWithPasswordOptions, this.ActiveContestWithPasswordOptions);

            this.EmptyOjsData.Participants.Add(new Participant(contest.Id, this.FakeUserProfile.Id, this.IsCompete));
            this.EmptyOjsData.SaveChanges();

            var contestRegistrationModel = new ContestRegistrationModel(this.EmptyOjsData);

            contestRegistrationModel.ContestId = contest.Id;

            var result = this.CompeteController.Register(this.IsCompete, contestRegistrationModel) as RedirectToRouteResult;

            Assert.IsNull(result.RouteValues["controller"]);
            Assert.AreEqual(GlobalConstants.Index, result.RouteValues["action"]);
            Assert.AreEqual(contest.Id, result.RouteValues["id"]);
            Assert.AreEqual(this.IsCompete, result.RouteValues["official"]);
        }
Ejemplo n.º 7
0
        public void RegisterActionWhenPostedDataPracticeHasPasswordAndProvidedIncorrectPasswordShouldReturnView()
        {
            var contest = this.CreateAndSaveContest("testContest", this.InactiveContestOptions, this.ActiveContestWithPasswordOptions);

            var contestRegistrationModel = new ContestRegistrationModel(this.EmptyOjsData);

            contestRegistrationModel.Password  = "******";
            contestRegistrationModel.ContestId = contest.Id;

            var result = this.CompeteController.Register(this.IsPractice, contestRegistrationModel) as ViewResult;
            var model  = result.Model as ContestRegistrationViewModel;

            Assert.IsNotNull(model);
            Assert.IsNull(model.Password);
            Assert.IsFalse(this.CompeteController.ModelState.IsValid);
            Assert.IsNotNull(this.CompeteController.ModelState["Password"]);
            Assert.AreEqual(contest.Name, model.ContestName);
            Assert.IsTrue(model.RequirePassword);
        }
        public void RegisterActionWhenPostedDataContestHasPasswordQuestionAnsweredWithEmptyStringShouldReturnView()
        {
            var contest = this.CreateAndSaveContest("testContest", this.ActiveContestWithQuestionsOptions, this.InactiveContestOptions);

            var registerModel = new ContestRegistrationModel
            {
                Password  = this.DefaultCompetePassword,
                Questions = contest.Questions.Select(x => new ContestQuestionAnswerModel
                {
                    QuestionId = x.Id,
                    Answer     = string.Empty
                })
            };

            var result = this.CompeteController.Register(contest.Id, this.IsCompete, registerModel) as ViewResult;
            var model  = result.Model as ContestRegistrationViewModel;

            Assert.IsNotNull(model);
        }
Ejemplo n.º 9
0
        public void RegisterActionWhenPostedDataContestHasPasswordAndAllQuestionsAnsweredShouldRedirectToIndex()
        {
            var contest = this.CreateAndSaveContest("testContest", this.ActiveContestWithQuestionsOptions, this.InactiveContestOptions);

            var contestRegistrationModel = new ContestRegistrationModel(this.EmptyOjsData);

            contestRegistrationModel.Password  = this.DefaultCompetePassword;
            contestRegistrationModel.ContestId = contest.Id;
            contestRegistrationModel.Questions = contest.Questions.Select(x => new ContestQuestionAnswerModel
            {
                QuestionId = x.Id,
                Answer     = "answer"
            });

            var result = this.CompeteController.Register(this.IsCompete, contestRegistrationModel) as RedirectToRouteResult;

            Assert.IsNull(result.RouteValues["controller"]);
            Assert.AreEqual(GlobalConstants.Index, result.RouteValues["action"]);
            Assert.AreEqual(contest.Id, result.RouteValues["id"]);
            Assert.AreEqual(this.IsCompete, result.RouteValues["official"]);
        }
Ejemplo n.º 10
0
        public ActionResult Register(bool official, ContestRegistrationModel registrationData)
        {
            // check if the user has already registered for participation and redirect him to the correct action
            var participantFound = this.Data.Participants.Any(registrationData.ContestId, this.UserProfile.Id, official);

            if (participantFound)
            {
                return(this.RedirectToAction(GlobalConstants.Index, new { id = registrationData.ContestId, official }));
            }

            var contest = this.Data.Contests.GetById(registrationData.ContestId);

            ValidateContest(contest, official);

            if (official && contest.HasContestPassword)
            {
                if (string.IsNullOrEmpty(registrationData.Password))
                {
                    this.ModelState.AddModelError("Password", Resource.Views.CompeteRegister.Empty_Password);
                }
                else if (contest.ContestPassword != registrationData.Password)
                {
                    this.ModelState.AddModelError("Password", Resource.Views.CompeteRegister.Incorrect_password);
                }
            }

            if (!official && contest.HasPracticePassword)
            {
                if (string.IsNullOrEmpty(registrationData.Password))
                {
                    this.ModelState.AddModelError("Password", Resource.Views.CompeteRegister.Empty_Password);
                }
                else if (contest.PracticePassword != registrationData.Password)
                {
                    this.ModelState.AddModelError("Password", Resource.Views.CompeteRegister.Incorrect_password);
                }
            }

            var questionsToAnswerCount = official ?
                                         contest.Questions.Count(x => !x.IsDeleted && x.AskOfficialParticipants) :
                                         contest.Questions.Count(x => !x.IsDeleted && x.AskPracticeParticipants);

            if (questionsToAnswerCount != registrationData.Questions.Count())
            {
                this.ModelState.AddModelError("Questions", Resource.Views.CompeteRegister.Not_all_questions_answered);
            }

            var contestQuestions = contest.Questions.Where(x => !x.IsDeleted).ToList();

            var participant = new Participant(registrationData.ContestId, this.UserProfile.Id, official);

            this.Data.Participants.Add(participant);
            var counter = 0;

            foreach (var question in registrationData.Questions)
            {
                var contestQuestion = contestQuestions.FirstOrDefault(x => x.Id == question.QuestionId);

                var  regularExpression = contestQuestion.RegularExpressionValidation;
                bool correctlyAnswered = false;

                if (!string.IsNullOrEmpty(regularExpression))
                {
                    correctlyAnswered = Regex.IsMatch(question.Answer, regularExpression);
                }

                if (contestQuestion.Type == ContestQuestionType.DropDown)
                {
                    int contestAnswerId;
                    if (int.TryParse(question.Answer, out contestAnswerId) && contestQuestion.Answers.Where(x => !x.IsDeleted).Any(x => x.Id == contestAnswerId))
                    {
                        correctlyAnswered = true;
                    }

                    if (!correctlyAnswered)
                    {
                        this.ModelState.AddModelError(string.Format("Questions[{0}].Answer", counter), Resource.ContestsGeneral.Invalid_selection);
                    }
                }

                participant.Answers.Add(new ParticipantAnswer
                {
                    ContestQuestionId = question.QuestionId,
                    Answer            = question.Answer
                });

                counter++;
            }

            if (!this.ModelState.IsValid)
            {
                return(this.View(new ContestRegistrationViewModel(contest, registrationData, official)));
            }

            this.Data.SaveChanges();

            return(this.RedirectToAction(GlobalConstants.Index, new { id = registrationData.ContestId, official }));
        }
Ejemplo n.º 11
0
        public ActionResult Register(bool official, ContestRegistrationModel registrationData)
        {
            var participant = this.participantsData.GetByContestByUserAndByIsOfficial(registrationData.ContestId, this.UserProfile.Id, official);

            if (participant != null && !participant.IsInvalidated)
            {
                return(this.RedirectToAction(GlobalConstants.Index, new { id = registrationData.ContestId, official }));
            }

            var contest = this.contestsData.GetById(registrationData.ContestId);

            try
            {
                this.ValidateContest(contest, official);

                if (official && contest.HasContestPassword)
                {
                    if (string.IsNullOrEmpty(registrationData.Password))
                    {
                        this.ModelState.AddModelError("Password", Resource.Views.CompeteRegister.Empty_Password);
                    }
                    else if (contest.ContestPassword != registrationData.Password)
                    {
                        this.ModelState.AddModelError("Password", Resource.Views.CompeteRegister.Incorrect_password);
                    }
                }

                if (!official && contest.HasPracticePassword)
                {
                    if (string.IsNullOrEmpty(registrationData.Password))
                    {
                        this.ModelState.AddModelError("Password", Resource.Views.CompeteRegister.Empty_Password);
                    }
                    else if (contest.PracticePassword != registrationData.Password)
                    {
                        this.ModelState.AddModelError("Password", Resource.Views.CompeteRegister.Incorrect_password);
                    }
                }

                var questionsToAnswerCount = official ?
                                             contest.Questions.Count(x => !x.IsDeleted && x.AskOfficialParticipants) :
                                             contest.Questions.Count(x => !x.IsDeleted && x.AskPracticeParticipants);

                if (questionsToAnswerCount != registrationData.Questions.Count())
                {
                    this.ModelState.AddModelError("Questions", Resource.Views.CompeteRegister.Not_all_questions_answered);
                }

                var contestQuestions = contest.Questions.Where(x => !x.IsDeleted).ToList();

                var counter = 0;
                var answers = new List <ParticipantAnswer>();
                foreach (var question in registrationData.Questions)
                {
                    var contestQuestion = contestQuestions.FirstOrDefault(x => x.Id == question.QuestionId);

                    var regularExpression = contestQuestion.RegularExpressionValidation;
                    var correctlyAnswered = false;

                    if (!string.IsNullOrEmpty(regularExpression))
                    {
                        correctlyAnswered = Regex.IsMatch(question.Answer, regularExpression);
                    }

                    if (contestQuestion.Type == ContestQuestionType.DropDown)
                    {
                        if (int.TryParse(question.Answer, out var contestAnswerId) &&
                            contestQuestion.Answers.Where(x => !x.IsDeleted).Any(x => x.Id == contestAnswerId))
                        {
                            correctlyAnswered = true;
                        }

                        if (!correctlyAnswered)
                        {
                            this.ModelState.AddModelError(
                                $"Questions[{counter}].Answer",
                                Resource.ContestsGeneral.Invalid_selection);
                        }
                    }

                    answers.Add(
                        new ParticipantAnswer
                    {
                        ContestQuestionId = question.QuestionId,
                        Answer            = question.Answer
                    });

                    counter++;
                }

                if (!this.ModelState.IsValid)
                {
                    return(this.View(new ContestRegistrationViewModel(contest, registrationData, official)));
                }

                if (participant == null)
                {
                    participant = this.AddNewParticipantToContest(contest, official);
                }
                else
                {
                    participant.IsInvalidated = false;
                }

                foreach (var participantAnswer in answers)
                {
                    participant.Answers.Add(participantAnswer);
                }

                this.participantsData.Update(participant);
            }
            catch (HttpException httpEx)
            {
                this.TempData.AddDangerMessage(httpEx.Message);
                return(this.RedirectToHome());
            }

            return(this.RedirectToAction(GlobalConstants.Index, new { id = registrationData.ContestId, official }));
        }
Ejemplo n.º 12
0
        public ActionResult Register(int id, bool official, ContestRegistrationModel registrationData)
        {
            // check if the user has already registered for participation and redirect him to the correct
            // action
            var participantFound = this.Data.Participants.Any(id, this.UserProfile.Id, official);

            if (participantFound)
            {
                return(this.RedirectToAction("Index", new { id, official }));
            }

            var contest = this.Data.Contests.GetById(id);

            ValidateContest(contest, official);

            // check if the contest is official, has a password and if the user entered the correct password
            if (official && contest.HasContestPassword && !contest.ContestPassword.Equals(registrationData.Password))
            {
                this.ModelState.AddModelError("Password", "Incorrect password!");
            }

            // check if the contest is practice, has a password and if the user entered the correct password
            if (!official && contest.HasPracticePassword && !contest.PracticePassword.Equals(registrationData.Password))
            {
                this.ModelState.AddModelError("Password", "Incorrect password!");
            }

            var questionsToAnswerCount = official ?
                                         contest.Questions.Where(x => x.AskOfficialParticipants).Count() :
                                         contest.Questions.Where(x => x.AskPracticeParticipants).Count();

            if (questionsToAnswerCount != registrationData.Questions.Count())
            {
                // TODO: add an appropriate model error
                this.ModelState.AddModelError(string.Empty, string.Empty);
            }

            registrationData.Questions.Each((x, i) =>
            {
                if (string.IsNullOrEmpty(x.Answer))
                {
                    this.ModelState.AddModelError(string.Format("Questions[{0}].Answer", i), "Answer is required");
                }
            });

            if (!ModelState.IsValid)
            {
                return(this.View(new ContestRegistrationViewModel(contest, registrationData, official)));
            }

            registrationData.Questions.Each(q =>
            {
                var contestQuestion = contest.Questions.FirstOrDefault(x => x.Id == q.QuestionId);

                contestQuestion.Answers.Add(new ContestQuestionAnswer
                {
                    QuestionId = q.QuestionId,
                    Text       = q.Answer
                });
            });

            var participant = new Participant(id, this.UserProfile.Id, official);

            this.Data.Participants.Add(participant);
            this.Data.SaveChanges();

            return(this.RedirectToAction("Index", new { id, official }));
        }