Skip to content

Commit

Permalink
Usando TLSv1 nas requisições.
Browse files Browse the repository at this point in the history
  • Loading branch information
allisson committed Jan 14, 2016
1 parent 3efd354 commit ac82506
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
18 changes: 18 additions & 0 deletions cielo_webservice/adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import ssl

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager


class TLSv1HttpAdapter(HTTPAdapter):
""""Transport adapter" that allows us to use TLSv1."""

def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = PoolManager(
num_pools=connections,
maxsize=maxsize,
block=block,
ssl_version=ssl.PROTOCOL_TLSv1
)
11 changes: 7 additions & 4 deletions cielo_webservice/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Cartao, Transacao, Comercial, Erro, xml_to_object
)
from cielo_webservice.exceptions import CieloRequestError
from cielo_webservice.adapter import TLSv1HttpAdapter


BASE_URL = 'https://ecommerce.cielo.com.br/servicos/ecommwsec.do'
Expand All @@ -28,6 +29,8 @@ def __init__(self, sandbox=False):
if sandbox:
self.base_url = SANDBOX_BASE_URL
self.template_env = self.create_template_env()
self.session = requests.Session()
self.session.mount('https://', TLSv1HttpAdapter())

def create_template_env(self):
template_dirs = []
Expand All @@ -54,7 +57,7 @@ def autorizar(self, transacao):
xml = self.render_template(
'transacao.xml', id=str(uuid.uuid4()), transacao=transacao
)
response = requests.post(self.base_url, data={'mensagem': xml})
response = self.session.post(self.base_url, data={'mensagem': xml})
object_data = xml_to_object(response.text)

if isinstance(object_data, Erro):
Expand All @@ -80,7 +83,7 @@ def capturar(self, tid=None, comercial=None, valor=None,
'captura.xml', id=str(uuid.uuid4()), tid=tid, comercial=comercial,
valor=valor, taxa_embarque=taxa_embarque
)
response = requests.post(self.base_url, data={'mensagem': xml})
response = self.session.post(self.base_url, data={'mensagem': xml})
object_data = xml_to_object(response.text)

if isinstance(object_data, Erro):
Expand All @@ -104,7 +107,7 @@ def gerar_token(self, comercial=None, cartao=None):
'token.xml', id=str(uuid.uuid4()), comercial=comercial,
cartao=cartao
)
response = requests.post(self.base_url, data={'mensagem': xml})
response = self.session.post(self.base_url, data={'mensagem': xml})
object_data = xml_to_object(response.text)

if isinstance(object_data, Erro):
Expand All @@ -129,7 +132,7 @@ def cancelar(self, tid=None, comercial=None, valor=None):
'cancelamento.xml', id=str(uuid.uuid4()), tid=tid,
comercial=comercial, valor=valor
)
response = requests.post(self.base_url, data={'mensagem': xml})
response = self.session.post(self.base_url, data={'mensagem': xml})
object_data = xml_to_object(response.text)

if isinstance(object_data, Erro):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import cielo_webservice

requires = [
'requests',
'requests>=2.9.1',
'jinja2',
'six',
'xmltodict',
Expand Down
18 changes: 9 additions & 9 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_render_template(self):
self.request.render_template('notfound.xml', id=str(uuid.uuid4()))
assert 'notfound.xml' in str(excinfo.value)

@mock.patch('requests.post', autorizar_mocked_response)
@mock.patch('requests.Session.post', autorizar_mocked_response)
def test_autorizar(self):
with pytest.raises(TypeError) as excinfo:
self.request.autorizar('transacao')
Expand All @@ -103,7 +103,7 @@ def test_autorizar(self):
transacao.pan, 'IqVz7P9zaIgTYdU41HaW/OB/d7Idwttqwb2vaTt8MT0='
)

@mock.patch('requests.post', erro_mocked_response)
@mock.patch('requests.Session.post', erro_mocked_response)
def test_autorizar_com_erro(self):
transacao = Transacao(
comercial=self.comercial, cartao=self.cartao, pedido=self.pedido,
Expand All @@ -113,7 +113,7 @@ def test_autorizar_com_erro(self):
self.request.autorizar(transacao)
assert '000 - Mensagem' in str(excinfo.value)

@mock.patch('requests.post', autorizar_com_rejeicao_mocked_response)
@mock.patch('requests.Session.post', autorizar_com_rejeicao_mocked_response)
def test_autorizar_com_rejeicao(self):
self.pedido = Pedido(
numero='1234', valor=553482920, moeda=986,
Expand All @@ -133,7 +133,7 @@ def test_autorizar_com_rejeicao(self):
transacao.autorizacao.mensagem, 'Autorização negada'
)

@mock.patch('requests.post', capturar_mocked_response)
@mock.patch('requests.Session.post', capturar_mocked_response)
def test_capturar(self):
with pytest.raises(TypeError) as excinfo:
self.request.capturar(tid=1, comercial=self.comercial)
Expand All @@ -155,15 +155,15 @@ def test_capturar(self):
self.assertTrue(isinstance(transacao, Transacao))
self.assertTrue(isinstance(transacao.captura, Captura))

@mock.patch('requests.post', erro_mocked_response)
@mock.patch('requests.Session.post', erro_mocked_response)
def test_capturar_com_erro(self):
with pytest.raises(CieloRequestError) as excinfo:
self.request.capturar(
tid='10069930694849051001', comercial=self.comercial
)
assert '000 - Mensagem' in str(excinfo.value)

@mock.patch('requests.post', token_mocked_response)
@mock.patch('requests.Session.post', token_mocked_response)
def test_gerar_token(self):
with pytest.raises(TypeError) as excinfo:
self.request.gerar_token(comercial=1, cartao=self.cartao)
Expand All @@ -178,15 +178,15 @@ def test_gerar_token(self):
)
self.assertTrue(isinstance(token, Token))

@mock.patch('requests.post', erro_mocked_response)
@mock.patch('requests.Session.post', erro_mocked_response)
def test_gerar_token_com_erro(self):
with pytest.raises(CieloRequestError) as excinfo:
self.request.gerar_token(
comercial=self.comercial, cartao=self.cartao
)
assert '000 - Mensagem' in str(excinfo.value)

@mock.patch('requests.post', cancelar_mocked_response)
@mock.patch('requests.Session.post', cancelar_mocked_response)
def test_cancelar(self):
with pytest.raises(TypeError) as excinfo:
self.request.cancelar(tid=1, comercial=self.comercial)
Expand All @@ -208,7 +208,7 @@ def test_cancelar(self):
self.assertTrue(isinstance(transacao, Transacao))
self.assertTrue(isinstance(transacao.cancelamento, Cancelamento))

@mock.patch('requests.post', erro_mocked_response)
@mock.patch('requests.Session.post', erro_mocked_response)
def test_cancelar_com_erro(self):
with pytest.raises(CieloRequestError) as excinfo:
self.request.cancelar(
Expand Down

0 comments on commit ac82506

Please sign in to comment.