Skip to content

Commit

Permalink
测试
Browse files Browse the repository at this point in the history
  • Loading branch information
kang.peng committed Nov 29, 2023
1 parent 837acdb commit 501f888
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 205 deletions.
6 changes: 6 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,9 @@ def create_audit_workflow(normal_user, create_resource_group):
)
yield audit_wf
audit_wf.delete()


@pytest.fixture
def clean_auth_group(db):
yield
Group.objects.all().delete()
4 changes: 0 additions & 4 deletions sql/engines/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,6 @@ def filter_sql(self, sql="", limit_num=0):
# 对查询sql增加limit限制
if re.match(r"^select", sql_lower):
if sql_lower.find(" top ") == -1:
if sql_lower.find(" distinct ") > 0:
return sql_lower.replace(
"distinct", "distinct top {}".format(limit_num)
)
return sql_lower.replace("select", "select top {}".format(limit_num))
return sql.strip()

Expand Down
7 changes: 0 additions & 7 deletions sql/engines/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,6 @@ def test_filter_sql(self):
check_result = new_engine.filter_sql(sql=banned_sql, limit_num=10)
self.assertEqual(check_result, "select top 10 user from user_table")

def test_filter_sql_with_distinct(self):
new_engine = MssqlEngine(instance=self.ins1)
# 只抽查一个函数
banned_sql = "select distinct * from user_table"
check_result = new_engine.filter_sql(sql=banned_sql, limit_num=10)
self.assertEqual(check_result, "select distinct top 10 * from user_table")

