Skip to content

Commit

Permalink
待审批的查询和上线工单, 都显示审批人用户名 (#2394)
Browse files Browse the repository at this point in the history
* 待审批的查询和上线工单, 都显示审批人用户名

* black

* 补充工单详情的单元测试

* 补充工单详情的单元测试
  • Loading branch information
LeoQuote authored Nov 21, 2023
1 parent 594b146 commit 3625b33
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion common/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse

from archery import settings
from django.conf import settings
from common.config import SysConfig
from common.utils.ding_api import get_ding_user_id
from sql.models import Users, ResourceGroup, TwoFactorAuthConfig
Expand Down
13 changes: 11 additions & 2 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest
from pytest_mock import MockFixture
from django.contrib.auth.models import Group

from common.utils.const import WorkflowStatus
from sql.models import (
Expand Down Expand Up @@ -133,11 +134,19 @@ def setup_sys_config(db):


@pytest.fixture
def fake_generate_audit_setting(mocker: MockFixture):
def create_auth_group(db):
auth_group = Group.objects.create(name="test_group")
yield auth_group
auth_group.delete()


@pytest.fixture
def fake_generate_audit_setting(mocker: MockFixture, super_user, create_auth_group):
super_user.groups.add(create_auth_group)
mock_generate_audit_setting = mocker.patch.object(AuditV2, "generate_audit_setting")
fake_audit_setting = AuditSetting(
auto_pass=False,
audit_auth_groups=[123],
audit_auth_groups=[create_auth_group.id],
)
mock_generate_audit_setting.return_value = fake_audit_setting
yield mock_generate_audit_setting
Expand Down
2 changes: 1 addition & 1 deletion sql/data_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import MySQLdb
import simplejson as json
from django.template import loader
from archery import settings
from django.conf import settings
from sql.engines import get_engine
from django.contrib.auth.decorators import permission_required
from django.http import HttpResponse, JsonResponse, FileResponse
Expand Down
25 changes: 25 additions & 0 deletions sql/test_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pytest_django.asserts import assertTemplateUsed, assertContains

from sql.utils.workflow_audit import AuditV2


def test_get_sql_workflow(
sql_workflow,
fake_generate_audit_setting,
super_user,
admin_client,
create_auth_group,
):
sql_workflow, _ = sql_workflow
audit = AuditV2(workflow=sql_workflow)
audit.create_audit()
audit.workflow.status = "workflow_manreviewing"
audit.workflow.save()
response = admin_client.get(f"/detail/{sql_workflow.id}/")
assert response.status_code == 200
assertTemplateUsed(response, "detail.html")
# 展示审批人用户名
assert (
response.context["current_audit_auth_group"]
== f"{create_auth_group.name}: {super_user.username}"
)
3 changes: 2 additions & 1 deletion sql/utils/test_workflow_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ def test_review_info_no_review(self):
def test_review_info(self):
"""测试获取当前工单审批流程和当前审核组,无需审批"""
aug = Group.objects.create(name="DBA")
self.user.groups.add(aug)
self.audit.workflow_type = WorkflowType.SQL_REVIEW
self.audit.workflow_id = self.wf.id
self.audit.audit_auth_groups = str(aug.id)
Expand All @@ -327,7 +328,7 @@ def test_review_info(self):
self.audit.workflow_id, self.audit.workflow_type
)
self.assertEqual(audit_auth_group, "DBA")
self.assertEqual(current_audit_auth_group, "DBA")
self.assertEqual(current_audit_auth_group, f"DBA: {self.user.username}")

def test_logs(self):
"""测试获取工单日志"""
Expand Down
11 changes: 6 additions & 5 deletions sql/utils/workflow_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,10 +588,10 @@ def get_workflow_applicant(workflow_id, workflow_type):
except Exception:
raise Exception("当前审批auth_group_id不存在,请检查并清洗历史数据")
if (
auth_group_users([audit_auth_group], group_id)
user.is_superuser
or auth_group_users([audit_auth_group], group_id)
.filter(id=user.id)
.exists()
or user.is_superuser == 1
):
if workflow_type == 1:
if user.has_perm("sql.query_review"):
Expand Down Expand Up @@ -626,9 +626,10 @@ def review_info(workflow_id, workflow_type):
current_audit_auth_group = None
else:
try:
current_audit_auth_group = Group.objects.get(
id=audit_info.current_audit
).name
auth_group_in_db = Group.objects.get(id=audit_info.current_audit)
users = auth_group_in_db.user_set.all()
users_display = ",".join(x.username for x in users) or "组内无用户, 请联系管理员"
current_audit_auth_group = f"{auth_group_in_db.name}: {users_display}"
except Exception:
current_audit_auth_group = audit_info.current_audit
return audit_auth_group, current_audit_auth_group
Expand Down
12 changes: 4 additions & 8 deletions sql/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.http import HttpResponseRedirect, FileResponse, Http404
from django.urls import reverse

from archery import settings
from django.conf import settings
from common.config import SysConfig
from sql.engines import get_engine, engine_map
from common.utils.permission import superuser_required
Expand Down Expand Up @@ -215,14 +215,10 @@ def detail(request, workflow_id):
)
# 等待审批的展示当前全部审批人
if workflow_detail.status == "workflow_manreviewing":
auth_group_name = Group.objects.get(id=audit_detail.current_audit).name
current_audit_users = auth_group_users(
[auth_group_name], audit_detail.group_id
_, current_audit_users_display = Audit.review_info(
workflow_id, WorkflowType.SQL_REVIEW
)
current_audit_users_display = [
user.display for user in current_audit_users
]
last_operation_info += ",当前审批人:" + ",".join(current_audit_users_display)
last_operation_info += f",当前审批节点:{current_audit_users_display}"
except Exception as e:
logger.debug(f"无审核日志记录,错误信息{e}")
last_operation_info = ""
Expand Down

0 comments on commit 3625b33

Please sign in to comment.