# WARNING: Do not edit by hand, this file was generated by Crank:
#
#   https://github.com/gocardless/crank
#

import json

import pytest
import requests
import responses

from gocardless_pro.errors import MalformedResponseError
from gocardless_pro import resources
from gocardless_pro import list_response

from .. import helpers
  

@responses.activate
def test_billing_requests_create():
    fixture = helpers.load_fixture('billing_requests')['create']
    helpers.stub_response(fixture)
    response = helpers.client.billing_requests.create(*fixture['url_params'])
    body = fixture['body']['billing_requests']

    assert isinstance(response, resources.BillingRequest)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is not None
    assert response.actions == body.get('actions')
    assert response.created_at == body.get('created_at')
    assert response.fallback_enabled == body.get('fallback_enabled')
    assert response.id == body.get('id')
    assert response.metadata == body.get('metadata')
    assert response.purpose_code == body.get('purpose_code')
    assert response.status == body.get('status')
    assert response.links.bank_authorisation == body.get('links')['bank_authorisation']
    assert response.links.creditor == body.get('links')['creditor']
    assert response.links.customer == body.get('links')['customer']
    assert response.links.customer_bank_account == body.get('links')['customer_bank_account']
    assert response.links.customer_billing_detail == body.get('links')['customer_billing_detail']
    assert response.links.mandate_request == body.get('links')['mandate_request']
    assert response.links.mandate_request_mandate == body.get('links')['mandate_request_mandate']
    assert response.links.organisation == body.get('links')['organisation']
    assert response.links.payment_provider == body.get('links')['payment_provider']
    assert response.links.payment_request == body.get('links')['payment_request']
    assert response.links.payment_request_payment == body.get('links')['payment_request_payment']
    assert response.mandate_request.authorisation_source == body.get('mandate_request')['authorisation_source']
    assert response.mandate_request.constraints == body.get('mandate_request')['constraints']
    assert response.mandate_request.currency == body.get('mandate_request')['currency']
    assert response.mandate_request.description == body.get('mandate_request')['description']
    assert response.mandate_request.links == body.get('mandate_request')['links']
    assert response.mandate_request.metadata == body.get('mandate_request')['metadata']
    assert response.mandate_request.scheme == body.get('mandate_request')['scheme']
    assert response.mandate_request.verify == body.get('mandate_request')['verify']
    assert response.payment_request.amount == body.get('payment_request')['amount']
    assert response.payment_request.app_fee == body.get('payment_request')['app_fee']
    assert response.payment_request.currency == body.get('payment_request')['currency']
    assert response.payment_request.description == body.get('payment_request')['description']
    assert response.payment_request.links == body.get('payment_request')['links']
    assert response.payment_request.metadata == body.get('payment_request')['metadata']
    assert response.payment_request.scheme == body.get('payment_request')['scheme']
    assert response.resources.customer == body.get('resources')['customer']
    assert response.resources.customer_bank_account == body.get('resources')['customer_bank_account']
    assert response.resources.customer_billing_detail == body.get('resources')['customer_billing_detail']

@responses.activate
def test_billing_requests_create_new_idempotency_key_for_each_call():
    fixture = helpers.load_fixture('billing_requests')['create']
    helpers.stub_response(fixture)
    helpers.client.billing_requests.create(*fixture['url_params'])
    helpers.client.billing_requests.create(*fixture['url_params'])
    assert responses.calls[0].request.headers.get('Idempotency-Key') != responses.calls[1].request.headers.get('Idempotency-Key')

def test_timeout_billing_requests_create_idempotency_conflict():
    create_fixture = helpers.load_fixture('billing_requests')['create']
    get_fixture = helpers.load_fixture('billing_requests')['get']
    with helpers.stub_timeout_then_idempotency_conflict(create_fixture, get_fixture) as rsps:
      response = helpers.client.billing_requests.create(*create_fixture['url_params'])
      assert len(rsps.calls) == 2

    assert isinstance(response, resources.BillingRequest)

@responses.activate
def test_timeout_billing_requests_create_retries():
    fixture = helpers.load_fixture('billing_requests')['create']
    with helpers.stub_timeout_then_response(fixture) as rsps:
      response = helpers.client.billing_requests.create(*fixture['url_params'])
      assert len(rsps.calls) == 2
      assert rsps.calls[0].request.headers.get('Idempotency-Key') == rsps.calls[1].request.headers.get('Idempotency-Key')
    body = fixture['body']['billing_requests']

    assert isinstance(response, resources.BillingRequest)

def test_502_billing_requests_create_retries():
    fixture = helpers.load_fixture('billing_requests')['create']
    with helpers.stub_502_then_response(fixture) as rsps:
      response = helpers.client.billing_requests.create(*fixture['url_params'])
      assert len(rsps.calls) == 2
      assert rsps.calls[0].request.headers.get('Idempotency-Key') == rsps.calls[1].request.headers.get('Idempotency-Key')
    body = fixture['body']['billing_requests']

    assert isinstance(response, resources.BillingRequest)
  

