def it_creates_orders_and_tickets(self, mocker, client, db_session, sample_event_with_dates): mocker.patch('app.routes.orders.rest.Storage') mocker.patch( 'app.routes.orders.rest.Storage.upload_blob_from_base64string') mocker.patch('app.routes.orders.rest.send_email') txn_ids = ['112233', '112244', '112255'] txn_types = ['cart', 'cart', 'paypal_here'] num_tickets = [1, 2, 1] for i in range(len(txn_ids)): _sample_ipn = sample_ipns[i].format(id=sample_event_with_dates.id, txn_id=txn_ids[i], txn_type=txn_types[i]) with requests_mock.mock() as r: r.post(current_app.config['PAYPAL_VERIFY_URL'], text='VERIFIED') client.post(url_for('orders.paypal_ipn'), data=_sample_ipn, content_type="application/x-www-form-urlencoded") orders = dao_get_orders() assert len(orders) == 3 for i in range(len(sample_ipns)): assert orders[i].txn_id == txn_ids[i] assert orders[i].txn_type == txn_types[i] tickets = dao_get_tickets_for_order(orders[i].id) assert len(tickets) == num_tickets[i]
def get_orders(year=None, _filter=None): orders = dao_get_orders(year) if _filter == 'invalid': json_orders = [ o.serialize() for o in orders if o.txn_id.startswith("XX-") or o.txn_id.startswith("INVALID_") ] else: json_orders = ([ o.serialize() for o in orders if not o.txn_id.startswith("XX-") and not o.txn_id.startswith("INVALID_") ]) linked_orders = {} for o in json_orders: if 'linked_txn_id' in o: if o['linked_txn_id'] not in linked_orders: linked_orders[o['linked_txn_id']] = [o] else: linked_orders[o['linked_txn_id']].append(o) for k in linked_orders: for o in linked_orders[k]: json_orders.remove(o) for o in json_orders: if o['txn_id'] in linked_orders: o['linked_transactions'] = linked_orders[o['txn_id']] return jsonify(json_orders)
def it_does_not_create_orders_with_duplicate_txn_ids( self, mocker, client, db_session, sample_event_with_dates): txn_ids = ['112233', '112233'] txn_types = ['cart', 'cart'] for i in range(len(txn_ids)): _sample_ipn = sample_ipns[i].format(id=sample_event_with_dates.id, txn_id=txn_ids[i], txn_type=txn_types[i]) with requests_mock.mock() as r: r.post(current_app.config['PAYPAL_VERIFY_URL'], text='VERIFIED') client.post(url_for('orders.paypal_ipn'), data=_sample_ipn, content_type="application/x-www-form-urlencoded") orders = dao_get_orders() assert len(orders) == 1 assert orders[0].txn_id == txn_ids[0] assert orders[0].txn_type == txn_types[0] tickets = dao_get_tickets_for_order(orders[0].id) assert len(tickets) == 1
def it_gets_all_orders(self, db_session): create_order(created_at='2021-06-01 12:00', txn_id='112233a') create_order(created_at='2021-01-01 00:00', txn_id='112233b') create_order(created_at='2020-10-01 12:00', txn_id='112233c') create_order(created_at='2022-01-01 00:01', txn_id='112233d') orders = dao_get_orders() assert len(orders) == 4
def it_does_not_create_an_order_if_payment_not_complete( self, mocker, client, db_session): with requests_mock.mock() as r: r.post(current_app.config['PAYPAL_VERIFY_URL'], text='VERIFIED') client.post(url_for('orders.paypal_ipn'), data=sample_incomplete_ipn, content_type="application/x-www-form-urlencoded") orders = dao_get_orders() assert not orders
def it_gets_orders_for_a_year(self, db_session): # within year order1 = create_order(created_at='2021-06-01 12:00') # within year order2 = create_order(created_at='2021-01-01 00:00', txn_id='11223344a') # start of year # outside year create_order(created_at='2020-10-01 12:00', txn_id='123123') # before year create_order(created_at='2022-01-01 00:01', txn_id='321321') # after year orders = dao_get_orders(2021) assert len(orders) == 2 assert set([o.id for o in orders]) == set([order1.id, order2.id])
def it_does_not_create_an_order_if_not_verified(self, mocker, client, db_session, sample_event_with_dates, resp): sample_ipn = sample_ipns[0].format(id=sample_event_with_dates.id, txn_id='112233', txn_type='Cart') with requests_mock.mock() as r: r.post(current_app.config['PAYPAL_VERIFY_URL'], text=resp) client.post(url_for('orders.paypal_ipn'), data=sample_ipn, content_type="application/x-www-form-urlencoded") orders = dao_get_orders() assert not orders
def it_does_not_create_an_order_if_wrong_receiver(self, mocker, client, db_session, sample_event): mock_logger = mocker.patch( 'app.routes.orders.rest.current_app.logger.error') sample_ipn = sample_wrong_receiver.format(id=sample_event.id) with requests_mock.mock() as r: r.post(current_app.config['PAYPAL_VERIFY_URL'], text='VERIFIED') client.post(url_for('orders.paypal_ipn'), data=sample_ipn, content_type="application/x-www-form-urlencoded") orders = dao_get_orders() assert not orders assert mock_logger.call_args == call( 'Paypal receiver not valid: %s for %s', u'*****@*****.**', u'112233')
def it_does_not_create_an_order_if_no_event_matched( self, mocker, client, db_session, sample_uuid): mock_logger = mocker.patch( 'app.routes.orders.rest.current_app.logger.error') sample_ipn = sample_ipns[0].format(id=sample_uuid, txn_id='112233', txn_type='Cart') with requests_mock.mock() as r: r.post(current_app.config['PAYPAL_VERIFY_URL'], text='VERIFIED') client.post(url_for('orders.paypal_ipn'), data=sample_ipn, content_type="application/x-www-form-urlencoded") orders = dao_get_orders() assert mock_logger.call_args == call( 'No valid tickets, no order created: %s', u'112233') assert not orders