1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# -*- coding: utf-8 -*-
"""
@File    : run.py
@Time    : 2023/2/6 10:51
@Author  : geekbing
@LastEditTime : -
@LastEditors : -
@Description : 运行API
"""
import logging
from ast import literal_eval
 
from django.core.exceptions import ObjectDoesNotExist
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework.decorators import api_view
from rest_framework.response import Response
 
from lunarlink.utils import loader, response
from lunarlink import tasks
from lunarlink.utils.decorator import request_log
from lunarlink.utils.parser import Format
from lunarlink import models
from apps.exceptions.error import (
    ApiNotFound,
    ConfigNotFound,
    CaseStepNotFound,
)
 
logger = logging.getLogger(__name__)
 
"""运行方式
"""
 
config_err = {
    "success": False,
    "msg": "指定的配置文件不存在",
    "code": "9999",
}
 
 
@api_view(["GET"])
@request_log(level="INFO")
def run_api_pk(request, pk):
    """
    运行单个接口
    """
    config_name = request.query_params.get("config", "请选择")
    try:
        api = models.API.objects.get(id=pk)
    except ObjectDoesNotExist:
        return Response(response.API_NOT_FOUND)
 
    config = (
        None
        if config_name == "请选择"
        else literal_eval(
            models.Config.objects.get(name=config_name, project=api.project).body
        )
    )
 
    summary = loader.debug_api(
        api=literal_eval(api.body),
        project=api.project.id,
        name=api.name,
        config=config,
        user=request.user.id,
    )
 
    return Response(summary)
 
 
@api_view(["POST"])
@request_log(level="INFO")
def run_api(request):
    """run api by body"""
 
    config_name = request.data.pop("config", "请选择")
 
    api = Format(request.data)
    api.parse()
 
    config = None
    if config_name != "请选择":
        try:
            config = literal_eval(
                models.Config.objects.get(
                    name=config_name, project__id=api.project
                ).body
            )
        except ObjectDoesNotExist:
            logger.error(f"指定配置文件不存在:{config_name}")
            return Response(config_err)
 
    summary = loader.debug_api(
        api=api.testcase,
        project=api.project,
        name=api.name,
        config=config,
        user=request.user.id,
    )
 
    return Response(summary)
 
 
@swagger_auto_schema(
    method="get",
    manual_parameters=[
        openapi.Parameter(
            "project",
            openapi.IN_QUERY,
            description="project id",
            type=openapi.TYPE_INTEGER,
            required=True,
        ),
        openapi.Parameter(
            "name",
            openapi.IN_QUERY,
            description="case name",
            type=openapi.TYPE_STRING,
            required=True,
        ),
        openapi.Parameter(
            "async",
            openapi.IN_QUERY,
            description="async",
            type=openapi.TYPE_STRING,
        ),
    ],
    operation_summary="执行单个测试用例集",
)
@api_view(["GET"])
@request_log(level="INFO")
def run_testsuite_pk(request, **kwargs):
    """
    执行测试用例集
 
    URL参数 query string:
        project
        name
    """
 
    pk = kwargs.get("pk")
    project = request.query_params["project"]
    name = request.query_params["name"]
    back_async = request.query_params.get("async", False)
 
    test_case = []
    config = None
    test_list = (
        models.CaseStep.objects.filter(case__id=pk).order_by("step").values("body")
    )
    for content in test_list:
        body = literal_eval(content["body"])
        if "base_url" in body["request"].keys():
            try:
                config = literal_eval(
                    models.Config.objects.get(
                        name=body["name"], project__id=project
                    ).body
                )
            except ObjectDoesNotExist:
                return Response(response.CONFIG_NOT_EXISTS)
            else:
                continue
 
        test_case.append(body)
 
    # 异步执行
    if back_async:
        tasks.async_debug_api.delay(
            api=test_case,
            project=project,
            name=name,
            config=config,
            user=request.user.id,
        )
        summary = response.TASK_RUN_SUCCESS
    else:  # 同步
        summary = loader.debug_api(
            api=test_case,
            project=project,
            name=name,
            config=config,
            user=request.user.id,
        )
 
    return Response(summary)
 
 
@swagger_auto_schema(
    method="post",
    request_body=openapi.Schema(
        type=openapi.TYPE_OBJECT,
        required=[
            "body",
            "project",
            "name",
        ],
        properties={
            "body": openapi.Schema(
                type=openapi.TYPE_ARRAY,
                items=openapi.Schema(
                    type=openapi.TYPE_OBJECT,
                ),
                description="body",
            ),
            "project": openapi.Schema(type=openapi.TYPE_INTEGER, description="project"),
            "name": openapi.Schema(type=openapi.TYPE_STRING, description="name"),
        },
    ),
)
@api_view(["POST"])
@request_log(level="INFO")
def run_testsuite(request):
    """
    调式测试用例集
    {
        name: str,
        body: dict,
        project: int,
    }
    """
    try:
        body = request.data["body"]
        project = request.data["project"]
        name = request.data["name"]
    except ObjectDoesNotExist:
        return Response(response.KEY_MISS)
 
    test_case = []
    config = None
    for test in body:
        try:
            test = loader.load_test(test=test, project=project)
        except ConfigNotFound:
            return Response(response.CONFIG_NOT_EXISTS)
        except ApiNotFound:
            return Response(response.API_NOT_FOUND)
        except CaseStepNotFound:
            return Response(response.CASE_STEP_NOT_EXIST)
        if "base_url" in test["request"].keys():
            config = test
            continue
 
        test_case.append(test)
 
    summary = loader.debug_api(
        api=test_case,
        project=project,
        name=name,
        config=config,
        user=request.user.id,
    )
 
    return Response(summary)
 
 
