Esempio n. 1
0
    def test_it_returns_an_empty_list_if_everything_has_a_cost(self):
        # given
        reimbursement1 = BookingReimbursement(
            Booking(), ReimbursementRules.PHYSICAL_OFFERS, Decimal(0))
        reimbursement2 = BookingReimbursement(
            Booking(), ReimbursementRules.PHYSICAL_OFFERS, Decimal(0))

        # when
        bookings_reimbursements_with_cost = filter_out_bookings_without_cost(
            [reimbursement1, reimbursement2])

        # then
        assert bookings_reimbursements_with_cost == []
Esempio n. 2
0
    def test_value_error_is_raised_if_payments_ids_do_not_match_payments(self):
        # given
        payments = [
            create_payment(Booking(), Offerer(), 10, idx=111),
            create_payment(Booking(), Offerer(), 10, idx=222),
        ]
        ids_to_ban = [222, 333]

        # when
        with pytest.raises(UnmatchedPayments) as e:
            apply_banishment(payments, ids_to_ban)

        # then
        assert e.value.payment_ids == {333}
Esempio n. 3
0
    def test_no_payments_are_returned_if_no_ids_are_provided(self):
        # given
        payments = [
            create_payment(Booking(), Offerer(), 10, idx=111),
            create_payment(Booking(), Offerer(), 10, idx=222),
        ]
        ids_to_ban = []

        # when
        banned_payments, retry_payments = apply_banishment(
            payments, ids_to_ban)

        # then
        assert banned_payments == []
        assert retry_payments == []
Esempio n. 4
0
    def test_no_payments_to_retry_if_all_are_banned(self):
        # given
        payments = [
            create_payment(Booking(), Offerer(), 10, idx=111),
            create_payment(Booking(), Offerer(), 10, idx=222),
        ]
        ids_to_ban = [111, 222]

        # when
        banned_payments, retry_payments = apply_banishment(
            payments, ids_to_ban)

        # then
        assert len(banned_payments) == 2
        assert retry_payments == []
Esempio n. 5
0
    def test_it_returns_an_empty_list_if_everything_has_a_cost(self):
        # given
        reimbursement1 = BookingReimbursement(Booking(),
                                              PhysicalOffersReimbursement(),
                                              Decimal(0))
        reimbursement2 = BookingReimbursement(Booking(),
                                              PhysicalOffersReimbursement(),
                                              Decimal(0))

        # when
        bookings_reimbursements_with_cost = filter_out_bookings_without_cost(
            [reimbursement1, reimbursement2])

        # then
        assert bookings_reimbursements_with_cost == []
Esempio n. 6
0
    def test_it_returns_reimbursements_on_bookings_with_reimbursed_value_greater_than_zero(
            self):
        # given
        reimbursement1 = BookingReimbursement(
            Booking(), ReimbursementRules.PHYSICAL_OFFERS, Decimal(10))
        reimbursement2 = BookingReimbursement(
            Booking(), ReimbursementRules.PHYSICAL_OFFERS, Decimal(0))

        # when
        bookings_reimbursements_with_cost = filter_out_bookings_without_cost(
            [reimbursement1, reimbursement2])

        # then
        assert len(bookings_reimbursements_with_cost) == 1
        assert bookings_reimbursements_with_cost[
            0].reimbursed_amount > Decimal(0)
class ReimbursementRuleIsActiveTest:
    class DummyRule(payments_models.ReimbursementRule):
        rate = Decimal(10)
        description = "Dummy rule"

        def __init__(self, valid_from=None, valid_until=None):
            self.valid_from = valid_from
            self.valid_until = valid_until

        def is_relevant(self, booking, cumulative_revenue):
            return True

    booking = Booking(dateCreated=datetime.now() + timedelta(days=365),
                      dateUsed=datetime.now())

    def test_is_active_if_rule_has_no_start_nor_end(self):
        rule = self.DummyRule(None, None)
        assert rule.is_active(self.booking)

    def test_is_active_if_valid_since_yesterday_without_end_date(self):
        yesterday = datetime.utcnow() - timedelta(days=1)
        rule = self.DummyRule(valid_from=yesterday)
        assert rule.is_active(self.booking)

    def test_is_active_if_valid_since_yesterday_with_end_date(self):
        yesterday = datetime.utcnow() - timedelta(days=1)
        tomorrow = datetime.utcnow() + timedelta(days=1)
        rule = self.DummyRule(valid_from=yesterday, valid_until=tomorrow)
        assert rule.is_active(self.booking)

    def test_is_not_active_if_not_yet_valid_without_end_date(self):
        future = datetime.utcnow() + timedelta(weeks=3)
        rule = self.DummyRule(valid_from=future)
        assert not rule.is_active(self.booking)

    def test_is_not_active_if_not_yet_valid_with_end_date(self):
        future = datetime.utcnow() + timedelta(weeks=3)
        far_future = datetime.utcnow() + timedelta(weeks=5)
        rule = self.DummyRule(valid_from=future, valid_until=far_future)
        assert not rule.is_active(self.booking)

    def test_is_active_if_no_start_date_and_until_later(self):
        future = datetime.utcnow() + timedelta(weeks=3)
        rule = self.DummyRule(valid_until=future)
        assert rule.is_active(self.booking)

    def test_is_not_active_if_rule_is_not_valid_anymore_with_no_start_date(
            self):
        yesterday = datetime.utcnow() - timedelta(days=1)
        rule = self.DummyRule(valid_until=yesterday)
        assert not rule.is_active(self.booking)

    def test_is_not_active_if_rule_is_not_valid_anymore_with_start_date(self):
        a_month_ago = datetime.utcnow() - timedelta(days=30)
        yesterday = datetime.utcnow() - timedelta(days=1)
        rule = self.DummyRule(valid_from=a_month_ago, valid_until=yesterday)
        assert not rule.is_active(self.booking)
