public async Task NoFilter_ReturnAllApartments()
        {
            var request = new GetAllApartmentsQuery();

            var apartments = await this.sut.Handle(request, CancellationToken.None).ConfigureAwait(false);

            Assert.Equal(this.dbApartments.Select(a => a.Id), apartments.Select(a => a.Id));
        }
Exemplo n.º 2
0
        public async Task WhenUserIsAdminAndStranger_CallsMediatorWithRequestedQuery()
        {
            var controller = this.CreateController(userId: 3, role: RoleNames.Administrator);
            var query      = new GetAllApartmentsQuery()
            {
                ActivityState = ActivityStates.Inactive, HostId = 5
            };
            var result = await controller.Get(query).ConfigureAwait(false);

            this.mediatorMock.Verify(m => m.Send(query, It.IsAny <CancellationToken>()), Times.Once);
        }
Exemplo n.º 3
0
        public async Task WhenUserIsHostAndStranger_CallsMediatorWithHostIdChangedToUserId()
        {
            var controller = this.CreateController(userId: 2, role: RoleNames.Host);
            var query      = new GetAllApartmentsQuery()
            {
                ActivityState = ActivityStates.Inactive, HostId = 3
            };
            var result = await controller.Get(query).ConfigureAwait(false);

            this.mediatorMock.Verify(m => m.Send(It.Is <GetAllApartmentsQuery>(q => q.HostId == 2), It.IsAny <CancellationToken>()), Times.Once);
        }
Exemplo n.º 4
0
        public async Task WhenUserIsGuest_CallsMediatorWithActiveQuery()
        {
            var controller = this.CreateController(userId: 1, role: RoleNames.Guest);
            var query      = new GetAllApartmentsQuery()
            {
                ActivityState = ActivityStates.Inactive
            };
            var result = await controller.Get(query).ConfigureAwait(false);

            this.mediatorMock.Verify(m => m.Send(It.Is <GetAllApartmentsQuery>(q => q.ActivityState == ActivityStates.Active), It.IsAny <CancellationToken>()), Times.Once);
        }
        public async Task FilterByHostId_ReturnApartmentsWithRequestedHostId()
        {
            var request = new GetAllApartmentsQuery()
            {
                HostId = this.dbHost.UserId
            };

            var apartments = await this.sut.Handle(request, CancellationToken.None).ConfigureAwait(false);

            Assert.NotEmpty(apartments);
            Assert.All(apartments, apartment => Assert.Equal(apartment.Host.Id, request.HostId));
        }
        public async Task FilterByActiveState_ReturnApartmentsWithRequestedActivity(string activityState)
        {
            var request = new GetAllApartmentsQuery()
            {
                ActivityState = activityState
            };

            var apartments = await this.sut.Handle(request, CancellationToken.None).ConfigureAwait(false);

            Assert.NotEmpty(apartments);
            Assert.All(apartments, apartment => Assert.Equal(apartment.ActivityState, activityState));
        }
        public async Task FilterByDateRange_ReturnsApartmentsThatAreAvailableInGivenRange()
        {
            this.mediatorMock.Setup(m => m.Send(It.Is <GetAvailableDatesQuery>(q => q.ApartmentId == this.dbApartments[0].Id), It.IsAny <CancellationToken>()))
            .ReturnsAsync(DateTimeHelpers.GetDateDayRange(DateTime.Now, 5));
            this.mediatorMock.Setup(m => m.Send(It.Is <GetAvailableDatesQuery>(q => q.ApartmentId == this.dbApartments[1].Id), It.IsAny <CancellationToken>()))
            .ReturnsAsync(DateTimeHelpers.GetDateDayRange(DateTime.Now, 2));

            var request = new GetAllApartmentsQuery()
            {
                FromDate = DateTime.Now, ToDate = DateTime.Now.AddDays(4)
            };

            var apartments = await this.sut.Handle(request, CancellationToken.None).ConfigureAwait(false);

            var apartment = Assert.Single(apartments);

            Assert.Equal(this.dbApartments[0].Id, apartment.Id);
        }
        public async Task FilterByAmenity_ReturnApartmentsWithRequestedAmenity(string amenityName)
        {
            var request = new GetAllApartmentsQuery()
            {
                AmenityName = amenityName
            };

            var apartments = await this.sut.Handle(request, CancellationToken.None).ConfigureAwait(false);

            Assert.NotEmpty(apartments);
            Assert.All(apartments, apartment =>
            {
                Assert.Contains(apartment.Amenities, (amenity) =>
                {
                    return(string.Equals(amenity.Name, amenityName, System.StringComparison.OrdinalIgnoreCase));
                });
            });
        }
Exemplo n.º 9
0
        public async Task <IActionResult> Get([FromQuery] GetAllApartmentsQuery query)
        {
            if (await authService.CheckIfBanned(this.User).ConfigureAwait(false))
            {
                return(this.Forbid());
            }

            if (this.User.IsInRole(RoleNames.Host))
            {
                var currClaim = this.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
                if (long.TryParse(currClaim?.Value ?? "", out long id))
                {
                    query.HostId = id;
                }
            }
            else if (!this.User.IsInRole(RoleNames.Administrator))
            {
                query.ActivityState = ActivityStates.Active;
            }

            return(this.Ok(await this.mediator.Send(query).ConfigureAwait(false)));
        }