@responses.activate
def test_billing_requests_collect_customer_details():
    fixture = helpers.load_fixture('billing_requests')['collect_customer_details']
    helpers.stub_response(fixture)
    response = helpers.client.billing_requests.collect_customer_details(*fixture['url_params'])
    body = fixture['body']['billing_requests']

    assert isinstance(response, resources.BillingRequest)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is not None
    assert response.actions == body.get('actions')
    assert response.created_at == body.get('created_at')
    assert response.fallback_enabled == body.get('fallback_enabled')
    assert response.id == body.get('id')
    assert response.metadata == body.get('metadata')
    assert response.purpose_code == body.get('purpose_code')
    assert response.status == body.get('status')
    assert response.links.bank_authorisation == body.get('links')['bank_authorisation']
    assert response.links.creditor == body.get('links')['creditor']
    assert response.links.customer == body.get('links')['customer']
    assert response.links.customer_bank_account == body.get('links')['customer_bank_account']
    assert response.links.customer_billing_detail == body.get('links')['customer_billing_detail']
    assert response.links.mandate_request == body.get('links')['mandate_request']
    assert response.links.mandate_request_mandate == body.get('links')['mandate_request_mandate']
    assert response.links.organisation == body.get('links')['organisation']
    assert response.links.payment_provider == body.get('links')['payment_provider']
    assert response.links.payment_request == body.get('links')['payment_request']
    assert response.links.payment_request_payment == body.get('links')['payment_request_payment']
    assert response.mandate_request.authorisation_source == body.get('mandate_request')['authorisation_source']
    assert response.mandate_request.constraints == body.get('mandate_request')['constraints']
    assert response.mandate_request.currency == body.get('mandate_request')['currency']
    assert response.mandate_request.description == body.get('mandate_request')['description']
    assert response.mandate_request.links == body.get('mandate_request')['links']
    assert response.mandate_request.metadata == body.get('mandate_request')['metadata']
    assert response.mandate_request.scheme == body.get('mandate_request')['scheme']
    assert response.mandate_request.verify == body.get('mandate_request')['verify']
    assert response.payment_request.amount == body.get('payment_request')['amount']
    assert response.payment_request.app_fee == body.get('payment_request')['app_fee']
    assert response.payment_request.currency == body.get('payment_request')['currency']
    assert response.payment_request.description == body.get('payment_request')['description']
    assert response.payment_request.links == body.get('payment_request')['links']
    assert response.payment_request.metadata == body.get('payment_request')['metadata']
    assert response.payment_request.scheme == body.get('payment_request')['scheme']
    assert response.resources.customer == body.get('resources')['customer']
    assert response.resources.customer_bank_account == body.get('resources')['customer_bank_account']
    assert response.resources.customer_billing_detail == body.get('resources')['customer_billing_detail']

def test_timeout_billing_requests_collect_customer_details_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['collect_customer_details']
    with helpers.stub_timeout(fixture) as rsps:
      with pytest.raises(requests.ConnectTimeout):
        response = helpers.client.billing_requests.collect_customer_details(*fixture['url_params'])
      assert len(rsps.calls) == 1

def test_502_billing_requests_collect_customer_details_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['collect_customer_details']
    with helpers.stub_502(fixture) as rsps:
      with pytest.raises(MalformedResponseError):
        response = helpers.client.billing_requests.collect_customer_details(*fixture['url_params'])
      assert len(rsps.calls) == 1
  

@responses.activate
def test_billing_requests_collect_bank_account():
    fixture = helpers.load_fixture('billing_requests')['collect_bank_account']
    helpers.stub_response(fixture)
    response = helpers.client.billing_requests.collect_bank_account(*fixture['url_params'])
    body = fixture['body']['billing_requests']

    assert isinstance(response, resources.BillingRequest)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is not None
    assert response.actions == body.get('actions')
    assert response.created_at == body.get('created_at')
    assert response.fallback_enabled == body.get('fallback_enabled')
    assert response.id == body.get('id')
    assert response.metadata == body.get('metadata')
    assert response.purpose_code == body.get('purpose_code')
    assert response.status == body.get('status')
    assert response.links.bank_authorisation == body.get('links')['bank_authorisation']
    assert response.links.creditor == body.get('links')['creditor']
    assert response.links.customer == body.get('links')['customer']
    assert response.links.customer_bank_account == body.get('links')['customer_bank_account']
    assert response.links.customer_billing_detail == body.get('links')['customer_billing_detail']
    assert response.links.mandate_request == body.get('links')['mandate_request']
    assert response.links.mandate_request_mandate == body.get('links')['mandate_request_mandate']
    assert response.links.organisation == body.get('links')['organisation']
    assert response.links.payment_provider == body.get('links')['payment_provider']
    assert response.links.payment_request == body.get('links')['payment_request']
    assert response.links.payment_request_payment == body.get('links')['payment_request_payment']
    assert response.mandate_request.authorisation_source == body.get('mandate_request')['authorisation_source']
    assert response.mandate_request.constraints == body.get('mandate_request')['constraints']
    assert response.mandate_request.currency == body.get('mandate_request')['currency']
    assert response.mandate_request.description == body.get('mandate_request')['description']
    assert response.mandate_request.links == body.get('mandate_request')['links']
    assert response.mandate_request.metadata == body.get('mandate_request')['metadata']
    assert response.mandate_request.scheme == body.get('mandate_request')['scheme']
    assert response.mandate_request.verify == body.get('mandate_request')['verify']
    assert response.payment_request.amount == body.get('payment_request')['amount']
    assert response.payment_request.app_fee == body.get('payment_request')['app_fee']
    assert response.payment_request.currency == body.get('payment_request')['currency']
    assert response.payment_request.description == body.get('payment_request')['description']
    assert response.payment_request.links == body.get('payment_request')['links']
    assert response.payment_request.metadata == body.get('payment_request')['metadata']
    assert response.payment_request.scheme == body.get('payment_request')['scheme']
    assert response.resources.customer == body.get('resources')['customer']
    assert response.resources.customer_bank_account == body.get('resources')['customer_bank_account']
    assert response.resources.customer_billing_detail == body.get('resources')['customer_billing_detail']

