Skip to content

Commit

Permalink
Merge pull request #9 from cuenca-mx/some-fixes
Browse files Browse the repository at this point in the history
Some fixes
  • Loading branch information
agarrido19 authored Feb 5, 2020
2 parents 06d2e6d + 9011b59 commit 1544455
Show file tree
Hide file tree
Showing 8 changed files with 2,900 additions and 7 deletions.
9 changes: 7 additions & 2 deletions dhlmex/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
from typing import Any, ClassVar, Dict, Optional

Expand All @@ -14,6 +15,8 @@
)
DHL_CERT = 'prepaid-dhl-com-mx.pem'

logging.basicConfig(level=logging.DEBUG)


class Client:

Expand All @@ -34,7 +37,7 @@ def __init__(
self.session = Session()
self.session.headers['User-Agent'] = USER_AGENT
if os.getenv('DEBUG'):
print(f'Client using Charles certificate')
logging.debug(f'Client using Charles certificate')
self.session.verify = DHL_CERT
self._login(username, password)

Expand All @@ -59,12 +62,14 @@ def _login(self, username: str, password: str) -> Response:
else:
raise httpe
except SSLError:
raise DhlmexException('Cient on debug, but Charles not running')
raise DhlmexException('Client on debug, but Charles not running')
# DHL always return 200 although there is an existing session
if 'Ya existe una sesión' in resp.text:
raise DhlmexException(
f'There is an exisiting session on DHL for {username}'
)
if 'Verifique su usuario' in resp.text:
raise DhlmexException('Invalid credentials')
return resp

def _logout(self) -> Response:
Expand Down
14 changes: 12 additions & 2 deletions dhlmex/resources/guides.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,19 @@ def _fill_guide_table(
fill_data['javax.faces.ViewState'] = view_state
fill_data['datos:j_id105'] = 'datos:j_id105'

self._client.post(self._urls['capture'], fill_data)
msg = self._validate_data(fill_data)
if msg:
raise DhlmexException(f'Invalid data: {msg}')
else:
return fill_data['javax.faces.ViewState']

return fill_data['javax.faces.ViewState']
def _validate_data(self, fill_data: Dict) -> str:
resp = self._client.post(self._urls['capture'], fill_data)
msg = ''
if 'messageError' in resp.text:
soup = BeautifulSoup(resp.text, features='html.parser')
msg = soup.find('span', {'class': 'rich-messages-label'}).text
return msg

def _confirm_capture(self, view_state: str) -> Response:
confirm_data = {
Expand Down
2 changes: 1 addition & 1 deletion dhlmex/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.4' # pragma: no cover
__version__ = '0.0.5' # pragma: no cover
318 changes: 318 additions & 0 deletions tests/cassettes/test_invalid_creds.yaml

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ def invalid_destination() -> Destination:
)


@pytest.fixture
def missing_data() -> Destination:
return Destination(
company='GREGORIO CASTRO',
contact='GREGORIO CASTRO',
email='[email protected]',
phone='550909090',
address1='REFORMA 222',
postal_code='06600',
neighborhood='JUAREZ',
city='',
state='CDMX',
)


@pytest.fixture
def invalid_postal_code() -> Destination:
return Destination(
Expand Down
2,520 changes: 2,520 additions & 0 deletions tests/resources/cassettes/test_missing_city.yaml

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions tests/resources/test_invalid_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@

@pytest.mark.vcr
def test_invalid_destination(client, origin, invalid_destination, details):

with pytest.raises(DhlmexException) as execinfo:
client.guides.create_guide(origin, invalid_destination, details)
assert str(execinfo) == 'Error while creating guide'


@pytest.mark.vcr
def test_invalid_postal_code(client, origin, invalid_postal_code, details):

with pytest.raises(DhlmexException) as execinfo:
client.guides.create_guide(origin, invalid_postal_code, details)
assert str(execinfo.value) == 'Invalid destiny postal code'


@pytest.mark.vcr
def test_missing_city(client, origin, missing_data, details):
with pytest.raises(DhlmexException) as execinfo:
client.guides.create_guide(origin, missing_data, details)
assert str(execinfo.value) == 'Invalid value'
20 changes: 20 additions & 0 deletions tests/test_client_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ def test_successful_login(site_urls):
assert 'Administrar' in soup.find('title').text


@pytest.mark.vcr
def test_invalid_creds():
with pytest.raises(DhlmexException) as execinfo:
client = Client('invalidUsername', 'invalidPassword')
assert str(execinfo.value) == f'Invalid credentials'
assert client


def test_debug_login():
os.environ['DEBUG'] = 'True'
with pytest.raises(DhlmexException) as execinfo:
client = Client(DHLMEX_USERNAME, DHLMEX_PASSWORD)
assert (
str(execinfo.value) == f'Client on debug, but Charles not running'
)
assert client.session.cert
client._logout()
os.environ['DEBUG'] = ''


@pytest.mark.vcr
def test_client_log_out():
client = Client(DHLMEX_USERNAME, DHLMEX_PASSWORD)
Expand Down

0 comments on commit 1544455

Please sign in to comment.