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 login #15

Merged
merged 7 commits into from
Oct 4, 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
247 changes: 121 additions & 126 deletions docs/spec.openapi.yaml

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import flask
import pages.home
from src.views import views

app = flask.Flask(__name__, template_folder="templates")
app = flask.Flask(__name__)
app.register_blueprint(views)

app.register_blueprint(pages.home.home)
if __name__ == "__main__":
app.run(debug=True)
6 changes: 0 additions & 6 deletions pages/home/__init__.py

This file was deleted.

8 changes: 0 additions & 8 deletions pages/home/_view.py

This file was deleted.

6 changes: 0 additions & 6 deletions pages/home/_view_test.py

This file was deleted.

Binary file modified requirements.txt
Binary file not shown.
5 changes: 5 additions & 0 deletions src/config/environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import os

variables = {
"GATEWAY_BASEURL": os.getenv("GATEWAY_BASEURL", "http://localhost:8080"),
}
5 changes: 5 additions & 0 deletions src/config/soap_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from zeep import Client
from .environment import variables

soap_wsdl = f"{variables['GATEWAY_BASEURL']}/service?wsdl"
soap_client = Client(soap_wsdl)
31 changes: 31 additions & 0 deletions src/controllers/_authentication_controllers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from flask import request
from src.config.soap_client import soap_client


def login_handler():
try:
# Get JSON data from the request
data = request.json

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

username = data.get("username")
password = data.get("password")

if not username or not password:
return {"msg": "Required fields are missing in JSON data"}, 400

result = soap_client.service.auth_login(
{"username": username, "password": password}
)

if result.auth is not None:
jwt = result.auth.token
return {"msg": "Login successful", "jwt": jwt}, 200

return {"msg": "Invalid credentials"}, 401

except Exception as e:
print("SOAP Error:", str(e))
return {"msg": "Internal error", "error": str(e)}, 500
9 changes: 9 additions & 0 deletions src/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import flask
from ._authentication_views import views as authentication_views


# NOTE: Register all views / routes using the following blueprint
views = flask.Blueprint("views", __name__)


views.register_blueprint(authentication_views)
9 changes: 9 additions & 0 deletions src/views/_authentication_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import flask
from src.controllers import _authentication_controllers

views = flask.Blueprint("authentication", __name__)


@views.route("/auth_login", methods=["POST"])
def auth_login():
return _authentication_controllers.login_handler()
43 changes: 43 additions & 0 deletions src/views/_authentication_views_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import json
from main import app
from config.soap_client import soap_client


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

registration_result = soap_client.service.account_register(registration_data)

assert registration_result.error is False

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

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

assert response.status_code == 200

assert json.loads(response.data)["msg"] == "Login successful"

assert "jwt" in json.loads(response.data)


def test_auth_login_invalid_credentials() -> None:
data = {"username": "miguel", "password": "miguel"}

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

assert response.status_code == 401

assert json.loads(response.data)["msg"] == "Invalid credentials"


def test_auth_login_missing_fields() -> None:
data = {"username": "miguel"}

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

assert response.status_code == 400

assert (
json.loads(response.data)["msg"] == "Required fields are missing in JSON data"
) # noqa: E501
1 change: 0 additions & 1 deletion templates/index.html

This file was deleted.