def test_timeout_billing_requests_collect_bank_account_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['collect_bank_account']
    with helpers.stub_timeout(fixture) as rsps:
      with pytest.raises(requests.ConnectTimeout):
        response = helpers.client.billing_requests.collect_bank_account(*fixture['url_params'])
      assert len(rsps.calls) == 1

def test_502_billing_requests_collect_bank_account_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['collect_bank_account']
    with helpers.stub_502(fixture) as rsps:
      with pytest.raises(MalformedResponseError):
        response = helpers.client.billing_requests.collect_bank_account(*fixture['url_params'])
      assert len(rsps.calls) == 1
  

@responses.activate
def test_billing_requests_confirm_payer_details():
    fixture = helpers.load_fixture('billing_requests')['confirm_payer_details']
    helpers.stub_response(fixture)
    response = helpers.client.billing_requests.confirm_payer_details(*fixture['url_params'])
    body = fixture['body']['billing_requests']

    assert isinstance(response, resources.BillingRequest)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is not None
    assert response.actions == body.get('actions')
    assert response.created_at == body.get('created_at')
    assert response.fallback_enabled == body.get('fallback_enabled')
    assert response.id == body.get('id')
    assert response.metadata == body.get('metadata')
    assert response.purpose_code == body.get('purpose_code')
    assert response.status == body.get('status')
    assert response.links.bank_authorisation == body.get('links')['bank_authorisation']
    assert response.links.creditor == body.get('links')['creditor']
    assert response.links.customer == body.get('links')['customer']
    assert response.links.customer_bank_account == body.get('links')['customer_bank_account']
    assert response.links.customer_billing_detail == body.get('links')['customer_billing_detail']
    assert response.links.mandate_request == body.get('links')['mandate_request']
    assert response.links.mandate_request_mandate == body.get('links')['mandate_request_mandate']
    assert response.links.organisation == body.get('links')['organisation']
    assert response.links.payment_provider == body.get('links')['payment_provider']
    assert response.links.payment_request == body.get('links')['payment_request']
    assert response.links.payment_request_payment == body.get('links')['payment_request_payment']
    assert response.mandate_request.authorisation_source == body.get('mandate_request')['authorisation_source']
    assert response.mandate_request.constraints == body.get('mandate_request')['constraints']
    assert response.mandate_request.currency == body.get('mandate_request')['currency']
    assert response.mandate_request.description == body.get('mandate_request')['description']
    assert response.mandate_request.links == body.get('mandate_request')['links']
    assert response.mandate_request.metadata == body.get('mandate_request')['metadata']
    assert response.mandate_request.scheme == body.get('mandate_request')['scheme']
    assert response.mandate_request.verify == body.get('mandate_request')['verify']
    assert response.payment_request.amount == body.get('payment_request')['amount']
    assert response.payment_request.app_fee == body.get('payment_request')['app_fee']
    assert response.payment_request.currency == body.get('payment_request')['currency']
    assert response.payment_request.description == body.get('payment_request')['description']
    assert response.payment_request.links == body.get('payment_request')['links']
    assert response.payment_request.metadata == body.get('payment_request')['metadata']
    assert response.payment_request.scheme == body.get('payment_request')['scheme']
    assert response.resources.customer == body.get('resources')['customer']
    assert response.resources.customer_bank_account == body.get('resources')['customer_bank_account']
    assert response.resources.customer_billing_detail == body.get('resources')['customer_billing_detail']

def test_timeout_billing_requests_confirm_payer_details_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['confirm_payer_details']
    with helpers.stub_timeout(fixture) as rsps:
      with pytest.raises(requests.ConnectTimeout):
        response = helpers.client.billing_requests.confirm_payer_details(*fixture['url_params'])
      assert len(rsps.calls) == 1

def test_502_billing_requests_confirm_payer_details_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['confirm_payer_details']
    with helpers.stub_502(fixture) as rsps:
      with pytest.raises(MalformedResponseError):
        response = helpers.client.billing_requests.confirm_payer_details(*fixture['url_params'])
      assert len(rsps.calls) == 1
  

