public async void Index_ReturnsMatchingModel(int futureEventCount, int pastEventCount)
        {
            DateTime futureDate = DateTime.Now.AddDays(1);
            DateTime pastDate = DateTime.Now.AddDays(-1);

            List<Event> events = new List<Event>();

            for (int i = 0; i < futureEventCount; i++)
            {
                events.Add(new Event()
                {
                    Date = futureDate
                });
            }

            for (int i = 0; i < futureEventCount; i++)
            {
                events.Add(new Event()
                {
                    Date = pastDate
                });
            }

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub = TestHelpers.GetFakeAsyncDbSet(events.AsQueryable());
            dbStub.Setup(db => db.Events).Returns(eventDbSetStub.Object);

            EventsController controller = new EventsController(dbStub.Object, idGetter: null);

            var result = await controller.Index() as ViewResult;

            Assert.That(result != null);
            Assert.That(result.Model, Is.InstanceOf<EventListViewModel>());

            var model = (EventListViewModel)result.Model;

            Assert.That(model.Events, Has.Count.EqualTo(futureEventCount));
        }
        public async void MyEvents_ReturnsMatchingModel()
        {
            DateTime futureDate = DateTime.Now.AddDays(1);

            Member member = new Member
            {
                UserId = UserId,
                RegisteredEvents = new List<Event>
                {  
                    new Event
                    {
                        Date = futureDate
                    },
                    new Event
                    {
                        Date = futureDate
                    }
                }
            };

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();

            Mock<DbSet<Member>> memberDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Member> { member }.AsQueryable());
            memberDbSetStub.Setup(db => db.FindAsync(member.UserId)).ReturnsAsync(member);
            dbStub.Setup(db => db.Members).Returns(memberDbSetStub.Object);

            Mock<ControllerContext> context = new Mock<ControllerContext>();
            context.Setup(c => c.HttpContext.User.Identity).Returns<IIdentity>(null);

            Mock<IGuidUserIdGetter> idGetterStub = new Mock<IGuidUserIdGetter>();
            idGetterStub.Setup(id => id.GetUserId(It.IsAny<IIdentity>())).Returns(member.UserId);

            EventsController controller = new EventsController(dbStub.Object, idGetterStub.Object)
            {
                ControllerContext = context.Object
            };

            var result = await controller.MyEvents() as ViewResult;

            Assert.That(result != null);
            Assert.That(result.Model, Is.InstanceOf<EventListViewModel>());

            var model = (EventListViewModel)result.Model;

            Assert.That(model.Events.Count(), Is.EqualTo(2));
        }
        public async void DeleteConfirmed_EmptyGuid_RedirectsToIndex()
        {
            EventsController controller = new EventsController(veilDataAccess: null, idGetter: null);

            var result = await controller.DeleteConfirmed(Guid.Empty) as RedirectToRouteResult;

            Assert.That(result != null);
            Assert.That(result.RouteValues["Action"], Is.EqualTo("Index"));
        }
        public async void Delete_IdInDb_ReturnsMatchedEventAsModel()
        {
            Event item = new Event
            {
                Id = Id
            };

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Event> { item }.AsQueryable());
            eventDbSetStub.Setup(edb => edb.FindAsync(item.Id)).ReturnsAsync(item);

            dbStub.Setup(db => db.Events).Returns(eventDbSetStub.Object);

            EventsController controller = new EventsController(dbStub.Object, idGetter: null);

            var result = await controller.Delete(item.Id) as ViewResult;

            Assert.That(result != null);
            Assert.That(result.Model, Is.InstanceOf<Event>());

            var model = (Event) result.Model;

            Assert.That(model.Id, Is.EqualTo(item.Id));
        }
        public void Delete_NullId_Throws404Exception()
        {
            EventsController controller = new EventsController(veilDataAccess: null, idGetter: null);

            Assert.That(async () => await controller.Delete(null), Throws.InstanceOf<HttpException>().And.Matches<HttpException>(ex => ex.GetHttpCode() == 404));
        }
        public async void Unregister_IdInDb_ReturnsMatchedEventAsModel()
        {
            Event eventItem = new Event
            {
                Id = Id,
                RegisteredMembers = new List<Member>()
            };

            Member member = new Member
            {
                UserId = UserId,
                RegisteredEvents = new List<Event>()
            };

            eventItem.RegisteredMembers.Add(member);
            member.RegisteredEvents.Add(eventItem);

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();

            Mock<DbSet<Event>> eventDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Event> { eventItem }.AsQueryable());
            eventDbSetStub.Setup(db => db.FindAsync(eventItem.Id)).ReturnsAsync(eventItem);
            dbStub.Setup(db => db.Events).Returns(eventDbSetStub.Object);

            Mock<DbSet<Member>> memberDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Member> { member }.AsQueryable());
            memberDbSetStub.Setup(db => db.FindAsync(member.UserId)).ReturnsAsync(member);
            dbStub.Setup(db => db.Members).Returns(memberDbSetStub.Object);

            Mock<ControllerContext> context = new Mock<ControllerContext>();
            context.Setup(c => c.HttpContext.User.Identity).Returns<IIdentity>(null);

            Mock<IGuidUserIdGetter> idGetterStub = new Mock<IGuidUserIdGetter>();
            idGetterStub.Setup(id => id.GetUserId(It.IsAny<IIdentity>())).Returns(member.UserId);

            EventsController controller = new EventsController(dbStub.Object, idGetterStub.Object)
            {
                ControllerContext = context.Object
            };

            var result = await controller.Unregister(eventItem.Id) as ViewResult;

            Assert.That(result != null);
            Assert.That(result.Model, Is.InstanceOf<EventDetailsViewModel>());

            var model = (EventDetailsViewModel)result.Model;

            Assert.That(model.Event.Id, Is.EqualTo(eventItem.Id));
        }
        public async void EditPOST_UpdateSuccess_RedirectToDetailsForTheItem()
        {
            Event item = new Event
            {
                Id = Id,
            };

            EventViewModel viewModel = new EventViewModel
            {
                Id = item.Id,
            };

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Event>().AsQueryable());
            eventDbSetStub.Setup(edb => edb.FindAsync(viewModel.Id)).ReturnsAsync(item);

            dbStub.Setup(db => db.Events).Returns(eventDbSetStub.Object);

            EventsController controller = new EventsController(dbStub.Object, idGetter: null);

            var result = await controller.Edit(viewModel) as RedirectToRouteResult;

            Assert.That(result != null);
            Assert.That(result.RouteValues["Id"], Is.EqualTo(item.Id));
            Assert.That(result.RouteValues["Action"], Is.EqualTo("Details"));
        }
        public void CreateGET()
        {
            EventsController controller = new EventsController(veilDataAccess: null, idGetter: null);

            Assert.That(controller.Create() != null);
        }
        public async void EditPOST_IdInDb_UpdatesQueriedItemToMatchEditedViewModel()
        {
            Event item = new Event
            {
                Id = Id,
                Name = "An Event",
                Description = "A description",
                Date = new DateTime(635826779187565538L, DateTimeKind.Local),
                Duration = "A duration"
            };

            EventViewModel viewModel = new EventViewModel
            {
                Id = item.Id,
                Name = "Edit Name",
                Description = "Edit Description",
                Time = new DateTime(635826926597335093L, DateTimeKind.Local),
                Date = new DateTime(2015, 11, 9),
                Duration = "Edit Duration"
            };

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Event>().AsQueryable());
            eventDbSetStub.Setup(edb => edb.FindAsync(viewModel.Id)).ReturnsAsync(item);

            dbStub.Setup(db => db.Events).Returns(eventDbSetStub.Object);

            EventsController controller = new EventsController(dbStub.Object, idGetter: null);

            await controller.Edit(viewModel);

            Assert.That(item.Id, Is.EqualTo(viewModel.Id));
            Assert.That(item.Name, Is.EqualTo(viewModel.Name));
            Assert.That(item.Description, Is.EqualTo(viewModel.Description));
            Assert.That(item.Date, Is.EqualTo(viewModel.DateTime));
            Assert.That(item.Duration, Is.EqualTo(viewModel.Duration));
        }
