Skip to content

Commit

Permalink
feat(Hexa): better errors handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cheikhgwane committed Aug 26, 2024
1 parent cb7871d commit d015db7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
12 changes: 9 additions & 3 deletions openhexa/toolbox/hexa/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def authenticate(
mutation Login($input: LoginInput!) {
login(input: $input) {
success
errors
}
}
""",
Expand All @@ -41,10 +42,15 @@ def authenticate(
)
resp.raise_for_status()
data = resp.json()["data"]
if data["login"]["success"]:
self.session.headers["Cookie"] = resp.headers["Set-Cookie"]

if not data["login"]["success"]:
if "OTP_REQUIRED" in data["login"]["errors"]:
raise Exception("Login failed : two-factor authentication needs to be disabled.")

raise Exception(f"Login failed : {data['login']['errors']}")
else:
raise Exception("Login failed : verify if two-factor authentication is enabled.")
self.session.headers["Cookie"] = resp.headers["Set-Cookie"]

elif with_token:
self.session.headers.update({"Authorization": f"Bearer {with_token}"})

Expand Down
46 changes: 31 additions & 15 deletions tests/hexa/test_hexa.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest

from unittest import mock
from uuid import uuid4

Expand All @@ -12,31 +13,46 @@


class TestOpenHEXAClient:
def _mock_response(self, data):
mock_response = mock.MagicMock()
mock_response.json.return_value = data
return mock_response

def test_authenticate_with_creds_success(self):
hexa = OpenHEXAClient("https://app.demo.openhexa.org")
mock_response = mock.MagicMock()
mock_response.status_code = 401
mock_response.json.return_value = {
"data": {
"login": {
"success": True,
mock_response = self._mock_response(
{
"data": {
"login": {
"success": True,
}
}
}
}
},
)

with mock.patch.object(hexa, "_graphql_request", return_value=mock_response):
assert hexa.authenticate(with_credentials=("username", "password")) is True

def test_authenticate_with_creds_failed(self):
hexa = OpenHEXAClient("https://app.demo.openhexa.org")
result = {
"login": {
"success": False,
}
}
with mock.patch.object(hexa, "_graphql_request", return_value=result):
with pytest.raises(Exception):
mock_response = self._mock_response(
{"data": {"login": {"success": False, "errors": ["INVALID_CREDENTIALS"]}}},
)

with mock.patch.object(hexa, "_graphql_request", return_value=mock_response):
with pytest.raises(Exception) as e:
hexa.authenticate(with_credentials=("username", "password"))
assert str(e) == "Login failed : ['INVALID_CREDENTIALSS']"

def test_authenticate_with_otq_enabled_failed(self):
hexa = OpenHEXAClient("https://app.demo.openhexa.org")
mock_response = self._mock_response(
{"data": {"login": {"success": False, "errors": ["OTP_REQUIRED"]}}},
)
with mock.patch.object(hexa, "_graphql_request", return_value=mock_response):
with pytest.raises(Exception) as e:
hexa.authenticate(with_credentials=("username", "password"))
assert str(e) == "Login failed : you need to disable two-factor authentication."


@mock.patch("openhexa.toolbox.hexa.hexa.OpenHEXAClient")
Expand Down

0 comments on commit d015db7

Please sign in to comment.