tests.unit.server_tests.test_endpoints_handler   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 147
Duplicated Lines 61.9 %

Importance

Changes 0
Metric Value
wmc 10
eloc 82
dl 91
loc 147
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A TestEndpointsHandlerWithAuth.tearDownClass() 0 6 1
A TestEndpointsHandlerWithAuth.test_invalid_creds_fails() 0 13 1
A TestEndpointsHandlerWithAuth.get_app() 0 3 1
A TestEndpointsHandlerWithAuth.test_no_creds_required_auth_fails() 0 3 1
A TestEndpointsHandlerWithAuth.test_valid_creds_pass() 0 13 1
A TestEndpointsHandlerWithAuth.setUpClass() 43 43 1
A TestEndpointsHandlerWithoutAuth.test_creds_no_auth_fails() 13 13 1
A TestEndpointsHandlerWithoutAuth.get_app() 3 3 1
A TestEndpointsHandlerWithoutAuth.tearDownClass() 4 4 1
A TestEndpointsHandlerWithoutAuth.setUpClass() 24 24 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
import base64
2
import os
3
import tempfile
4
5
from tabpy.tabpy_server.app.app import TabPyApp
6
from tabpy.tabpy_server.handlers.util import hash_password
7
from tornado.testing import AsyncHTTPTestCase
8
9
10
class TestEndpointsHandlerWithAuth(AsyncHTTPTestCase):
11 View Code Duplication
    @classmethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
12
    def setUpClass(cls):
13
        prefix = "__TestEndpointsHandlerWithAuth_"
14
        # create password file
15
        cls.pwd_file = tempfile.NamedTemporaryFile(
16
            mode="w+t", prefix=prefix, suffix=".txt", delete=False
17
        )
18
        username = "username"
19
        password = "password"
20
        cls.pwd_file.write(f"{username} {hash_password(username, password)}")
21
        cls.pwd_file.close()
22
23
        # create state.ini dir and file
24
        cls.state_dir = tempfile.mkdtemp(prefix=prefix)
25
        cls.state_file = open(os.path.join(cls.state_dir, "state.ini"), "w+")
26
        cls.state_file.write(
27
            "[Service Info]\n"
28
            "Name = TabPy Serve\n"
29
            "Description = \n"
30
            "Creation Time = 0\n"
31
            "Access-Control-Allow-Origin = \n"
32
            "Access-Control-Allow-Headers = \n"
33
            "Access-Control-Allow-Methods = \n"
34
            "\n"
35
            "[Query Objects Service Versions]\n"
36
            "\n"
37
            "[Query Objects Docstrings]\n"
38
            "\n"
39
            "[Meta]\n"
40
            "Revision Number = 1\n"
41
        )
42
        cls.state_file.close()
43
44
        # create config file
45
        cls.config_file = tempfile.NamedTemporaryFile(
46
            mode="w+t", prefix=prefix, suffix=".conf", delete=False
47
        )
48
        cls.config_file.write(
49
            "[TabPy]\n"
50
            f"TABPY_PWD_FILE = {cls.pwd_file.name}\n"
51
            f"TABPY_STATE_PATH = {cls.state_dir}"
52
        )
53
        cls.config_file.close()
54
55
    @classmethod
56
    def tearDownClass(cls):
57
        os.remove(cls.pwd_file.name)
58
        os.remove(cls.state_file.name)
59
        os.remove(cls.config_file.name)
60
        os.rmdir(cls.state_dir)
61
62
    def get_app(self):
63
        self.app = TabPyApp(self.config_file.name)
64
        return self.app._create_tornado_web_app()
65
66
    def test_no_creds_required_auth_fails(self):
67
        response = self.fetch("/endpoints")
68
        self.assertEqual(401, response.code)
69
70
    def test_invalid_creds_fails(self):
71
        response = self.fetch(
72
            "/endpoints",
73
            method="GET",
74
            headers={
75
                "Authorization": "Basic {}".format(
76
                    base64.b64encode("user:wrong_password".encode("utf-8")).decode(
77
                        "utf-8"
78
                    )
79
                )
80
            },
81
        )
82
        self.assertEqual(401, response.code)
83
84
    def test_valid_creds_pass(self):
85
        response = self.fetch(
86
            "/endpoints",
87
            method="GET",
88
            headers={
89
                "Authorization": "Basic {}".format(
90
                    base64.b64encode("username:password".encode("utf-8")).decode(
91
                        "utf-8"
92
                    )
93
                )
94
            },
95
        )
96
        self.assertEqual(200, response.code)
97
98
99 View Code Duplication
class TestEndpointsHandlerWithoutAuth(AsyncHTTPTestCase):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
100
    @classmethod
101
    def setUpClass(cls):
102
        prefix = "__TestEndpointsHandlerWithoutAuth_"
103
104
        # create state.ini dir and file
105
        cls.state_dir = tempfile.mkdtemp(prefix=prefix)
106
        cls.state_file = open(os.path.join(cls.state_dir, "state.ini"), "w+")
107
        cls.state_file.write(
108
            "[Service Info]\n"
109
            "Name = TabPy Serve\n"
110
            "Description = \n"
111
            "Creation Time = 0\n"
112
            "Access-Control-Allow-Origin = \n"
113
            "Access-Control-Allow-Headers = \n"
114
            "Access-Control-Allow-Methods = \n"
115
            "\n"
116
            "[Query Objects Service Versions]\n"
117
            "\n"
118
            "[Query Objects Docstrings]\n"
119
            "\n"
120
            "[Meta]\n"
121
            "Revision Number = 1\n"
122
        )
123
        cls.state_file.close()
124
125
    @classmethod
126
    def tearDownClass(cls):
127
        os.remove(cls.state_file.name)
128
        os.rmdir(cls.state_dir)
129
130
    def get_app(self):
131
        self.app = TabPyApp(None)
132
        return self.app._create_tornado_web_app()
133
134
    def test_creds_no_auth_fails(self):
135
        response = self.fetch(
136
            "/endpoints",
137
            method="GET",
138
            headers={
139
                "Authorization": "Basic {}".format(
140
                    base64.b64encode("username:password".encode("utf-8")).decode(
141
                        "utf-8"
142
                    )
143
                )
144
            },
145
        )
146
        self.assertEqual(406, response.code)
147