Skip to content

Commit

Permalink
Add missing data tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agarrido19 committed Feb 5, 2020
1 parent 36716a6 commit c311634
Show file tree
Hide file tree
Showing 8 changed files with 2,883 additions and 26 deletions.
1 change: 0 additions & 1 deletion dhlmex/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def _login(self, username: str, password: str) -> Response:
'javax.faces.ViewState': 'j_id1',
'j_id6:j_id29': 'j_id6:j_id29',
}
print(f'DEBUG: {data}')
resp = self.post(endpoint, data)
except HTTPError as httpe:
if 'Su sesión ha caducado' in resp.text:
Expand Down
29 changes: 11 additions & 18 deletions dhlmex/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from bs4 import BeautifulSoup
from requests import Response

from dhlmex.exceptions import DhlmexException


class Resource:
_client: ClassVar["dhlmex.Client"] # type: ignore
Expand Down Expand Up @@ -41,20 +39,15 @@ def get_data(resp: Response, action: Dict) -> Dict:
view_state = soup.find('input', id='javax.faces.ViewState').attrs[
'value'
]
a = soup.find('a', text=action['text'])
if a is None:
print(f'DEBUG: {resp.text}')
raise DhlmexException('Debug')
else:
js = soup.find('a', text=action['text']).attrs['onclick']
matches = re.findall(r"\'(.+?)\'", js)
form_ids = [match for match in matches if match.startswith('j_id')]
j_pair_id = form_ids[1].split(',')[0]
j_id = form_ids[0]
js = soup.find('a', text=action['text']).attrs['onclick']
matches = re.findall(r"\'(.+?)\'", js)
form_ids = [match for match in matches if match.startswith('j_id')]
j_pair_id = form_ids[1].split(',')[0]
j_id = form_ids[0]

return {
j_id: j_id,
j_pair_id: action['code'],
'javax.faces.ViewState': view_state,
action['end']: action['end'],
}
return {
j_id: j_id,
j_pair_id: action['code'],
'javax.faces.ViewState': view_state,
action['end']: action['end'],
}
8 changes: 3 additions & 5 deletions dhlmex/resources/guides.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
class Guide(Resource):
@classmethod
def create_guide(
cls, origin: Origin, destination: Destination,
details: OrderDetails,
cls, origin: Origin, destination: Destination, details: OrderDetails,
) -> Tuple[str, Optional[bytes]]:
guide = cls()
try:
Expand Down Expand Up @@ -96,8 +95,7 @@ def _select_guide(self, guides_data: Dict) -> Dict:
return select_data

def _fill_guide_table(
self, origin: Origin, destination: Destination,
details: OrderDetails
self, origin: Origin, destination: Destination, details: OrderDetails
) -> str:
resp = self._client.get(self._urls['capture'])
soup = BeautifulSoup(resp.text, features='html.parser')
Expand Down Expand Up @@ -130,7 +128,7 @@ def _fill_guide_table(
fill_data['javax.faces.ViewState'] = view_state
fill_data['datos:j_id105'] = 'datos:j_id105'

msg = self._validate_data()
msg = self._validate_data(fill_data)
if msg:
raise DhlmexException(f'Invalid data: {msg}')
else:
Expand Down
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'
9 changes: 9 additions & 0 deletions tests/test_client_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ 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:
Expand All @@ -30,6 +38,7 @@ def test_debug_login():
)
assert client.session.cert
client._logout()
os.environ['DEBUG'] = ''


@pytest.mark.vcr
Expand Down

0 comments on commit c311634

Please sign in to comment.