Ejemplo n.º 10
0
        public async void EditPOST_InvalidModelState_RedisplaysViewWithSameViewModel()
        {
            EventViewModel item = new EventViewModel
            {
                Id = Id
            };

            EventsController controller = new EventsController(veilDataAccess: null, idGetter: null);
            controller.ModelState.AddModelError("Name", "Name is required");

            var result = await controller.Edit(item) as ViewResult;

            Assert.That(result != null);
            Assert.That(result.Model, Is.InstanceOf<EventViewModel>());
            Assert.That(result.Model, Is.EqualTo(item));
        }
Ejemplo n.º 11
0
        public void EditPOST_IdNotInDb_Throws404Exception()
        {
            EventViewModel viewModel = new EventViewModel
            {
                Id = Id
            };

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Event>().AsQueryable());
            eventDbSetStub.Setup(edb => edb.FindAsync(Id)).ReturnsAsync(null);

            dbStub.Setup(db => db.Events).Returns(eventDbSetStub.Object);

            EventsController controller = new EventsController(dbStub.Object, idGetter: null);

            Assert.That(async () => await controller.Edit(viewModel), Throws.InstanceOf<HttpException>().And.Matches<HttpException>(ex => ex.GetHttpCode() == 404));
        }
Ejemplo n.º 12
0
        public async void EditGET_IdInDb_ReturnsViewModelWithMatchedEventDetails()
        {
            Event item = new Event
            {
                Id = Id,
                Date = new DateTime(635826779187565538L, DateTimeKind.Local),
                Description = "A Description",
                Duration = "Some duration",
                Name = "An event"
            };

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub =
                TestHelpers.GetFakeAsyncDbSet(new List<Event> { item }.AsQueryable());

            eventDbSetStub.Setup(edb => edb.FindAsync(item.Id)).ReturnsAsync(item);

            dbStub.Setup(db => db.Events).Returns(eventDbSetStub.Object);

            EventsController controller = new EventsController(dbStub.Object, idGetter: null);

            var result = await controller.Edit(item.Id) as ViewResult;

            Assert.That(result != null);
            Assert.That(result.Model, Is.InstanceOf<EventViewModel>());

            var model = (EventViewModel) result.Model;

            Assert.That(model.Id, Is.EqualTo(item.Id));
            Assert.That(model.Date, Is.EqualTo(item.Date.Date));
            Assert.That(model.Time.TimeOfDay, Is.EqualTo(item.Date.TimeOfDay));
            Assert.That(model.Description, Is.EqualTo(item.Description));
            Assert.That(model.Duration, Is.EqualTo(item.Duration));
            Assert.That(model.Name, Is.EqualTo(item.Name));
        }
