Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: endpoint challenge #33

Merged
merged 6 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified requirements.txt
Binary file not shown.
2 changes: 1 addition & 1 deletion src/config/soap_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from zeep import Client
from .environment import variables

soap_wsdl = f"{variables['GATEWAY_BASEURL']}/service?wsdl"
soap_wsdl = f"{variables['GATEWAY_BASEURL']}/gw/service?wsdl"
soap_client = Client(soap_wsdl)
30 changes: 30 additions & 0 deletions src/controllers/_authentication_controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,33 @@ def login_handler():
except Exception as e:
print("SOAP Error:", str(e))
return {"msg": "Internal error", "error": str(e)}, 500


def challenge():
try:
data = request.json

if not data:
return {"msg": "No JSON data provided in the request"}, 400

token = data.get("jwt")

if not token:
return {"msg": "Token is missing in JSON data"}, 400

response = soap_client.service.auth_refresh({"token": token})

print(response)

if hasattr(response, "auth") and hasattr(response.auth, "token"):
jwt = response.auth.token
return {
"msg": "JWT refreshed successfully",
"jwt": jwt,
}, 200
else:
return {"msg": response.msg}, response.code

except Exception as e:
print("Error:", str(e))
return {"msg": "Internal error: " + str(e)}, 500
5 changes: 5 additions & 0 deletions src/views/_authentication_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
@views.route("/auth_login", methods=["POST"])
def auth_login():
return _authentication_controllers.login_handler()


@views.route("/auth_refresh", methods=["POST"])
def auth_refresh():
return _authentication_controllers.challenge()
58 changes: 55 additions & 3 deletions src/views/_authentication_views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@


def test_auth_login_successful() -> None:
registration_data = {"username": "miguel1", "password": "miguel1"}
registration_data = {"username": "miguel12", "password": "miguel12"}

registration_result = soap_client.service.account_register(registration_data)

assert registration_result.error is False

login_data = {"username": "miguel1", "password": "miguel1"}
login_data = {"username": "miguel12", "password": "miguel12"}

response = app.test_client().post("/auth_login", json=login_data)

Expand Down Expand Up @@ -40,4 +40,56 @@ def test_auth_login_missing_fields() -> None:

assert (
json.loads(response.data)["msg"] == "Required fields are missing in JSON data"
) # noqa: E501
)


def test_auth_refresh_missing_token() -> None:
# Simulate a request without a token
data = {"jwt": ""}

response = app.test_client().post("/auth_refresh", json=data)

assert response.status_code == 400

assert json.loads(response.data)["msg"] == "Token is missing in JSON data"


def test_no_valid() -> None:
registration_data = {"username": "pedro19", "password": "pedro19"}
registration_result = soap_client.service.account_register(registration_data)
assert registration_result.error is False

login_data = {"username": "pedro19", "password": "pedro19"}
login_response = app.test_client().post("/auth_login", json=login_data)
assert login_response.status_code == 200
jwt = json.loads(login_response.data)["jwt"]

data = {"jwt": jwt + "a"}
response = app.test_client().post("/auth_refresh", json=data)

assert response.status_code == 401

response_data = json.loads(response.data)
assert "msg" in response_data
assert response_data["msg"] != ""


def test_challenge_valid_token() -> None:
registration_data = {"username": "gloria1", "password": "gloria1"}
registration_result = soap_client.service.account_register(registration_data)
assert registration_result.error is False

login_data = {"username": "gloria1", "password": "gloria1"}
login_response = app.test_client().post("/auth_login", json=login_data)
assert login_response.status_code == 200
jwt = json.loads(login_response.data)["jwt"]

data = {"jwt": jwt}
response = app.test_client().post("/auth_refresh", json=data)

assert response.status_code == 200

# Verificar si la respuesta contiene un token JWT válido
response_data = json.loads(response.data)
assert "jwt" in response_data
assert response_data["jwt"] != ""