public void DownloadResourceActionWhenCompetingResourceIsAvailableAndUserIsRegisteredForCompeteShouldReturnResource()
        {
            var contest = this.CreateAndSaveContest("testContest", this.ActiveContestWithPasswordOptions, this.InactiveContestOptions);
            var problem = new Problem
            {
                Name = "test problem"
            };

            var resource = new ProblemResource
            {
                File = new[]
                {
                    (byte)this.RandomGenerator.Next(0, byte.MaxValue),
                    (byte)this.RandomGenerator.Next(0, byte.MaxValue)
                },
                Name = "resourceName",
                FileExtension = "test"
            };

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

            var result = this.CompeteController.DownloadResource(resource.Id, this.IsCompete) as FileContentResult;
            var expectedFileName = string.Format("{0}_{1}.{2}", problem.Name, resource.Name, resource.FileExtension);
            Assert.AreEqual(expectedFileName, result.FileDownloadName);
            Assert.IsTrue(resource.File.SequenceEqual(result.FileContents));
        }
        public void DownloadTaskLinkShouldNavigateProperly()
        {
            var contest = new Contest { Name = "1337", };
            var problem = new Problem { Contest = contest, Name = "1337", OldId = 1337 };
            var resource = new ProblemResource { Problem = problem, Type = ProblemResourceType.ProblemDescription };
            this.EmptyOjsData.Resources.Add(resource);
            this.EmptyOjsData.SaveChanges();

            this.VerifyUrlRedirection("~/Contest/DownloadTask/1337", string.Format("/Contests/Practice/DownloadResource/{0}", resource.Id));
        }
        public void DownloadResourceActionWhenCompeteAndUserNotRegisteredForCompeteShouldRegirectToRegistrationPage()
        {
            var contest = this.CreateAndSaveContest("testContest", this.InactiveContestOptions, this.ActiveContestWithPasswordOptions);
            var problem = new Problem
            {
                Name = "test problem"
            };

            var resource = new ProblemResource
            {
                File = new byte[1],
                FileExtension = "test"
            };

            problem.Resources.Add(resource);
            contest.Problems.Add(problem);
            this.EmptyOjsData.SaveChanges();

            var result = this.CompeteController.DownloadResource(resource.Id, this.IsPractice) as RedirectToRouteResult;

            Assert.AreEqual("Register", result.RouteValues["action"]);
            Assert.AreEqual(contest.Id, result.RouteValues["id"]);
            Assert.AreEqual(this.IsPractice, result.RouteValues["official"]);
        }
        public void Copy(OjsDbContext context, TelerikContestSystemEntities oldDb)
        {
            context.Configuration.AutoDetectChangesEnabled = false;
            foreach (var task in oldDb.Tasks.OrderBy(x => x.Id).Include(x => x.Contest1).ToList())
            {
                var contest = context.Contests.FirstOrDefault(x => x.OldId == task.Contest);
                var problem = new Problem
                                  {
                                      OldId = task.Id,
                                      Contest = contest,
                                      Name = task.Name,
                                      MaximumPoints = (short)task.MaxPoints,
                                      TimeLimit = task.TimeLimit,
                                      MemoryLimit = (int)task.MemoryLimit * 2, // Multiplying memory limit by 2 because the previous worker didn't respect the memory limit
                                      OrderBy = 0,
                                      ShowResults = task.Contest1.ShowResults,
                                      CreatedOn = task.AddedOn,
                                      PreserveCreatedOn = true,
                                  };

                if (task.DescriptionLink != null || (task.Description != null && task.Description.Length != 0))
                {
                    var resource = new ProblemResource
                                              {
                                                  CreatedOn = task.AddedOn,
                                                  PreserveCreatedOn = true,
                                                  Name = string.Format("Условие на задачата"),
                                                  Type = ProblemResourceType.ProblemDescription,
                                              };

                    if (task.DescriptionLink != null)
                    {
                        if (task.Id == 368)
                        {
                            task.DescriptionLink =
                                "http://downloads.academy.telerik.com/svn/oop/Lectures/9.%20Exam%20Preparation/DocumentSystem-Skeleton.rar";
                        }

                        if (task.Id == 369)
                        {
                            task.DescriptionLink =
                                "http://downloads.academy.telerik.com/svn/oop/Lectures/9.%20Exam%20Preparation/AcademyGeometry-Skeleton.zip";
                        }

                        var web = new WebClient();
                        var data = web.DownloadData(task.DescriptionLink);
                        resource.File = data;
                        resource.FileExtension = task.DescriptionLink.GetFileExtension();
                    }
                    else
                    {
                        resource.File = task.Description;
                        resource.FileExtension = task.DescriptionFormat;
                    }

                    problem.Resources.Add(resource);
                }

                switch (task.Checker1.Name)
                {
                    case "Exact":
                        problem.Checker = context.Checkers.FirstOrDefault(x => x.Name == "Exact");
                        break;
                    case "Trim":
                        problem.Checker = context.Checkers.FirstOrDefault(x => x.Name == "Trim");
                        break;
                    case "Sort":
                        problem.Checker = context.Checkers.FirstOrDefault(x => x.Name == "Sort lines");
                        break;
                    default:
                        problem.Checker = null;
                        break;
                }

                context.Problems.Add(problem);
            }

            context.SaveChanges();
            context.Configuration.AutoDetectChangesEnabled = true;
        }
        public ActionResult Create(int id, ProblemResourceViewModel resource)
        {
            if (resource == null)
            {
                this.TempData.AddDangerMessage(Resource.Invalid_resource);
                return this.RedirectToAction("Resource", "Problems", new { id });
            }

            if (resource.Type == ProblemResourceType.Video && string.IsNullOrEmpty(resource.Link))
            {
                this.ModelState.AddModelError("Link", Resource.Link_not_empty);
            }
            else if (resource.Type != ProblemResourceType.Video && (resource.File == null || resource.File.ContentLength == 0))
            {
                this.ModelState.AddModelError("File", Resource.File_required);
            }

            if (this.ModelState.IsValid)
            {
                var problem = this.Data.Problems
                    .All()
                    .FirstOrDefault(x => x.Id == id);

                if (problem == null)
                {
                    this.TempData.AddDangerMessage(Resource.Problem_not_found);
                    return this.RedirectToAction(GlobalConstants.Index, "Problems");
                }

                var newResource = new ProblemResource
                {
                    Name = resource.Name,
                    Type = resource.Type,
                    OrderBy = resource.OrderBy,
                };

                if (resource.Type == ProblemResourceType.Video)
                {
                    newResource.Link = resource.Link;
                }
                else
                {
                    newResource.File = resource.File.InputStream.ToByteArray();
                    newResource.FileExtension = resource.FileExtension;
                }

                problem.Resources.Add(newResource);
                this.Data.SaveChanges();

                return this.RedirectToAction("Resource", "Problems", new { id });
            }

            resource.AllTypes = EnumConverter.GetSelectListItems<ProblemResourceType>();
            return this.View(resource);
        }
        public ActionResult Create(int id, ProblemResourceViewModel resource)
        {
            if (resource == null)
            {
                this.TempData[GlobalConstants.DangerMessage] = "Ресурсът е невалиден";
                return this.RedirectToAction("Resource", "Problems", new { id });
            }

            if (resource.Type == ProblemResourceType.Video && string.IsNullOrEmpty(resource.Link))
            {
                this.ModelState.AddModelError("Link", "Линкът не може да бъде празен");
            }
            else if (resource.Type != ProblemResourceType.Video && (resource.File == null || resource.File.ContentLength == 0))
            {
                this.ModelState.AddModelError("File", "Файлът е задължителен");
            }

            if (this.ModelState.IsValid)
            {
                var problem = this.Data.Problems
                    .All()
                    .FirstOrDefault(x => x.Id == id);

                if (problem == null)
                {
                    this.TempData[GlobalConstants.DangerMessage] = "Задачата не е намерена";
                    return this.RedirectToAction(GlobalConstants.Index, "Problems");
                }

                var newResource = new ProblemResource
                {
                    Name = resource.Name,
                    Type = resource.Type,
                    OrderBy = resource.OrderBy,
                };

                if (resource.Type == ProblemResourceType.Video)
                {
                    newResource.Link = resource.Link;
                }
                else
                {
                    newResource.File = resource.File.InputStream.ToByteArray();
                    newResource.FileExtension = resource.FileExtension;
                }

                problem.Resources.Add(newResource);
                this.Data.SaveChanges();

                return this.RedirectToAction("Resource", "Problems", new { id });
            }

            resource.AllTypes = EnumConverter.GetSelectListItems<ProblemResourceType>();
            return this.View(resource);
        }
        public void DownloadResourceActionWhenPracticingWhenNoFileExtensionAndUserIsRegisteredForContestShouldThrowAnException()
        {
            var contest = this.CreateAndSaveContest("testContest", this.InactiveContestOptions, this.ActiveContestWithPasswordOptions);
            var problem = new Problem
            {
                Name = "test problem"
            };

            var resource = new ProblemResource
            {
                File = new[]
                {
                    (byte)this.RandomGenerator.Next(0, byte.MaxValue),
                    (byte)this.RandomGenerator.Next(0, byte.MaxValue)
                }
            };

            problem.Resources.Add(resource);
            contest.Problems.Add(problem);
            contest.Participants.Add(new Participant(contest.Id, this.FakeUserProfile.Id, this.IsPractice));
            this.EmptyOjsData.SaveChanges();

            try
            {
                var result = this.CompeteController.DownloadResource(resource.Id, this.IsPractice) as FileContentResult;
                Assert.Fail("Expected an exception when trying to download a resource without a file extension.");
            }
            catch (HttpException ex)
            {
                Assert.AreEqual((int)HttpStatusCode.Forbidden, ex.GetHttpCode());
            }
        }
        public void DownloadResourceActionWhenPracticingButPracticeNotAllowedShouldThrowException()
        {
            var contest = this.CreateAndSaveContest("testContest", this.InactiveContestOptions, this.InactiveContestOptions);
            var problem = new Problem
            {
                Name = "test problem"
            };

            var resource = new ProblemResource
            {
                File = new byte[1],
                FileExtension = "test"
            };

            problem.Resources.Add(resource);
            contest.Problems.Add(problem);
            this.EmptyOjsData.SaveChanges();

            try
            {
                var result = this.CompeteController.DownloadResource(resource.Id, this.IsPractice) as RedirectToRouteResult;
                Assert.Fail("Expected an exception when a user is trying to download a resource for a contest that cannot be practiced.");
            }
            catch (HttpException ex)
            {
                Assert.AreEqual((int)HttpStatusCode.Forbidden, ex.GetHttpCode());
            }
        }