def test_that_we_can_get_multiple_trips(monkeypatch, fake_response_from_route): """ Ensure that our threads can actually yield multiple trips. """ monkeypatch.setattr( "tripit.trips.get_from_tripit_v1", lambda *args, **kwargs: fake_response_from_route( fake_trip_name="Personal: Multiple Trip", fake_flights_scenario="personal_multi_trip_without_flights", *args, **kwargs, ), ) expected_trip = { "id": 123456789, "name": "Personal: Multiple Trip", "city": "Dayton, OH", "ends_on": 1576713600, "link": "https://www.tripit.com/trip/show/id/123456789", "starts_on": 1576368000, "ended": False, "flights": [], } expected_trips = [expected_trip, expected_trip] assert get_all_trips(token="token", token_secret="token_secret") == expected_trips
def test_fetching_trips_when_none_are_present(monkeypatch, fake_response_from_route): """ Behold! The test. """ monkeypatch.setattr("tripit.trips.get_from_tripit_v1", fake_response_from_route) assert get_all_trips(token="token", token_secret="token_secret") == []
def test_fetching_trips_without_flights(monkeypatch, fake_response_from_route): """ We should get back a valid trip for personal trips. """ monkeypatch.setattr( "tripit.trips.get_from_tripit_v1", lambda *args, **kwargs: fake_response_from_route( fake_trip_name="Personal: Some Trip", fake_flights_scenario="personal_trip_without_flights", *args, **kwargs, ), ) expected_trips = [{ "id": 123456789, "name": "Personal: Some Trip", "city": "Dayton, OH", "ends_on": 1576713600, "link": "https://www.tripit.com/trip/show/id/123456789", "starts_on": 1576368000, "ended": False, "flights": [], }] assert get_all_trips(token="token", token_secret="token_secret") == expected_trips
def test_fetching_trips_with_single_segment_flights(monkeypatch, fake_response_from_route): """ Same as test_fetching_trips_with_flights except for situations where we only have one segment in the flight. """ test_outbound_ingress_seconds = 90 * 60 outbound_date = "2019-11-27" outbound_tz = "-05:00" inbound_date = "2019-11-27" inbound_tz = "-08:00" outbound_start_time = normalize_flight_time_to_tz({ "date": outbound_date, "time": "11:19:00", "utc_offset": outbound_tz }) inbound_end_time = normalize_flight_time_to_tz({ "date": inbound_date, "time": "13:01:00", "utc_offset": inbound_tz }) monkeypatch.setenv("TRIPIT_INGRESS_TIME_MINUTES", "90") monkeypatch.setattr( "tripit.trips.get_from_tripit_v1", lambda *args, **kwargs: fake_response_from_route( fake_trip_name="Work: Test Client - Week 3", fake_flights_scenario="trip_with_single_segment", *args, **kwargs, ), ) expected_trips = [{ "id": 234567890, "name": "Work: Test Client - Week 3", "city": "Los Angeles, CA", "ends_on": inbound_end_time, "link": "https://www.tripit.com/trip/show/id/234567890", "starts_on": (outbound_start_time + test_outbound_ingress_seconds), "ended": False, "flights": [{ # Since this is a one-way single-segment flight, # its end time == inbound end time. "flight_number": "AA1", "origin": "JFK", "destination": "LAX", "depart_time": outbound_start_time, "arrive_time": inbound_end_time, "offset": "-05:00", }], }] assert get_all_trips(token="token", token_secret="token_secret") == expected_trips
def test_fetching_trips_with_empty_trips(monkeypatch, fake_response_from_route): """ TripIt will, sometimes, create empty trips with no start/end times or location data. These should be filtered out from the list of trips returned. """ monkeypatch.setattr( "tripit.trips.get_from_tripit_v1", lambda *args, **kwargs: fake_response_from_route( fake_trip_name="Your trip", fake_flights_scenario="empty_trip", *args, **kwargs, ), ) expected_trips = [] assert get_all_trips(token="token", token_secret="token_secret") == expected_trips
def get_trips(event, _context=None): """ Gets all trips associated with a TripIt account. """ access_key = get_access_key(event) show_human_times = get_query_parameter(event, "human_times") or False if not access_key: return return_error(message="Failed to get access key from event.") token_data = get_token_data_for_access_key(access_key) if not token_data: return return_error(code=403, message="Access denied; go to /auth first.") return return_ok( additional_json={ "trips": get_all_trips( token=token_data["token"], token_secret=token_data["token_secret"], human_times=show_human_times, ) })
def test_fetching_trips_with_flights(monkeypatch, fake_response_from_route): """ We should get back a valid trip with flight data for trips with flights in them. Additionally, the start and end time of the trip should now match the departure time and arrival time of the first and last flight segment, respectively, with some additional padding for trip ingress (getting to the airport and waiting for the flight to depart) to the start time. """ test_outbound_ingress_seconds = 90 * 60 outbound_date = "2019-12-01" outbound_tz = "-06:00" inbound_date = "2019-12-05" inbound_tz = "-06:00" outbound_start_time = normalize_flight_time_to_tz({ "date": outbound_date, "time": "17:11:00", "utc_offset": outbound_tz }) outbound_end_time = normalize_flight_time_to_tz({ "date": outbound_date, "time": "18:56:00", "utc_offset": outbound_tz }) inbound_start_time = normalize_flight_time_to_tz({ "date": inbound_date, "time": "16:02:00", "utc_offset": inbound_tz }) inbound_end_time = normalize_flight_time_to_tz({ "date": inbound_date, "time": "17:58:00", "utc_offset": inbound_tz }) monkeypatch.setenv("TRIPIT_INGRESS_TIME_MINUTES", "90") monkeypatch.setattr( "tripit.trips.get_from_tripit_v1", lambda *args, **kwargs: fake_response_from_route( fake_trip_name="Work: Test Client - Week 2", fake_flights_scenario="trip_with_flights", *args, **kwargs, ), ) expected_trips = [{ "id": 293554134, "name": "Work: Test Client - Week 2", "city": "Omaha, NE", "ends_on": inbound_end_time, # Should match AA2360 arrive_time "link": "https://www.tripit.com/trip/show/id/293554134", "starts_on": (outbound_start_time + test_outbound_ingress_seconds ), # Should match AA356 depart_time + ingress "ended": False, "flights": [ { "flight_number": "AA356", "origin": "DFW", "destination": "OMA", "depart_time": outbound_start_time, "arrive_time": outbound_end_time, # offset should be accounted for "offset": "-06:00", }, { "flight_number": "AA2360", "origin": "OMA", "destination": "DFW", "depart_time": inbound_start_time, # offset should be accounted for "arrive_time": inbound_end_time, # offset should be accounted for "offset": "-06:00", }, ], }] assert get_all_trips(token="token", token_secret="token_secret") == expected_trips
def test_fetching_trips_that_have_ended_due_to_note(monkeypatch, fake_response_from_route): """ If there's a note in the trip that says "TRIP_ENDED", then that means that we need to manually mark the trip as such. This is a legacy feature from the Ruby version of this API that did not adjust the end times of trips to match the last flight on the trip. (It was too difficult to write a test for this because I was newb at testing then). I plan on deprecating this eventually, but I want to include it in the port-over to maintain feature parity. """ fake_trip_name = "Personal: Some Trip That Ended" test_outbound_ingress_seconds = 90 * 60 outbound_date = "2019-11-27" outbound_tz = "-05:00" inbound_date = "2019-11-27" inbound_tz = "-08:00" outbound_start_time = normalize_flight_time_to_tz({ "date": outbound_date, "time": "11:19:00", "utc_offset": outbound_tz }) inbound_end_time = normalize_flight_time_to_tz({ "date": inbound_date, "time": "13:01:00", "utc_offset": inbound_tz }) monkeypatch.setenv("TRIPIT_INGRESS_TIME_MINUTES", "90") monkeypatch.setattr( "tripit.trips.get_from_tripit_v1", lambda *args, **kwargs: fake_response_from_route( fake_trip_name=fake_trip_name, fake_flights_scenario="trip_that_ended", *args, **kwargs, ), ) expected_trips = [{ "id": 345678901, "name": fake_trip_name, "city": "Dallas, TX", "ends_on": inbound_end_time, "link": "https://www.tripit.com/trip/show/id/345678901", "starts_on": (outbound_start_time + test_outbound_ingress_seconds), "ended": True, "flights": [{ # Since this is a one-way single-segment flight, # its end time == inbound end time. "flight_number": "AA1", "origin": "JFK", "destination": "LAX", "depart_time": outbound_start_time, "arrive_time": inbound_end_time, "offset": "-05:00", }], }] assert get_all_trips(token="token", token_secret="token_secret") == expected_trips
def test_fetching_trips_that_have_ended_due_to_flight_time( monkeypatch, fake_response_from_route): """ Trips are considered to have ended if our current time is greater than the time of the last flight in our trip or if there is a note on the trip marking the trip as having ended. """ fake_trip_name = "Personal: Some Trip That Ended" test_outbound_ingress_seconds = 90 * 60 outbound_date = "2019-11-27" outbound_tz = "-05:00" inbound_date = "2019-11-27" inbound_tz = "-08:00" outbound_start_time = normalize_flight_time_to_tz({ "date": outbound_date, "time": "11:19:00", "utc_offset": outbound_tz }) inbound_end_time = normalize_flight_time_to_tz({ "date": inbound_date, "time": "13:01:00", "utc_offset": inbound_tz }) monkeypatch.setenv("TRIPIT_INGRESS_TIME_MINUTES", "90") monkeypatch.setattr( "tripit.trips.get_from_tripit_v1", lambda *args, **kwargs: fake_response_from_route( fake_trip_name=fake_trip_name, fake_flights_scenario="trip_that_ended", filter_notes=True, # we'll test notes in the next test. *args, **kwargs, ), ) expected_trips = [{ "id": 345678901, "name": fake_trip_name, "city": "Dallas, TX", "ends_on": inbound_end_time, "link": "https://www.tripit.com/trip/show/id/345678901", "starts_on": (outbound_start_time + test_outbound_ingress_seconds), "ended": True, "flights": [{ # Since this is a one-way single-segment flight, # its end time == inbound end time. "flight_number": "AA1", "origin": "JFK", "destination": "LAX", "depart_time": outbound_start_time, "arrive_time": inbound_end_time, "offset": "-05:00", }], }] assert get_all_trips(token="token", token_secret="token_secret") == expected_trips