From 5452b48d080a1c8fa9d39afd890106303bc16fd0 Mon Sep 17 00:00:00 2001 From: jasquat Date: Mon, 6 Feb 2023 14:11:15 -0500 Subject: [PATCH] use our json encoder to dump the user for get_current_user script w/ burnettk --- .../realm_exports/spiffworkflow-realm.json | 10 ++++---- .../src/spiffworkflow_backend/models/user.py | 6 ++--- .../scripts/get_current_user.py | 6 ++++- .../lanes_with_owner_dict.bpmn | 3 ++- .../scripts/test_get_current_user.py | 24 ++++++++++--------- 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/spiffworkflow-backend/keycloak/realm_exports/spiffworkflow-realm.json b/spiffworkflow-backend/keycloak/realm_exports/spiffworkflow-realm.json index 58a045b51..09347cbc2 100644 --- a/spiffworkflow-backend/keycloak/realm_exports/spiffworkflow-realm.json +++ b/spiffworkflow-backend/keycloak/realm_exports/spiffworkflow-realm.json @@ -1006,7 +1006,7 @@ "emailVerified" : false, "email" : "kb@sartography.com", "attributes" : { - "bamboo_id" : [ "42" ] + "spiffworkflow-employeeid" : [ "42" ] }, "credentials" : [ { "id" : "2c0be363-038f-48f1-86d6-91fdd28657cf", @@ -2120,7 +2120,7 @@ } }, { "id" : "a7692d41-b905-4049-9004-f6bea690051d", - "name" : "bamboo_id", + "name" : "spiffworkflow-employeeid", "protocol" : "openid-connect", "protocolMapper" : "oidc-usermodel-attribute-mapper", "consentRequired" : false, @@ -2128,10 +2128,10 @@ "aggregate.attrs" : "false", "userinfo.token.claim" : "true", "multivalued" : "false", - "user.attribute" : "bamboo_id", + "user.attribute" : "spiffworkflow-employeeid", "id.token.claim" : "true", "access.token.claim" : "true", - "claim.name" : "bamboo_id" + "claim.name" : "spiffworkflow-employeeid" } } ], "defaultClientScopes" : [ "web-origins", "acr", "profile", "roles", "email" ], @@ -3652,4 +3652,4 @@ "clientPolicies" : { "policies" : [ ] } -} \ No newline at end of file +} diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/models/user.py b/spiffworkflow-backend/src/spiffworkflow_backend/models/user.py index 3ecb8b284..c124b3ea7 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/models/user.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/models/user.py @@ -34,9 +34,9 @@ class UserModel(SpiffworkflowBaseDBModel): service_id = db.Column(db.String(255), nullable=False, unique=False) display_name = db.Column(db.String(255)) email = db.Column(db.String(255)) - tenant_specific_field_1 = db.Column(db.String(255)) - tenant_specific_field_2 = db.Column(db.String(255)) - tenant_specific_field_3 = db.Column(db.String(255)) + tenant_specific_field_1: str = db.Column(db.String(255)) + tenant_specific_field_2: str = db.Column(db.String(255)) + tenant_specific_field_3: str = db.Column(db.String(255)) updated_at_in_seconds: int = db.Column(db.Integer) created_at_in_seconds: int = db.Column(db.Integer) diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/scripts/get_current_user.py b/spiffworkflow-backend/src/spiffworkflow_backend/scripts/get_current_user.py index 53ebd1c5b..9921c18b1 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/scripts/get_current_user.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/scripts/get_current_user.py @@ -1,6 +1,7 @@ """Get current user.""" from typing import Any +from flask import current_app from flask import g from spiffworkflow_backend.models.script_attributes_context import ( @@ -26,4 +27,7 @@ class GetCurrentUser(Script): **kwargs: Any ) -> Any: """Run.""" - return g.user.__dict__ + # dump the user using our json encoder and then load it back up as a dict + # to remove unwanted field types + user_as_json_string = current_app.json.dumps(g.user) + return current_app.json.loads(user_as_json_string) diff --git a/spiffworkflow-backend/tests/data/model_with_lanes/lanes_with_owner_dict.bpmn b/spiffworkflow-backend/tests/data/model_with_lanes/lanes_with_owner_dict.bpmn index 1a6b52cbd..0c2af8d48 100644 --- a/spiffworkflow-backend/tests/data/model_with_lanes/lanes_with_owner_dict.bpmn +++ b/spiffworkflow-backend/tests/data/model_with_lanes/lanes_with_owner_dict.bpmn @@ -82,7 +82,8 @@ Flow_0bgkfue Flow_1ivhu7x approver = get_current_user() -lane_owners["Finance Team"].remove(approver) +username = approver['username'] +lane_owners["Finance Team"].remove(username) diff --git a/spiffworkflow-backend/tests/spiffworkflow_backend/scripts/test_get_current_user.py b/spiffworkflow-backend/tests/spiffworkflow_backend/scripts/test_get_current_user.py index 146baaf65..c2c0f6741 100644 --- a/spiffworkflow-backend/tests/spiffworkflow_backend/scripts/test_get_current_user.py +++ b/spiffworkflow-backend/tests/spiffworkflow_backend/scripts/test_get_current_user.py @@ -1,23 +1,20 @@ """Test_get_localtime.""" -from flask.app import Flask +import json + from flask import g +from flask.app import Flask from flask.testing import FlaskClient -from spiffworkflow_backend.models.script_attributes_context import ScriptAttributesContext -from spiffworkflow_backend.scripts.get_current_user import GetCurrentUser from tests.spiffworkflow_backend.helpers.base_test import BaseTest -from tests.spiffworkflow_backend.helpers.test_data import load_test_spec from spiffworkflow_backend.models.db import db -from spiffworkflow_backend.models.group import GroupModel -from spiffworkflow_backend.models.user import UserModel -from spiffworkflow_backend.services.process_instance_processor import ( - ProcessInstanceProcessor, +from spiffworkflow_backend.models.script_attributes_context import ( + ScriptAttributesContext, ) -from spiffworkflow_backend.services.user_service import UserService +from spiffworkflow_backend.models.user import UserModel +from spiffworkflow_backend.scripts.get_current_user import GetCurrentUser class TestGetCurrentUser(BaseTest): - def test_get_current_user( self, app: Flask, @@ -29,6 +26,9 @@ class TestGetCurrentUser(BaseTest): testuser1 = self.find_or_create_user("testuser1") testuser1.tenant_specific_field_1 = "456" db.session.add(testuser1) + db.session.commit() + + testuser1 = self.find_or_create_user("testuser1") g.user = testuser1 process_model_identifier = "test_process_model" process_instance_id = 1 @@ -41,4 +41,6 @@ class TestGetCurrentUser(BaseTest): result = GetCurrentUser().run( script_attributes_context, ) - assert result['username'] == "testuser1" + assert result["username"] == "testuser1" + assert result["tenant_specific_field_1"] == "456" + json.dumps(result)