# 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_bank_authorisations_create():
    fixture = helpers.load_fixture('bank_authorisations')['create']
    helpers.stub_response(fixture)
    response = helpers.client.bank_authorisations.create(*fixture['url_params'])
    body = fixture['body']['bank_authorisations']

    assert isinstance(response, resources.BankAuthorisation)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is not None
    assert response.authorisation_type == body.get('authorisation_type')
    assert response.authorised_at == body.get('authorised_at')
    assert response.created_at == body.get('created_at')
    assert response.expires_at == body.get('expires_at')
    assert response.id == body.get('id')
    assert response.last_visited_at == body.get('last_visited_at')
    assert response.qr_code_url == body.get('qr_code_url')
    assert response.redirect_uri == body.get('redirect_uri')
    assert response.url == body.get('url')
    assert response.links.billing_request == body.get('links')['billing_request']
    assert response.links.institution == body.get('links')['institution']

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

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

    assert isinstance(response, resources.BankAuthorisation)

@responses.activate
def test_timeout_bank_authorisations_create_retries():
    fixture = helpers.load_fixture('bank_authorisations')['create']
    with helpers.stub_timeout_then_response(fixture) as rsps:
      response = helpers.client.bank_authorisations.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']['bank_authorisations']

    assert isinstance(response, resources.BankAuthorisation)

def test_502_bank_authorisations_create_retries():
    fixture = helpers.load_fixture('bank_authorisations')['create']
    with helpers.stub_502_then_response(fixture) as rsps:
      response = helpers.client.bank_authorisations.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']['bank_authorisations']

    assert isinstance(response, resources.BankAuthorisation)
  

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

    assert isinstance(response, resources.BankAuthorisation)
    assert responses.calls[-1].request.headers.get('Idempotency-Key') is None
    assert response.authorisation_type == body.get('authorisation_type')
    assert response.authorised_at == body.get('authorised_at')
    assert response.created_at == body.get('created_at')
    assert response.expires_at == body.get('expires_at')
    assert response.id == body.get('id')
    assert response.last_visited_at == body.get('last_visited_at')
    assert response.qr_code_url == body.get('qr_code_url')
    assert response.redirect_uri == body.get('redirect_uri')
    assert response.url == body.get('url')
    assert response.links.billing_request == body.get('links')['billing_request']
    assert response.links.institution == body.get('links')['institution']

@responses.activate
def test_timeout_bank_authorisations_get_retries():
    fixture = helpers.load_fixture('bank_authorisations')['get']
    with helpers.stub_timeout_then_response(fixture) as rsps:
      response = helpers.client.bank_authorisations.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']['bank_authorisations']

    assert isinstance(response, resources.BankAuthorisation)

def test_502_bank_authorisations_get_retries():
    fixture = helpers.load_fixture('bank_authorisations')['get']
    with helpers.stub_502_then_response(fixture) as rsps:
      response = helpers.client.bank_authorisations.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']['bank_authorisations']

    assert isinstance(response, resources.BankAuthorisation)
  