Ejemplo n.º 13
0
        public async void CreatePOST_DbSavedChagnesCalled_ConfirmsSavedChanges()
        {
            EventViewModel viewModel = new EventViewModel
            {
                Name = "New Name",
                Description = "New Description",
                Time = new DateTime(635827525788997554L, DateTimeKind.Local),
                Date = new DateTime(2015, 11, 10),
                Duration = "New Duration"
            };

            Mock<IVeilDataAccess> dbMock = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Event>().AsQueryable());
            eventDbSetStub.Setup(edb => edb.Add(viewModel));

            dbMock.Setup(db => db.Events).Returns(eventDbSetStub.Object);
            dbMock.Setup(db => db.SaveChangesAsync()).ReturnsAsync(1).Verifiable();

            EventsController controller = new EventsController(dbMock.Object, idGetter: null);

            await controller.Create(viewModel);

            Assert.That(
                () =>
                    dbMock.Verify(db => db.SaveChangesAsync(),
                    Times.Exactly(1)),
                Throws.Nothing);
        }
Ejemplo n.º 14
0
        public async void CreatePOST_EventCreatedWithCorrectData_ItemFromDbMatchesSubmittedViewModel()
        {
            Event addedEvent = null;

            EventViewModel viewModel = new EventViewModel
            {
                Name = "New Name",
                Description = "New Description",
                Time = new DateTime(635827525788997554L, DateTimeKind.Local),
                Date = new DateTime(2015, 11, 10),
                Duration = "New Duration"
            };

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();

            dbStub.Setup(db => db.Events.Add(It.IsAny<Event>())).Callback<Event>((model) =>
            {
                addedEvent = model;
            });

            EventsController controller = new EventsController(dbStub.Object, idGetter: null);

            await controller.Create(viewModel);

            Assert.That(addedEvent != null);
            Assert.That(addedEvent.Name, Is.EqualTo(viewModel.Name));
            Assert.That(addedEvent.Description, Is.EqualTo(viewModel.Description));
            Assert.That(addedEvent.Date, Is.EqualTo(viewModel.DateTime));
            Assert.That(addedEvent.Duration, Is.EqualTo(viewModel.Duration));
        }