@responses.activate
def test_billing_requests_fulfil():
    fixture = helpers.load_fixture('billing_requests')['fulfil']
    helpers.stub_response(fixture)
    response = helpers.client.billing_requests.fulfil(*fixture['url_params'])
    body = fixture['body']['billing_requests']

    assert isinstance(response, resources.BillingRequest)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is not None
    assert response.actions == body.get('actions')
    assert response.created_at == body.get('created_at')
    assert response.fallback_enabled == body.get('fallback_enabled')
    assert response.id == body.get('id')
    assert response.metadata == body.get('metadata')
    assert response.purpose_code == body.get('purpose_code')
    assert response.status == body.get('status')
    assert response.links.bank_authorisation == body.get('links')['bank_authorisation']
    assert response.links.creditor == body.get('links')['creditor']
    assert response.links.customer == body.get('links')['customer']
    assert response.links.customer_bank_account == body.get('links')['customer_bank_account']
    assert response.links.customer_billing_detail == body.get('links')['customer_billing_detail']
    assert response.links.mandate_request == body.get('links')['mandate_request']
    assert response.links.mandate_request_mandate == body.get('links')['mandate_request_mandate']
    assert response.links.organisation == body.get('links')['organisation']
    assert response.links.payment_provider == body.get('links')['payment_provider']
    assert response.links.payment_request == body.get('links')['payment_request']
    assert response.links.payment_request_payment == body.get('links')['payment_request_payment']
    assert response.mandate_request.authorisation_source == body.get('mandate_request')['authorisation_source']
    assert response.mandate_request.constraints == body.get('mandate_request')['constraints']
    assert response.mandate_request.currency == body.get('mandate_request')['currency']
    assert response.mandate_request.description == body.get('mandate_request')['description']
    assert response.mandate_request.links == body.get('mandate_request')['links']
    assert response.mandate_request.metadata == body.get('mandate_request')['metadata']
    assert response.mandate_request.scheme == body.get('mandate_request')['scheme']
    assert response.mandate_request.verify == body.get('mandate_request')['verify']
    assert response.payment_request.amount == body.get('payment_request')['amount']
    assert response.payment_request.app_fee == body.get('payment_request')['app_fee']
    assert response.payment_request.currency == body.get('payment_request')['currency']
    assert response.payment_request.description == body.get('payment_request')['description']
    assert response.payment_request.links == body.get('payment_request')['links']
    assert response.payment_request.metadata == body.get('payment_request')['metadata']
    assert response.payment_request.scheme == body.get('payment_request')['scheme']
    assert response.resources.customer == body.get('resources')['customer']
    assert response.resources.customer_bank_account == body.get('resources')['customer_bank_account']
    assert response.resources.customer_billing_detail == body.get('resources')['customer_billing_detail']

def test_timeout_billing_requests_fulfil_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['fulfil']
    with helpers.stub_timeout(fixture) as rsps:
      with pytest.raises(requests.ConnectTimeout):
        response = helpers.client.billing_requests.fulfil(*fixture['url_params'])
      assert len(rsps.calls) == 1

def test_502_billing_requests_fulfil_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['fulfil']
    with helpers.stub_502(fixture) as rsps:
      with pytest.raises(MalformedResponseError):
        response = helpers.client.billing_requests.fulfil(*fixture['url_params'])
      assert len(rsps.calls) == 1
  

@responses.activate
def test_billing_requests_cancel():
    fixture = helpers.load_fixture('billing_requests')['cancel']
    helpers.stub_response(fixture)
    response = helpers.client.billing_requests.cancel(*fixture['url_params'])
    body = fixture['body']['billing_requests']

    assert isinstance(response, resources.BillingRequest)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is not None
    assert response.actions == body.get('actions')
    assert response.created_at == body.get('created_at')
    assert response.fallback_enabled == body.get('fallback_enabled')
    assert response.id == body.get('id')
    assert response.metadata == body.get('metadata')
    assert response.purpose_code == body.get('purpose_code')
    assert response.status == body.get('status')
    assert response.links.bank_authorisation == body.get('links')['bank_authorisation']
    assert response.links.creditor == body.get('links')['creditor']
    assert response.links.customer == body.get('links')['customer']
    assert response.links.customer_bank_account == body.get('links')['customer_bank_account']
    assert response.links.customer_billing_detail == body.get('links')['customer_billing_detail']
    assert response.links.mandate_request == body.get('links')['mandate_request']
    assert response.links.mandate_request_mandate == body.get('links')['mandate_request_mandate']
    assert response.links.organisation == body.get('links')['organisation']
    assert response.links.payment_provider == body.get('links')['payment_provider']
    assert response.links.payment_request == body.get('links')['payment_request']
    assert response.links.payment_request_payment == body.get('links')['payment_request_payment']
    assert response.mandate_request.authorisation_source == body.get('mandate_request')['authorisation_source']
    assert response.mandate_request.constraints == body.get('mandate_request')['constraints']
    assert response.mandate_request.currency == body.get('mandate_request')['currency']
    assert response.mandate_request.description == body.get('mandate_request')['description']
    assert response.mandate_request.links == body.get('mandate_request')['links']
    assert response.mandate_request.metadata == body.get('mandate_request')['metadata']
    assert response.mandate_request.scheme == body.get('mandate_request')['scheme']
    assert response.mandate_request.verify == body.get('mandate_request')['verify']
    assert response.payment_request.amount == body.get('payment_request')['amount']
    assert response.payment_request.app_fee == body.get('payment_request')['app_fee']
    assert response.payment_request.currency == body.get('payment_request')['currency']
    assert response.payment_request.description == body.get('payment_request')['description']
    assert response.payment_request.links == body.get('payment_request')['links']
    assert response.payment_request.metadata == body.get('payment_request')['metadata']
    assert response.payment_request.scheme == body.get('payment_request')['scheme']
    assert response.resources.customer == body.get('resources')['customer']
    assert response.resources.customer_bank_account == body.get('resources')['customer_bank_account']
    assert response.resources.customer_billing_detail == body.get('resources')['customer_billing_detail']

