def test_raises_when_response_is_400(conference, requests_mock, invoice_information): requests_mock.post(f"https://pretix/api/organizers/events/orders/", status_code=400, json={}) requests_mock.get(f"https://pretix/api/organizers/events/questions", json={"results": []}) requests_mock.get( f"https://pretix/api/organizers/events/items", json={"results": [{ "id": "123", "admission": False }]}, ) order_data = CreateOrderInput( email="*****@*****.**", locale="en", payment_provider="stripe", invoice_information=invoice_information, hotel_rooms=[], tickets=[ CreateOrderTicket( ticket_id="123", attendee_name="Example", attendee_email="Example", variation=None, answers=None, voucher=None, ) ], ) with pytest.raises(PretixError): create_order(conference, order_data)
def create_order( self, info, conference: str, input: CreateOrderInput) -> typing.Union[CreateOrderResult, Error]: conference_obj = Conference.objects.get(code=conference) validation_error = validate_hotel_rooms(input.hotel_rooms, conference=conference_obj) if validation_error: return validation_error try: pretix_order = create_order(conference_obj, input) except PretixError as e: return Error(message=e.message) if len(input.hotel_rooms) > 0: create_hotel_reservations(pretix_order, input.hotel_rooms, user=info.context["request"].user) return_url = urljoin( settings.FRONTEND_URL, f"/{input.locale}/orders/{pretix_order.code}/confirmation", ) if pretix_order.payment_url is None: return CreateOrderResult(payment_url=return_url) payment_url = pretix_order.payment_url payment_url += f"?return_url={return_url}" return CreateOrderResult(payment_url=payment_url)
def test_create_order_with_positions_with_voucher_and_one_without( conference, requests_mock, invoice_information): orders_mock = requests_mock.post( "https://pretix/api/organizers/base-pretix-organizer-id/events/base-pretix-event-id/orders/", json={ "payments": [{ "payment_url": "http://example.com" }], "code": 123 }, ) requests_mock.get( "https://pretix/api/organizers/base-pretix-organizer-id/events/base-pretix-event-id/questions", json={ "results": [ { "id": "1", "type": "S" }, { "id": "2", "type": "C", "options": [{ "id": 1, "identifier": "abc" }] }, ] }, ) requests_mock.get( "https://pretix/api/organizers/base-pretix-organizer-id/events/base-pretix-event-id/items", json={"results": [{ "id": "123", "admission": True }]}, ) order_data = CreateOrderInput( email="*****@*****.**", locale="en", payment_provider="stripe", invoice_information=invoice_information, hotel_rooms=[], tickets=[ CreateOrderTicket( ticket_id="123", attendee_name="Example", attendee_email="Example", variation=None, voucher=None, answers=[ CreateOrderTicketAnswer(question_id="1", value="ABC"), CreateOrderTicketAnswer(question_id="2", value="1"), ], ), CreateOrderTicket( ticket_id="123", attendee_name="Example", attendee_email="Example", variation=None, voucher="friendly-human-being", answers=[ CreateOrderTicketAnswer(question_id="1", value="ABC"), CreateOrderTicketAnswer(question_id="2", value="1"), ], ), ], ) result = create_order(conference, order_data) assert result.payment_url == "http://example.com" body = orders_mock.request_history[0].json() assert "voucher" not in body["positions"][0] assert body["positions"][1]["voucher"] == "friendly-human-being"
def test_creates_order(conference, hotel_room, requests_mock, invoice_information): hotel_room.conference = conference hotel_room.save() requests_mock.post( "https://pretix/api/organizers/base-pretix-organizer-id/events/base-pretix-event-id/orders/", json={ "payments": [{ "payment_url": "http://example.com" }], "code": 123 }, ) requests_mock.get( "https://pretix/api/organizers/base-pretix-organizer-id/events/base-pretix-event-id/questions", json={ "results": [ { "id": "1", "type": "S" }, { "id": "2", "type": "C", "options": [{ "id": 1, "identifier": "abc" }] }, ] }, ) requests_mock.get( "https://pretix/api/organizers/base-pretix-organizer-id/events/base-pretix-event-id/items", json={"results": [{ "id": "123", "admission": True }]}, ) order_data = CreateOrderInput( email="*****@*****.**", locale="en", payment_provider="stripe", invoice_information=invoice_information, hotel_rooms=[ CreateOrderHotelRoom( room_id=str(hotel_room.id), checkin=timezone.datetime(2020, 1, 1).date(), checkout=timezone.datetime(2020, 1, 3).date(), ) ], tickets=[ CreateOrderTicket( ticket_id="123", attendee_name="Example", attendee_email="Example", variation=None, voucher=None, answers=[ CreateOrderTicketAnswer(question_id="1", value="ABC"), CreateOrderTicketAnswer(question_id="2", value="1"), ], ) ], ) result = create_order(conference, order_data) assert result.payment_url == "http://example.com"
def test_not_required_and_empty_answer_is_skipped(conference, requests_mock, invoice_information): orders_mock = requests_mock.post( "https://pretix/api/organizers/base-pretix-organizer-id/events/base-pretix-event-id/orders/", json={ "payments": [{ "payment_url": "http://example.com" }], "code": 123 }, ) requests_mock.get( "https://pretix/api/organizers/base-pretix-organizer-id/events/base-pretix-event-id/questions", json={ "results": [ { "id": "1", "type": "S", "required": False }, { "id": "2", "type": "C", "required": True, "options": [{ "id": 1, "identifier": "abc" }], }, ] }, ) requests_mock.get( "https://pretix/api/organizers/base-pretix-organizer-id/events/base-pretix-event-id/items", json={"results": [{ "id": "123", "admission": True }]}, ) order_data = CreateOrderInput( email="*****@*****.**", locale="en", payment_provider="stripe", invoice_information=invoice_information, hotel_rooms=[], tickets=[ CreateOrderTicket( ticket_id="123", attendee_name="Example", attendee_email="Example", variation=None, voucher=None, answers=[ CreateOrderTicketAnswer(question_id="1", value=""), CreateOrderTicketAnswer(question_id="2", value="1"), ], ) ], ) result = create_order(conference, order_data) assert result.payment_url == "http://example.com" body = orders_mock.request_history[0].json() answers = body["positions"][0]["answers"] assert len(answers) == 1 assert answers == [{ "question": "2", "answer": "1", "options": [1], "option_identifier": [], "option_identifiers": ["abc"], }]
def test_raises_value_error_if_answer_value_is_wrong(conference, requests_mock, invoice_information): requests_mock.post( "https://pretix/api/organizers/base-pretix-organizer-id/events/base-pretix-event-id/orders/", json={ "payments": [{ "payment_url": "http://example.com" }], "code": 123 }, ) requests_mock.get( "https://pretix/api/organizers/base-pretix-organizer-id/events/base-pretix-event-id/questions", json={ "results": [ { "id": "1", "type": "S" }, { "id": "2", "type": "C", "options": [{ "id": 1, "identifier": "abc" }] }, ] }, ) requests_mock.get( "https://pretix/api/organizers/base-pretix-organizer-id/events/base-pretix-event-id/items", json={"results": [{ "id": "123", "admission": True }]}, ) order_data = CreateOrderInput( email="*****@*****.**", locale="en", payment_provider="stripe", invoice_information=invoice_information, hotel_rooms=[], tickets=[ CreateOrderTicket( ticket_id="123", attendee_name="Example", attendee_email="Example", variation=None, voucher=None, answers=[ CreateOrderTicketAnswer(question_id="1", value="ABC"), # 100 doesn't exist as id in the questions CreateOrderTicketAnswer(question_id="2", value="100"), ], ) ], ) with pytest.raises(ValueError): create_order(conference, order_data)