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

🐛 FWF-3222: [Bugfix] Handle service-account in application history #2144

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
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ def get_application_history(cls, application_id: int):
cls.submitted_by,
)
)

@classmethod
def get_application_history_by_id(cls, application_id: int):
"""Find application history by id."""
return cls.query.filter(cls.application_id == application_id).first()
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ def post(application_id):
"""Post a new history entry using the request body."""
application_history_json = request.get_json()

# try:
application_history_schema = ApplicationHistorySchema()
dict_data = application_history_schema.load(application_history_json)
dict_data["application_id"] = application_id
application_history = ApplicationHistoryService.create_application_history(
data=dict_data
data=dict_data, application_id=application_id
)

response, status = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@
from flask import current_app
from formsflow_api_utils.utils import get_form_and_submission_id_from_form_url

from formsflow_api.models import ApplicationHistory
from formsflow_api.models import Application, ApplicationHistory
from formsflow_api.schemas import ApplicationHistorySchema


class ApplicationHistoryService:
"""This class manages application service."""

@staticmethod
def create_application_history(data):
def create_application_history(data, application_id):
"""Create new application history."""
# Replace service-account with application creator in initial history.
if data.get("submitted_by") and data.get("submitted_by").startswith(
"service-account"
):
application_history = ApplicationHistory.get_application_history_by_id(
application_id
)
if not application_history:
application = Application.find_by_id(application_id)
data["submitted_by"] = application.created_by
(form_id, submission_id) = get_form_and_submission_id_from_form_url(
data["form_url"]
)
Expand Down
26 changes: 26 additions & 0 deletions forms-flow-api/tests/unit/api/test_application_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Dict, List

from tests.utilities.base_test import get_token
from formsflow_api.models import Application


def get_history_create_payload():
Expand Down Expand Up @@ -68,3 +69,28 @@ def test_application_history_get_un_authorized(app, client, session, jwt):
# sending get request withouttoken
rv = client.get("/application/1/history")
assert rv.status_code == 401


def create_application_history_service_account(app, client, session, jwt):
"""Tests if the initial application history created with a service account replaced by application creator."""
application = Application()
application.created_by = "client"
application.application_status = "New"
application.form_process_mapper_id = 1
application.submission_id = "2345"
application.latest_form_id = "1234"
application.save()

payload = {
"applicationId": 1,
"applicationStatus": "New",
"formUrl": "http://testsample.com/form/23/submission/3423",
"submittedBy": "service-account-bpmn",
}
token = get_token(jwt)
headers = {"Authorization": f"Bearer {token}", "content-type": "application/json"}
new_entry = client.post(
"/application/1/history", headers=headers, json=payload
)
assert new_entry.status_code == 201
assert new_entry.submitted_by == "client"
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_create_application_history(app, client, session):
}
payload["application_id"] = 1222 # sample value
application_history = application_history_service.create_application_history(
data=payload
data=payload, application_id=1222
)
assert application_history.application_id == 1222
assert application_history.application_status == "Pending"
Expand Down
Loading