Esempio n. 8
0
    def test_payments_not_matching_given_ids_must_be_retried(self):
        # given
        payments = [
            create_payment(Booking(), Offerer(), 10, idx=111),
            create_payment(Booking(), Offerer(), 10, idx=222),
            create_payment(Booking(), Offerer(), 10, idx=333),
            create_payment(Booking(), Offerer(), 10, idx=444),
        ]
        ids_to_ban = [222, 333]

        # when
        _banned_payments, retry_payments = apply_banishment(
            payments, ids_to_ban)

        # then
        assert len(retry_payments) == 2
        for p in retry_payments:
            assert p.currentStatus.status == TransactionStatus.RETRY
            assert p.id not in ids_to_ban
Esempio n. 9
0
    def test_it_returns_reimbursements_on_bookings_with_no_existing_payments(
            self):
        # Given
        booking_paid = Booking()
        booking_paid.payments = [Payment()]
        booking_reimbursement1 = BookingReimbursement(
            booking_paid, ReimbursementRules.PHYSICAL_OFFERS, Decimal(10))
        booking_not_paid = Booking()
        booking_reimbursement2 = BookingReimbursement(
            booking_not_paid, ReimbursementRules.PHYSICAL_OFFERS, Decimal(10))
        booking_reimbursements = [
            booking_reimbursement1, booking_reimbursement2
        ]

        # When
        bookings_not_paid = filter_out_already_paid_for_bookings(
            booking_reimbursements)

        # Then
        assert len(bookings_not_paid) == 1
        assert not bookings_not_paid[0].booking.payments
Esempio n. 10
0
def create_booking_for_event(  # pylint: disable=redefined-builtin
    amount: int = 50,
    date_created: datetime = datetime.utcnow(),
    is_cancelled: bool = False,
    quantity: int = 1,
    type: EventType = EventType.CINEMA,
    user: User = None,
) -> Booking:
    product = Product(from_dict={"type": str(type)})
    offer = Offer()
    stock = Stock()
    booking = Booking(from_dict={"amount": amount})
    offer.product = product
    stock.offer = offer
    booking.stock = stock
    booking.quantity = quantity
    booking.user = user
    booking.isCancelled = is_cancelled
    booking.token = random_token()
    booking.dateCreated = date_created

    return booking
Esempio n. 11
0
    def test_it_returns_an_empty_list_if_everything_has_been_reimbursed(self):
        # Given
        booking_paid1 = Booking()
        booking_paid1.payments = [Payment()]
        booking_reimbursement1 = BookingReimbursement(
            booking_paid1, ReimbursementRules.PHYSICAL_OFFERS, Decimal(10))

        booking_paid2 = Booking()
        booking_paid2.payments = [Payment()]
        booking_reimbursement2 = BookingReimbursement(
            booking_paid2, ReimbursementRules.PHYSICAL_OFFERS, Decimal(10))

        # When
        bookings_not_paid = filter_out_already_paid_for_bookings(
            [booking_reimbursement1, booking_reimbursement2])

        # Then
        assert bookings_not_paid == []
Esempio n. 12
0
def create_booking_for_thing(
    amount: int = 50,
    date_created: datetime = datetime.utcnow(),
    is_cancelled: bool = False,
    quantity: int = 1,
    product_type: ThingType = ThingType.JEUX,
    url: str = None,
    user: User = None,
) -> Booking:
    product = Product(from_dict={"url": url, "type": str(product_type)})
    offer = Offer(from_dict={"url": url, "type": str(product_type)})
    stock = Stock()
    booking = Booking(from_dict={"amount": amount})
    offer.product = product
    stock.offer = offer
    booking.stock = stock
    booking.quantity = quantity
    booking.user = user
    booking.isCancelled = is_cancelled
    booking.dateCreated = date_created

    return booking
