public void SendsGetActivitiesWithUnlockedCampaignsQuery()
        {
            var mediator = new Mock<IMediator>();
            var sut = new ActivityApiController(mediator.Object);
            sut.Get();

            mediator.Verify(x => x.Send(It.IsAny<ActivitiesWithUnlockedCampaignsQuery>()), Times.Once);
        }
        public void GetByIdReturnsCorrectViewModel()
        {
            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<ActivityByActivityIdQuery>())).Returns(new Activity { Campaign = new Campaign { ManagingOrganization = new Organization() }});
            var sut = new ActivityApiController(mediator.Object);
            var result = sut.Get(It.IsAny<int>());

            Assert.IsType<ActivityViewModel>(result);
        }
        public void GetReturnsCorrectModel()
        {
            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<ActivitiesWithUnlockedCampaignsQuery>())).Returns(new List<ActivityViewModel>());

            var sut = new ActivityApiController(mediator.Object);
            var results = sut.Get();

            Assert.IsType<List<ActivityViewModel>>(results);
        }
        public void GetByIdSendsActivityByActivityIdQueryWithCorrectData()
        {
            const int activityId = 1;

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<ActivityByActivityIdQuery>())).Returns(new Activity { Campaign = new Campaign { ManagingOrganization = new Organization() }});
            var sut = new ActivityApiController(mediator.Object);

            sut.Get(activityId);

            mediator.Verify(x => x.Send(It.Is<ActivityByActivityIdQuery>(y => y.ActivityId == activityId)));
        }
        public void GetAllActivities()
        {
            // Arrange
            var AllReadyContext = _serviceProvider.GetService<AllReadyContext>();
            var controller = new ActivityApiController(AllReadyContext, null, null);
            PopulateData(AllReadyContext);

            // Act
            var result = new List<ActivityViewModel>(controller.Get().AsEnumerable());

            // Assert
            var viewResult = Assert.IsType<List<ActivityViewModel>>(result);
            Assert.Equal(result.Count(), 10);
        }
        public void GetSingleActivity()
        {
            // Arrange
            var AllReadyContext = _serviceProvider.GetService<AllReadyContext>();
            var controller = new ActivityApiController(AllReadyContext, null, null);
            PopulateData(AllReadyContext);

            // Act
            var result = controller.Get(5);

            // Assert
            var viewResult = Assert.IsType<ActivityViewModel>(result);
            Assert.Equal(viewResult.Id, 5);
            Assert.Equal(viewResult.CampaignName, "Campaign 4");
            Assert.Equal(viewResult.CampaignId, 4);
            Assert.Equal(viewResult.Description, "ActivityViewModel.cs: Needs a Description");
            Assert.Equal(viewResult.EndDateTime, DateTime.MaxValue.ToUniversalTime());
            Assert.Equal(viewResult.StartDateTime, DateTime.MinValue.ToUniversalTime());
        }
 public void ControllerHasProducesAtttributeWithTheCorrectContentType()
 {
     var sut = new ActivityApiController(null);
     var attribute = sut.GetAttributes().OfType<ProducesAttribute>().SingleOrDefault();
     Assert.NotNull(attribute);
     Assert.Equal(attribute.ContentTypes.Select(x => x.MediaType).First(), "application/json");
 }
 public void UnregisterActivityHasAuthorizeAttribute()
 {
     var sut = new ActivityApiController(null);
     var attribute = (AuthorizeAttribute)sut.GetAttributesOn(x => x.UnregisterActivity(It.IsAny<int>())).SingleOrDefault(x => x.GetType() == typeof(AuthorizeAttribute));
     Assert.NotNull(attribute);
 }
 public void ControllerHasRouteAtttributeWithTheCorrectRoute()
 {
     var sut = new ActivityApiController(null);
     var attribute = sut.GetAttributes().OfType<RouteAttribute>().SingleOrDefault();
     Assert.NotNull(attribute);
     Assert.Equal(attribute.Template, "api/activity");
 }
 public async Task RegisterActivityReturnsHttpBadRequetWhenSignupModelIsNull()
 {
     var sut = new ActivityApiController(null);
     var result = await sut.RegisterActivity(null);
     Assert.IsType<BadRequestResult>(result);
 }
 public void UnregisterActivityHasHttpDeleteAttributeWithCorrectTemplate()
 {
     var sut = new ActivityApiController(null);
     var attribute = (HttpDeleteAttribute)sut.GetAttributesOn(x => x.UnregisterActivity(It.IsAny<int>())).SingleOrDefault(x => x.GetType() == typeof(HttpDeleteAttribute));
     Assert.NotNull(attribute);
     Assert.Equal(attribute.Template, "{id}/signup");
 }
 public void GetByIdHasHttpGetAttributeWithCorrectTemplate()
 {
     var sut = new ActivityApiController(null);
     var attribute = sut.GetAttributesOn(x => x.Get(It.IsAny<int>())).OfType<HttpGetAttribute>().SingleOrDefault();
     Assert.NotNull(attribute);
     Assert.Equal(attribute.Template, "{id}");
 }
        public async Task PutCheckinSendsActivityByActivityIdQueryWithCorrectActivityId()
        {
            const int activityId = 1;

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<ActivityByActivityIdQuery>())).Returns(new Activity());

            var sut = new ActivityApiController(mediator.Object);
            await sut.PutCheckin(activityId);

            mediator.Verify(x => x.Send(It.Is<ActivityByActivityIdQuery>(y => y.ActivityId == activityId)), Times.Once);
        }
        public async Task UnregisterActivityReturnsHttpNotFoundWhenUnableToGetActivitySignupByActivitySignupIdAndUserId()
        {
            var controller = new ActivityApiController(Mock.Of<IMediator>());
            controller.SetDefaultHttpContext();

            var result = await controller.UnregisterActivity(It.IsAny<int>());
            Assert.IsType<HttpNotFoundResult>(result);
        }
 private ActivityApiController GetActivityController()
 {
     var allReadyContext = _serviceProvider.GetService<AllReadyContext>();
     var allReadyDataAccess = new AllReadyDataAccessEF7(allReadyContext);
     var controller = new ActivityApiController(allReadyDataAccess, null);
     PopulateData(allReadyContext);
     return controller;
 }
 public void RegisterActivityHasValidateAntiForgeryTokenAttribute()
 {
     var sut = new ActivityApiController(null);
     var attribute = (ValidateAntiForgeryTokenAttribute)sut.GetAttributesOn(x => x.RegisterActivity(It.IsAny<ActivitySignupViewModel>())).SingleOrDefault(x => x.GetType() == typeof(ValidateAntiForgeryTokenAttribute));
     Assert.NotNull(attribute);
 }
 public void RegisterActivityHasHttpPostAttributeWithCorrectTemplate()
 {
     var sut = new ActivityApiController(null);
     var attribute = (HttpPostAttribute)sut.GetAttributesOn(x => x.RegisterActivity(It.IsAny<ActivitySignupViewModel>())).SingleOrDefault(x => x.GetType() == typeof(HttpPostAttribute));
     Assert.NotNull(attribute);
     Assert.Equal(attribute.Template, "signup");
 }
        public async Task RegisterActivityReturnsHttpStatusResultOfOk()
        {
            var sut = new ActivityApiController(Mock.Of<IMediator>());
            var result = (HttpStatusCodeResult)await sut.RegisterActivity(new ActivitySignupViewModel());

            Assert.IsType<HttpStatusCodeResult>(result);
            Assert.Equal(result.StatusCode, (int)HttpStatusCode.OK);
        }
        public async Task RegisterActivitySendsActivitySignupCommandAsyncWithCorrectData()
        {
            var model = new ActivitySignupViewModel();
            var mediator = new Mock<IMediator>();

            var sut = new ActivityApiController(mediator.Object);
            await sut.RegisterActivity(model);

            mediator.Verify(x => x.SendAsync(It.Is<ActivitySignupCommand>(command => command.ActivitySignup.Equals(model))));
        }
        public async Task RegisterActivityReturnsCorrectJsonWhenModelStateIsNotValid()
        {
            const string modelStateErrorMessage = "modelStateErrorMessage";

            var sut = new ActivityApiController(null);
            sut.AddModelStateError(modelStateErrorMessage);

            var jsonResult = (JsonResult)await sut.RegisterActivity(new ActivitySignupViewModel());
            var result = jsonResult.GetValueForProperty<List<string>>("errors");

            Assert.IsType<JsonResult>(jsonResult);
            Assert.IsType<List<string>>(result);
            Assert.Equal(result.First(), modelStateErrorMessage);
        }
        public async Task PutCheckinReturnsCorrectJsonWhenUsersSignedUpIsNullAndCheckinDateTimeIsNotNull()
        {
            const string userId = "userId";
            var activity = new Activity { Name = "ActivityName", Description = "ActivityDescription" };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<ActivityByActivityIdQuery>())).Returns(activity);

            var sut = new ActivityApiController(mediator.Object)
                .SetFakeUser(userId);

            var expected = $"{{ NeedsSignup = True, Activity = {{ Name = {activity.Name}, Description = {activity.Description} }} }}";

            var result = (JsonResult)await sut.PutCheckin(It.IsAny<int>());

            Assert.IsType<JsonResult>(result);
            Assert.Equal(expected, result.Value.ToString());
        }
        public async Task PutCheckinReturnsHttpNotFoundWhenUnableToFindActivityByActivityId()
        {
            var sut = new ActivityApiController(Mock.Of<IMediator>());
            var result = await sut.PutCheckin(It.IsAny<int>());

            Assert.IsType<HttpNotFoundResult>(result);
        }
        public async Task PutCheckinSendsAddActivitySignupCommandAsyncWithCorrectDataWhenUsersSignedUpIsNotNullAndCheckinDateTimeIsNull()
        {
            const string userId = "userId";
            var utcNow = DateTime.UtcNow;

            var activity = new Activity();
            var activitySignup = new ActivitySignup { User = new ApplicationUser { Id = userId } };
            activity.UsersSignedUp.Add(activitySignup);

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<ActivityByActivityIdQuery>())).Returns(activity);

            var sut = new ActivityApiController(mediator.Object) { DateTimeUtcNow = () => utcNow }
                .SetFakeUser(userId);
            await sut.PutCheckin(It.IsAny<int>());

            mediator.Verify(x => x.SendAsync(It.Is<AddActivitySignupCommandAsync>(y => y.ActivitySignup == activitySignup)));
            mediator.Verify(x => x.SendAsync(It.Is<AddActivitySignupCommandAsync>(y => y.ActivitySignup.CheckinDateTime == utcNow)));
        }
 public void GetHasHttpGetAttribute()
 {
     var sut = new ActivityApiController(null);
     var attribute = sut.GetAttributesOn(x => x.Get()).OfType<HttpGetAttribute>().SingleOrDefault();
     Assert.NotNull(attribute);
 }
 public void GetByIdHasProducesAttributeWithCorrectContentTypes()
 {
     var sut = new ActivityApiController(null);
     var attribute = sut.GetAttributesOn(x => x.Get(It.IsAny<int>())).OfType<ProducesAttribute>().SingleOrDefault();
     Assert.NotNull(attribute);
     Assert.Equal(attribute.Type, typeof(ActivityViewModel));
     Assert.Equal(attribute.ContentTypes.Select(x => x.MediaType).First(), "application/json");
 }
        public async Task UnregisterActivitySendsDeleteActivityAndTaskSignupsCommandAsyncWithCorrectActivitySignupId()
        {
            const int activityId = 1;
            const int activitySignupId = 1;

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<ActivitySignupByActivityIdAndUserIdQuery>()))
                .Returns(new ActivitySignup { Id = activitySignupId, Activity = new Activity(), User = new ApplicationUser() });

            var controller = new ActivityApiController(mediator.Object);
            controller.SetDefaultHttpContext();

            await controller.UnregisterActivity(activityId);

            mediator.Verify(x => x.SendAsync(It.Is<DeleteActivityAndTaskSignupsCommandAsync>(y => y.ActivitySignupId == activitySignupId)));
        }
        public async Task UnregisterActivitySendsActivitySignupByActivityIdAndUserIdQueryWithCorrectActivityIdAndUserId()
        {
            const int activityId = 1;
            const string userId = "1";

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<ActivitySignupByActivityIdAndUserIdQuery>()))
                .Returns(new ActivitySignup { Activity = new Activity(), User = new ApplicationUser() });

            var controller = new ActivityApiController(mediator.Object)
                .SetFakeUser(userId);

            await controller.UnregisterActivity(activityId);

            mediator.Verify(x => x.Send(It.Is<ActivitySignupByActivityIdAndUserIdQuery>(y => y.ActivityId == activityId && y.UserId == userId)));
        }
 public void PutCheckinHasHttpPutAttributeWithCorrectTemplate()
 {
     var sut = new ActivityApiController(null);
     var attribute = (HttpPutAttribute)sut.GetAttributesOn(x => x.PutCheckin(It.IsAny<int>())).SingleOrDefault(x => x.GetType() == typeof(HttpPutAttribute));
     Assert.NotNull(attribute);
     Assert.Equal(attribute.Template, "{id}/checkin");
 }
        private ActivityApiController GetActivityController()
        {
            var allReadyContext = _serviceProvider.GetService<AllReadyContext>();
            var allReadyDataAccess = new AllReadyDataAccessEF7(allReadyContext);

            _bus = new Mock<IMediator>();
            var controller = new ActivityApiController(allReadyDataAccess, _bus.Object);

            PopulateData(allReadyContext);
            return controller;
        }
        public async Task UnregisterActivityPublishesUserUnenrollsWithCorrectData()
        {
            const int activityId = 1;
            const string applicationUserId = "applicationUserId";

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<ActivitySignupByActivityIdAndUserIdQuery>()))
                .Returns(new ActivitySignup { Activity = new Activity { Id = activityId }, User = new ApplicationUser { Id = applicationUserId }});

            var controller = new ActivityApiController(mediator.Object);
            controller.SetDefaultHttpContext();
                    
            await controller.UnregisterActivity(activityId);

            mediator.Verify(mock => mock.PublishAsync(It.Is<UserUnenrolls>(ue => ue.ActivityId == activityId && ue.UserId == applicationUserId)), Times.Once);
        }