def test_execute_check(self):
new_engine = MssqlEngine(instance=self.ins1)
test_sql = (
Expand Down
20 changes: 9 additions & 11 deletions sql/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
SqlWorkflowContent,
)
from sql.utils.resource_group import auth_group_users
from sql.utils.workflow_audit import Audit
from sql.utils.workflow_audit import Audit, AuditV2
from sql_api.serializers import (
WorkflowContentSerializer,
WorkflowAuditListSerializer,
Expand Down Expand Up @@ -156,9 +156,8 @@ def render_audit(self):
workflow_from = self.audit.create_user_display
group_name = self.audit.group_name
# 获取当前审批和审批流程
workflow_auditors, current_workflow_auditors = Audit.review_info(
self.audit.workflow_id, self.audit.workflow_type
)
audit_handler = AuditV2(workflow=self.workflow, audit=self.audit)
review_info = audit_handler.get_review_info()
# workflow content, 即申请通过后要执行什么东西
# 执行的 SQL 语句, 授权的范围
if workflow_type == WorkflowType.QUERY:
Expand Down Expand Up @@ -221,8 +220,8 @@ def render_audit(self):
group_name,
instance,
db_name,
workflow_auditors,
current_workflow_auditors,
review_info.readable_info,
review_info.current_node.group.name,
workflow_title,
workflow_url,
workflow_content,
Expand All @@ -238,7 +237,7 @@ def render_audit(self):
group_name,
instance,
db_name,
workflow_auditors,
review_info.readable_info,
workflow_title,
workflow_url,
workflow_content,
Expand Down Expand Up @@ -284,9 +283,8 @@ def render_execute(self):
base_url = self.sys_config.get(
"archery_base_url", "http://127.0.0.1:8000"
).rstrip("/")
audit_auth_group, current_audit_auth_group = Audit.review_info(
self.workflow.id, 2
)
audit_handler = AuditV2(workflow=self.workflow, audit=self.audit)
review_info = audit_handler.get_review_info()
audit_id = Audit.detail_by_workflow_id(self.workflow.id, 2).audit_id
url = "{base_url}/workflow/{audit_id}".format(
base_url=base_url, audit_id=audit_id
Expand All @@ -305,7 +303,7 @@ def render_execute(self):
组:{self.workflow.group_name}
目标实例:{self.workflow.instance.instance_name}
数据库:{self.workflow.db_name}
审批流程:{audit_auth_group}
审批流程:{review_info.readable_info}
工单名称:{self.workflow.workflow_name}
工单地址:{url}
工单详情预览:{preview}"""
Expand Down
22 changes: 9 additions & 13 deletions sql/templates/archivedetail.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ <h4 style="display: inline;">工单名称:<span>{{ archive_config.title }}</sp
<input type="hidden" id="sqlMaxRowNumber" value="{{ rows|length }}">
<input type="hidden" id="editSqlContent" value="{{ archive_config.sql_content }}"/>
<hr>
<h4>
审批流
</h4>
<h5>
{% include "workflow_display.html" %}
<h4>
其他信息
</h4>
<table data-toggle="table" class="table table-striped table-hover"
style="table-layout:inherit;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">
<thead>
<tr>
<th>
申请人
</th>
<th>
审批流程
</th>
<th>
当前审批
</th>
<th>
实例
</th>
Expand Down Expand Up @@ -65,12 +67,6 @@ <h4 style="display: inline;">工单名称:<span>{{ archive_config.title }}</sp
<td>
{{ archive_config.user_display }}
</td>
<td>
{{ audit_auth_group }}
</td>
<td>
{{ current_audit_auth_group }}
</td>
<td>
{{ archive_config.src_instance }}
</td>
Expand Down Expand Up @@ -240,7 +236,7 @@ <h4><b>归档条件</b></h4>
<br>
{% endif %}
{% if archive_config.status == 0 %}
{% if is_can_review %}
{% if can_review %}
<textarea id="remark" name="remark" class="form-control" data-name="审核备注"
placeholder="请填写审核备注" rows=3></textarea>
<br>
Expand Down
20 changes: 8 additions & 12 deletions sql/templates/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ <h4 style="display: inline;">
<input type="hidden" id="sqlMaxRowNumber" value="{{ rows|length }}">
<input type="hidden" id="editSqlContent" value="{{ workflow_detail.sqlworkflowcontent.sql_content }}"/>
<hr>
<h4>
审批流
</h4>
<h5>
{% include "workflow_display.html" %}
<h4>
其他信息
</h4>
<table data-toggle="table" class="table table-striped table-hover"
style="table-layout:inherit;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">
<thead>
<tr>
<th>
发起人
</th>
<th>
审批流程
</th>
<th>
当前审批
</th>
<th>
目标实例
</th>
Expand Down Expand Up @@ -65,12 +67,6 @@ <h4 style="display: inline;">
<td>
{{ workflow_detail.engineer_display }}
</td>
<td>
{{ audit_auth_group }}
</td>
<td>
{{ current_audit_auth_group }}
</td>
<td>
{{ workflow_detail.instance.instance_name }}
</td>
Expand Down
20 changes: 8 additions & 12 deletions sql/templates/queryapplydetail.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ <h4 style="display: inline;">工单名称:<span>{{ workflow_detail.title }}</s
<input type="hidden" id="sqlMaxRowNumber" value="{{ rows|length }}">
<input type="hidden" id="editSqlContent" value="{{ workflow_detail.sql_content }}"/>
<hr>
<h4>
审批流
</h4>
<h5>
{% include "workflow_display.html" %}
<h4>
其他信息
</h4>
<table data-toggle="table" class="table table-striped table-hover"
style="table-layout:inherit;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">
<thead>
<tr>
<th>
申请人
</th>
<th>
审批流程
</th>
<th>
当前审批
</th>
<th>
实例
</th>
Expand Down Expand Up @@ -54,12 +56,6 @@ <h4 style="display: inline;">工单名称:<span>{{ workflow_detail.title }}</s
<td>
{{ workflow_detail.user_display }}
</td>
<td>
{{ audit_auth_group }}
</td>
<td>
{{ current_audit_auth_group }}
</td>
<td>
{{ workflow_detail.instance.instance_name }}
</td>
Expand Down
23 changes: 22 additions & 1 deletion sql/test_archiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from django.conf import settings
from django.contrib.auth.models import Permission
from django.test import TestCase, Client
from pytest_django.asserts import assertTemplateUsed

from common.config import SysConfig
from common.utils.const import WorkflowStatus, WorkflowType
from sql.utils.workflow_audit import AuditSetting
from sql.utils.workflow_audit import AuditSetting, AuditV2
from sql.archiver import add_archive_task, archive
from sql.models import (
Instance,
Expand Down Expand Up @@ -299,3 +300,23 @@ def test_archive_log(self, _async_task):
self.client.force_login(self.superuser)
r = self.client.post(path="/archive/log/", data=data)
self.assertDictEqual(json.loads(r.content), {"total": 0, "rows": []})


def test_archive_detail_view(
archive_apply,
resource_group,
admin_client,
fake_generate_audit_setting,
create_auth_group,
):
audit = AuditV2(workflow=archive_apply, resource_group=resource_group.group_name)
audit.create_audit()
audit.workflow.save()
response = admin_client.get(f"/archive/{archive_apply.id}/")
assert response.status_code == 200
assertTemplateUsed(response, "archivedetail.html")
review_info = response.context["review_info"]
assert len(review_info.nodes) == len(
fake_generate_audit_setting.return_value.audit_auth_groups
)
assert review_info.nodes[0].group.name == create_auth_group.name
12 changes: 6 additions & 6 deletions sql/test_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ def setUp(self):
workflow_type=1,
workflow_title="申请标题",
workflow_remark="申请备注",
audit_auth_groups="1,2,3",
current_audit="1",
next_audit="2",
audit_auth_groups=",".join([str(self.aug.id)]),
current_audit=str(self.aug.id),
next_audit="-1",
current_status=0,
)
self.audit_query_detail = WorkflowAuditDetail.objects.create(
Expand Down Expand Up @@ -167,9 +167,9 @@ def setUp(self):
workflow_type=3,
workflow_title=self.archive_apply.title,
workflow_remark="申请备注",
audit_auth_groups="1,2,3",
current_audit="1",
next_audit="2",
audit_auth_groups=",".join([str(self.aug.id)]),
current_audit=str(self.aug.id),
next_audit="-1",
current_status=0,
)

Expand Down
7 changes: 4 additions & 3 deletions sql/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def test_get_sql_workflow(
assert response.status_code == 200
assertTemplateUsed(response, "detail.html")
# 展示审批人用户名
assert (
response.context["current_audit_auth_group"]
== f"{create_auth_group.name}: {super_user.username}"
review_info = response.context["review_info"]
assert len(review_info.nodes) == len(
fake_generate_audit_setting.return_value.audit_auth_groups
)
assert review_info.nodes[0].group.name == create_auth_group.name
46 changes: 0 additions & 46 deletions sql/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,52 +825,6 @@ def test_alter_run_date(self, _can_review):
r, f"/detail/{self.wf1.id}/", fetch_redirect_response=False
)

@patch("sql.utils.workflow_audit.Audit.logs")
@patch("sql.utils.workflow_audit.Audit.detail_by_workflow_id")
@patch("sql.utils.workflow_audit.Audit.review_info")
@patch("sql.utils.workflow_audit.Audit.can_review")
def testWorkflowDetailView(self, _can_review, _review_info, _detail_by_id, _logs):
"""测试工单详情"""
_review_info.return_value = ("some_auth_group", "current_auth_group")
_can_review.return_value = False
_detail_by_id.return_value.audit_id = 123
_logs.return_value.latest("id").operation_info = ""
c = Client()
c.force_login(self.u1)
r = c.get("/detail/{}/".format(self.wf1.id))
expected_status_display = r"""id="workflow_detail_disaply">已正常结束"""
self.assertContains(r, expected_status_display)
exepcted_status = r"""id="workflow_detail_status">workflow_finish"""
self.assertContains(r, exepcted_status)

# 测试执行详情解析失败
self.wfc1.execute_result = "cannotbedecode:1,:"
self.wfc1.save()
r = c.get("/detail/{}/".format(self.wf1.id))
self.assertContains(r, expected_status_display)
self.assertContains(r, exepcted_status)

# 执行详情为空
self.wfc1.review_content = [
{
"id": 1,
"stage": "CHECKED",
"errlevel": 0,
"stagestatus": "Audit completed",
"errormessage": "None",
"sql": "use archery",
"affected_rows": 0,
"sequence": "'0_0_0'",
"backup_dbname": "None",
"execute_time": "0",
"sqlsha1": "",
"actual_affected_rows": "",
}
]
self.wfc1.execute_result = ""
self.wfc1.save()
r = c.get("/detail/{}/".format(self.wf1.id))

def testWorkflowListView(self):
"""测试工单列表"""
c = Client()
Expand Down
Loading

0 comments on commit 501f888

Please sign in to comment.