@api_view(["POST"])
@request_log(level="INFO")
def run_test(request):
    """
    测试用例中调式单个接口
 
    入参:
    {
        body: dict,
        project: int,
        config: null or dict,
    }
    """
    body = request.data.get("body")
    config = request.data.get("config", None)
    project = request.data.get("project")
 
    if config:
        try:
            config_obj = models.Config.objects.get(project=project, name=config["name"])
        except ObjectDoesNotExist:
            return Response(response.CONFIG_NOT_EXISTS)
        config = literal_eval(config_obj.body)
 
    try:
        test = loader.load_test(test=body)
    except ConfigNotFound:
        return Response(response.CONFIG_NOT_EXISTS)
    except ApiNotFound:
        return Response(response.API_NOT_FOUND)
    except CaseStepNotFound:
        return Response(response.CASE_STEP_NOT_EXIST)
 
    summary = loader.debug_api(
        api=test,
        project=project,
        name=body.get("name", None),
        config=config,
        user=request.user.id,
    )
 
    return Response(summary)
 
 
@api_view(["POST"])
@request_log(level="INFO")
def run_suite_tree(request):
    """
    选择目录节点执行用例集
 
    {
        project: int
        relation: list
        name: str
        async: bool
    }
    """
    config = None
    try:
        project = request.data["project"]
        relation = request.data["relation"]
        back_async = request.data["async"]
        report = request.data["name"]
        config_id = request.data.get("config_id")
    except KeyError:
        return Response(response.KEY_MISS)
    if config_id:
        # 前端有指定config, 会覆盖用例本身的config
        config = literal_eval(models.Config.objects.get(id=config_id).body)
 
    test_sets = []
    suite_list = []
    config_list = []
    for relation_id in relation:
        case_name_id_mapping_list = list(
            models.Case.objects.filter(project__id=project, relation=relation_id)
            .order_by("id")
            .values("id", "name")
        )
 
        for content in case_name_id_mapping_list:
            test_list = (
                models.CaseStep.objects.filter(case__id=content["id"])
                .order_by("step")
                .values("body")
            )
 
            testcase_list = []
            for test in test_list:
                body = literal_eval(test["body"])
                if body["request"].get("url"):
                    testcase_list.append(body)
                elif config is None and body["request"].get("base_url"):
                    config = literal_eval(
                        models.Config.objects.get(
                            name=body["name"], project__id=project
                        ).body
                    )
            config_list.append(config)
            test_sets.append(testcase_list)
            config = None
        suite_list.extend(case_name_id_mapping_list)
 
    if back_async:  # 异步
        tasks.async_debug_suite.delay(
            suite=test_sets,
            project=project,
            obj=suite_list,
            report=report,
            config=config_list,
            user=request.user.id,
        )
        summary = loader.TEST_NOTE_EXISTS
        summary["msg"] = "用例运行中,请稍后查看报告"
    else:
        summary, _ = loader.debug_suite(
            suite=test_sets,
            project=project,
            obj=suite_list,
            config=config_list,
            save=True,
            user=request.user.id,
        )
 
    return Response(summary)
 
 
@api_view(["POST"])
@request_log(level="INFO")
def run_multi_tests(request):
    """
    通过指定id, 运行多个指定用例
 
    {
        "name": "批量运行2条用例",  # 报告名
        "project": 11,
        "case_config_mapping_list": [
            {
                "config_name": "config_name1,
                "id": 153,  # 用例id
                "name": "case_name1"  # 用例名
            }
        ]
    }
    """
    try:
        project = request.data["project"]
        report_name = request.data["name"]
    except KeyError:
        return Response(response.KEY_MISS)
 
    # 默认同步运行用例
    back_async = request.data.get("async") or False
    case_config_mapping_list = request.data["case_config_mapping_list"]
    config_body_mapping = {}
 
    # 解析用例列表中的配置
    for config in case_config_mapping_list:
        config_name = config["config_name"]
        if not config_body_mapping.get(config_name):
            config_body_mapping[config_name] = literal_eval(
                models.Config.objects.get(
                    name=config["config_name"], project__id=project
                ).body
            )
    test_sets = []
    suite_list = []
    config_list = []
    for case_config_mapping in case_config_mapping_list:
        case_id = case_config_mapping["id"]
        config_name = case_config_mapping["config_name"]
        # 获取用例的所有步骤
        case_step_list = (
            models.CaseStep.objects.filter(case__id=case_id)
            .order_by("step")
            .values("body")
        )
        parsed_case_step_list = []
        for case_step in case_step_list:
            body = literal_eval(case_step["body"])
            if body["request"].get("url"):
                parsed_case_step_list.append(body)
        config_body = config_body_mapping[config_name]
        # 记录当前用例的配置信息
        config_list.append(config_body)
        # 记录已经解析好的用例
        test_sets.append(parsed_case_step_list)
    # 用例和配置的映射关系
    suite_list.extend(case_config_mapping_list)
 
    if back_async:  # 异步
        tasks.async_debug_suite.delay(
            suite=test_sets,
            project=project,
            obj=suite_list,
            report=report_name,
            config=config_list,
            user=request.user.id,
        )
        summary = loader.TEST_NOTE_EXISTS
        summary["msg"] = "用例运行中,请稍后查看报告"
    else:
        summary, _ = loader.debug_suite(
            suite=test_sets,
            project=project,
            obj=suite_list,
            config=config_list,
            save=True,
            user=request.user,
            report_name=report_name,
        )
 
    return Response(summary)