def test_timeout_billing_requests_cancel_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['cancel']
    with helpers.stub_timeout(fixture) as rsps:
      with pytest.raises(requests.ConnectTimeout):
        response = helpers.client.billing_requests.cancel(*fixture['url_params'])
      assert len(rsps.calls) == 1

def test_502_billing_requests_cancel_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['cancel']
    with helpers.stub_502(fixture) as rsps:
      with pytest.raises(MalformedResponseError):
        response = helpers.client.billing_requests.cancel(*fixture['url_params'])
      assert len(rsps.calls) == 1
  

@responses.activate
def test_billing_requests_list():
    fixture = helpers.load_fixture('billing_requests')['list']
    helpers.stub_response(fixture)
    response = helpers.client.billing_requests.list(*fixture['url_params'])
    body = fixture['body']['billing_requests']

    assert isinstance(response, list_response.ListResponse)
    assert isinstance(response.records[0], resources.BillingRequest)

    assert response.before == fixture['body']['meta']['cursors']['before']
    assert response.after == fixture['body']['meta']['cursors']['after']
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is None
    assert [r.actions for r in response.records] == [b.get('actions') for b in body]
    assert [r.created_at for r in response.records] == [b.get('created_at') for b in body]
    assert [r.fallback_enabled for r in response.records] == [b.get('fallback_enabled') for b in body]
    assert [r.id for r in response.records] == [b.get('id') for b in body]
    assert [r.metadata for r in response.records] == [b.get('metadata') for b in body]
    assert [r.purpose_code for r in response.records] == [b.get('purpose_code') for b in body]
    assert [r.status for r in response.records] == [b.get('status') for b in body]

@responses.activate
def test_timeout_billing_requests_list_retries():
    fixture = helpers.load_fixture('billing_requests')['list']
    with helpers.stub_timeout_then_response(fixture) as rsps:
      response = helpers.client.billing_requests.list(*fixture['url_params'])
      assert len(rsps.calls) == 2
      assert rsps.calls[0].request.headers.get('Idempotency-Key') == rsps.calls[1].request.headers.get('Idempotency-Key')
    body = fixture['body']['billing_requests']

    assert isinstance(response, list_response.ListResponse)
    assert isinstance(response.records[0], resources.BillingRequest)

    assert response.before == fixture['body']['meta']['cursors']['before']
    assert response.after == fixture['body']['meta']['cursors']['after']

def test_502_billing_requests_list_retries():
    fixture = helpers.load_fixture('billing_requests')['list']
    with helpers.stub_502_then_response(fixture) as rsps:
      response = helpers.client.billing_requests.list(*fixture['url_params'])
      assert len(rsps.calls) == 2
      assert rsps.calls[0].request.headers.get('Idempotency-Key') == rsps.calls[1].request.headers.get('Idempotency-Key')
    body = fixture['body']['billing_requests']

    assert isinstance(response, list_response.ListResponse)
    assert isinstance(response.records[0], resources.BillingRequest)

    assert response.before == fixture['body']['meta']['cursors']['before']
    assert response.after == fixture['body']['meta']['cursors']['after']

@responses.activate
def test_billing_requests_all():
    fixture = helpers.load_fixture('billing_requests')['list']

    def callback(request):
        if 'after=123' in request.url:
          fixture['body']['meta']['cursors']['after'] = None
        else:
          fixture['body']['meta']['cursors']['after'] = '123'
        return [200, {}, json.dumps(fixture['body'])]

    url = 'http://example.com' + fixture['path_template']
    responses.add_callback(fixture['method'], url, callback)

    all_records = list(helpers.client.billing_requests.all())
    assert len(all_records) == len(fixture['body']['billing_requests']) * 2
    for record in all_records:
      assert isinstance(record, resources.BillingRequest)
    
  

