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_current_trip_endpoint_authenticated( monkeypatch, set_access_token_table, drop_access_token_table, fake_response_from_route ): # pylint: enable=bad-continuation # Lots of vars needed to mock this flight. # pylint: disable=too-many-locals """ Gets the current trip in progress. Uses a trip with flights as an example. Full test suite is in tests/unit/flights. """ set_access_token_table(access_key="fake-key", token="fake-token", secret="fake-secret") fake_event = { "requestContext": {"path": "/develop/current_trip", "identity": {"apiKey": "fake-key"},}, } 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, ), ) outbound_date = "2019-12-01" outbound_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} ) expected_trip = { "trip_name": "Work: Test Client - Week 2", "current_city": "Omaha, NE", "todays_flight": { "flight_number": "AA356", "origin": "DFW", "destination": "OMA", "offset": "-06:00", "depart_time": outbound_start_time, "arrive_time": outbound_end_time, # offset should be accounted for }, } expected_response = { "statusCode": 200, "body": json.dumps({"status": "ok", "trip": expected_trip}), } assert current_trip(fake_event) == expected_response drop_access_token_table()
def test_getting_active_trip_with_flight(monkeypatch, fake_response_from_route): """ If we're currently on a trip and are flying, then we should get a trip back but with the flight that we're currently on. """ 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, ), ) outbound_date = "2019-12-01" outbound_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 }) expected_trip = { "trip_name": "Work: Test Client - Week 2", "current_city": "Omaha, NE", "todays_flight": { "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", }, } assert get_current_trip(token="token", token_secret="token_secret") == expected_trip
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