Esempio n. 13
0
def create_booking(
    user: User,
    amount: Optional[Union[Decimal, float]] = None,
    date_created: datetime = datetime.utcnow(),
    date_used: datetime = None,
    idx: int = None,
    is_cancelled: bool = False,
    is_used: bool = False,
    quantity: int = 1,
    stock: Stock = None,
    token: str = None,
    venue: VenueSQLEntity = None,
) -> Booking:
    booking = Booking()
    offerer = create_offerer(siren="987654321",
                             address="Test address",
                             city="Test city",
                             postal_code="93000",
                             name="Test name")
    if venue is None:
        venue = create_venue(
            offerer=offerer,
            name="Test offerer",
            booking_email="*****@*****.**",
            address="123 rue test",
            postal_code="93000",
            city="Test city",
            departement_code="93",
        )
    if stock is None:
        price = amount if amount is not None else 10
        product_with_thing_type = create_offer_with_thing_product(venue)
        stock = create_stock_with_thing_offer(offerer=offerer,
                                              venue=venue,
                                              offer=product_with_thing_type,
                                              price=price)

    if not stock.offer:
        stock.offer = create_offer_with_thing_product(venue)

    booking.user = user
    booking.amount = amount if amount is not None else stock.price
    booking.dateCreated = date_created
    booking.dateUsed = date_used
    booking.id = idx
    booking.isCancelled = is_cancelled
    booking.isUsed = is_used
    booking.quantity = quantity
    booking.stock = stock
    booking.token = token if token is not None else random_token()
    booking.userId = user.id
    booking.confirmationDate = bookings_api.compute_confirmation_date(
        stock.beginningDatetime, date_created)

    return booking
Esempio n. 14
0
class ReimbursementRuleIsActiveTest:
    class DummyRule(ReimbursementRule):
        rate = Decimal(10)
        description = "Dummy rule"
        valid_from = None
        valid_until = None

        def is_relevant(self, booking, **kwargs):
            return True

    booking = Booking()

    def test_is_active_if_valid_from_is_none_and_valid_until_is_none(self):
        # given
        self.booking.dateCreated = datetime.utcnow()
        self.DummyRule.valid_from = None
        self.DummyRule.valid_until = None

        # then
        assert self.DummyRule().is_active(self.booking) is True

    def test_is_active_if_valid_from_is_past_and_valid_until_is_none(self):
        # given
        self.booking.dateCreated = datetime.utcnow()
        self.DummyRule.valid_from = datetime.utcnow() - timedelta(weeks=3)
        self.DummyRule.valid_until = None

        # then
        assert self.DummyRule().is_active(self.booking) is True

    def test_is_not_active_if_valid_from_is_future_and_valid_until_is_none(self):
        # given
        self.booking.dateCreated = datetime.utcnow()
        self.DummyRule.valid_from = datetime.utcnow() + timedelta(weeks=3)
        self.DummyRule.valid_until = None

        # then
        assert self.DummyRule().is_active(self.booking) is False

    def test_is_active_if_valid_from_is_none_and_valid_until_is_future(self):
        # given
        self.booking.dateCreated = datetime.utcnow()
        self.DummyRule.valid_from = None
        self.DummyRule.valid_until = datetime.utcnow() + timedelta(weeks=3)

        # then
        assert self.DummyRule().is_active(self.booking) is True

    def test_is_not_active_if_valid_from_is_none_and_valid_until_is_past(self):
        # given
        self.booking.dateCreated = datetime.utcnow()
        self.DummyRule.valid_from = None
        self.DummyRule.valid_until = datetime.utcnow() - timedelta(weeks=3)

        # then
        assert self.DummyRule().is_active(self.booking) is False

    def test_is_active_if_valid_from_is_past_and_valid_until_is_future(self):
        # given
        self.booking.dateCreated = datetime.utcnow()
        self.DummyRule.valid_from = datetime.utcnow() - timedelta(weeks=3)
        self.DummyRule.valid_until = datetime.utcnow() + timedelta(weeks=3)

        # then
        assert self.DummyRule().is_active(self.booking) is True

    def test_is_not_active_if_valid_from_is_future_and_valid_until_is_future(self):
        # given
        self.booking.dateCreated = datetime.utcnow()
        self.DummyRule.valid_from = datetime.utcnow() + timedelta(weeks=3)
        self.DummyRule.valid_until = datetime.utcnow() + timedelta(weeks=6)

        # then
        assert self.DummyRule().is_active(self.booking) is False

    def test_is_not_active_if_valid_from_is_past_and_valid_until_is_past(self):
        # given
        self.booking.dateCreated = datetime.utcnow()
        self.DummyRule.valid_from = datetime.utcnow() - timedelta(weeks=3)
        self.DummyRule.valid_until = datetime.utcnow() - timedelta(weeks=6)

        # then
        assert self.DummyRule().is_active(self.booking) is False