@responses.activate
def test_billing_requests_get():
    fixture = helpers.load_fixture('billing_requests')['get']
    helpers.stub_response(fixture)
    response = helpers.client.billing_requests.get(*fixture['url_params'])
    body = fixture['body']['billing_requests']

    assert isinstance(response, resources.BillingRequest)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is None
    assert response.actions == body.get('actions')
    assert response.created_at == body.get('created_at')
    assert response.fallback_enabled == body.get('fallback_enabled')
    assert response.id == body.get('id')
    assert response.metadata == body.get('metadata')
    assert response.purpose_code == body.get('purpose_code')
    assert response.status == body.get('status')
    assert response.links.bank_authorisation == body.get('links')['bank_authorisation']
    assert response.links.creditor == body.get('links')['creditor']
    assert response.links.customer == body.get('links')['customer']
    assert response.links.customer_bank_account == body.get('links')['customer_bank_account']
    assert response.links.customer_billing_detail == body.get('links')['customer_billing_detail']
    assert response.links.mandate_request == body.get('links')['mandate_request']
    assert response.links.mandate_request_mandate == body.get('links')['mandate_request_mandate']
    assert response.links.organisation == body.get('links')['organisation']
    assert response.links.payment_provider == body.get('links')['payment_provider']
    assert response.links.payment_request == body.get('links')['payment_request']
    assert response.links.payment_request_payment == body.get('links')['payment_request_payment']
    assert response.mandate_request.authorisation_source == body.get('mandate_request')['authorisation_source']
    assert response.mandate_request.constraints == body.get('mandate_request')['constraints']
    assert response.mandate_request.currency == body.get('mandate_request')['currency']
    assert response.mandate_request.description == body.get('mandate_request')['description']
    assert response.mandate_request.links == body.get('mandate_request')['links']
    assert response.mandate_request.metadata == body.get('mandate_request')['metadata']
    assert response.mandate_request.scheme == body.get('mandate_request')['scheme']
    assert response.mandate_request.verify == body.get('mandate_request')['verify']
    assert response.payment_request.amount == body.get('payment_request')['amount']
    assert response.payment_request.app_fee == body.get('payment_request')['app_fee']
    assert response.payment_request.currency == body.get('payment_request')['currency']
    assert response.payment_request.description == body.get('payment_request')['description']
    assert response.payment_request.links == body.get('payment_request')['links']
    assert response.payment_request.metadata == body.get('payment_request')['metadata']
    assert response.payment_request.scheme == body.get('payment_request')['scheme']
    assert response.resources.customer == body.get('resources')['customer']
    assert response.resources.customer_bank_account == body.get('resources')['customer_bank_account']
    assert response.resources.customer_billing_detail == body.get('resources')['customer_billing_detail']

@responses.activate
def test_timeout_billing_requests_get_retries():
    fixture = helpers.load_fixture('billing_requests')['get']
    with helpers.stub_timeout_then_response(fixture) as rsps:
      response = helpers.client.billing_requests.get(*fixture['url_params'])
      assert len(rsps.calls) == 2
      assert rsps.calls[0].request.headers.get('Idempotency-Key') == rsps.calls[1].request.headers.get('Idempotency-Key')
    body = fixture['body']['billing_requests']

    assert isinstance(response, resources.BillingRequest)

def test_502_billing_requests_get_retries():
    fixture = helpers.load_fixture('billing_requests')['get']
    with helpers.stub_502_then_response(fixture) as rsps:
      response = helpers.client.billing_requests.get(*fixture['url_params'])
      assert len(rsps.calls) == 2
      assert rsps.calls[0].request.headers.get('Idempotency-Key') == rsps.calls[1].request.headers.get('Idempotency-Key')
    body = fixture['body']['billing_requests']

    assert isinstance(response, resources.BillingRequest)
  

@responses.activate
def test_billing_requests_notify():
    fixture = helpers.load_fixture('billing_requests')['notify']
    helpers.stub_response(fixture)
    response = helpers.client.billing_requests.notify(*fixture['url_params'])
    body = fixture['body']['billing_requests']

    assert isinstance(response, resources.BillingRequest)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is not None
    assert response.actions == body.get('actions')
    assert response.created_at == body.get('created_at')
    assert response.fallback_enabled == body.get('fallback_enabled')
    assert response.id == body.get('id')
    assert response.metadata == body.get('metadata')
    assert response.purpose_code == body.get('purpose_code')
    assert response.status == body.get('status')
    assert response.links.bank_authorisation == body.get('links')['bank_authorisation']
    assert response.links.creditor == body.get('links')['creditor']
    assert response.links.customer == body.get('links')['customer']
    assert response.links.customer_bank_account == body.get('links')['customer_bank_account']
    assert response.links.customer_billing_detail == body.get('links')['customer_billing_detail']
    assert response.links.mandate_request == body.get('links')['mandate_request']
    assert response.links.mandate_request_mandate == body.get('links')['mandate_request_mandate']
    assert response.links.organisation == body.get('links')['organisation']
    assert response.links.payment_provider == body.get('links')['payment_provider']
    assert response.links.payment_request == body.get('links')['payment_request']
    assert response.links.payment_request_payment == body.get('links')['payment_request_payment']
    assert response.mandate_request.authorisation_source == body.get('mandate_request')['authorisation_source']
    assert response.mandate_request.constraints == body.get('mandate_request')['constraints']
    assert response.mandate_request.currency == body.get('mandate_request')['currency']
    assert response.mandate_request.description == body.get('mandate_request')['description']
    assert response.mandate_request.links == body.get('mandate_request')['links']
    assert response.mandate_request.metadata == body.get('mandate_request')['metadata']
    assert response.mandate_request.scheme == body.get('mandate_request')['scheme']
    assert response.mandate_request.verify == body.get('mandate_request')['verify']
    assert response.payment_request.amount == body.get('payment_request')['amount']
    assert response.payment_request.app_fee == body.get('payment_request')['app_fee']
    assert response.payment_request.currency == body.get('payment_request')['currency']
    assert response.payment_request.description == body.get('payment_request')['description']
    assert response.payment_request.links == body.get('payment_request')['links']
    assert response.payment_request.metadata == body.get('payment_request')['metadata']
    assert response.payment_request.scheme == body.get('payment_request')['scheme']
    assert response.resources.customer == body.get('resources')['customer']
    assert response.resources.customer_bank_account == body.get('resources')['customer_bank_account']
    assert response.resources.customer_billing_detail == body.get('resources')['customer_billing_detail']

