public void Add_ValidModelLicenseTypeWithResultPost_ReturnsRedirect()
        {
            var computerFake = new Computer
                                   {
                                       LicenseType = "WinXP",
                                   };

            var computerDataService = new Mock<IDataService<Computer>>();
            computerDataService.Setup(c => c.Insert(It.IsAny<Computer>(), It.IsAny<Guid>())).Verifiable();

            var windowsLicenseDataServiceMock = new Mock<IDataService<WindowsLicense>>();
            windowsLicenseDataServiceMock.Setup(w => w.SelectOne(It.IsAny<Expression<Func<WindowsLicense, bool>>>()))
                .Returns(new WindowsLicense
                             {
                                 Id = "123456",
                                 LicenseType = computerFake.LicenseType,
                             });

            var controller = new ComputerController(computerDataService.Object,
                                                    new Mock<IFormsAuthenticationService>().Object,
                                                    windowsLicenseDataServiceMock.Object,
                                                    new Mock<IDataService<Volunteer>>().Object,
                                                    new Mock<IDataService<Recipient>>().Object);

            ActionResult result = controller.Add(computerFake);

            Assert.IsInstanceOf<RedirectToRouteResult>(result);
            computerDataService.VerifyAll();
        }
Пример #2
0
        public ActionResult Add(Computer model)
        {
            // Only Windows machines require a license.
            if (!string.IsNullOrEmpty(model.LicenseType))
            {
                WindowsLicense license =
                    windowsLicenseDataService.SelectOne(w => !w.Installed && w.LicenseType == model.LicenseType);
                if (license == null)
                {
                    ModelState.AddModelError("LicenseType",
                                             string.Format("Sorry, we are out of {0} licenses.", model.LicenseType));
                    return View(model);
                }
                model.WindowsLicense = license.Id;
            }

            Volunteer volunteer =
                volunteerDataService.SelectOne(v => v.Id == formsAuthenticationService.GetVolunteerID(User));

            model.Active = true;
            model.ComputerStatus = "Build";
            model.CreateByVolunteer = volunteer;
            model.CreateDate = DateTime.Now;
            model.Id = Guid.NewGuid();
            model.LifebyteNumber = computerDataService.NextLbNumber();
            model.LastModByVolunteer = volunteer;
            model.LastModDate = DateTime.Now;

            computerDataService.Insert(model, model.Id);

            return RedirectToAction("Edit", "Computer", new {id = model.Id});
        }
        public void Manifest_ReturnsView()
        {
            var fakeComputer = new Computer
                                   {
                                       Id = Guid.NewGuid(),
                                       ManifestHtml =
                                           @"
                                                <html>
                                                    <head><title>LB0124</title></head>
                                                    <body>
                                                        <p>Hello World LB0124</p>
                                                    </body>
                                                </html>"
                                   };

            var computerDataService = new Mock<IDataService<Computer>>();

            computerDataService.Setup(c => c.SelectOne(It.IsAny<Expression<Func<Computer, bool>>>()))
                .Returns(fakeComputer);

            var controller = new ComputerController(computerDataService.Object,
                                                    new Mock<IFormsAuthenticationService>().Object,
                                                    new Mock<IDataService<WindowsLicense>>().Object,
                                                    new Mock<IDataService<Volunteer>>().Object,
                                                    new Mock<IDataService<Recipient>>().Object);

            ActionResult result = controller.Manifest(fakeComputer.Id);

            Assert.IsInstanceOf<ViewResult>(result);
        }
        public void Edit_ValidModel_ReturnsRedirect()
        {
            var computerFake = new Computer
                                   {
                                       WindowsLicense = "1234",
                                   };

            var computerDataServiceMock = new Mock<IDataService<Computer>>();
            computerDataServiceMock.Setup(c => c.Update(It.IsAny<Computer>())).Verifiable();
            computerDataServiceMock.Setup(c => c.SelectOne(It.IsAny<Expression<Func<Computer, bool>>>()))
                .Returns(computerFake);

            var formsAuthenticationServiceMock = new Mock<IFormsAuthenticationService>();
            formsAuthenticationServiceMock.Setup(f => f.GetVolunteerID(null))
                .Returns(Guid.NewGuid());

            var volunterDataServiceMock = new Mock<IDataService<Volunteer>>();
            volunterDataServiceMock.Setup(v => v.SelectOne(It.IsAny<Expression<Func<Volunteer, bool>>>()))
                .Returns(new Volunteer());

            var windowsLicenseFake = new WindowsLicense();
            var windowsLicenseDataServiceMock = new Mock<IDataService<WindowsLicense>>();
            windowsLicenseDataServiceMock.Setup(w => w.SelectOne(It.IsAny<Expression<Func<WindowsLicense, bool>>>()))
                .Returns(windowsLicenseFake);
            windowsLicenseDataServiceMock.Setup(w => w.Update(windowsLicenseFake)).Verifiable();

            var controller = new ComputerController(computerDataServiceMock.Object,
                                                    formsAuthenticationServiceMock.Object,
                                                    windowsLicenseDataServiceMock.Object,
                                                    volunterDataServiceMock.Object,
                                                    new Mock<IDataService<Recipient>>().Object);

            ActionResult result = controller.Edit(computerFake);

            Assert.IsInstanceOf<RedirectToRouteResult>(result);
            computerDataServiceMock.VerifyAll();
            windowsLicenseDataServiceMock.VerifyAll();
        }
Пример #5
0
        public ActionResult Edit(Computer model)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }

            Volunteer volunteer =
                volunteerDataService.SelectOne(v => v.Id == formsAuthenticationService.GetVolunteerID(User));

            Computer originalComputer = computerDataService.SelectOne(c => c.Id == model.Id);

            model.Active = originalComputer.Active;
            model.CreateByVolunteer = originalComputer.CreateByVolunteer;
            model.CreateDate = originalComputer.CreateDate;
            model.LastModByVolunteer = volunteer;
            model.LastModDate = DateTime.Now;

            // The request is null if this action is being tested.
            // Hanselman's post in the URL above has a good way of mocking the request when we get time to work on it.
            if (Request != null && Request.Files.Count == 1)
            {
                HttpPostedFileBase manifest = Request.Files[0];
                if (manifest != null
                    && manifest.ContentLength > 0
                    && manifest.ContentType == "text/html")
                {
                    var manifestContent = new StringBuilder();
                    Stream manifestStream = manifest.InputStream;
                    var manifestReader = new StreamReader(manifestStream);
                    string line;
                    while ((line = manifestReader.ReadLine()) != null)
                    {
                        manifestContent.Append(line);
                    }

                    model.ManifestHtml = manifestContent.ToString();
                }
            }

            // You cannot update the license or the LB number.
            model.WindowsLicense = originalComputer.WindowsLicense;
            model.LicenseType = originalComputer.LicenseType;
            model.LifebyteNumber = originalComputer.LifebyteNumber;

            computerDataService.Update(model);

            WindowsLicense windowsLicense = windowsLicenseDataService.SelectOne(w => w.Id == model.WindowsLicense);
            windowsLicense.Installed = true;

            windowsLicenseDataService.Update(windowsLicense);

            return RedirectToAction("Index");
        }