public void IndexFilteredThenApprove()
        {
            var mockApproveUser = University.GetUser("o101");

            ApproveController controller = new ApproveController();

            controller.ControllerContext = new ControllerContext(MockAuthContext(mockApproveUser).Object, new RouteData(), controller);

            //get list
            ViewResult result = controller.Index("", "", "Test Student11", 1) as ViewResult;

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result.Model, typeof(PagedList <StickerApplication>));

            PagedList <StickerApplication> model = result.Model as PagedList <StickerApplication>;

            Assert.AreEqual(0, model.Where(a => a.Status != StickerApplicationStatus.WaitingForApproval).Count());
            Assert.AreEqual(1, model.Count);

            //approve
            StickerApplication       stickerApplication = model.FirstOrDefault();
            List <ApprovementOption> approvementOptions = stickerApplication.GetApprovementOptions();

            Assert.AreEqual(2, approvementOptions.Count);
            ApprovementOption otherOption = approvementOptions.Where(a => a.QuotaID != stickerApplication.Quota.ID).FirstOrDefault();

            controller.Approve(stickerApplication.ID, otherOption.QuotaID);

            //get list
            result = controller.Index("", "", "Test Student11", 1) as ViewResult;
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result.Model, typeof(PagedList <StickerApplication>));
            model = result.Model as PagedList <StickerApplication>;
            Assert.AreEqual(0, model.Count);

            //applications index page for user
            StickerController scontroller = new StickerController
            {
                ControllerContext = new ControllerContext(MockAuthContext(University.GetUser(stickerApplication.User.UID)).Object, new RouteData(), controller)
            };
            ViewResult sresult = scontroller.Index() as ViewResult;

            Assert.IsNotNull(sresult);
            Assert.IsInstanceOfType(sresult.Model, typeof(List <StickerApplication>));

            List <StickerApplication> smodel = sresult.Model as List <StickerApplication>;

            Assert.AreNotEqual(0, smodel.Count);
            Assert.AreEqual(StickerApplicationStatus.WaitingForPayment, smodel.FirstOrDefault().Status);
        }
Exemple #2
0
        public ActionResult Approve(int Id, int QuotaID)
        {
            Trace.WriteLine("GET /Approve/Approve");
            using (DatabaseContext db = GetNewDBContext())
            {
                var application = db.StickerApplications
                                  .Include(a => a.User.Division)
                                  .Include(a => a.User.Category)
                                  .Include(a => a.Vehicle)
                                  .Where(a => a.ID == Id)
                                  .FirstOrDefault();

                if (application == null)
                {
                    throw new HttpAntiForgeryException("Application not found");
                }

                if (application.User.Division.ID != ((METUPrincipal)User).User.Division.ID)
                {
                    throw new HttpAntiForgeryException();
                }

                ApprovementOption approvementOption = application.GetApprovementOptions().FirstOrDefault(a => a.QuotaID == QuotaID);
                if (approvementOption == null)
                {
                    throw new HttpAntiForgeryException();
                }

                Quota quota = University.GetQuota(approvementOption.QuotaID);
                if (quota.RemainingQuota == 0)
                {
                    return(RedirectToAction("Index"));
                }
                db.Quotas.Attach(quota);
                application.Status      = StickerApplicationStatus.WaitingForPayment;
                application.ApproveDate = DateTime.Now;
                application.Quota       = quota;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
        }