def test_timeout_billing_requests_notify_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['notify']
    with helpers.stub_timeout(fixture) as rsps:
      with pytest.raises(requests.ConnectTimeout):
        response = helpers.client.billing_requests.notify(*fixture['url_params'])
      assert len(rsps.calls) == 1

def test_502_billing_requests_notify_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['notify']
    with helpers.stub_502(fixture) as rsps:
      with pytest.raises(MalformedResponseError):
        response = helpers.client.billing_requests.notify(*fixture['url_params'])
      assert len(rsps.calls) == 1
  

@responses.activate
def test_billing_requests_fallback():
    fixture = helpers.load_fixture('billing_requests')['fallback']
    helpers.stub_response(fixture)
    response = helpers.client.billing_requests.fallback(*fixture['url_params'])
    body = fixture['body']['billing_requests']

    assert isinstance(response, resources.BillingRequest)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is not None
    assert response.actions == body.get('actions')
    assert response.created_at == body.get('created_at')
    assert response.fallback_enabled == body.get('fallback_enabled')
    assert response.id == body.get('id')
    assert response.metadata == body.get('metadata')
    assert response.purpose_code == body.get('purpose_code')
    assert response.status == body.get('status')
    assert response.links.bank_authorisation == body.get('links')['bank_authorisation']
    assert response.links.creditor == body.get('links')['creditor']
    assert response.links.customer == body.get('links')['customer']
    assert response.links.customer_bank_account == body.get('links')['customer_bank_account']
    assert response.links.customer_billing_detail == body.get('links')['customer_billing_detail']
    assert response.links.mandate_request == body.get('links')['mandate_request']
    assert response.links.mandate_request_mandate == body.get('links')['mandate_request_mandate']
    assert response.links.organisation == body.get('links')['organisation']
    assert response.links.payment_provider == body.get('links')['payment_provider']
    assert response.links.payment_request == body.get('links')['payment_request']
    assert response.links.payment_request_payment == body.get('links')['payment_request_payment']
    assert response.mandate_request.authorisation_source == body.get('mandate_request')['authorisation_source']
    assert response.mandate_request.constraints == body.get('mandate_request')['constraints']
    assert response.mandate_request.currency == body.get('mandate_request')['currency']
    assert response.mandate_request.description == body.get('mandate_request')['description']
    assert response.mandate_request.links == body.get('mandate_request')['links']
    assert response.mandate_request.metadata == body.get('mandate_request')['metadata']
    assert response.mandate_request.scheme == body.get('mandate_request')['scheme']
    assert response.mandate_request.verify == body.get('mandate_request')['verify']
    assert response.payment_request.amount == body.get('payment_request')['amount']
    assert response.payment_request.app_fee == body.get('payment_request')['app_fee']
    assert response.payment_request.currency == body.get('payment_request')['currency']
    assert response.payment_request.description == body.get('payment_request')['description']
    assert response.payment_request.links == body.get('payment_request')['links']
    assert response.payment_request.metadata == body.get('payment_request')['metadata']
    assert response.payment_request.scheme == body.get('payment_request')['scheme']
    assert response.resources.customer == body.get('resources')['customer']
    assert response.resources.customer_bank_account == body.get('resources')['customer_bank_account']
    assert response.resources.customer_billing_detail == body.get('resources')['customer_billing_detail']

def test_timeout_billing_requests_fallback_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['fallback']
    with helpers.stub_timeout(fixture) as rsps:
      with pytest.raises(requests.ConnectTimeout):
        response = helpers.client.billing_requests.fallback(*fixture['url_params'])
      assert len(rsps.calls) == 1

def test_502_billing_requests_fallback_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['fallback']
    with helpers.stub_502(fixture) as rsps:
      with pytest.raises(MalformedResponseError):
        response = helpers.client.billing_requests.fallback(*fixture['url_params'])
      assert len(rsps.calls) == 1
  

@responses.activate
def test_billing_requests_choose_currency():
    fixture = helpers.load_fixture('billing_requests')['choose_currency']
    helpers.stub_response(fixture)
    response = helpers.client.billing_requests.choose_currency(*fixture['url_params'])
    body = fixture['body']['billing_requests']

    assert isinstance(response, resources.BillingRequest)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is not None
    assert response.actions == body.get('actions')
    assert response.created_at == body.get('created_at')
    assert response.fallback_enabled == body.get('fallback_enabled')
    assert response.id == body.get('id')
    assert response.metadata == body.get('metadata')
    assert response.purpose_code == body.get('purpose_code')
    assert response.status == body.get('status')
    assert response.links.bank_authorisation == body.get('links')['bank_authorisation']
    assert response.links.creditor == body.get('links')['creditor']
    assert response.links.customer == body.get('links')['customer']
    assert response.links.customer_bank_account == body.get('links')['customer_bank_account']
    assert response.links.customer_billing_detail == body.get('links')['customer_billing_detail']
    assert response.links.mandate_request == body.get('links')['mandate_request']
    assert response.links.mandate_request_mandate == body.get('links')['mandate_request_mandate']
    assert response.links.organisation == body.get('links')['organisation']
    assert response.links.payment_provider == body.get('links')['payment_provider']
    assert response.links.payment_request == body.get('links')['payment_request']
    assert response.links.payment_request_payment == body.get('links')['payment_request_payment']
    assert response.mandate_request.authorisation_source == body.get('mandate_request')['authorisation_source']
    assert response.mandate_request.constraints == body.get('mandate_request')['constraints']
    assert response.mandate_request.currency == body.get('mandate_request')['currency']
    assert response.mandate_request.description == body.get('mandate_request')['description']
    assert response.mandate_request.links == body.get('mandate_request')['links']
    assert response.mandate_request.metadata == body.get('mandate_request')['metadata']
    assert response.mandate_request.scheme == body.get('mandate_request')['scheme']
    assert response.mandate_request.verify == body.get('mandate_request')['verify']
    assert response.payment_request.amount == body.get('payment_request')['amount']
    assert response.payment_request.app_fee == body.get('payment_request')['app_fee']
    assert response.payment_request.currency == body.get('payment_request')['currency']
    assert response.payment_request.description == body.get('payment_request')['description']
    assert response.payment_request.links == body.get('payment_request')['links']
    assert response.payment_request.metadata == body.get('payment_request')['metadata']
    assert response.payment_request.scheme == body.get('payment_request')['scheme']
    assert response.resources.customer == body.get('resources')['customer']
    assert response.resources.customer_bank_account == body.get('resources')['customer_bank_account']
    assert response.resources.customer_billing_detail == body.get('resources')['customer_billing_detail']

