Passed
Pull Request — master (#608)
by
unknown
14:06
created

tests.integration.test_max_request_size   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 50.54 %

Importance

Changes 0
Metric Value
wmc 10
eloc 59
dl 47
loc 93
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B TestMaxRequestSize._get_config_file_name() 47 47 6
A TestMaxRequestSize.test_payload_exceeds_max_request_size_query() 0 5 1
A TestMaxRequestSize.test_payload_exceeds_max_request_size_evaluate() 0 5 1
A TestMaxRequestSize.create_large_payload() 0 10 1
A TestMaxRequestSize.test_no_content_length_header_present() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
"""
2
Max request size tests.
3
"""
4
5
from . import integ_test_base
6
import json
7
import gzip
8
import os
9
import requests
10
import string
11
12
13
class TestMaxRequestSize(integ_test_base.IntegTestBase):
14 View Code Duplication
    def _get_config_file_name(self) -> str:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
15
        """
16
        Generates config file. Overwrite this function for tests to
17
        run against not default state file.
18
19
        Returns
20
        -------
21
        str
22
            Absolute path to config file.
23
        """
24
        config_file = open(os.path.join(self.tmp_dir, "test.conf"), "w+")
25
        config_file.write(
26
            "[TabPy]\n"
27
            f"TABPY_QUERY_OBJECT_PATH = {self.tmp_dir}/query_objects\n"
28
            f"TABPY_PORT = {self._get_port()}\n"
29
            f"TABPY_GZIP_ENABLE = TRUE\n"
30
            f"TABPY_STATE_PATH = {self.tmp_dir}\n"
31
            "TABPY_MAX_REQUEST_SIZE_MB = 1\n"
32
        )
33
34
        pwd_file = self._get_pwd_file()
35
        if pwd_file is not None:
36
            pwd_file = os.path.abspath(pwd_file)
37
            config_file.write(f"TABPY_PWD_FILE = {pwd_file}\n")
38
39
        transfer_protocol = self._get_transfer_protocol()
40
        if transfer_protocol is not None:
41
            config_file.write(f"TABPY_TRANSFER_PROTOCOL = {transfer_protocol}\n")
42
43
        cert_file_name = self._get_certificate_file_name()
44
        if cert_file_name is not None:
45
            cert_file_name = os.path.abspath(cert_file_name)
46
            config_file.write(f"TABPY_CERTIFICATE_FILE = {cert_file_name}\n")
47
48
        key_file_name = self._get_key_file_name()
49
        if key_file_name is not None:
50
            key_file_name = os.path.abspath(key_file_name)
51
            config_file.write(f"TABPY_KEY_FILE = {key_file_name}\n")
52
53
        evaluate_timeout = self._get_evaluate_timeout()
54
        if evaluate_timeout is not None:
55
            config_file.write(f"TABPY_EVALUATE_TIMEOUT = {evaluate_timeout}\n")
56
57
        config_file.close()
58
59
        self.delete_config_file = True
60
        return config_file.name
61
62
    def create_large_payload(self):
63
        size_mb = 2
64
        num_chars = size_mb * 1024 * 1024
65
        large_string = string.printable * (num_chars // len(string.printable))
66
        large_string += string.printable[:num_chars % len(string.printable)]
67
        payload = {
68
            "data": { "_arg1": large_string },
69
            "script": "return _arg1"
70
        }
71
        return json.dumps(payload).encode('utf-8')
72
73
    def test_payload_exceeds_max_request_size_evaluate(self):
74
        headers = { "Content-Type": "application/json" }
75
        url = self._get_url() + "/evaluate"
76
        response = requests.post(url, data=self.create_large_payload(), headers=headers)
77
        self.assertEqual(413, response.status_code)
78
79
    def test_payload_exceeds_max_request_size_query(self):
80
        headers = { "Content-Type": "application/json" }
81
        url = self._get_url() + "/query/model_name"
82
        response = requests.post(url, data=self.create_large_payload(), headers=headers)
83
        self.assertEqual(413, response.status_code)
84
85
    def test_no_content_length_header_present(self):
86
        headers = { "Content-Type": "application/json" }
87
        url = self._get_url() + "/evaluate"
88
        response = requests.post(url, headers=headers)
89
        message = json.loads(response.text)["message"]
90
        # Ensure it gets to processing message stage in EvaluationPlaneHandler.post
91
        self.assertEqual("Error processing script", message)
92
        self.assertEqual(500, response.status_code)
93
94