2022-07-25 13:35:10 -04:00
|
|
|
"""Base_test."""
|
2022-08-02 16:27:47 -04:00
|
|
|
import time
|
|
|
|
|
2022-07-25 13:35:10 -04:00
|
|
|
from flask.app import Flask
|
2022-08-02 11:06:40 -04:00
|
|
|
from flask_bpmn.api.api_error import ApiError
|
2022-08-02 16:27:47 -04:00
|
|
|
from flask_bpmn.models.db import db
|
2022-07-29 13:39:57 -04:00
|
|
|
|
2022-08-02 16:27:47 -04:00
|
|
|
from spiffworkflow_backend.models.process_instance import ProcessInstanceModel
|
|
|
|
from spiffworkflow_backend.models.process_model import ProcessModelInfo
|
2022-08-02 10:46:53 -04:00
|
|
|
from spiffworkflow_backend.models.user import UserModel
|
|
|
|
from spiffworkflow_backend.services.user_service import UserService
|
2022-07-25 13:35:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
class BaseTest:
|
|
|
|
"""BaseTest."""
|
|
|
|
|
2022-08-02 10:46:53 -04:00
|
|
|
@staticmethod
|
|
|
|
def find_or_create_user(username: str = "test_user1") -> UserModel:
|
|
|
|
"""Find_or_create_user."""
|
|
|
|
user = UserModel.query.filter_by(username=username).first()
|
|
|
|
if isinstance(user, UserModel):
|
|
|
|
return user
|
|
|
|
|
2022-08-02 15:59:43 -04:00
|
|
|
user = UserService().create_user("internal", username, username=username)
|
2022-08-02 11:06:40 -04:00
|
|
|
if isinstance(user, UserModel):
|
2022-08-02 10:46:53 -04:00
|
|
|
UserService().create_principal(user_id=user.id)
|
|
|
|
return user
|
|
|
|
|
2022-08-02 11:08:37 -04:00
|
|
|
raise ApiError(
|
|
|
|
code="create_user_error", message=f"Cannot find or create user: {username}"
|
|
|
|
)
|
2022-08-02 11:06:40 -04:00
|
|
|
|
2022-07-25 13:35:10 -04:00
|
|
|
@staticmethod
|
2022-08-02 15:59:43 -04:00
|
|
|
def get_open_id_constants(app: Flask) -> tuple:
|
|
|
|
"""Get_open_id_constants."""
|
|
|
|
open_id_server_url = app.config["OPEN_ID_SERVER_URL"]
|
|
|
|
open_id_client_id = app.config["OPEN_ID_CLIENT_ID"]
|
|
|
|
open_id_realm_name = app.config["OPEN_ID_REALM_NAME"]
|
|
|
|
open_id_client_secret_key = app.config[
|
|
|
|
"OPEN_ID_CLIENT_SECRET_KEY"
|
2022-07-29 13:39:57 -04:00
|
|
|
] # noqa: S105
|
2022-07-25 13:35:10 -04:00
|
|
|
|
2022-07-29 13:39:57 -04:00
|
|
|
return (
|
2022-08-02 15:59:43 -04:00
|
|
|
open_id_server_url,
|
|
|
|
open_id_client_id,
|
|
|
|
open_id_realm_name,
|
|
|
|
open_id_client_secret_key,
|
2022-07-29 13:39:57 -04:00
|
|
|
)
|
2022-07-25 14:05:32 -04:00
|
|
|
|
2022-08-02 12:52:46 -04:00
|
|
|
# @staticmethod
|
|
|
|
# def get_public_access_token(username: str, password: str) -> dict:
|
|
|
|
# """Get_public_access_token."""
|
|
|
|
# public_access_token = PublicAuthenticationService().get_public_access_token(
|
|
|
|
# username, password
|
|
|
|
# )
|
|
|
|
# return public_access_token
|
2022-08-02 16:27:47 -04:00
|
|
|
|
|
|
|
def create_process_instance_from_process_model(
|
|
|
|
self, process_model: ProcessModelInfo, status: str
|
|
|
|
) -> ProcessInstanceModel:
|
|
|
|
"""Create_process_instance_from_process_model."""
|
|
|
|
user = self.find_or_create_user()
|
|
|
|
current_time = round(time.time())
|
|
|
|
process_instance = ProcessInstanceModel(
|
|
|
|
status=status,
|
|
|
|
process_initiator=user,
|
|
|
|
process_model_identifier=process_model.id,
|
|
|
|
process_group_identifier=process_model.process_group_id,
|
|
|
|
updated_at_in_seconds=round(time.time()),
|
|
|
|
start_in_seconds=current_time - (3600 * 1),
|
|
|
|
end_in_seconds=current_time - (3600 * 1 - 20),
|
|
|
|
)
|
|
|
|
db.session.add(process_instance)
|
|
|
|
db.session.commit()
|
|
|
|
return process_instance
|