def test_timeout_billing_requests_choose_currency_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['choose_currency']
    with helpers.stub_timeout(fixture) as rsps:
      with pytest.raises(requests.ConnectTimeout):
        response = helpers.client.billing_requests.choose_currency(*fixture['url_params'])
      assert len(rsps.calls) == 1

def test_502_billing_requests_choose_currency_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['choose_currency']
    with helpers.stub_502(fixture) as rsps:
      with pytest.raises(MalformedResponseError):
        response = helpers.client.billing_requests.choose_currency(*fixture['url_params'])
      assert len(rsps.calls) == 1
  

@responses.activate
def test_billing_requests_select_institution():
    fixture = helpers.load_fixture('billing_requests')['select_institution']
    helpers.stub_response(fixture)
    response = helpers.client.billing_requests.select_institution(*fixture['url_params'])
    body = fixture['body']['billing_requests']

    assert isinstance(response, resources.BillingRequest)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is not None
    assert response.actions == body.get('actions')
    assert response.created_at == body.get('created_at')
    assert response.fallback_enabled == body.get('fallback_enabled')
    assert response.id == body.get('id')
    assert response.metadata == body.get('metadata')
    assert response.purpose_code == body.get('purpose_code')
    assert response.status == body.get('status')
    assert response.links.bank_authorisation == body.get('links')['bank_authorisation']
    assert response.links.creditor == body.get('links')['creditor']
    assert response.links.customer == body.get('links')['customer']
    assert response.links.customer_bank_account == body.get('links')['customer_bank_account']
    assert response.links.customer_billing_detail == body.get('links')['customer_billing_detail']
    assert response.links.mandate_request == body.get('links')['mandate_request']
    assert response.links.mandate_request_mandate == body.get('links')['mandate_request_mandate']
    assert response.links.organisation == body.get('links')['organisation']
    assert response.links.payment_provider == body.get('links')['payment_provider']
    assert response.links.payment_request == body.get('links')['payment_request']
    assert response.links.payment_request_payment == body.get('links')['payment_request_payment']
    assert response.mandate_request.authorisation_source == body.get('mandate_request')['authorisation_source']
    assert response.mandate_request.constraints == body.get('mandate_request')['constraints']
    assert response.mandate_request.currency == body.get('mandate_request')['currency']
    assert response.mandate_request.description == body.get('mandate_request')['description']
    assert response.mandate_request.links == body.get('mandate_request')['links']
    assert response.mandate_request.metadata == body.get('mandate_request')['metadata']
    assert response.mandate_request.scheme == body.get('mandate_request')['scheme']
    assert response.mandate_request.verify == body.get('mandate_request')['verify']
    assert response.payment_request.amount == body.get('payment_request')['amount']
    assert response.payment_request.app_fee == body.get('payment_request')['app_fee']
    assert response.payment_request.currency == body.get('payment_request')['currency']
    assert response.payment_request.description == body.get('payment_request')['description']
    assert response.payment_request.links == body.get('payment_request')['links']
    assert response.payment_request.metadata == body.get('payment_request')['metadata']
    assert response.payment_request.scheme == body.get('payment_request')['scheme']
    assert response.resources.customer == body.get('resources')['customer']
    assert response.resources.customer_bank_account == body.get('resources')['customer_bank_account']
    assert response.resources.customer_billing_detail == body.get('resources')['customer_billing_detail']

def test_timeout_billing_requests_select_institution_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['select_institution']
    with helpers.stub_timeout(fixture) as rsps:
      with pytest.raises(requests.ConnectTimeout):
        response = helpers.client.billing_requests.select_institution(*fixture['url_params'])
      assert len(rsps.calls) == 1

def test_502_billing_requests_select_institution_doesnt_retry():
    fixture = helpers.load_fixture('billing_requests')['select_institution']
    with helpers.stub_502(fixture) as rsps:
      with pytest.raises(MalformedResponseError):
        response = helpers.client.billing_requests.select_institution(*fixture['url_params'])
      assert len(rsps.calls) == 1
  
