public async Task CreateAttraction_ShouldReturnSuccessWithCreatedData()
        {
            var client = _factory.GetClient();

            var token = ApiTokenHelper.GenerateFakeToken();

            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");

            var command = new CreateAttractionCommand
            {
                Name                 = "Pantheon",
                Date                 = "113–125 AD",
                Author               = "Apollodoros",
                Description          = "The building is cylindrical with a portico of large granite Corinthian columns (eight in the first rank and two groups of four behind) under a pediment.",
                AttractionCategoryId = Guid.Parse("{7e12db9e-5648-4385-a9dc-abe24f1fbc4b}"),
                IsFree               = true,
                CityId               = Guid.Parse("{b3ec95dd-c424-4a2b-a329-50e6bdfff8b8}"),
            };

            var content  = Utilities.GetRequestContent(command);
            var response = await client.PostAsync($"/attractions", content);

            response.EnsureSuccessStatusCode();

            var result = await Utilities.GetResponseContent <AttractionDto>(response);

            result.Should().BeOfType(typeof(AttractionDto));
            result.Should().NotBeNull();
            response.StatusCode.Should().Be(HttpStatusCode.Created);
            response.Headers.Location.LocalPath.Should().Be($"/Attractions/{result.AttractionId}");
        }
Example #2
0
        public async Task Handle_ValidCommand_ShouldAddToRepository()
        {
            var initialListCount = (await _mockAttractionRepository.Object.GetAllAsync()).Count;

            var mockAttractionCategoryRepository = RepositoryMocks.GetAttractionCategoryRepository();

            mockAttractionCategoryRepository.Setup(repo => repo.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(new AttractionCategory());

            var mockCityRepository = RepositoryMocks.GetCityRepository();

            mockCityRepository.Setup(repo => repo.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(new City());

            var handler = new CreateAttractionCommandHandler(_mapper, _mockAttractionRepository.Object, mockAttractionCategoryRepository.Object, mockCityRepository.Object);

            var command = new CreateAttractionCommand
            {
                Name                 = "Colosseum",
                Date                 = "70–80 AD",
                Author               = "Vespasian",
                Description          = "It is an oval amphitheatre in the centre of the city of Rome, Italy, just east of the Roman Forum and is the largest ancient amphitheatre ever built, and is still the largest standing amphitheater in the world today, despite its age.",
                AttractionCategoryId = Guid.Parse("{dcaf14b8-3a1e-4b77-9957-8e38c0025e9b}"),
                IsFree               = true,
                CityId               = Guid.Parse("{21d106af-0880-4eae-aa98-e8a5957a29c6}")
            };

            var attractionResult = await handler.Handle(command, CancellationToken.None);

            attractionResult.Should().BeOfType(typeof(AttractionDto));
            attractionResult.Name.Should().Be("Colosseum");
            attractionResult.Date.Should().Be("70–80 AD");
            attractionResult.Author.Should().Be("Vespasian");
            attractionResult.Description.Should().Be("It is an oval amphitheatre in the centre of the city of Rome, Italy, just east of the Roman Forum and is the largest ancient amphitheatre ever built, and is still the largest standing amphitheater in the world today, despite its age.");
            attractionResult.AttractionCategoryId.ToString().Should().Be("dcaf14b8-3a1e-4b77-9957-8e38c0025e9b");
            attractionResult.IsFree.Should().Be(true);
            attractionResult.CityId.ToString().Should().Be("21d106af-0880-4eae-aa98-e8a5957a29c6");

            var categories = await _mockAttractionRepository.Object.GetAllAsync();

            categories.Count.Should().Be(initialListCount + 1);

            _mockAttractionRepository.Verify(repo => repo.AddAsync(It.IsAny <Attraction>()), Times.Once());
        }
        public async Task CreateAttraction_ShouldReturnUnauthorized()
        {
            var client = _factory.GetClient();

            var attractionCommand = new CreateAttractionCommand
            {
                Name                 = "Pantheon",
                Date                 = "113–125 AD",
                Author               = "Apollodoros",
                Description          = "The building is cylindrical with a portico of large granite Corinthian columns (eight in the first rank and two groups of four behind) under a pediment.",
                AttractionCategoryId = Guid.Parse("{7e12db9e-5648-4385-a9dc-abe24f1fbc4b}"),
                IsFree               = true,
                CityId               = Guid.Parse("{b3ec95dd-c424-4a2b-a329-50e6bdfff8b8}"),
            };

            var content  = Utilities.GetRequestContent(attractionCommand);
            var response = await client.PostAsync($"/attractions", content);

            response.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
        }
        public async Task <ActionResult <AttractionDto> > CreateAttraction([FromBody] CreateAttractionCommand createAttractionCommand)
        {
            var attractionDto = await _mediator.Send(createAttractionCommand);

            return(CreatedAtAction(nameof(GetAttractionDetails), new { id = attractionDto.AttractionId }, attractionDto));
        }