public async Task Update_Flight_With_Departure_Date_After_Landing_Date_Should_Return_Bad_Request()
        {
            await TestHelpers.Airline_Company_Login(_httpClient);

            CreateFlightDTO createFlightDTO = new CreateFlightDTO
            {
                OriginCountryId      = 1,
                DestinationCountryId = 1,
                DepartureTime        = DateTime.Now.AddHours(12),
                LandingTime          = DateTime.Now.AddHours(16),
                RemainingTickets     = 15
            };

            var response = await _httpClient.PostAsync("api/flights",
                                                       new StringContent(JsonSerializer.Serialize(createFlightDTO), Encoding.UTF8, MediaTypeNames.Application.Json));

            var responseContent = await response.Content.ReadAsStringAsync();

            Flight flightResult = JsonSerializer.Deserialize <Flight>(responseContent, new JsonSerializerOptions {
                PropertyNameCaseInsensitive = true
            });

            UpdateFlightDTO updateFlightDTO = new UpdateFlightDTO
            {
                Id = flightResult.Id,
                OriginCountryId      = 1,
                DestinationCountryId = 1,
                DepartureTime        = DateTime.Now.AddHours(10),
                LandingTime          = DateTime.Now.AddHours(6),
                RemainingTickets     = 10
            };

            var putResponse = await _httpClient.PutAsync($"api/flights/{flightResult.Id}",
                                                         new StringContent(JsonSerializer.Serialize(updateFlightDTO), Encoding.UTF8, MediaTypeNames.Application.Json));

            Assert.AreEqual(HttpStatusCode.BadRequest, putResponse.StatusCode);

            var putResponseContent = await putResponse.Content.ReadAsStringAsync();

            var result = JsonSerializer.Deserialize <ValidationProblemDetails>(putResponseContent, new JsonSerializerOptions {
                PropertyNameCaseInsensitive = true
            });

            Assert.AreEqual(result.Errors.Count, 2);
        }
        public async Task Update_Flight_That_Belongs_To_Another_Airline_Should_Return_Forbidden()
        {
            CreateAirlineCompanyDTO createAirlineCompanyDTO = new CreateAirlineCompanyDTO
            {
                Name      = "Arkia",
                CountryId = 1,
                User      = new CreateUserDTO
                {
                    UserName = "******",
                    Password = "******",
                    Email    = "*****@*****.**",
                }
            };

            await TestHelpers.Airline_Company_Login(_httpClient, createAirlineCompanyDTO);

            CreateFlightDTO createFlightDTO = new CreateFlightDTO
            {
                OriginCountryId      = 1,
                DestinationCountryId = 1,
                DepartureTime        = DateTime.Now.AddHours(12),
                LandingTime          = DateTime.Now.AddHours(16),
                RemainingTickets     = 15
            };

            await _httpClient.PostAsync("api/flights",
                                        new StringContent(JsonSerializer.Serialize(createFlightDTO), Encoding.UTF8, MediaTypeNames.Application.Json));

            await TestHelpers.Airline_Company_Login(_httpClient);

            UpdateFlightDTO updateFlightDTO = new UpdateFlightDTO
            {
                Id = 1,
                OriginCountryId      = 1,
                DestinationCountryId = 1,
                DepartureTime        = DateTime.Now.AddHours(13),
                LandingTime          = DateTime.Now.AddHours(17),
                RemainingTickets     = 10
            };

            var putResponse = await _httpClient.PutAsync($"api/flights/1",
                                                         new StringContent(JsonSerializer.Serialize(updateFlightDTO), Encoding.UTF8, MediaTypeNames.Application.Json));

            Assert.AreEqual(HttpStatusCode.Forbidden, putResponse.StatusCode);
        }
예제 #3
0
        public IActionResult UpdateFlight(long id, UpdateFlightDTO updateFlightDTO)
        {
            LoginToken <AirlineCompany> airline_token = DesirializeToken();

            if (id == 0)
            {
                return(NotFound());
            }

            if (id != updateFlightDTO.Id)
            {
                return(BadRequest());
            }

            Flight flight = _mapper.Map <Flight>(updateFlightDTO);

            Flight original_flight = _loggedInAirlineFacade.GetFlightById(flight.Id);

            if (original_flight == null)
            {
                return(NotFound());
            }

            if (original_flight.AirlineCompany != airline_token.User)
            {
                return(Forbid());
            }

            flight.AirlineCompany = airline_token.User;

            try
            {
                _loggedInAirlineFacade.UpdateFlight(airline_token, flight);
            }
            catch (NotAllowedAirlineActionException)//might be irrelevant, we are checking it here also
            {
                return(Forbid());
            }
            catch (RelatedRecordNotExistsException)
            {
                return(NotFound());
            }

            return(NoContent());
        }
        public async Task Update_Flight_That_Not_Exist_Should_Return_Not_Found()
        {
            await TestHelpers.Airline_Company_Login(_httpClient);

            UpdateFlightDTO updateFlightDTO = new UpdateFlightDTO
            {
                Id = 1,
                OriginCountryId      = 1,
                DestinationCountryId = 1,
                DepartureTime        = DateTime.Now.AddHours(13),
                LandingTime          = DateTime.Now.AddHours(17),
                RemainingTickets     = 10
            };

            var putResponse = await _httpClient.PutAsync($"api/flights/{updateFlightDTO.Id}",
                                                         new StringContent(JsonSerializer.Serialize(updateFlightDTO), Encoding.UTF8, MediaTypeNames.Application.Json));

            Assert.AreEqual(HttpStatusCode.NotFound, putResponse.StatusCode);
        }