Ejemplo n.º 15
0
        public void DeleteConfirmed_IdNotInDb_Throws404Exception()
        {
            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Event>().AsQueryable());

            dbStub.Setup(db => db.Events).Returns(eventDbSetStub.Object);

            EventsController controller = new EventsController(dbStub.Object, idGetter: null);

            Assert.That(async () => await controller.DeleteConfirmed(Id), Throws.InstanceOf<HttpException>().And.Matches<HttpException>(ex => ex.GetHttpCode() == 404));
        }
Ejemplo n.º 16
0
        public async void EditPOST_IdInDb_CallsSaveChangesAsync()
        {
            Event item = new Event
            {
                Id = Id,
            };

            EventViewModel viewModel = new EventViewModel
            {
                Id = item.Id,
            };

            Mock<IVeilDataAccess> dbMock = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Event>().AsQueryable());
            eventDbSetStub.Setup(edb => edb.FindAsync(viewModel.Id)).ReturnsAsync(item);

            dbMock.Setup(db => db.Events).Returns(eventDbSetStub.Object);
            dbMock.Setup(db => db.SaveChangesAsync()).ReturnsAsync(1).Verifiable();

            EventsController controller = new EventsController(dbMock.Object, idGetter: null);

            await controller.Edit(viewModel);

            Assert.That(
                () =>
                    dbMock.Verify(db => db.SaveChangesAsync(),
                    Times.Exactly(1)),
                Throws.Nothing);
        }
Ejemplo n.º 17
0
        public async void DeleteConfirmed_IdInDb_CallsSaveChangesOnTheDb()
        {
            Event item = new Event
            {
                Id = Id
            };

            Mock<IVeilDataAccess> dbMock = TestHelpers.GetVeilDataAccessFake();
            Mock<DbSet<Event>> eventDbSetStub =
                TestHelpers.GetFakeAsyncDbSet(new List<Event> { item }.AsQueryable());
            eventDbSetStub.Setup(edb => edb.FindAsync(item.Id)).ReturnsAsync(item);
            eventDbSetStub.Setup(edb => edb.Remove(item)).Returns(item);

            dbMock.Setup(db => db.Events).Returns(eventDbSetStub.Object);
            dbMock.Setup(db => db.SaveChangesAsync()).ReturnsAsync(1).Verifiable();

            EventsController controller = new EventsController(dbMock.Object, idGetter: null);

            await controller.DeleteConfirmed(item.Id);

            Assert.That(
                () =>
                    dbMock.Verify(db => db.SaveChangesAsync(),
                    Times.Exactly(1)),
                Throws.Nothing);
        }
Ejemplo n.º 18
0
        public void RenderEventListItem_MemberNotRegistered_ReturnsMatchingModel()
        {
            DateTime futureDate = DateTime.Now.AddDays(1);

            Event eventItem = new Event
            {
                Id = Id,
                Date = futureDate
            };

            Member member = new Member
            {
                UserId = UserId,
                RegisteredEvents = new List<Event>()
            };

            Mock<IVeilDataAccess> dbStub = TestHelpers.GetVeilDataAccessFake();

            Mock<DbSet<Member>> memberDbSetStub = TestHelpers.GetFakeAsyncDbSet(new List<Member> { member }.AsQueryable());
            memberDbSetStub.Setup(db => db.Find(member.UserId)).Returns(member);
            dbStub.Setup(db => db.Members).Returns(memberDbSetStub.Object);

            Mock<ControllerContext> context = new Mock<ControllerContext>();
            context.Setup(c => c.HttpContext.User.Identity).Returns<IIdentity>(null);

            Mock<IGuidUserIdGetter> idGetterStub = new Mock<IGuidUserIdGetter>();
            idGetterStub.Setup(id => id.GetUserId(It.IsAny<IIdentity>())).Returns(member.UserId);

            EventsController controller = new EventsController(dbStub.Object, idGetterStub.Object)
            {
                ControllerContext = context.Object
            };

            var result = controller.RenderEventListItem(eventItem, false) as PartialViewResult;

            Assert.That(result != null);
            Assert.That(result.Model, Is.InstanceOf<EventListItemViewModel>());

            var model = (EventListItemViewModel)result.Model;

            Assert.That(model.Event.Id, Is.EqualTo(eventItem.Id));
        }