def test_checks_number_of_reservations(self): stock = factories.EventStockFactory() bookings_factories.BookingFactory(stock=stock) bookings_factories.BookingFactory(stock=stock) bookings_factories.BookingFactory(stock=stock, isCancelled=True) # With a quantity too low quantity = 0 with pytest.raises(api_errors.ApiErrors) as error: api.edit_stock( stock, price=stock.price, quantity=quantity, beginning=stock.beginningDatetime, booking_limit_datetime=stock.bookingLimitDatetime, ) msg = "Le stock total ne peut être inférieur au nombre de réservations" assert error.value.errors["quantity"][0] == msg # With enough quantity quantity = 2 api.edit_stock( stock, price=stock.price, quantity=quantity, beginning=stock.beginningDatetime, booking_limit_datetime=stock.bookingLimitDatetime, ) stock = models.Stock.query.one() assert stock.quantity == 2
def test_can_edit_non_restricted_fields_if_provider_is_allocine(self): provider = offerers_factories.ProviderFactory(localClass="AllocineStocks") stock = factories.EventStockFactory( offer__lastProvider=provider, offer__idAtProviders="1", ) initial_beginning = stock.beginningDatetime # Change various attributes, but keep the same beginning # (which we are not allowed to change). new_booking_limit = datetime.datetime.now() + datetime.timedelta(days=1) changes = { "price": 5, "quantity": 20, # FIXME (dbaty, 2020-11-25): see comment in edit_stock, # this is to match what the frontend sends. "beginning": stock.beginningDatetime.replace(tzinfo=pytz.UTC), "booking_limit_datetime": new_booking_limit, } api.edit_stock(stock, **changes) stock = models.Stock.query.one() assert stock.price == 5 assert stock.quantity == 20 assert stock.beginningDatetime == initial_beginning assert stock.bookingLimitDatetime == new_booking_limit assert set(stock.fieldsUpdated) == {"price", "quantity", "bookingLimitDatetime"}
def test_checks_booking_limit_is_after_beginning(self): stock = factories.EventStockFactory() with pytest.raises(api_errors.ApiErrors) as error: api.edit_stock( stock, price=stock.price, quantity=stock.quantity, beginning=datetime.datetime.now(), booking_limit_datetime=datetime.datetime.now() + datetime.timedelta(days=1), ) msg = "La date limite de réservation pour cette offre est postérieure à la date de début de l'évènement" assert error.value.errors["bookingLimitDatetime"][0] == msg
def test_cannot_edit_if_provider_is_titelive(self): provider = offerers_factories.ProviderFactory(localClass="TiteLiveStocks") stock = factories.EventStockFactory(offer__lastProvider=provider, offer__idAtProviders="1") with pytest.raises(api_errors.ApiErrors) as error: api.edit_stock( stock, price=stock.price, quantity=stock.quantity, beginning=stock.beginningDatetime, booking_limit_datetime=stock.bookingLimitDatetime, ) msg = "Les offres importées ne sont pas modifiables" assert error.value.errors["global"][0] == msg
def should_update_bookings_confirmation_date_if_report_of_event(self, mock_update_confirmation_dates): now = datetime.datetime.now() event_in_4_days = now + datetime.timedelta(days=4) event_reported_in_10_days = now + datetime.timedelta(days=10) stock = factories.EventStockFactory(beginningDatetime=event_in_4_days) booking = bookings_factories.BookingFactory(stock=stock, dateCreated=now) api.edit_stock( stock, price=5, quantity=20, beginning=event_reported_in_10_days, booking_limit_datetime=stock.bookingLimitDatetime, ) mock_update_confirmation_dates.assert_called_once_with([booking], event_reported_in_10_days)
def test_sends_email_if_beginning_changes(self, mocked_send_email): stock = factories.EventStockFactory() booking1 = bookings_factories.BookingFactory(stock=stock) bookings_factories.BookingFactory(stock=stock, isCancelled=True) beginning = datetime.datetime.now() + datetime.timedelta(days=10) api.edit_stock( stock, price=5, quantity=20, beginning=beginning, booking_limit_datetime=stock.bookingLimitDatetime, ) stock = models.Stock.query.one() assert stock.beginningDatetime == beginning notified_bookings = mocked_send_email.call_args_list[0][0][0] assert notified_bookings == [booking1]
def test_edit_stock_basics(self, mocked_add_offer_id): stock = factories.EventStockFactory() beginning = datetime.datetime.now() + datetime.timedelta(days=2) booking_limit_datetime = datetime.datetime.now() + datetime.timedelta(days=1) api.edit_stock( stock, price=5, quantity=20, beginning=beginning, booking_limit_datetime=booking_limit_datetime, ) stock = models.Stock.query.one() assert stock.price == 5 assert stock.quantity == 20 assert stock.beginningDatetime == beginning assert stock.bookingLimitDatetime == booking_limit_datetime mocked_add_offer_id.assert_called_once_with(client=app.redis_client, offer_id=stock.offerId)
def test_cannot_edit_restricted_fields_if_provider_is_allocine(self): provider = offerers_factories.ProviderFactory(localClass="AllocineStocks") stock = factories.EventStockFactory( offer__lastProvider=provider, offer__idAtProviders="1", ) # Try to change everything, including "beginningDatetime" which is forbidden. new_booking_limit = datetime.datetime.now() + datetime.timedelta(days=1) changes = { "price": 5, "quantity": 20, "beginning": datetime.datetime.now() + datetime.timedelta(days=2), "booking_limit_datetime": new_booking_limit, } with pytest.raises(api_errors.ApiErrors) as error: api.edit_stock(stock, **changes) msg = "Pour les offres importées, certains champs ne sont pas modifiables" assert error.value.errors["global"][0] == msg
def should_invalidate_booking_token_when_event_is_reported(self): # Given now = datetime.datetime.now() booking_made_3_days_ago = now - datetime.timedelta(days=3) event_in_4_days = now + datetime.timedelta(days=4) event_reported_in_10_days = now + datetime.timedelta(days=10) stock = factories.EventStockFactory(beginningDatetime=event_in_4_days) booking = bookings_factories.BookingFactory(stock=stock, dateCreated=booking_made_3_days_ago, isUsed=True) # When api.edit_stock( stock, price=5, quantity=20, beginning=event_reported_in_10_days, booking_limit_datetime=stock.bookingLimitDatetime, ) # Then updated_booking = Booking.query.get(booking.id) assert updated_booking.isUsed is False assert updated_booking.dateUsed is None assert updated_booking.confirmationDate == booking.confirmationDate
def should_not_invalidate_booking_token_when_event_is_reported_in_less_than_48_hours(self, mock_mark_as_unused): # Given now = datetime.datetime.now() date_used_in_48_hours = datetime.datetime.now() + datetime.timedelta(days=2) event_in_3_days = now + datetime.timedelta(days=3) event_reported_in_less_48_hours = now + datetime.timedelta(days=1) stock = factories.EventStockFactory(beginningDatetime=event_in_3_days) booking = bookings_factories.BookingFactory( stock=stock, dateCreated=now, isUsed=True, dateUsed=date_used_in_48_hours ) # When api.edit_stock( stock, price=5, quantity=20, beginning=event_reported_in_less_48_hours, booking_limit_datetime=event_reported_in_less_48_hours, ) # Then updated_booking = Booking.query.get(booking.id) assert updated_booking.isUsed is True assert updated_booking.dateUsed == date_used_in_48_hours
def edit_stock(stock_id: str, body: StockEditionBodyModel) -> StockResponseIdModel: stock = Stock.queryNotSoftDeleted().filter_by( id=dehumanize(stock_id)).join(Offer, VenueSQLEntity).first_or_404() offerer_id = stock.offer.venue.managingOffererId ensure_current_user_has_rights(RightsType.editor, offerer_id) stock = offers_api.edit_stock( stock, price=body.price, quantity=body.quantity, beginning=body.beginning_datetime, booking_limit_datetime=body.booking_limit_datetime, ) return StockResponseIdModel.from_orm(stock)